blob: 550c8daaee5d179c784151b2b56845422506487b [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"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000017#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000026#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Cameron Zwaricha86686e2011-06-10 20:59:24 +000075namespace llvm {
76 class ARMCCState : public CCState {
77 public:
78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
80 LLVMContext &C, ParmContext PC)
81 : CCState(CC, isVarArg, MF, TM, locs, C) {
82 assert(((PC == Call) || (PC == Prologue)) &&
83 "ARMCCState users must specify whether their context is call"
84 "or prologue generation.");
85 CallOrPrologue = PC;
86 }
87 };
88}
89
Stuart Hastingsc7315872011-04-20 16:47:52 +000090// The APCS parameter registers.
91static const unsigned GPRArgRegs[] = {
92 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93};
94
Owen Andersone50ed302009-08-10 22:56:29 +000095void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
96 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000097 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
100 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 }
106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000109 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000110 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000111 if (ElemTy != MVT::i32) {
112 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
116 }
Owen Anderson70671842009-08-10 20:18:46 +0000117 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
118 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000119 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000120 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000121 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
122 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
125 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
126 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000127 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
128 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000129 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
130 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
131 setTruncStoreAction(VT.getSimpleVT(),
132 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000134 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135
136 // Promote all bit-wise operations.
137 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000139 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000146 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000147 }
Bob Wilson16330762009-09-16 00:17:28 +0000148
149 // Neon does not support vector divide/remainder operations.
150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Owen Andersone50ed302009-08-10 22:56:29 +0000158void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000159 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000161}
162
Owen Andersone50ed302009-08-10 22:56:29 +0000163void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000164 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000166}
167
Chris Lattnerf0144122009-07-28 03:13:23 +0000168static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000170 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000171
Chris Lattner80ec2792009-08-02 00:34:36 +0000172 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173}
174
Evan Chenga8e29892007-01-19 07:51:42 +0000175ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000176 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000178 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000179 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 // Uses VFP for Thumb libfuncs if available.
183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
184 // Single-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 // Double-precision floating-point arithmetic.
191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 // Single-precision comparisons.
197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 // Double-precision comparisons.
216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000224
Evan Chengb1df8f22007-04-27 08:15:43 +0000225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000233
Evan Chengb1df8f22007-04-27 08:15:43 +0000234 // Floating-point to integer conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000241
Evan Chengb1df8f22007-04-27 08:15:43 +0000242 // Conversions between floating types.
243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
245
246 // Integer to floating-point conversions.
247 // i64 conversions are done via library routines even when generating VFP
248 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000249 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
250 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256 }
257
Bob Wilson2f954612009-05-22 17:38:41 +0000258 // These libcalls are not available in 32-bit.
259 setLibcallName(RTLIB::SHL_I128, 0);
260 setLibcallName(RTLIB::SRL_I128, 0);
261 setLibcallName(RTLIB::SRA_I128, 0);
262
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000263 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000265 // RTABI chapter 4.1.2, Table 2
266 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
267 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
268 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
269 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
270 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
274
275 // Double-precision floating-point comparison helper functions
276 // RTABI chapter 4.1.2, Table 3
277 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
278 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
279 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
281 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
282 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
284 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
286 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
288 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
289 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
290 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
291 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
293 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
301
302 // Single-precision floating-point arithmetic helper functions
303 // RTABI chapter 4.1.2, Table 4
304 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
305 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
306 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
307 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
308 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
312
313 // Single-precision floating-point comparison helper functions
314 // RTABI chapter 4.1.2, Table 5
315 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
316 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
317 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
319 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
320 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
322 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
324 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
326 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
327 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
328 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
329 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
331 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
339
340 // Floating-point to integer conversions.
341 // RTABI chapter 4.1.2, Table 6
342 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
343 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
346 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
347 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
350 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
358
359 // Conversions between floating types.
360 // RTABI chapter 4.1.2, Table 7
361 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
362 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
363 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000365
366 // Integer to floating-point conversions.
367 // RTABI chapter 4.1.2, Table 8
368 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
369 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
370 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
371 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
372 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
373 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
374 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
375 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384
385 // Long long helper functions
386 // RTABI chapter 4.2, Table 9
387 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
388 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
389 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
390 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
391 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
392 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
393 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
399
400 // Integer division functions
401 // RTABI chapter 4.3.1
402 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
403 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
404 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
405 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
407 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
408 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000413 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000414
415 // Memory operations
416 // RTABI chapter 4.3.4
417 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
418 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
419 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000420 }
421
David Goodwinf1daf7d2009-07-08 23:10:31 +0000422 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000424 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000426 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000428 if (!Subtarget->isFPOnlySP())
429 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000430
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000432 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000433
434 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 addDRTypeForNEON(MVT::v2f32);
436 addDRTypeForNEON(MVT::v8i8);
437 addDRTypeForNEON(MVT::v4i16);
438 addDRTypeForNEON(MVT::v2i32);
439 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addQRTypeForNEON(MVT::v4f32);
442 addQRTypeForNEON(MVT::v2f64);
443 addQRTypeForNEON(MVT::v16i8);
444 addQRTypeForNEON(MVT::v8i16);
445 addQRTypeForNEON(MVT::v4i32);
446 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000447
Bob Wilson74dc72e2009-09-15 23:55:57 +0000448 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
449 // neither Neon nor VFP support any arithmetic operations on it.
450 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
451 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
452 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
453 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
454 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
455 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
456 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
457 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
458 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
459 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
460 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
461 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
462 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
463 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
464 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
465 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
467 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
468 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
469 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
470 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
471 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
472 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
473 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
474
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000475 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
476
Bob Wilson642b3292009-09-16 00:32:15 +0000477 // Neon does not support some operations on v1i64 and v2i64 types.
478 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000479 // Custom handling for some quad-vector types to detect VMULL.
480 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
481 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
482 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000483 // Custom handling for some vector types to avoid expensive expansions
484 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
485 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
486 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
487 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000488 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
489 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000490 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
491 // a destination type that is wider than the source.
492 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
493 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000494
Bob Wilson1c3ef902011-02-07 17:43:21 +0000495 setTargetDAGCombine(ISD::INTRINSIC_VOID);
496 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000497 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
498 setTargetDAGCombine(ISD::SHL);
499 setTargetDAGCombine(ISD::SRL);
500 setTargetDAGCombine(ISD::SRA);
501 setTargetDAGCombine(ISD::SIGN_EXTEND);
502 setTargetDAGCombine(ISD::ZERO_EXTEND);
503 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000504 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000505 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000506 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000507 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
508 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000509 setTargetDAGCombine(ISD::FP_TO_SINT);
510 setTargetDAGCombine(ISD::FP_TO_UINT);
511 setTargetDAGCombine(ISD::FDIV);
Bob Wilson5bafff32009-06-22 23:27:02 +0000512 }
513
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000514 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000515
516 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000518
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000519 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000521
Evan Chenga8e29892007-01-19 07:51:42 +0000522 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000523 if (!Subtarget->isThumb1Only()) {
524 for (unsigned im = (unsigned)ISD::PRE_INC;
525 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setIndexedLoadAction(im, MVT::i1, Legal);
527 setIndexedLoadAction(im, MVT::i8, Legal);
528 setIndexedLoadAction(im, MVT::i16, Legal);
529 setIndexedLoadAction(im, MVT::i32, Legal);
530 setIndexedStoreAction(im, MVT::i1, Legal);
531 setIndexedStoreAction(im, MVT::i8, Legal);
532 setIndexedStoreAction(im, MVT::i16, Legal);
533 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000534 }
Evan Chenga8e29892007-01-19 07:51:42 +0000535 }
536
537 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000538 setOperationAction(ISD::MUL, MVT::i64, Expand);
539 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000540 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
542 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000543 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000544 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
545 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000546 setOperationAction(ISD::MULHS, MVT::i32, Expand);
547
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000548 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000549 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000550 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::SRL, MVT::i64, Custom);
552 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000553
Evan Cheng342e3162011-08-30 01:34:54 +0000554 if (!Subtarget->isThumb1Only()) {
555 // FIXME: We should do this for Thumb1 as well.
556 setOperationAction(ISD::ADDC, MVT::i32, Custom);
557 setOperationAction(ISD::ADDE, MVT::i32, Custom);
558 setOperationAction(ISD::SUBC, MVT::i32, Custom);
559 setOperationAction(ISD::SUBE, MVT::i32, Custom);
560 }
561
Evan Chenga8e29892007-01-19 07:51:42 +0000562 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000564 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000566 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000568
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000569 // Only ARMv6 has BSWAP.
570 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000572
Evan Chenga8e29892007-01-19 07:51:42 +0000573 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000574 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000575 // v7M has a hardware divider
576 setOperationAction(ISD::SDIV, MVT::i32, Expand);
577 setOperationAction(ISD::UDIV, MVT::i32, Expand);
578 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 setOperationAction(ISD::SREM, MVT::i32, Expand);
580 setOperationAction(ISD::UREM, MVT::i32, Expand);
581 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
582 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000583
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
585 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
586 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
587 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000588 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000589
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000590 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000591
Evan Chenga8e29892007-01-19 07:51:42 +0000592 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::VASTART, MVT::Other, Custom);
594 setOperationAction(ISD::VAARG, MVT::Other, Expand);
595 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
596 setOperationAction(ISD::VAEND, MVT::Other, Expand);
597 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
598 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000599 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000600 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
601 setExceptionPointerRegister(ARM::R0);
602 setExceptionSelectorRegister(ARM::R1);
603
Evan Cheng3a1588a2010-04-15 22:20:34 +0000604 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000605 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
606 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000607 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000608 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000609 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000610 // membarrier needs custom lowering; the rest are legal and handled
611 // normally.
612 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000613 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000614 // Custom lowering for 64-bit ops
615 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
616 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
617 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
618 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
619 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
620 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000621 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
622 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000623 } else {
624 // Set them all for expansion, which will force libcalls.
625 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000626 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000627 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000628 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000629 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000630 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000631 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000632 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000633 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000634 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000635 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000636 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000637 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000638 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman069e2ed2011-08-26 02:59:24 +0000639 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
640 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000641 // Since the libcalls include locking, fold in the fences
642 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000643 }
Evan Chenga8e29892007-01-19 07:51:42 +0000644
Evan Cheng416941d2010-11-04 05:19:35 +0000645 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000646
Eli Friedmana2c6f452010-06-26 04:36:50 +0000647 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
648 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000649 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
650 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000651 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000653
Nate Begemand1fb5832010-08-03 21:31:55 +0000654 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000655 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
656 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000657 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000658 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
659 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000660
661 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000663 if (Subtarget->isTargetDarwin()) {
664 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
665 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000666 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000667 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000668 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000669
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::SETCC, MVT::i32, Expand);
671 setOperationAction(ISD::SETCC, MVT::f32, Expand);
672 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000673 setOperationAction(ISD::SELECT, MVT::i32, Custom);
674 setOperationAction(ISD::SELECT, MVT::f32, Custom);
675 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
677 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
678 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000679
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
681 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
682 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
683 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
684 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000685
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000686 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::FSIN, MVT::f64, Expand);
688 setOperationAction(ISD::FSIN, MVT::f32, Expand);
689 setOperationAction(ISD::FCOS, MVT::f32, Expand);
690 setOperationAction(ISD::FCOS, MVT::f64, Expand);
691 setOperationAction(ISD::FREM, MVT::f64, Expand);
692 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000693 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
695 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000696 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::FPOW, MVT::f64, Expand);
698 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000699
Cameron Zwarich33390842011-07-08 21:39:21 +0000700 setOperationAction(ISD::FMA, MVT::f64, Expand);
701 setOperationAction(ISD::FMA, MVT::f32, Expand);
702
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000703 // Various VFP goodness
704 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000705 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
706 if (Subtarget->hasVFP2()) {
707 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
708 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
709 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
710 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
711 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000712 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000713 if (!Subtarget->hasFP16()) {
714 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
715 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000716 }
Evan Cheng110cf482008-04-01 01:50:16 +0000717 }
Evan Chenga8e29892007-01-19 07:51:42 +0000718
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000719 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000720 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000721 setTargetDAGCombine(ISD::ADD);
722 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000723 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000724
Owen Anderson080c0922010-11-05 19:27:46 +0000725 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000726 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000727 if (Subtarget->hasNEON())
728 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000729
Evan Chenga8e29892007-01-19 07:51:42 +0000730 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000731
Evan Chengf7d87ee2010-05-21 00:43:17 +0000732 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
733 setSchedulingPreference(Sched::RegPressure);
734 else
735 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000736
Evan Cheng05219282011-01-06 06:52:41 +0000737 //// temporary - rewrite interface to use type
738 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000739
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000740 // On ARM arguments smaller than 4 bytes are extended, so all arguments
741 // are at least 4 bytes aligned.
742 setMinStackArgumentAlignment(4);
743
Evan Chengfff606d2010-09-24 19:07:23 +0000744 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000745
746 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000747}
748
Andrew Trick32cec0a2011-01-19 02:35:27 +0000749// FIXME: It might make sense to define the representative register class as the
750// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
751// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
752// SPR's representative would be DPR_VFP2. This should work well if register
753// pressure tracking were modified such that a register use would increment the
754// pressure of the register class's representative and all of it's super
755// classes' representatives transitively. We have not implemented this because
756// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000757// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000758// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000759std::pair<const TargetRegisterClass*, uint8_t>
760ARMTargetLowering::findRepresentativeClass(EVT VT) const{
761 const TargetRegisterClass *RRC = 0;
762 uint8_t Cost = 1;
763 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000764 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000765 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000766 // Use DPR as representative register class for all floating point
767 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
768 // the cost is 1 for both f32 and f64.
769 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000770 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000771 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000772 // When NEON is used for SP, only half of the register file is available
773 // because operations that define both SP and DP results will be constrained
774 // to the VFP2 class (D0-D15). We currently model this constraint prior to
775 // coalescing by double-counting the SP regs. See the FIXME above.
776 if (Subtarget->useNEONForSinglePrecisionFP())
777 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000778 break;
779 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
780 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000781 RRC = ARM::DPRRegisterClass;
782 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000783 break;
784 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000785 RRC = ARM::DPRRegisterClass;
786 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000787 break;
788 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000789 RRC = ARM::DPRRegisterClass;
790 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000791 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000792 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000793 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000794}
795
Evan Chenga8e29892007-01-19 07:51:42 +0000796const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
797 switch (Opcode) {
798 default: return 0;
799 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000800 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000801 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000802 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
803 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000804 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000805 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
806 case ARMISD::tCALL: return "ARMISD::tCALL";
807 case ARMISD::BRCOND: return "ARMISD::BRCOND";
808 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000809 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000810 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
811 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
812 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000813 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000814 case ARMISD::CMPFP: return "ARMISD::CMPFP";
815 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000816 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000817 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
818 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000819
Jim Grosbach3482c802010-01-18 19:58:49 +0000820 case ARMISD::RBIT: return "ARMISD::RBIT";
821
Bob Wilson76a312b2010-03-19 22:51:32 +0000822 case ARMISD::FTOSI: return "ARMISD::FTOSI";
823 case ARMISD::FTOUI: return "ARMISD::FTOUI";
824 case ARMISD::SITOF: return "ARMISD::SITOF";
825 case ARMISD::UITOF: return "ARMISD::UITOF";
826
Evan Chenga8e29892007-01-19 07:51:42 +0000827 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
828 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
829 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000830
Evan Cheng342e3162011-08-30 01:34:54 +0000831 case ARMISD::ADDC: return "ARMISD::ADDC";
832 case ARMISD::ADDE: return "ARMISD::ADDE";
833 case ARMISD::SUBC: return "ARMISD::SUBC";
834 case ARMISD::SUBE: return "ARMISD::SUBE";
835
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000836 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
837 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000838
Evan Chengc5942082009-10-28 06:55:03 +0000839 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
840 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000841 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000842
Dale Johannesen51e28e62010-06-03 21:09:53 +0000843 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000844
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000845 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000846
Evan Cheng86198642009-08-07 00:34:42 +0000847 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
848
Jim Grosbach3728e962009-12-10 00:11:09 +0000849 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000850 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000851
Evan Chengdfed19f2010-11-03 06:34:55 +0000852 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
853
Bob Wilson5bafff32009-06-22 23:27:02 +0000854 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000855 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000856 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000857 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
858 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000859 case ARMISD::VCGEU: return "ARMISD::VCGEU";
860 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000861 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
862 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000863 case ARMISD::VCGTU: return "ARMISD::VCGTU";
864 case ARMISD::VTST: return "ARMISD::VTST";
865
866 case ARMISD::VSHL: return "ARMISD::VSHL";
867 case ARMISD::VSHRs: return "ARMISD::VSHRs";
868 case ARMISD::VSHRu: return "ARMISD::VSHRu";
869 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
870 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
871 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
872 case ARMISD::VSHRN: return "ARMISD::VSHRN";
873 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
874 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
875 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
876 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
877 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
878 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
879 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
880 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
881 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
882 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
883 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
884 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
885 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
886 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000887 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000888 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000889 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000890 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000891 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000892 case ARMISD::VREV64: return "ARMISD::VREV64";
893 case ARMISD::VREV32: return "ARMISD::VREV32";
894 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000895 case ARMISD::VZIP: return "ARMISD::VZIP";
896 case ARMISD::VUZP: return "ARMISD::VUZP";
897 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000898 case ARMISD::VTBL1: return "ARMISD::VTBL1";
899 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000900 case ARMISD::VMULLs: return "ARMISD::VMULLs";
901 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000902 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000903 case ARMISD::FMAX: return "ARMISD::FMAX";
904 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000905 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000906 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
907 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000908 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000909 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
910 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
911 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000912 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
913 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
914 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
915 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
916 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
917 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
918 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
919 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
920 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
921 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
922 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
923 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
924 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
925 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
926 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
927 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
928 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000929 }
930}
931
Evan Cheng06b666c2010-05-15 02:18:07 +0000932/// getRegClassFor - Return the register class that should be used for the
933/// specified value type.
934TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
935 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
936 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
937 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000938 if (Subtarget->hasNEON()) {
939 if (VT == MVT::v4i64)
940 return ARM::QQPRRegisterClass;
941 else if (VT == MVT::v8i64)
942 return ARM::QQQQPRRegisterClass;
943 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000944 return TargetLowering::getRegClassFor(VT);
945}
946
Eric Christopherab695882010-07-21 22:26:11 +0000947// Create a fast isel object.
948FastISel *
949ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
950 return ARM::createFastISel(funcInfo);
951}
952
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000953/// getMaximalGlobalOffset - Returns the maximal possible offset which can
954/// be used for loads / stores from the global.
955unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
956 return (Subtarget->isThumb1Only() ? 127 : 4095);
957}
958
Evan Cheng1cc39842010-05-20 23:26:43 +0000959Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000960 unsigned NumVals = N->getNumValues();
961 if (!NumVals)
962 return Sched::RegPressure;
963
964 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000965 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000966 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000967 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000968 if (VT.isFloatingPoint() || VT.isVector())
969 return Sched::Latency;
970 }
Evan Chengc10f5432010-05-28 23:25:23 +0000971
972 if (!N->isMachineOpcode())
973 return Sched::RegPressure;
974
975 // Load are scheduled for latency even if there instruction itinerary
976 // is not available.
977 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000978 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000979
Evan Chenge837dea2011-06-28 19:10:37 +0000980 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +0000981 return Sched::RegPressure;
982 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +0000983 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000984 return Sched::Latency;
985
Evan Cheng1cc39842010-05-20 23:26:43 +0000986 return Sched::RegPressure;
987}
988
Evan Chenga8e29892007-01-19 07:51:42 +0000989//===----------------------------------------------------------------------===//
990// Lowering Code
991//===----------------------------------------------------------------------===//
992
Evan Chenga8e29892007-01-19 07:51:42 +0000993/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
994static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
995 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000996 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000997 case ISD::SETNE: return ARMCC::NE;
998 case ISD::SETEQ: return ARMCC::EQ;
999 case ISD::SETGT: return ARMCC::GT;
1000 case ISD::SETGE: return ARMCC::GE;
1001 case ISD::SETLT: return ARMCC::LT;
1002 case ISD::SETLE: return ARMCC::LE;
1003 case ISD::SETUGT: return ARMCC::HI;
1004 case ISD::SETUGE: return ARMCC::HS;
1005 case ISD::SETULT: return ARMCC::LO;
1006 case ISD::SETULE: return ARMCC::LS;
1007 }
1008}
1009
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001010/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1011static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001012 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001013 CondCode2 = ARMCC::AL;
1014 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001015 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001016 case ISD::SETEQ:
1017 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1018 case ISD::SETGT:
1019 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1020 case ISD::SETGE:
1021 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1022 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001023 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001024 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1025 case ISD::SETO: CondCode = ARMCC::VC; break;
1026 case ISD::SETUO: CondCode = ARMCC::VS; break;
1027 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1028 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1029 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1030 case ISD::SETLT:
1031 case ISD::SETULT: CondCode = ARMCC::LT; break;
1032 case ISD::SETLE:
1033 case ISD::SETULE: CondCode = ARMCC::LE; break;
1034 case ISD::SETNE:
1035 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1036 }
Evan Chenga8e29892007-01-19 07:51:42 +00001037}
1038
Bob Wilson1f595bb2009-04-17 19:07:39 +00001039//===----------------------------------------------------------------------===//
1040// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001041//===----------------------------------------------------------------------===//
1042
1043#include "ARMGenCallingConv.inc"
1044
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001045/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1046/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001047CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001048 bool Return,
1049 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001050 switch (CC) {
1051 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001052 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001053 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001054 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001055 if (!Subtarget->isAAPCS_ABI())
1056 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1057 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1058 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1059 }
1060 // Fallthrough
1061 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001062 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001063 if (!Subtarget->isAAPCS_ABI())
1064 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1065 else if (Subtarget->hasVFP2() &&
1066 FloatABIType == FloatABI::Hard && !isVarArg)
1067 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1068 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1069 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001070 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001071 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001072 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001073 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001074 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001075 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001076 }
1077}
1078
Dan Gohman98ca4f22009-08-05 01:29:28 +00001079/// LowerCallResult - Lower the result values of a call into the
1080/// appropriate copies out of appropriate physical registers.
1081SDValue
1082ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001083 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001084 const SmallVectorImpl<ISD::InputArg> &Ins,
1085 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001086 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001087
Bob Wilson1f595bb2009-04-17 19:07:39 +00001088 // Assign locations to each value returned by this call.
1089 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001090 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1091 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001093 CCAssignFnForNode(CallConv, /* Return*/ true,
1094 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001095
1096 // Copy all of the result registers out of their specified physreg.
1097 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1098 CCValAssign VA = RVLocs[i];
1099
Bob Wilson80915242009-04-25 00:33:20 +00001100 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001101 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001102 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001103 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001105 Chain = Lo.getValue(1);
1106 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001108 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001109 InFlag);
1110 Chain = Hi.getValue(1);
1111 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001112 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001113
Owen Anderson825b72b2009-08-11 20:47:22 +00001114 if (VA.getLocVT() == MVT::v2f64) {
1115 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1116 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1117 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001118
1119 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001120 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001121 Chain = Lo.getValue(1);
1122 InFlag = Lo.getValue(2);
1123 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001124 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001125 Chain = Hi.getValue(1);
1126 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001127 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001128 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1129 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001130 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001132 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1133 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001134 Chain = Val.getValue(1);
1135 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136 }
Bob Wilson80915242009-04-25 00:33:20 +00001137
1138 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001139 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001140 case CCValAssign::Full: break;
1141 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001142 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001143 break;
1144 }
1145
Dan Gohman98ca4f22009-08-05 01:29:28 +00001146 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001147 }
1148
Dan Gohman98ca4f22009-08-05 01:29:28 +00001149 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001150}
1151
Bob Wilsondee46d72009-04-17 20:35:10 +00001152/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001153SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001154ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1155 SDValue StackPtr, SDValue Arg,
1156 DebugLoc dl, SelectionDAG &DAG,
1157 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001158 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001159 unsigned LocMemOffset = VA.getLocMemOffset();
1160 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1161 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001162 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001163 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001164 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001165}
1166
Dan Gohman98ca4f22009-08-05 01:29:28 +00001167void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001168 SDValue Chain, SDValue &Arg,
1169 RegsToPassVector &RegsToPass,
1170 CCValAssign &VA, CCValAssign &NextVA,
1171 SDValue &StackPtr,
1172 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001173 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001174
Jim Grosbache5165492009-11-09 00:11:35 +00001175 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001176 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001177 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1178
1179 if (NextVA.isRegLoc())
1180 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1181 else {
1182 assert(NextVA.isMemLoc());
1183 if (StackPtr.getNode() == 0)
1184 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1185
Dan Gohman98ca4f22009-08-05 01:29:28 +00001186 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1187 dl, DAG, NextVA,
1188 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001189 }
1190}
1191
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001193/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1194/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001195SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001196ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001197 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001198 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001199 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001200 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001201 const SmallVectorImpl<ISD::InputArg> &Ins,
1202 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001203 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001204 MachineFunction &MF = DAG.getMachineFunction();
1205 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1206 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001207 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001208 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001209 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001210 if (isTailCall) {
1211 // Check if it's really possible to do a tail call.
1212 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1213 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001214 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001215 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1216 // detected sibcalls.
1217 if (isTailCall) {
1218 ++NumTailCalls;
1219 IsSibCall = true;
1220 }
1221 }
Evan Chenga8e29892007-01-19 07:51:42 +00001222
Bob Wilson1f595bb2009-04-17 19:07:39 +00001223 // Analyze operands of the call, assigning locations to each operand.
1224 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001225 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1226 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001227 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001228 CCAssignFnForNode(CallConv, /* Return*/ false,
1229 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001230
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 // Get a count of how many bytes are to be pushed on the stack.
1232 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001233
Dale Johannesen51e28e62010-06-03 21:09:53 +00001234 // For tail calls, memory operands are available in our caller's stack.
1235 if (IsSibCall)
1236 NumBytes = 0;
1237
Evan Chenga8e29892007-01-19 07:51:42 +00001238 // Adjust the stack pointer for the new arguments...
1239 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001240 if (!IsSibCall)
1241 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001242
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001243 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001244
Bob Wilson5bafff32009-06-22 23:27:02 +00001245 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001246 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001247
Bob Wilson1f595bb2009-04-17 19:07:39 +00001248 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001249 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1251 i != e;
1252 ++i, ++realArgIdx) {
1253 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001254 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001255 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001256 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001257
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258 // Promote the value if needed.
1259 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001260 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261 case CCValAssign::Full: break;
1262 case CCValAssign::SExt:
1263 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1264 break;
1265 case CCValAssign::ZExt:
1266 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1267 break;
1268 case CCValAssign::AExt:
1269 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1270 break;
1271 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001272 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001273 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001274 }
1275
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001276 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001277 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001278 if (VA.getLocVT() == MVT::v2f64) {
1279 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1280 DAG.getConstant(0, MVT::i32));
1281 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1282 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001283
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001285 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1286
1287 VA = ArgLocs[++i]; // skip ahead to next loc
1288 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001290 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1291 } else {
1292 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001293
Dan Gohman98ca4f22009-08-05 01:29:28 +00001294 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1295 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001296 }
1297 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001298 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001299 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001300 }
1301 } else if (VA.isRegLoc()) {
1302 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001303 } else if (isByVal) {
1304 assert(VA.isMemLoc());
1305 unsigned offset = 0;
1306
1307 // True if this byval aggregate will be split between registers
1308 // and memory.
1309 if (CCInfo.isFirstByValRegValid()) {
1310 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1311 unsigned int i, j;
1312 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1313 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1314 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1315 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1316 MachinePointerInfo(),
1317 false, false, 0);
1318 MemOpChains.push_back(Load.getValue(1));
1319 RegsToPass.push_back(std::make_pair(j, Load));
1320 }
1321 offset = ARM::R4 - CCInfo.getFirstByValReg();
1322 CCInfo.clearFirstByValReg();
1323 }
1324
1325 unsigned LocMemOffset = VA.getLocMemOffset();
1326 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1327 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1328 StkPtrOff);
1329 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1330 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1331 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1332 MVT::i32);
1333 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1334 Flags.getByValAlign(),
1335 /*isVolatile=*/false,
1336 /*AlwaysInline=*/false,
1337 MachinePointerInfo(0),
1338 MachinePointerInfo(0)));
1339
1340 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001341 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001342
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1344 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001345 }
Evan Chenga8e29892007-01-19 07:51:42 +00001346 }
1347
1348 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001349 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001350 &MemOpChains[0], MemOpChains.size());
1351
1352 // Build a sequence of copy-to-reg nodes chained together with token chain
1353 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001354 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001355 // Tail call byval lowering might overwrite argument registers so in case of
1356 // tail call optimization the copies to registers are lowered later.
1357 if (!isTailCall)
1358 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1359 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1360 RegsToPass[i].second, InFlag);
1361 InFlag = Chain.getValue(1);
1362 }
Evan Chenga8e29892007-01-19 07:51:42 +00001363
Dale Johannesen51e28e62010-06-03 21:09:53 +00001364 // For tail calls lower the arguments to the 'real' stack slot.
1365 if (isTailCall) {
1366 // Force all the incoming stack arguments to be loaded from the stack
1367 // before any new outgoing arguments are stored to the stack, because the
1368 // outgoing stack slots may alias the incoming argument stack slots, and
1369 // the alias isn't otherwise explicit. This is slightly more conservative
1370 // than necessary, because it means that each store effectively depends
1371 // on every argument instead of just those arguments it would clobber.
1372
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001373 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001374 InFlag = SDValue();
1375 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1376 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1377 RegsToPass[i].second, InFlag);
1378 InFlag = Chain.getValue(1);
1379 }
1380 InFlag =SDValue();
1381 }
1382
Bill Wendling056292f2008-09-16 21:48:12 +00001383 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1384 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1385 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001386 bool isDirect = false;
1387 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001388 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001389 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001390
1391 if (EnableARMLongCalls) {
1392 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1393 && "long-calls with non-static relocation model!");
1394 // Handle a global address or an external symbol. If it's not one of
1395 // those, the target's already in a register, so we don't need to do
1396 // anything extra.
1397 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001398 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001399 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001400 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001401 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1402 ARMPCLabelIndex,
1403 ARMCP::CPValue, 0);
1404 // Get the address of the callee into a register
1405 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1406 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1407 Callee = DAG.getLoad(getPointerTy(), dl,
1408 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001409 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001410 false, false, 0);
1411 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1412 const char *Sym = S->getSymbol();
1413
1414 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001415 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001416 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1417 Sym, ARMPCLabelIndex, 0);
1418 // Get the address of the callee into a register
1419 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1420 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1421 Callee = DAG.getLoad(getPointerTy(), dl,
1422 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001423 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001424 false, false, 0);
1425 }
1426 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001427 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001428 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001429 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001430 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001431 getTargetMachine().getRelocationModel() != Reloc::Static;
1432 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001433 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001434 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001435 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001436 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001437 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001438 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001439 ARMPCLabelIndex,
1440 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001441 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001442 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001443 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001444 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001445 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001446 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001447 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001448 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001449 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001450 } else {
1451 // On ELF targets for PIC code, direct calls should go through the PLT
1452 unsigned OpFlags = 0;
1453 if (Subtarget->isTargetELF() &&
1454 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1455 OpFlags = ARMII::MO_PLT;
1456 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1457 }
Bill Wendling056292f2008-09-16 21:48:12 +00001458 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001459 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001460 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001461 getTargetMachine().getRelocationModel() != Reloc::Static;
1462 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001463 // tBX takes a register source operand.
1464 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001465 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001466 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001467 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001468 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001469 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001470 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001471 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001472 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001473 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001474 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001475 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001476 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001477 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001478 } else {
1479 unsigned OpFlags = 0;
1480 // On ELF targets for PIC code, direct calls should go through the PLT
1481 if (Subtarget->isTargetELF() &&
1482 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1483 OpFlags = ARMII::MO_PLT;
1484 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1485 }
Evan Chenga8e29892007-01-19 07:51:42 +00001486 }
1487
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001488 // FIXME: handle tail calls differently.
1489 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001490 if (Subtarget->isThumb()) {
1491 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001492 CallOpc = ARMISD::CALL_NOLINK;
1493 else
1494 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1495 } else {
1496 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001497 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1498 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001499 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001500
Dan Gohman475871a2008-07-27 21:46:04 +00001501 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001502 Ops.push_back(Chain);
1503 Ops.push_back(Callee);
1504
1505 // Add argument registers to the end of the list so that they are known live
1506 // into the call.
1507 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1508 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1509 RegsToPass[i].second.getValueType()));
1510
Gabor Greifba36cb52008-08-28 21:40:38 +00001511 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001512 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001513
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001514 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001515 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001516 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001517
Duncan Sands4bdcb612008-07-02 17:40:58 +00001518 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001519 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001520 InFlag = Chain.getValue(1);
1521
Chris Lattnere563bbc2008-10-11 22:08:30 +00001522 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1523 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001524 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001525 InFlag = Chain.getValue(1);
1526
Bob Wilson1f595bb2009-04-17 19:07:39 +00001527 // Handle result values, copying them out of physregs into vregs that we
1528 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001529 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1530 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001531}
1532
Stuart Hastingsf222e592011-02-28 17:17:53 +00001533/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001534/// on the stack. Remember the next parameter register to allocate,
1535/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001536/// this.
1537void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001538llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1539 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1540 assert((State->getCallOrPrologue() == Prologue ||
1541 State->getCallOrPrologue() == Call) &&
1542 "unhandled ParmContext");
1543 if ((!State->isFirstByValRegValid()) &&
1544 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1545 State->setFirstByValReg(reg);
1546 // At a call site, a byval parameter that is split between
1547 // registers and memory needs its size truncated here. In a
1548 // function prologue, such byval parameters are reassembled in
1549 // memory, and are not truncated.
1550 if (State->getCallOrPrologue() == Call) {
1551 unsigned excess = 4 * (ARM::R4 - reg);
1552 assert(size >= excess && "expected larger existing stack allocation");
1553 size -= excess;
1554 }
1555 }
1556 // Confiscate any remaining parameter registers to preclude their
1557 // assignment to subsequent parameters.
1558 while (State->AllocateReg(GPRArgRegs, 4))
1559 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001560}
1561
Dale Johannesen51e28e62010-06-03 21:09:53 +00001562/// MatchingStackOffset - Return true if the given stack call argument is
1563/// already available in the same position (relatively) of the caller's
1564/// incoming argument stack.
1565static
1566bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1567 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1568 const ARMInstrInfo *TII) {
1569 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1570 int FI = INT_MAX;
1571 if (Arg.getOpcode() == ISD::CopyFromReg) {
1572 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001573 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001574 return false;
1575 MachineInstr *Def = MRI->getVRegDef(VR);
1576 if (!Def)
1577 return false;
1578 if (!Flags.isByVal()) {
1579 if (!TII->isLoadFromStackSlot(Def, FI))
1580 return false;
1581 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001582 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001583 }
1584 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1585 if (Flags.isByVal())
1586 // ByVal argument is passed in as a pointer but it's now being
1587 // dereferenced. e.g.
1588 // define @foo(%struct.X* %A) {
1589 // tail call @bar(%struct.X* byval %A)
1590 // }
1591 return false;
1592 SDValue Ptr = Ld->getBasePtr();
1593 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1594 if (!FINode)
1595 return false;
1596 FI = FINode->getIndex();
1597 } else
1598 return false;
1599
1600 assert(FI != INT_MAX);
1601 if (!MFI->isFixedObjectIndex(FI))
1602 return false;
1603 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1604}
1605
1606/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1607/// for tail call optimization. Targets which want to do tail call
1608/// optimization should implement this function.
1609bool
1610ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1611 CallingConv::ID CalleeCC,
1612 bool isVarArg,
1613 bool isCalleeStructRet,
1614 bool isCallerStructRet,
1615 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001616 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001617 const SmallVectorImpl<ISD::InputArg> &Ins,
1618 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001619 const Function *CallerF = DAG.getMachineFunction().getFunction();
1620 CallingConv::ID CallerCC = CallerF->getCallingConv();
1621 bool CCMatch = CallerCC == CalleeCC;
1622
1623 // Look for obvious safe cases to perform tail call optimization that do not
1624 // require ABI changes. This is what gcc calls sibcall.
1625
Jim Grosbach7616b642010-06-16 23:45:49 +00001626 // Do not sibcall optimize vararg calls unless the call site is not passing
1627 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001628 if (isVarArg && !Outs.empty())
1629 return false;
1630
1631 // Also avoid sibcall optimization if either caller or callee uses struct
1632 // return semantics.
1633 if (isCalleeStructRet || isCallerStructRet)
1634 return false;
1635
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001636 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001637 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1638 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1639 // support in the assembler and linker to be used. This would need to be
1640 // fixed to fully support tail calls in Thumb1.
1641 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001642 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1643 // LR. This means if we need to reload LR, it takes an extra instructions,
1644 // which outweighs the value of the tail call; but here we don't know yet
1645 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001646 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001647 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001648
1649 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1650 // but we need to make sure there are enough registers; the only valid
1651 // registers are the 4 used for parameters. We don't currently do this
1652 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001653 if (Subtarget->isThumb1Only())
1654 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001655
Dale Johannesen51e28e62010-06-03 21:09:53 +00001656 // If the calling conventions do not match, then we'd better make sure the
1657 // results are returned in the same way as what the caller expects.
1658 if (!CCMatch) {
1659 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001660 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1661 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001662 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1663
1664 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001665 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1666 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001667 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1668
1669 if (RVLocs1.size() != RVLocs2.size())
1670 return false;
1671 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1672 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1673 return false;
1674 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1675 return false;
1676 if (RVLocs1[i].isRegLoc()) {
1677 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1678 return false;
1679 } else {
1680 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1681 return false;
1682 }
1683 }
1684 }
1685
1686 // If the callee takes no arguments then go on to check the results of the
1687 // call.
1688 if (!Outs.empty()) {
1689 // Check if stack adjustment is needed. For now, do not do this if any
1690 // argument is passed on the stack.
1691 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001692 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1693 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001694 CCInfo.AnalyzeCallOperands(Outs,
1695 CCAssignFnForNode(CalleeCC, false, isVarArg));
1696 if (CCInfo.getNextStackOffset()) {
1697 MachineFunction &MF = DAG.getMachineFunction();
1698
1699 // Check if the arguments are already laid out in the right way as
1700 // the caller's fixed stack objects.
1701 MachineFrameInfo *MFI = MF.getFrameInfo();
1702 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1703 const ARMInstrInfo *TII =
1704 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001705 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1706 i != e;
1707 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001708 CCValAssign &VA = ArgLocs[i];
1709 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001710 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001711 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001712 if (VA.getLocInfo() == CCValAssign::Indirect)
1713 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001714 if (VA.needsCustom()) {
1715 // f64 and vector types are split into multiple registers or
1716 // register/stack-slot combinations. The types will not match
1717 // the registers; give up on memory f64 refs until we figure
1718 // out what to do about this.
1719 if (!VA.isRegLoc())
1720 return false;
1721 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001722 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001723 if (RegVT == MVT::v2f64) {
1724 if (!ArgLocs[++i].isRegLoc())
1725 return false;
1726 if (!ArgLocs[++i].isRegLoc())
1727 return false;
1728 }
1729 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001730 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1731 MFI, MRI, TII))
1732 return false;
1733 }
1734 }
1735 }
1736 }
1737
1738 return true;
1739}
1740
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741SDValue
1742ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001743 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001744 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001745 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001746 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001747
Bob Wilsondee46d72009-04-17 20:35:10 +00001748 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001749 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001750
Bob Wilsondee46d72009-04-17 20:35:10 +00001751 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001752 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1753 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001754
Dan Gohman98ca4f22009-08-05 01:29:28 +00001755 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001756 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1757 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001758
1759 // If this is the first return lowered for this function, add
1760 // the regs to the liveout set for the function.
1761 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1762 for (unsigned i = 0; i != RVLocs.size(); ++i)
1763 if (RVLocs[i].isRegLoc())
1764 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001765 }
1766
Bob Wilson1f595bb2009-04-17 19:07:39 +00001767 SDValue Flag;
1768
1769 // Copy the result values into the output registers.
1770 for (unsigned i = 0, realRVLocIdx = 0;
1771 i != RVLocs.size();
1772 ++i, ++realRVLocIdx) {
1773 CCValAssign &VA = RVLocs[i];
1774 assert(VA.isRegLoc() && "Can only return in registers!");
1775
Dan Gohmanc9403652010-07-07 15:54:55 +00001776 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001777
1778 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001779 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001780 case CCValAssign::Full: break;
1781 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001782 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001783 break;
1784 }
1785
Bob Wilson1f595bb2009-04-17 19:07:39 +00001786 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001787 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001788 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1790 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001791 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001792 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001793
1794 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1795 Flag = Chain.getValue(1);
1796 VA = RVLocs[++i]; // skip ahead to next loc
1797 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1798 HalfGPRs.getValue(1), Flag);
1799 Flag = Chain.getValue(1);
1800 VA = RVLocs[++i]; // skip ahead to next loc
1801
1802 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001803 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1804 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001805 }
1806 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1807 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001808 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001810 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001811 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001812 VA = RVLocs[++i]; // skip ahead to next loc
1813 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1814 Flag);
1815 } else
1816 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1817
Bob Wilsondee46d72009-04-17 20:35:10 +00001818 // Guarantee that all emitted copies are
1819 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001820 Flag = Chain.getValue(1);
1821 }
1822
1823 SDValue result;
1824 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001825 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001826 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001827 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001828
1829 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001830}
1831
Evan Cheng3d2125c2010-11-30 23:55:39 +00001832bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1833 if (N->getNumValues() != 1)
1834 return false;
1835 if (!N->hasNUsesOfValue(1, 0))
1836 return false;
1837
1838 unsigned NumCopies = 0;
1839 SDNode* Copies[2];
1840 SDNode *Use = *N->use_begin();
1841 if (Use->getOpcode() == ISD::CopyToReg) {
1842 Copies[NumCopies++] = Use;
1843 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1844 // f64 returned in a pair of GPRs.
1845 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1846 UI != UE; ++UI) {
1847 if (UI->getOpcode() != ISD::CopyToReg)
1848 return false;
1849 Copies[UI.getUse().getResNo()] = *UI;
1850 ++NumCopies;
1851 }
1852 } else if (Use->getOpcode() == ISD::BITCAST) {
1853 // f32 returned in a single GPR.
1854 if (!Use->hasNUsesOfValue(1, 0))
1855 return false;
1856 Use = *Use->use_begin();
1857 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1858 return false;
1859 Copies[NumCopies++] = Use;
1860 } else {
1861 return false;
1862 }
1863
1864 if (NumCopies != 1 && NumCopies != 2)
1865 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001866
1867 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001868 for (unsigned i = 0; i < NumCopies; ++i) {
1869 SDNode *Copy = Copies[i];
1870 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1871 UI != UE; ++UI) {
1872 if (UI->getOpcode() == ISD::CopyToReg) {
1873 SDNode *Use = *UI;
1874 if (Use == Copies[0] || Use == Copies[1])
1875 continue;
1876 return false;
1877 }
1878 if (UI->getOpcode() != ARMISD::RET_FLAG)
1879 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001880 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001881 }
1882 }
1883
Evan Cheng1bf891a2010-12-01 22:59:46 +00001884 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001885}
1886
Evan Cheng485fafc2011-03-21 01:19:09 +00001887bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1888 if (!EnableARMTailCalls)
1889 return false;
1890
1891 if (!CI->isTailCall())
1892 return false;
1893
1894 return !Subtarget->isThumb1Only();
1895}
1896
Bob Wilsonb62d2572009-11-03 00:02:05 +00001897// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1898// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1899// one of the above mentioned nodes. It has to be wrapped because otherwise
1900// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1901// be used to form addressing mode. These wrapped nodes will be selected
1902// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001903static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001904 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001905 // FIXME there is no actual debug info here
1906 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001907 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001908 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001909 if (CP->isMachineConstantPoolEntry())
1910 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1911 CP->getAlignment());
1912 else
1913 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1914 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001915 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001916}
1917
Jim Grosbache1102ca2010-07-19 17:20:38 +00001918unsigned ARMTargetLowering::getJumpTableEncoding() const {
1919 return MachineJumpTableInfo::EK_Inline;
1920}
1921
Dan Gohmand858e902010-04-17 15:26:15 +00001922SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1923 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001924 MachineFunction &MF = DAG.getMachineFunction();
1925 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1926 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001927 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001928 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001929 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001930 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1931 SDValue CPAddr;
1932 if (RelocM == Reloc::Static) {
1933 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1934 } else {
1935 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001936 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001937 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1938 ARMCP::CPBlockAddress,
1939 PCAdj);
1940 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1941 }
1942 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1943 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001944 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001945 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001946 if (RelocM == Reloc::Static)
1947 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001948 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001949 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001950}
1951
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001952// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001953SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001954ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001955 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001956 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001957 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001958 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001959 MachineFunction &MF = DAG.getMachineFunction();
1960 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001961 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001962 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001963 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001964 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001965 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001966 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001967 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001968 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001969 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001970 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001971
Evan Chenge7e0d622009-11-06 22:24:13 +00001972 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001973 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001974
1975 // call __tls_get_addr.
1976 ArgListTy Args;
1977 ArgListEntry Entry;
1978 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001979 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001980 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001981 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001982 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001983 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00001984 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001985 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001986 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001987 return CallResult.first;
1988}
1989
1990// Lower ISD::GlobalTLSAddress using the "initial exec" or
1991// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001992SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001993ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001994 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001995 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001996 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001997 SDValue Offset;
1998 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001999 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002000 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002001 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002002
Chris Lattner4fb63d02009-07-15 04:12:33 +00002003 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002004 MachineFunction &MF = DAG.getMachineFunction();
2005 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002006 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002007 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002008 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2009 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002010 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002011 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002012 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002014 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002015 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002016 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002017 Chain = Offset.getValue(1);
2018
Evan Chenge7e0d622009-11-06 22:24:13 +00002019 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002020 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002021
Evan Cheng9eda6892009-10-31 03:39:36 +00002022 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002023 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002024 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002025 } else {
2026 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002027 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002028 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002029 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002030 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002031 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002032 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002033 }
2034
2035 // The address of the thread local variable is the add of the thread
2036 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002037 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002038}
2039
Dan Gohman475871a2008-07-27 21:46:04 +00002040SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002041ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002042 // TODO: implement the "local dynamic" model
2043 assert(Subtarget->isTargetELF() &&
2044 "TLS not implemented for non-ELF targets");
2045 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2046 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2047 // otherwise use the "Local Exec" TLS Model
2048 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2049 return LowerToTLSGeneralDynamicModel(GA, DAG);
2050 else
2051 return LowerToTLSExecModels(GA, DAG);
2052}
2053
Dan Gohman475871a2008-07-27 21:46:04 +00002054SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002055 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002056 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002057 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002058 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002059 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2060 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002061 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002062 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002063 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002064 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002065 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002066 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002067 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002068 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002069 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002070 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002071 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002072 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002073 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002074 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002075 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002076 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002077 }
2078
2079 // If we have T2 ops, we can materialize the address directly via movt/movw
2080 // pair. This is always cheaper.
2081 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002082 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002083 // FIXME: Once remat is capable of dealing with instructions with register
2084 // operands, expand this into two nodes.
2085 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2086 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002087 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002088 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2089 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2090 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2091 MachinePointerInfo::getConstantPool(),
2092 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002093 }
2094}
2095
Dan Gohman475871a2008-07-27 21:46:04 +00002096SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002097 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002098 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002099 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002100 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002101 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002102 MachineFunction &MF = DAG.getMachineFunction();
2103 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2104
Evan Cheng4abce0c2011-05-27 20:11:27 +00002105 // FIXME: Enable this for static codegen when tool issues are fixed.
2106 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002107 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002108 // FIXME: Once remat is capable of dealing with instructions with register
2109 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002110 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002111 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2112 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2113
Evan Cheng53519f02011-01-21 18:55:51 +00002114 unsigned Wrapper = (RelocM == Reloc::PIC_)
2115 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2116 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002117 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002118 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2119 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2120 MachinePointerInfo::getGOT(), false, false, 0);
2121 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002122 }
2123
2124 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002125 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002126 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002127 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002128 } else {
2129 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002130 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2131 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002132 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002133 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002134 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002136
Evan Cheng9eda6892009-10-31 03:39:36 +00002137 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002138 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002139 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002140 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002141
2142 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002143 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002144 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002145 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002146
Evan Cheng63476a82009-09-03 07:04:02 +00002147 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002148 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002149 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002150
2151 return Result;
2152}
2153
Dan Gohman475871a2008-07-27 21:46:04 +00002154SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002155 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002156 assert(Subtarget->isTargetELF() &&
2157 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002158 MachineFunction &MF = DAG.getMachineFunction();
2159 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002160 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002161 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002162 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002163 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002164 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2165 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002166 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002167 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002168 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002169 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002170 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002171 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002172 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002173 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002174}
2175
Jim Grosbach0e0da732009-05-12 23:59:14 +00002176SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002177ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2178 const {
2179 DebugLoc dl = Op.getDebugLoc();
2180 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002181 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002182}
2183
2184SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002185ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2186 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002187 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002188 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2189 Op.getOperand(1), Val);
2190}
2191
2192SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002193ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2194 DebugLoc dl = Op.getDebugLoc();
2195 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2196 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2197}
2198
2199SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002200ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002201 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002202 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002203 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002204 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002205 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002206 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002207 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002208 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2209 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002210 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002211 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002212 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002213 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002214 EVT PtrVT = getPointerTy();
2215 DebugLoc dl = Op.getDebugLoc();
2216 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2217 SDValue CPAddr;
2218 unsigned PCAdj = (RelocM != Reloc::PIC_)
2219 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002220 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002221 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2222 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002223 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002224 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002225 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002226 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002227 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002228 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002229
2230 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002231 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002232 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2233 }
2234 return Result;
2235 }
Evan Cheng92e39162011-03-29 23:06:19 +00002236 case Intrinsic::arm_neon_vmulls:
2237 case Intrinsic::arm_neon_vmullu: {
2238 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2239 ? ARMISD::VMULLs : ARMISD::VMULLu;
2240 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2241 Op.getOperand(1), Op.getOperand(2));
2242 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002243 }
2244}
2245
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002246static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002247 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002248 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002249 if (!Subtarget->hasDataBarrier()) {
2250 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2251 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2252 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002253 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002254 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002255 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002256 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002257 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002258
2259 SDValue Op5 = Op.getOperand(5);
2260 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2261 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2262 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2263 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2264
2265 ARM_MB::MemBOpt DMBOpt;
2266 if (isDeviceBarrier)
2267 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2268 else
2269 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2270 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2271 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002272}
2273
Eli Friedman26689ac2011-08-03 21:06:02 +00002274
2275static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2276 const ARMSubtarget *Subtarget) {
2277 // FIXME: handle "fence singlethread" more efficiently.
2278 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002279 if (!Subtarget->hasDataBarrier()) {
2280 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2281 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2282 // here.
2283 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2284 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002285 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002286 DAG.getConstant(0, MVT::i32));
2287 }
2288
Eli Friedman26689ac2011-08-03 21:06:02 +00002289 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002290 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002291}
2292
Evan Chengdfed19f2010-11-03 06:34:55 +00002293static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2294 const ARMSubtarget *Subtarget) {
2295 // ARM pre v5TE and Thumb1 does not have preload instructions.
2296 if (!(Subtarget->isThumb2() ||
2297 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2298 // Just preserve the chain.
2299 return Op.getOperand(0);
2300
2301 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002302 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2303 if (!isRead &&
2304 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2305 // ARMv7 with MP extension has PLDW.
2306 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002307
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002308 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2309 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002310 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002311 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002312 isData = ~isData & 1;
2313 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002314
2315 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002316 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2317 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002318}
2319
Dan Gohman1e93df62010-04-17 14:41:14 +00002320static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2321 MachineFunction &MF = DAG.getMachineFunction();
2322 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2323
Evan Chenga8e29892007-01-19 07:51:42 +00002324 // vastart just stores the address of the VarArgsFrameIndex slot into the
2325 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002326 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002327 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002328 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002329 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002330 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2331 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002332}
2333
Dan Gohman475871a2008-07-27 21:46:04 +00002334SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002335ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2336 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002337 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002338 MachineFunction &MF = DAG.getMachineFunction();
2339 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2340
2341 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002342 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002343 RC = ARM::tGPRRegisterClass;
2344 else
2345 RC = ARM::GPRRegisterClass;
2346
2347 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002348 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002349 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002350
2351 SDValue ArgValue2;
2352 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002353 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002354 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002355
2356 // Create load node to retrieve arguments from the stack.
2357 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002358 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002359 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002360 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002361 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002362 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002363 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002364 }
2365
Jim Grosbache5165492009-11-09 00:11:35 +00002366 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002367}
2368
Stuart Hastingsc7315872011-04-20 16:47:52 +00002369void
2370ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2371 unsigned &VARegSize, unsigned &VARegSaveSize)
2372 const {
2373 unsigned NumGPRs;
2374 if (CCInfo.isFirstByValRegValid())
2375 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2376 else {
2377 unsigned int firstUnalloced;
2378 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2379 sizeof(GPRArgRegs) /
2380 sizeof(GPRArgRegs[0]));
2381 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2382 }
2383
2384 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2385 VARegSize = NumGPRs * 4;
2386 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2387}
2388
2389// The remaining GPRs hold either the beginning of variable-argument
2390// data, or the beginning of an aggregate passed by value (usuall
2391// byval). Either way, we allocate stack slots adjacent to the data
2392// provided by our caller, and store the unallocated registers there.
2393// If this is a variadic function, the va_list pointer will begin with
2394// these values; otherwise, this reassembles a (byval) structure that
2395// was split between registers and memory.
2396void
2397ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2398 DebugLoc dl, SDValue &Chain,
2399 unsigned ArgOffset) const {
2400 MachineFunction &MF = DAG.getMachineFunction();
2401 MachineFrameInfo *MFI = MF.getFrameInfo();
2402 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2403 unsigned firstRegToSaveIndex;
2404 if (CCInfo.isFirstByValRegValid())
2405 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2406 else {
2407 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2408 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2409 }
2410
2411 unsigned VARegSize, VARegSaveSize;
2412 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2413 if (VARegSaveSize) {
2414 // If this function is vararg, store any remaining integer argument regs
2415 // to their spots on the stack so that they may be loaded by deferencing
2416 // the result of va_next.
2417 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002418 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2419 ArgOffset + VARegSaveSize
2420 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002421 false));
2422 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2423 getPointerTy());
2424
2425 SmallVector<SDValue, 4> MemOps;
2426 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2427 TargetRegisterClass *RC;
2428 if (AFI->isThumb1OnlyFunction())
2429 RC = ARM::tGPRRegisterClass;
2430 else
2431 RC = ARM::GPRRegisterClass;
2432
2433 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2434 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2435 SDValue Store =
2436 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002437 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002438 false, false, 0);
2439 MemOps.push_back(Store);
2440 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2441 DAG.getConstant(4, getPointerTy()));
2442 }
2443 if (!MemOps.empty())
2444 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2445 &MemOps[0], MemOps.size());
2446 } else
2447 // This will point to the next argument passed via stack.
2448 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2449}
2450
Bob Wilson5bafff32009-06-22 23:27:02 +00002451SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002452ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002453 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002454 const SmallVectorImpl<ISD::InputArg>
2455 &Ins,
2456 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002457 SmallVectorImpl<SDValue> &InVals)
2458 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002459 MachineFunction &MF = DAG.getMachineFunction();
2460 MachineFrameInfo *MFI = MF.getFrameInfo();
2461
Bob Wilson1f595bb2009-04-17 19:07:39 +00002462 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2463
2464 // Assign locations to all of the incoming arguments.
2465 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002466 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2467 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002468 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002469 CCAssignFnForNode(CallConv, /* Return*/ false,
2470 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002471
2472 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002473 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002474
Stuart Hastingsf222e592011-02-28 17:17:53 +00002475 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002476 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2477 CCValAssign &VA = ArgLocs[i];
2478
Bob Wilsondee46d72009-04-17 20:35:10 +00002479 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002480 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002481 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002482
Bob Wilson1f595bb2009-04-17 19:07:39 +00002483 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002484 // f64 and vector types are split up into multiple registers or
2485 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002486 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002487 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002488 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002489 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002490 SDValue ArgValue2;
2491 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002492 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002493 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2494 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002495 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002496 false, false, 0);
2497 } else {
2498 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2499 Chain, DAG, dl);
2500 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002501 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2502 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002503 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002504 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002505 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2506 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002507 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002508
Bob Wilson5bafff32009-06-22 23:27:02 +00002509 } else {
2510 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002511
Owen Anderson825b72b2009-08-11 20:47:22 +00002512 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002513 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002514 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002515 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002516 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002517 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002518 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002519 RC = (AFI->isThumb1OnlyFunction() ?
2520 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002521 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002522 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002523
2524 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002525 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002526 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002527 }
2528
2529 // If this is an 8 or 16-bit value, it is really passed promoted
2530 // to 32 bits. Insert an assert[sz]ext to capture this, then
2531 // truncate to the right size.
2532 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002533 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002534 case CCValAssign::Full: break;
2535 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002536 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002537 break;
2538 case CCValAssign::SExt:
2539 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2540 DAG.getValueType(VA.getValVT()));
2541 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2542 break;
2543 case CCValAssign::ZExt:
2544 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2545 DAG.getValueType(VA.getValVT()));
2546 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2547 break;
2548 }
2549
Dan Gohman98ca4f22009-08-05 01:29:28 +00002550 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002551
2552 } else { // VA.isRegLoc()
2553
2554 // sanity check
2555 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002556 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002557
Stuart Hastingsf222e592011-02-28 17:17:53 +00002558 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002559
Stuart Hastingsf222e592011-02-28 17:17:53 +00002560 // Some Ins[] entries become multiple ArgLoc[] entries.
2561 // Process them only once.
2562 if (index != lastInsIndex)
2563 {
2564 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002565 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002566 // This can be changed with more analysis.
2567 // In case of tail call optimization mark all arguments mutable.
2568 // Since they could be overwritten by lowering of arguments in case of
2569 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002570 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002571 unsigned VARegSize, VARegSaveSize;
2572 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2573 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2574 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002575 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002576 int FI = MFI->CreateFixedObject(Bytes,
2577 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002578 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2579 } else {
2580 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2581 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002582
Stuart Hastingsf222e592011-02-28 17:17:53 +00002583 // Create load nodes to retrieve arguments from the stack.
2584 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2585 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2586 MachinePointerInfo::getFixedStack(FI),
2587 false, false, 0));
2588 }
2589 lastInsIndex = index;
2590 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002591 }
2592 }
2593
2594 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002595 if (isVarArg)
2596 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002597
Dan Gohman98ca4f22009-08-05 01:29:28 +00002598 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002599}
2600
2601/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002602static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002603 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002604 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002605 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002606 // Maybe this has already been legalized into the constant pool?
2607 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002608 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002609 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002610 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002611 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002612 }
2613 }
2614 return false;
2615}
2616
Evan Chenga8e29892007-01-19 07:51:42 +00002617/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2618/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002619SDValue
2620ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002621 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002622 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002623 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002624 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002625 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002626 // Constant does not fit, try adjusting it by one?
2627 switch (CC) {
2628 default: break;
2629 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002630 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002631 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002632 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002633 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002634 }
2635 break;
2636 case ISD::SETULT:
2637 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002638 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002639 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002640 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002641 }
2642 break;
2643 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002644 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002645 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002646 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002647 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002648 }
2649 break;
2650 case ISD::SETULE:
2651 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002652 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002653 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002655 }
2656 break;
2657 }
2658 }
2659 }
2660
2661 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002662 ARMISD::NodeType CompareType;
2663 switch (CondCode) {
2664 default:
2665 CompareType = ARMISD::CMP;
2666 break;
2667 case ARMCC::EQ:
2668 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002669 // Uses only Z Flag
2670 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002671 break;
2672 }
Evan Cheng218977b2010-07-13 19:27:42 +00002673 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002674 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002675}
2676
2677/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002678SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002679ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002680 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002681 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002682 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002683 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002684 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002685 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2686 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002687}
2688
Bob Wilson79f56c92011-03-08 01:17:20 +00002689/// duplicateCmp - Glue values can have only one use, so this function
2690/// duplicates a comparison node.
2691SDValue
2692ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2693 unsigned Opc = Cmp.getOpcode();
2694 DebugLoc DL = Cmp.getDebugLoc();
2695 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2696 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2697
2698 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2699 Cmp = Cmp.getOperand(0);
2700 Opc = Cmp.getOpcode();
2701 if (Opc == ARMISD::CMPFP)
2702 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2703 else {
2704 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2705 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2706 }
2707 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2708}
2709
Bill Wendlingde2b1512010-08-11 08:43:16 +00002710SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2711 SDValue Cond = Op.getOperand(0);
2712 SDValue SelectTrue = Op.getOperand(1);
2713 SDValue SelectFalse = Op.getOperand(2);
2714 DebugLoc dl = Op.getDebugLoc();
2715
2716 // Convert:
2717 //
2718 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2719 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2720 //
2721 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2722 const ConstantSDNode *CMOVTrue =
2723 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2724 const ConstantSDNode *CMOVFalse =
2725 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2726
2727 if (CMOVTrue && CMOVFalse) {
2728 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2729 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2730
2731 SDValue True;
2732 SDValue False;
2733 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2734 True = SelectTrue;
2735 False = SelectFalse;
2736 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2737 True = SelectFalse;
2738 False = SelectTrue;
2739 }
2740
2741 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002742 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002743 SDValue ARMcc = Cond.getOperand(2);
2744 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002745 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002746 assert(True.getValueType() == VT);
2747 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002748 }
2749 }
2750 }
2751
2752 return DAG.getSelectCC(dl, Cond,
2753 DAG.getConstant(0, Cond.getValueType()),
2754 SelectTrue, SelectFalse, ISD::SETNE);
2755}
2756
Dan Gohmand858e902010-04-17 15:26:15 +00002757SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002758 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002759 SDValue LHS = Op.getOperand(0);
2760 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002761 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002762 SDValue TrueVal = Op.getOperand(2);
2763 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002764 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002765
Owen Anderson825b72b2009-08-11 20:47:22 +00002766 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002767 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002768 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002769 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Evan Chenge721f5c2011-07-13 00:42:17 +00002770 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002771 }
2772
2773 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002774 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002775
Evan Cheng218977b2010-07-13 19:27:42 +00002776 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2777 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002778 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002779 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002780 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002781 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002782 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002783 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002784 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002785 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002786 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002787 }
2788 return Result;
2789}
2790
Evan Cheng218977b2010-07-13 19:27:42 +00002791/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2792/// to morph to an integer compare sequence.
2793static bool canChangeToInt(SDValue Op, bool &SeenZero,
2794 const ARMSubtarget *Subtarget) {
2795 SDNode *N = Op.getNode();
2796 if (!N->hasOneUse())
2797 // Otherwise it requires moving the value from fp to integer registers.
2798 return false;
2799 if (!N->getNumValues())
2800 return false;
2801 EVT VT = Op.getValueType();
2802 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2803 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2804 // vmrs are very slow, e.g. cortex-a8.
2805 return false;
2806
2807 if (isFloatingPointZero(Op)) {
2808 SeenZero = true;
2809 return true;
2810 }
2811 return ISD::isNormalLoad(N);
2812}
2813
2814static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2815 if (isFloatingPointZero(Op))
2816 return DAG.getConstant(0, MVT::i32);
2817
2818 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2819 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002820 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002821 Ld->isVolatile(), Ld->isNonTemporal(),
2822 Ld->getAlignment());
2823
2824 llvm_unreachable("Unknown VFP cmp argument!");
2825}
2826
2827static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2828 SDValue &RetVal1, SDValue &RetVal2) {
2829 if (isFloatingPointZero(Op)) {
2830 RetVal1 = DAG.getConstant(0, MVT::i32);
2831 RetVal2 = DAG.getConstant(0, MVT::i32);
2832 return;
2833 }
2834
2835 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2836 SDValue Ptr = Ld->getBasePtr();
2837 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2838 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002839 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002840 Ld->isVolatile(), Ld->isNonTemporal(),
2841 Ld->getAlignment());
2842
2843 EVT PtrType = Ptr.getValueType();
2844 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2845 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2846 PtrType, Ptr, DAG.getConstant(4, PtrType));
2847 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2848 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002849 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002850 Ld->isVolatile(), Ld->isNonTemporal(),
2851 NewAlign);
2852 return;
2853 }
2854
2855 llvm_unreachable("Unknown VFP cmp argument!");
2856}
2857
2858/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2859/// f32 and even f64 comparisons to integer ones.
2860SDValue
2861ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2862 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002863 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002864 SDValue LHS = Op.getOperand(2);
2865 SDValue RHS = Op.getOperand(3);
2866 SDValue Dest = Op.getOperand(4);
2867 DebugLoc dl = Op.getDebugLoc();
2868
2869 bool SeenZero = false;
2870 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2871 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002872 // If one of the operand is zero, it's safe to ignore the NaN case since
2873 // we only care about equality comparisons.
2874 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002875 // If unsafe fp math optimization is enabled and there are no other uses of
2876 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002877 // to an integer comparison.
2878 if (CC == ISD::SETOEQ)
2879 CC = ISD::SETEQ;
2880 else if (CC == ISD::SETUNE)
2881 CC = ISD::SETNE;
2882
2883 SDValue ARMcc;
2884 if (LHS.getValueType() == MVT::f32) {
2885 LHS = bitcastf32Toi32(LHS, DAG);
2886 RHS = bitcastf32Toi32(RHS, DAG);
2887 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2888 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2889 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2890 Chain, Dest, ARMcc, CCR, Cmp);
2891 }
2892
2893 SDValue LHS1, LHS2;
2894 SDValue RHS1, RHS2;
2895 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2896 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2897 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2898 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002899 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002900 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2901 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2902 }
2903
2904 return SDValue();
2905}
2906
2907SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2908 SDValue Chain = Op.getOperand(0);
2909 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2910 SDValue LHS = Op.getOperand(2);
2911 SDValue RHS = Op.getOperand(3);
2912 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002913 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002914
Owen Anderson825b72b2009-08-11 20:47:22 +00002915 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002916 SDValue ARMcc;
2917 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002918 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002919 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002920 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002921 }
2922
Owen Anderson825b72b2009-08-11 20:47:22 +00002923 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002924
2925 if (UnsafeFPMath &&
2926 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2927 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2928 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2929 if (Result.getNode())
2930 return Result;
2931 }
2932
Evan Chenga8e29892007-01-19 07:51:42 +00002933 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002934 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002935
Evan Cheng218977b2010-07-13 19:27:42 +00002936 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2937 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002938 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002939 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002940 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002941 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002942 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002943 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2944 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002945 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002946 }
2947 return Res;
2948}
2949
Dan Gohmand858e902010-04-17 15:26:15 +00002950SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002951 SDValue Chain = Op.getOperand(0);
2952 SDValue Table = Op.getOperand(1);
2953 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002954 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002955
Owen Andersone50ed302009-08-10 22:56:29 +00002956 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002957 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2958 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002959 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002960 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002961 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002962 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2963 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002964 if (Subtarget->isThumb2()) {
2965 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2966 // which does another jump to the destination. This also makes it easier
2967 // to translate it to TBB / TBH later.
2968 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002969 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002970 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002971 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002972 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002973 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002974 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002975 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002976 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002977 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002979 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002980 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002981 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002982 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002983 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002984 }
Evan Chenga8e29892007-01-19 07:51:42 +00002985}
2986
Bob Wilson76a312b2010-03-19 22:51:32 +00002987static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2988 DebugLoc dl = Op.getDebugLoc();
2989 unsigned Opc;
2990
2991 switch (Op.getOpcode()) {
2992 default:
2993 assert(0 && "Invalid opcode!");
2994 case ISD::FP_TO_SINT:
2995 Opc = ARMISD::FTOSI;
2996 break;
2997 case ISD::FP_TO_UINT:
2998 Opc = ARMISD::FTOUI;
2999 break;
3000 }
3001 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003002 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003003}
3004
Cameron Zwarich3007d332011-03-29 21:41:55 +00003005static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3006 EVT VT = Op.getValueType();
3007 DebugLoc dl = Op.getDebugLoc();
3008
Duncan Sands1f6a3292011-08-12 14:54:45 +00003009 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3010 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003011 if (VT != MVT::v4f32)
3012 return DAG.UnrollVectorOp(Op.getNode());
3013
3014 unsigned CastOpc;
3015 unsigned Opc;
3016 switch (Op.getOpcode()) {
3017 default:
3018 assert(0 && "Invalid opcode!");
3019 case ISD::SINT_TO_FP:
3020 CastOpc = ISD::SIGN_EXTEND;
3021 Opc = ISD::SINT_TO_FP;
3022 break;
3023 case ISD::UINT_TO_FP:
3024 CastOpc = ISD::ZERO_EXTEND;
3025 Opc = ISD::UINT_TO_FP;
3026 break;
3027 }
3028
3029 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3030 return DAG.getNode(Opc, dl, VT, Op);
3031}
3032
Bob Wilson76a312b2010-03-19 22:51:32 +00003033static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3034 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003035 if (VT.isVector())
3036 return LowerVectorINT_TO_FP(Op, DAG);
3037
Bob Wilson76a312b2010-03-19 22:51:32 +00003038 DebugLoc dl = Op.getDebugLoc();
3039 unsigned Opc;
3040
3041 switch (Op.getOpcode()) {
3042 default:
3043 assert(0 && "Invalid opcode!");
3044 case ISD::SINT_TO_FP:
3045 Opc = ARMISD::SITOF;
3046 break;
3047 case ISD::UINT_TO_FP:
3048 Opc = ARMISD::UITOF;
3049 break;
3050 }
3051
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003052 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003053 return DAG.getNode(Opc, dl, VT, Op);
3054}
3055
Evan Cheng515fe3a2010-07-08 02:08:50 +00003056SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003057 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003058 SDValue Tmp0 = Op.getOperand(0);
3059 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003060 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003061 EVT VT = Op.getValueType();
3062 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003063 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3064 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3065 bool UseNEON = !InGPR && Subtarget->hasNEON();
3066
3067 if (UseNEON) {
3068 // Use VBSL to copy the sign bit.
3069 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3070 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3071 DAG.getTargetConstant(EncodedVal, MVT::i32));
3072 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3073 if (VT == MVT::f64)
3074 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3075 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3076 DAG.getConstant(32, MVT::i32));
3077 else /*if (VT == MVT::f32)*/
3078 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3079 if (SrcVT == MVT::f32) {
3080 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3081 if (VT == MVT::f64)
3082 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3083 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3084 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003085 } else if (VT == MVT::f32)
3086 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3087 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3088 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003089 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3090 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3091
3092 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3093 MVT::i32);
3094 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3095 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3096 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003097
Evan Chenge573fb32011-02-23 02:24:55 +00003098 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3099 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3100 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003101 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003102 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3103 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3104 DAG.getConstant(0, MVT::i32));
3105 } else {
3106 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3107 }
3108
3109 return Res;
3110 }
Evan Chengc143dd42011-02-11 02:28:55 +00003111
3112 // Bitcast operand 1 to i32.
3113 if (SrcVT == MVT::f64)
3114 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3115 &Tmp1, 1).getValue(1);
3116 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3117
Evan Chenge573fb32011-02-23 02:24:55 +00003118 // Or in the signbit with integer operations.
3119 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3120 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3121 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3122 if (VT == MVT::f32) {
3123 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3124 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3125 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3126 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003127 }
3128
Evan Chenge573fb32011-02-23 02:24:55 +00003129 // f64: Or the high part with signbit and then combine two parts.
3130 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3131 &Tmp0, 1);
3132 SDValue Lo = Tmp0.getValue(0);
3133 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3134 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3135 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003136}
3137
Evan Cheng2457f2c2010-05-22 01:47:14 +00003138SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3139 MachineFunction &MF = DAG.getMachineFunction();
3140 MachineFrameInfo *MFI = MF.getFrameInfo();
3141 MFI->setReturnAddressIsTaken(true);
3142
3143 EVT VT = Op.getValueType();
3144 DebugLoc dl = Op.getDebugLoc();
3145 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3146 if (Depth) {
3147 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3148 SDValue Offset = DAG.getConstant(4, MVT::i32);
3149 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3150 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003151 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003152 }
3153
3154 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003155 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003156 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3157}
3158
Dan Gohmand858e902010-04-17 15:26:15 +00003159SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003160 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3161 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003162
Owen Andersone50ed302009-08-10 22:56:29 +00003163 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003164 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3165 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003166 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003167 ? ARM::R7 : ARM::R11;
3168 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3169 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003170 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3171 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003172 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003173 return FrameAddr;
3174}
3175
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003176/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003177/// expand a bit convert where either the source or destination type is i64 to
3178/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3179/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3180/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003181static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003182 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3183 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003184 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003185
Bob Wilson9f3f0612010-04-17 05:30:19 +00003186 // This function is only supposed to be called for i64 types, either as the
3187 // source or destination of the bit convert.
3188 EVT SrcVT = Op.getValueType();
3189 EVT DstVT = N->getValueType(0);
3190 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003191 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003192
Bob Wilson9f3f0612010-04-17 05:30:19 +00003193 // Turn i64->f64 into VMOVDRR.
3194 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3196 DAG.getConstant(0, MVT::i32));
3197 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3198 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003199 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003200 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003201 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003202
Jim Grosbache5165492009-11-09 00:11:35 +00003203 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003204 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3205 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3206 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3207 // Merge the pieces into a single i64 value.
3208 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3209 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003210
Bob Wilson9f3f0612010-04-17 05:30:19 +00003211 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003212}
3213
Bob Wilson5bafff32009-06-22 23:27:02 +00003214/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003215/// Zero vectors are used to represent vector negation and in those cases
3216/// will be implemented with the NEON VNEG instruction. However, VNEG does
3217/// not support i64 elements, so sometimes the zero vectors will need to be
3218/// explicitly constructed. Regardless, use a canonical VMOV to create the
3219/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003220static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003221 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003222 // The canonical modified immediate encoding of a zero vector is....0!
3223 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3224 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3225 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003226 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003227}
3228
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003229/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3230/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003231SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3232 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003233 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3234 EVT VT = Op.getValueType();
3235 unsigned VTBits = VT.getSizeInBits();
3236 DebugLoc dl = Op.getDebugLoc();
3237 SDValue ShOpLo = Op.getOperand(0);
3238 SDValue ShOpHi = Op.getOperand(1);
3239 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003240 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003241 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003242
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003243 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3244
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003245 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3246 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3247 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3248 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3249 DAG.getConstant(VTBits, MVT::i32));
3250 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3251 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003252 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003253
3254 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3255 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003256 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003257 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003258 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003259 CCR, Cmp);
3260
3261 SDValue Ops[2] = { Lo, Hi };
3262 return DAG.getMergeValues(Ops, 2, dl);
3263}
3264
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003265/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3266/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003267SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3268 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003269 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3270 EVT VT = Op.getValueType();
3271 unsigned VTBits = VT.getSizeInBits();
3272 DebugLoc dl = Op.getDebugLoc();
3273 SDValue ShOpLo = Op.getOperand(0);
3274 SDValue ShOpHi = Op.getOperand(1);
3275 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003276 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003277
3278 assert(Op.getOpcode() == ISD::SHL_PARTS);
3279 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3280 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3281 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3282 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3283 DAG.getConstant(VTBits, MVT::i32));
3284 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3285 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3286
3287 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3288 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3289 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003290 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003291 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003292 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003293 CCR, Cmp);
3294
3295 SDValue Ops[2] = { Lo, Hi };
3296 return DAG.getMergeValues(Ops, 2, dl);
3297}
3298
Jim Grosbach4725ca72010-09-08 03:54:02 +00003299SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003300 SelectionDAG &DAG) const {
3301 // The rounding mode is in bits 23:22 of the FPSCR.
3302 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3303 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3304 // so that the shift + and get folded into a bitfield extract.
3305 DebugLoc dl = Op.getDebugLoc();
3306 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3307 DAG.getConstant(Intrinsic::arm_get_fpscr,
3308 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003309 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003310 DAG.getConstant(1U << 22, MVT::i32));
3311 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3312 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003313 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003314 DAG.getConstant(3, MVT::i32));
3315}
3316
Jim Grosbach3482c802010-01-18 19:58:49 +00003317static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3318 const ARMSubtarget *ST) {
3319 EVT VT = N->getValueType(0);
3320 DebugLoc dl = N->getDebugLoc();
3321
3322 if (!ST->hasV6T2Ops())
3323 return SDValue();
3324
3325 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3326 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3327}
3328
Bob Wilson5bafff32009-06-22 23:27:02 +00003329static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3330 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003331 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003332 DebugLoc dl = N->getDebugLoc();
3333
Bob Wilsond5448bb2010-11-18 21:16:28 +00003334 if (!VT.isVector())
3335 return SDValue();
3336
Bob Wilson5bafff32009-06-22 23:27:02 +00003337 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003338 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003339
Bob Wilsond5448bb2010-11-18 21:16:28 +00003340 // Left shifts translate directly to the vshiftu intrinsic.
3341 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003342 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003343 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3344 N->getOperand(0), N->getOperand(1));
3345
3346 assert((N->getOpcode() == ISD::SRA ||
3347 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3348
3349 // NEON uses the same intrinsics for both left and right shifts. For
3350 // right shifts, the shift amounts are negative, so negate the vector of
3351 // shift amounts.
3352 EVT ShiftVT = N->getOperand(1).getValueType();
3353 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3354 getZeroVector(ShiftVT, DAG, dl),
3355 N->getOperand(1));
3356 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3357 Intrinsic::arm_neon_vshifts :
3358 Intrinsic::arm_neon_vshiftu);
3359 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3360 DAG.getConstant(vshiftInt, MVT::i32),
3361 N->getOperand(0), NegatedCount);
3362}
3363
3364static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3365 const ARMSubtarget *ST) {
3366 EVT VT = N->getValueType(0);
3367 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003368
Eli Friedmance392eb2009-08-22 03:13:10 +00003369 // We can get here for a node like i32 = ISD::SHL i32, i64
3370 if (VT != MVT::i64)
3371 return SDValue();
3372
3373 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003374 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003375
Chris Lattner27a6c732007-11-24 07:07:01 +00003376 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3377 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003378 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003379 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003380
Chris Lattner27a6c732007-11-24 07:07:01 +00003381 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003382 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003383
Chris Lattner27a6c732007-11-24 07:07:01 +00003384 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003385 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003386 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003387 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003388 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003389
Chris Lattner27a6c732007-11-24 07:07:01 +00003390 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3391 // captures the result into a carry flag.
3392 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003393 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003394
Chris Lattner27a6c732007-11-24 07:07:01 +00003395 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003397
Chris Lattner27a6c732007-11-24 07:07:01 +00003398 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003400}
3401
Bob Wilson5bafff32009-06-22 23:27:02 +00003402static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3403 SDValue TmpOp0, TmpOp1;
3404 bool Invert = false;
3405 bool Swap = false;
3406 unsigned Opc = 0;
3407
3408 SDValue Op0 = Op.getOperand(0);
3409 SDValue Op1 = Op.getOperand(1);
3410 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003411 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003412 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3413 DebugLoc dl = Op.getDebugLoc();
3414
3415 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3416 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003417 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003418 case ISD::SETUNE:
3419 case ISD::SETNE: Invert = true; // Fallthrough
3420 case ISD::SETOEQ:
3421 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3422 case ISD::SETOLT:
3423 case ISD::SETLT: Swap = true; // Fallthrough
3424 case ISD::SETOGT:
3425 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3426 case ISD::SETOLE:
3427 case ISD::SETLE: Swap = true; // Fallthrough
3428 case ISD::SETOGE:
3429 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3430 case ISD::SETUGE: Swap = true; // Fallthrough
3431 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3432 case ISD::SETUGT: Swap = true; // Fallthrough
3433 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3434 case ISD::SETUEQ: Invert = true; // Fallthrough
3435 case ISD::SETONE:
3436 // Expand this to (OLT | OGT).
3437 TmpOp0 = Op0;
3438 TmpOp1 = Op1;
3439 Opc = ISD::OR;
3440 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3441 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3442 break;
3443 case ISD::SETUO: Invert = true; // Fallthrough
3444 case ISD::SETO:
3445 // Expand this to (OLT | OGE).
3446 TmpOp0 = Op0;
3447 TmpOp1 = Op1;
3448 Opc = ISD::OR;
3449 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3450 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3451 break;
3452 }
3453 } else {
3454 // Integer comparisons.
3455 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003456 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003457 case ISD::SETNE: Invert = true;
3458 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3459 case ISD::SETLT: Swap = true;
3460 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3461 case ISD::SETLE: Swap = true;
3462 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3463 case ISD::SETULT: Swap = true;
3464 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3465 case ISD::SETULE: Swap = true;
3466 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3467 }
3468
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003469 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003470 if (Opc == ARMISD::VCEQ) {
3471
3472 SDValue AndOp;
3473 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3474 AndOp = Op0;
3475 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3476 AndOp = Op1;
3477
3478 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003479 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003480 AndOp = AndOp.getOperand(0);
3481
3482 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3483 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003484 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3485 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003486 Invert = !Invert;
3487 }
3488 }
3489 }
3490
3491 if (Swap)
3492 std::swap(Op0, Op1);
3493
Owen Andersonc24cb352010-11-08 23:21:22 +00003494 // If one of the operands is a constant vector zero, attempt to fold the
3495 // comparison to a specialized compare-against-zero form.
3496 SDValue SingleOp;
3497 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3498 SingleOp = Op0;
3499 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3500 if (Opc == ARMISD::VCGE)
3501 Opc = ARMISD::VCLEZ;
3502 else if (Opc == ARMISD::VCGT)
3503 Opc = ARMISD::VCLTZ;
3504 SingleOp = Op1;
3505 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003506
Owen Andersonc24cb352010-11-08 23:21:22 +00003507 SDValue Result;
3508 if (SingleOp.getNode()) {
3509 switch (Opc) {
3510 case ARMISD::VCEQ:
3511 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3512 case ARMISD::VCGE:
3513 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3514 case ARMISD::VCLEZ:
3515 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3516 case ARMISD::VCGT:
3517 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3518 case ARMISD::VCLTZ:
3519 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3520 default:
3521 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3522 }
3523 } else {
3524 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3525 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003526
3527 if (Invert)
3528 Result = DAG.getNOT(dl, Result, VT);
3529
3530 return Result;
3531}
3532
Bob Wilsond3c42842010-06-14 22:19:57 +00003533/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3534/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003535/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003536static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3537 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003538 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003539 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003540
Bob Wilson827b2102010-06-15 19:05:35 +00003541 // SplatBitSize is set to the smallest size that splats the vector, so a
3542 // zero vector will always have SplatBitSize == 8. However, NEON modified
3543 // immediate instructions others than VMOV do not support the 8-bit encoding
3544 // of a zero vector, and the default encoding of zero is supposed to be the
3545 // 32-bit version.
3546 if (SplatBits == 0)
3547 SplatBitSize = 32;
3548
Bob Wilson5bafff32009-06-22 23:27:02 +00003549 switch (SplatBitSize) {
3550 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003551 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003552 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003553 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003554 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003555 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003556 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003557 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003558 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003559
3560 case 16:
3561 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003562 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003563 if ((SplatBits & ~0xff) == 0) {
3564 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003565 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003566 Imm = SplatBits;
3567 break;
3568 }
3569 if ((SplatBits & ~0xff00) == 0) {
3570 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003571 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003572 Imm = SplatBits >> 8;
3573 break;
3574 }
3575 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003576
3577 case 32:
3578 // NEON's 32-bit VMOV supports splat values where:
3579 // * only one byte is nonzero, or
3580 // * the least significant byte is 0xff and the second byte is nonzero, or
3581 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003582 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003583 if ((SplatBits & ~0xff) == 0) {
3584 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003585 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003586 Imm = SplatBits;
3587 break;
3588 }
3589 if ((SplatBits & ~0xff00) == 0) {
3590 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003591 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003592 Imm = SplatBits >> 8;
3593 break;
3594 }
3595 if ((SplatBits & ~0xff0000) == 0) {
3596 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003597 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003598 Imm = SplatBits >> 16;
3599 break;
3600 }
3601 if ((SplatBits & ~0xff000000) == 0) {
3602 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003603 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003604 Imm = SplatBits >> 24;
3605 break;
3606 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003607
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003608 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3609 if (type == OtherModImm) return SDValue();
3610
Bob Wilson5bafff32009-06-22 23:27:02 +00003611 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003612 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3613 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003614 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003615 Imm = SplatBits >> 8;
3616 SplatBits |= 0xff;
3617 break;
3618 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003619
3620 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003621 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3622 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003623 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003624 Imm = SplatBits >> 16;
3625 SplatBits |= 0xffff;
3626 break;
3627 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003628
3629 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3630 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3631 // VMOV.I32. A (very) minor optimization would be to replicate the value
3632 // and fall through here to test for a valid 64-bit splat. But, then the
3633 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003634 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003635
3636 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003637 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003638 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003639 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003640 uint64_t BitMask = 0xff;
3641 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003642 unsigned ImmMask = 1;
3643 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003644 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003645 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003646 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003647 Imm |= ImmMask;
3648 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003649 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003650 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003651 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003652 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003653 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003654 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003655 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003656 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003657 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003658 break;
3659 }
3660
Bob Wilson1a913ed2010-06-11 21:34:50 +00003661 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003662 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003663 return SDValue();
3664 }
3665
Bob Wilsoncba270d2010-07-13 21:16:48 +00003666 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3667 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003668}
3669
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003670static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3671 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003672 unsigned NumElts = VT.getVectorNumElements();
3673 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003674
3675 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3676 if (M[0] < 0)
3677 return false;
3678
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003679 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003680
3681 // If this is a VEXT shuffle, the immediate value is the index of the first
3682 // element. The other shuffle indices must be the successive elements after
3683 // the first one.
3684 unsigned ExpectedElt = Imm;
3685 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003686 // Increment the expected index. If it wraps around, it may still be
3687 // a VEXT but the source vectors must be swapped.
3688 ExpectedElt += 1;
3689 if (ExpectedElt == NumElts * 2) {
3690 ExpectedElt = 0;
3691 ReverseVEXT = true;
3692 }
3693
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003694 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003695 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003696 return false;
3697 }
3698
3699 // Adjust the index value if the source operands will be swapped.
3700 if (ReverseVEXT)
3701 Imm -= NumElts;
3702
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003703 return true;
3704}
3705
Bob Wilson8bb9e482009-07-26 00:39:34 +00003706/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3707/// instruction with the specified blocksize. (The order of the elements
3708/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003709static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3710 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003711 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3712 "Only possible block sizes for VREV are: 16, 32, 64");
3713
Bob Wilson8bb9e482009-07-26 00:39:34 +00003714 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003715 if (EltSz == 64)
3716 return false;
3717
3718 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003719 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003720 // If the first shuffle index is UNDEF, be optimistic.
3721 if (M[0] < 0)
3722 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003723
3724 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3725 return false;
3726
3727 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003728 if (M[i] < 0) continue; // ignore UNDEF indices
3729 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003730 return false;
3731 }
3732
3733 return true;
3734}
3735
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003736static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3737 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3738 // range, then 0 is placed into the resulting vector. So pretty much any mask
3739 // of 8 elements can work here.
3740 return VT == MVT::v8i8 && M.size() == 8;
3741}
3742
Bob Wilsonc692cb72009-08-21 20:54:19 +00003743static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3744 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003745 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3746 if (EltSz == 64)
3747 return false;
3748
Bob Wilsonc692cb72009-08-21 20:54:19 +00003749 unsigned NumElts = VT.getVectorNumElements();
3750 WhichResult = (M[0] == 0 ? 0 : 1);
3751 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003752 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3753 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003754 return false;
3755 }
3756 return true;
3757}
3758
Bob Wilson324f4f12009-12-03 06:40:55 +00003759/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3760/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3761/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3762static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3763 unsigned &WhichResult) {
3764 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3765 if (EltSz == 64)
3766 return false;
3767
3768 unsigned NumElts = VT.getVectorNumElements();
3769 WhichResult = (M[0] == 0 ? 0 : 1);
3770 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003771 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3772 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003773 return false;
3774 }
3775 return true;
3776}
3777
Bob Wilsonc692cb72009-08-21 20:54:19 +00003778static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3779 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003780 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3781 if (EltSz == 64)
3782 return false;
3783
Bob Wilsonc692cb72009-08-21 20:54:19 +00003784 unsigned NumElts = VT.getVectorNumElements();
3785 WhichResult = (M[0] == 0 ? 0 : 1);
3786 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003787 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003788 if ((unsigned) M[i] != 2 * i + WhichResult)
3789 return false;
3790 }
3791
3792 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003793 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003794 return false;
3795
3796 return true;
3797}
3798
Bob Wilson324f4f12009-12-03 06:40:55 +00003799/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3800/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3801/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3802static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3803 unsigned &WhichResult) {
3804 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3805 if (EltSz == 64)
3806 return false;
3807
3808 unsigned Half = VT.getVectorNumElements() / 2;
3809 WhichResult = (M[0] == 0 ? 0 : 1);
3810 for (unsigned j = 0; j != 2; ++j) {
3811 unsigned Idx = WhichResult;
3812 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003813 int MIdx = M[i + j * Half];
3814 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003815 return false;
3816 Idx += 2;
3817 }
3818 }
3819
3820 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3821 if (VT.is64BitVector() && EltSz == 32)
3822 return false;
3823
3824 return true;
3825}
3826
Bob Wilsonc692cb72009-08-21 20:54:19 +00003827static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3828 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003829 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3830 if (EltSz == 64)
3831 return false;
3832
Bob Wilsonc692cb72009-08-21 20:54:19 +00003833 unsigned NumElts = VT.getVectorNumElements();
3834 WhichResult = (M[0] == 0 ? 0 : 1);
3835 unsigned Idx = WhichResult * NumElts / 2;
3836 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003837 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3838 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003839 return false;
3840 Idx += 1;
3841 }
3842
3843 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003844 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003845 return false;
3846
3847 return true;
3848}
3849
Bob Wilson324f4f12009-12-03 06:40:55 +00003850/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3851/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3852/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3853static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3854 unsigned &WhichResult) {
3855 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3856 if (EltSz == 64)
3857 return false;
3858
3859 unsigned NumElts = VT.getVectorNumElements();
3860 WhichResult = (M[0] == 0 ? 0 : 1);
3861 unsigned Idx = WhichResult * NumElts / 2;
3862 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003863 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3864 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003865 return false;
3866 Idx += 1;
3867 }
3868
3869 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3870 if (VT.is64BitVector() && EltSz == 32)
3871 return false;
3872
3873 return true;
3874}
3875
Dale Johannesenf630c712010-07-29 20:10:08 +00003876// If N is an integer constant that can be moved into a register in one
3877// instruction, return an SDValue of such a constant (will become a MOV
3878// instruction). Otherwise return null.
3879static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3880 const ARMSubtarget *ST, DebugLoc dl) {
3881 uint64_t Val;
3882 if (!isa<ConstantSDNode>(N))
3883 return SDValue();
3884 Val = cast<ConstantSDNode>(N)->getZExtValue();
3885
3886 if (ST->isThumb1Only()) {
3887 if (Val <= 255 || ~Val <= 255)
3888 return DAG.getConstant(Val, MVT::i32);
3889 } else {
3890 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3891 return DAG.getConstant(Val, MVT::i32);
3892 }
3893 return SDValue();
3894}
3895
Bob Wilson5bafff32009-06-22 23:27:02 +00003896// If this is a case we can't handle, return null and let the default
3897// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003898SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3899 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003900 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003901 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003902 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003903
3904 APInt SplatBits, SplatUndef;
3905 unsigned SplatBitSize;
3906 bool HasAnyUndefs;
3907 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003908 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003909 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003910 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003911 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003912 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003913 DAG, VmovVT, VT.is128BitVector(),
3914 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003915 if (Val.getNode()) {
3916 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003917 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003918 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003919
3920 // Try an immediate VMVN.
3921 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3922 ((1LL << SplatBitSize) - 1));
3923 Val = isNEONModifiedImm(NegatedImm,
3924 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003925 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003926 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003927 if (Val.getNode()) {
3928 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003929 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003930 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003931 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003932 }
3933
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003934 // Scan through the operands to see if only one value is used.
3935 unsigned NumElts = VT.getVectorNumElements();
3936 bool isOnlyLowElement = true;
3937 bool usesOnlyOneValue = true;
3938 bool isConstant = true;
3939 SDValue Value;
3940 for (unsigned i = 0; i < NumElts; ++i) {
3941 SDValue V = Op.getOperand(i);
3942 if (V.getOpcode() == ISD::UNDEF)
3943 continue;
3944 if (i > 0)
3945 isOnlyLowElement = false;
3946 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3947 isConstant = false;
3948
3949 if (!Value.getNode())
3950 Value = V;
3951 else if (V != Value)
3952 usesOnlyOneValue = false;
3953 }
3954
3955 if (!Value.getNode())
3956 return DAG.getUNDEF(VT);
3957
3958 if (isOnlyLowElement)
3959 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3960
Dale Johannesenf630c712010-07-29 20:10:08 +00003961 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3962
Dale Johannesen575cd142010-10-19 20:00:17 +00003963 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3964 // i32 and try again.
3965 if (usesOnlyOneValue && EltSize <= 32) {
3966 if (!isConstant)
3967 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3968 if (VT.getVectorElementType().isFloatingPoint()) {
3969 SmallVector<SDValue, 8> Ops;
3970 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003971 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003972 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003973 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3974 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003975 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3976 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003977 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003978 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003979 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3980 if (Val.getNode())
3981 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003982 }
3983
3984 // If all elements are constants and the case above didn't get hit, fall back
3985 // to the default expansion, which will generate a load from the constant
3986 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003987 if (isConstant)
3988 return SDValue();
3989
Bob Wilson11a1dff2011-01-07 21:37:30 +00003990 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3991 if (NumElts >= 4) {
3992 SDValue shuffle = ReconstructShuffle(Op, DAG);
3993 if (shuffle != SDValue())
3994 return shuffle;
3995 }
3996
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003997 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003998 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3999 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004000 if (EltSize >= 32) {
4001 // Do the expansion with floating-point types, since that is what the VFP
4002 // registers are defined to use, and since i64 is not legal.
4003 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4004 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004005 SmallVector<SDValue, 8> Ops;
4006 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004007 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004008 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004009 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004010 }
4011
4012 return SDValue();
4013}
4014
Bob Wilson11a1dff2011-01-07 21:37:30 +00004015// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004016// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004017SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4018 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004019 DebugLoc dl = Op.getDebugLoc();
4020 EVT VT = Op.getValueType();
4021 unsigned NumElts = VT.getVectorNumElements();
4022
4023 SmallVector<SDValue, 2> SourceVecs;
4024 SmallVector<unsigned, 2> MinElts;
4025 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004026
Bob Wilson11a1dff2011-01-07 21:37:30 +00004027 for (unsigned i = 0; i < NumElts; ++i) {
4028 SDValue V = Op.getOperand(i);
4029 if (V.getOpcode() == ISD::UNDEF)
4030 continue;
4031 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4032 // A shuffle can only come from building a vector from various
4033 // elements of other vectors.
4034 return SDValue();
4035 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004036
Bob Wilson11a1dff2011-01-07 21:37:30 +00004037 // Record this extraction against the appropriate vector if possible...
4038 SDValue SourceVec = V.getOperand(0);
4039 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4040 bool FoundSource = false;
4041 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4042 if (SourceVecs[j] == SourceVec) {
4043 if (MinElts[j] > EltNo)
4044 MinElts[j] = EltNo;
4045 if (MaxElts[j] < EltNo)
4046 MaxElts[j] = EltNo;
4047 FoundSource = true;
4048 break;
4049 }
4050 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004051
Bob Wilson11a1dff2011-01-07 21:37:30 +00004052 // Or record a new source if not...
4053 if (!FoundSource) {
4054 SourceVecs.push_back(SourceVec);
4055 MinElts.push_back(EltNo);
4056 MaxElts.push_back(EltNo);
4057 }
4058 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004059
Bob Wilson11a1dff2011-01-07 21:37:30 +00004060 // Currently only do something sane when at most two source vectors
4061 // involved.
4062 if (SourceVecs.size() > 2)
4063 return SDValue();
4064
4065 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4066 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004067
Bob Wilson11a1dff2011-01-07 21:37:30 +00004068 // This loop extracts the usage patterns of the source vectors
4069 // and prepares appropriate SDValues for a shuffle if possible.
4070 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4071 if (SourceVecs[i].getValueType() == VT) {
4072 // No VEXT necessary
4073 ShuffleSrcs[i] = SourceVecs[i];
4074 VEXTOffsets[i] = 0;
4075 continue;
4076 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4077 // It probably isn't worth padding out a smaller vector just to
4078 // break it down again in a shuffle.
4079 return SDValue();
4080 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004081
Bob Wilson11a1dff2011-01-07 21:37:30 +00004082 // Since only 64-bit and 128-bit vectors are legal on ARM and
4083 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004084 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4085 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004086
Bob Wilson11a1dff2011-01-07 21:37:30 +00004087 if (MaxElts[i] - MinElts[i] >= NumElts) {
4088 // Span too large for a VEXT to cope
4089 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004090 }
4091
Bob Wilson11a1dff2011-01-07 21:37:30 +00004092 if (MinElts[i] >= NumElts) {
4093 // The extraction can just take the second half
4094 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004095 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4096 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004097 DAG.getIntPtrConstant(NumElts));
4098 } else if (MaxElts[i] < NumElts) {
4099 // The extraction can just take the first half
4100 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004101 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4102 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004103 DAG.getIntPtrConstant(0));
4104 } else {
4105 // An actual VEXT is needed
4106 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004107 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4108 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004109 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004110 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4111 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004112 DAG.getIntPtrConstant(NumElts));
4113 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4114 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4115 }
4116 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004117
Bob Wilson11a1dff2011-01-07 21:37:30 +00004118 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004119
Bob Wilson11a1dff2011-01-07 21:37:30 +00004120 for (unsigned i = 0; i < NumElts; ++i) {
4121 SDValue Entry = Op.getOperand(i);
4122 if (Entry.getOpcode() == ISD::UNDEF) {
4123 Mask.push_back(-1);
4124 continue;
4125 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004126
Bob Wilson11a1dff2011-01-07 21:37:30 +00004127 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004128 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4129 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004130 if (ExtractVec == SourceVecs[0]) {
4131 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4132 } else {
4133 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4134 }
4135 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004136
Bob Wilson11a1dff2011-01-07 21:37:30 +00004137 // Final check before we try to produce nonsense...
4138 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004139 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4140 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004141
Bob Wilson11a1dff2011-01-07 21:37:30 +00004142 return SDValue();
4143}
4144
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004145/// isShuffleMaskLegal - Targets can use this to indicate that they only
4146/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4147/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4148/// are assumed to be legal.
4149bool
4150ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4151 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004152 if (VT.getVectorNumElements() == 4 &&
4153 (VT.is128BitVector() || VT.is64BitVector())) {
4154 unsigned PFIndexes[4];
4155 for (unsigned i = 0; i != 4; ++i) {
4156 if (M[i] < 0)
4157 PFIndexes[i] = 8;
4158 else
4159 PFIndexes[i] = M[i];
4160 }
4161
4162 // Compute the index in the perfect shuffle table.
4163 unsigned PFTableIndex =
4164 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4165 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4166 unsigned Cost = (PFEntry >> 30);
4167
4168 if (Cost <= 4)
4169 return true;
4170 }
4171
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004172 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004173 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004174
Bob Wilson53dd2452010-06-07 23:53:38 +00004175 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4176 return (EltSize >= 32 ||
4177 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004178 isVREVMask(M, VT, 64) ||
4179 isVREVMask(M, VT, 32) ||
4180 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004181 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004182 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004183 isVTRNMask(M, VT, WhichResult) ||
4184 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004185 isVZIPMask(M, VT, WhichResult) ||
4186 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4187 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4188 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004189}
4190
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004191/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4192/// the specified operations to build the shuffle.
4193static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4194 SDValue RHS, SelectionDAG &DAG,
4195 DebugLoc dl) {
4196 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4197 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4198 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4199
4200 enum {
4201 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4202 OP_VREV,
4203 OP_VDUP0,
4204 OP_VDUP1,
4205 OP_VDUP2,
4206 OP_VDUP3,
4207 OP_VEXT1,
4208 OP_VEXT2,
4209 OP_VEXT3,
4210 OP_VUZPL, // VUZP, left result
4211 OP_VUZPR, // VUZP, right result
4212 OP_VZIPL, // VZIP, left result
4213 OP_VZIPR, // VZIP, right result
4214 OP_VTRNL, // VTRN, left result
4215 OP_VTRNR // VTRN, right result
4216 };
4217
4218 if (OpNum == OP_COPY) {
4219 if (LHSID == (1*9+2)*9+3) return LHS;
4220 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4221 return RHS;
4222 }
4223
4224 SDValue OpLHS, OpRHS;
4225 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4226 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4227 EVT VT = OpLHS.getValueType();
4228
4229 switch (OpNum) {
4230 default: llvm_unreachable("Unknown shuffle opcode!");
4231 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004232 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004233 if (VT.getVectorElementType() == MVT::i32 ||
4234 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004235 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4236 // vrev <4 x i16> -> VREV32
4237 if (VT.getVectorElementType() == MVT::i16)
4238 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4239 // vrev <4 x i8> -> VREV16
4240 assert(VT.getVectorElementType() == MVT::i8);
4241 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004242 case OP_VDUP0:
4243 case OP_VDUP1:
4244 case OP_VDUP2:
4245 case OP_VDUP3:
4246 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004247 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004248 case OP_VEXT1:
4249 case OP_VEXT2:
4250 case OP_VEXT3:
4251 return DAG.getNode(ARMISD::VEXT, dl, VT,
4252 OpLHS, OpRHS,
4253 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4254 case OP_VUZPL:
4255 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004256 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004257 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4258 case OP_VZIPL:
4259 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004260 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004261 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4262 case OP_VTRNL:
4263 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004264 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4265 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004266 }
4267}
4268
Bill Wendling69a05a72011-03-14 23:02:38 +00004269static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4270 SmallVectorImpl<int> &ShuffleMask,
4271 SelectionDAG &DAG) {
4272 // Check to see if we can use the VTBL instruction.
4273 SDValue V1 = Op.getOperand(0);
4274 SDValue V2 = Op.getOperand(1);
4275 DebugLoc DL = Op.getDebugLoc();
4276
4277 SmallVector<SDValue, 8> VTBLMask;
4278 for (SmallVectorImpl<int>::iterator
4279 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4280 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4281
4282 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4283 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4284 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4285 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004286
Owen Anderson76706012011-04-05 21:48:57 +00004287 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004288 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4289 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004290}
4291
Bob Wilson5bafff32009-06-22 23:27:02 +00004292static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004293 SDValue V1 = Op.getOperand(0);
4294 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004295 DebugLoc dl = Op.getDebugLoc();
4296 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004297 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004298 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004299
Bob Wilson28865062009-08-13 02:13:04 +00004300 // Convert shuffles that are directly supported on NEON to target-specific
4301 // DAG nodes, instead of keeping them as shuffles and matching them again
4302 // during code selection. This is more efficient and avoids the possibility
4303 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004304 // FIXME: floating-point vectors should be canonicalized to integer vectors
4305 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004306 SVN->getMask(ShuffleMask);
4307
Bob Wilson53dd2452010-06-07 23:53:38 +00004308 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4309 if (EltSize <= 32) {
4310 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4311 int Lane = SVN->getSplatIndex();
4312 // If this is undef splat, generate it via "just" vdup, if possible.
4313 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004314
Bob Wilson53dd2452010-06-07 23:53:38 +00004315 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4316 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4317 }
4318 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4319 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004320 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004321
4322 bool ReverseVEXT;
4323 unsigned Imm;
4324 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4325 if (ReverseVEXT)
4326 std::swap(V1, V2);
4327 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4328 DAG.getConstant(Imm, MVT::i32));
4329 }
4330
4331 if (isVREVMask(ShuffleMask, VT, 64))
4332 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4333 if (isVREVMask(ShuffleMask, VT, 32))
4334 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4335 if (isVREVMask(ShuffleMask, VT, 16))
4336 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4337
4338 // Check for Neon shuffles that modify both input vectors in place.
4339 // If both results are used, i.e., if there are two shuffles with the same
4340 // source operands and with masks corresponding to both results of one of
4341 // these operations, DAG memoization will ensure that a single node is
4342 // used for both shuffles.
4343 unsigned WhichResult;
4344 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4345 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4346 V1, V2).getValue(WhichResult);
4347 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4348 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4349 V1, V2).getValue(WhichResult);
4350 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4351 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4352 V1, V2).getValue(WhichResult);
4353
4354 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4355 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4356 V1, V1).getValue(WhichResult);
4357 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4358 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4359 V1, V1).getValue(WhichResult);
4360 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4361 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4362 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004363 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004364
Bob Wilsonc692cb72009-08-21 20:54:19 +00004365 // If the shuffle is not directly supported and it has 4 elements, use
4366 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004367 unsigned NumElts = VT.getVectorNumElements();
4368 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004369 unsigned PFIndexes[4];
4370 for (unsigned i = 0; i != 4; ++i) {
4371 if (ShuffleMask[i] < 0)
4372 PFIndexes[i] = 8;
4373 else
4374 PFIndexes[i] = ShuffleMask[i];
4375 }
4376
4377 // Compute the index in the perfect shuffle table.
4378 unsigned PFTableIndex =
4379 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004380 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4381 unsigned Cost = (PFEntry >> 30);
4382
4383 if (Cost <= 4)
4384 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4385 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004386
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004387 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004388 if (EltSize >= 32) {
4389 // Do the expansion with floating-point types, since that is what the VFP
4390 // registers are defined to use, and since i64 is not legal.
4391 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4392 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004393 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4394 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004395 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004396 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004397 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004398 Ops.push_back(DAG.getUNDEF(EltVT));
4399 else
4400 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4401 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4402 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4403 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004404 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004405 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004406 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004407 }
4408
Bill Wendling69a05a72011-03-14 23:02:38 +00004409 if (VT == MVT::v8i8) {
4410 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4411 if (NewOp.getNode())
4412 return NewOp;
4413 }
4414
Bob Wilson22cac0d2009-08-14 05:16:33 +00004415 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004416}
4417
Bob Wilson5bafff32009-06-22 23:27:02 +00004418static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004419 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004420 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004421 if (!isa<ConstantSDNode>(Lane))
4422 return SDValue();
4423
4424 SDValue Vec = Op.getOperand(0);
4425 if (Op.getValueType() == MVT::i32 &&
4426 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4427 DebugLoc dl = Op.getDebugLoc();
4428 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4429 }
4430
4431 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004432}
4433
Bob Wilsona6d65862009-08-03 20:36:38 +00004434static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4435 // The only time a CONCAT_VECTORS operation can have legal types is when
4436 // two 64-bit vectors are concatenated to a 128-bit vector.
4437 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4438 "unexpected CONCAT_VECTORS");
4439 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004440 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004441 SDValue Op0 = Op.getOperand(0);
4442 SDValue Op1 = Op.getOperand(1);
4443 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004444 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004445 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004446 DAG.getIntPtrConstant(0));
4447 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004448 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004449 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004450 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004451 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004452}
4453
Bob Wilson626613d2010-11-23 19:38:38 +00004454/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4455/// element has been zero/sign-extended, depending on the isSigned parameter,
4456/// from an integer type half its size.
4457static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4458 bool isSigned) {
4459 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4460 EVT VT = N->getValueType(0);
4461 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4462 SDNode *BVN = N->getOperand(0).getNode();
4463 if (BVN->getValueType(0) != MVT::v4i32 ||
4464 BVN->getOpcode() != ISD::BUILD_VECTOR)
4465 return false;
4466 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4467 unsigned HiElt = 1 - LoElt;
4468 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4469 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4470 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4471 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4472 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4473 return false;
4474 if (isSigned) {
4475 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4476 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4477 return true;
4478 } else {
4479 if (Hi0->isNullValue() && Hi1->isNullValue())
4480 return true;
4481 }
4482 return false;
4483 }
4484
4485 if (N->getOpcode() != ISD::BUILD_VECTOR)
4486 return false;
4487
4488 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4489 SDNode *Elt = N->getOperand(i).getNode();
4490 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4491 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4492 unsigned HalfSize = EltSize / 2;
4493 if (isSigned) {
4494 int64_t SExtVal = C->getSExtValue();
4495 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4496 return false;
4497 } else {
4498 if ((C->getZExtValue() >> HalfSize) != 0)
4499 return false;
4500 }
4501 continue;
4502 }
4503 return false;
4504 }
4505
4506 return true;
4507}
4508
4509/// isSignExtended - Check if a node is a vector value that is sign-extended
4510/// or a constant BUILD_VECTOR with sign-extended elements.
4511static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4512 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4513 return true;
4514 if (isExtendedBUILD_VECTOR(N, DAG, true))
4515 return true;
4516 return false;
4517}
4518
4519/// isZeroExtended - Check if a node is a vector value that is zero-extended
4520/// or a constant BUILD_VECTOR with zero-extended elements.
4521static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4522 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4523 return true;
4524 if (isExtendedBUILD_VECTOR(N, DAG, false))
4525 return true;
4526 return false;
4527}
4528
4529/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4530/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004531static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4532 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4533 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004534 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4535 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4536 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4537 LD->isNonTemporal(), LD->getAlignment());
4538 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4539 // have been legalized as a BITCAST from v4i32.
4540 if (N->getOpcode() == ISD::BITCAST) {
4541 SDNode *BVN = N->getOperand(0).getNode();
4542 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4543 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4544 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4545 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4546 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4547 }
4548 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4549 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4550 EVT VT = N->getValueType(0);
4551 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4552 unsigned NumElts = VT.getVectorNumElements();
4553 MVT TruncVT = MVT::getIntegerVT(EltSize);
4554 SmallVector<SDValue, 8> Ops;
4555 for (unsigned i = 0; i != NumElts; ++i) {
4556 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4557 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004558 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004559 }
4560 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4561 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004562}
4563
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004564static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4565 unsigned Opcode = N->getOpcode();
4566 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4567 SDNode *N0 = N->getOperand(0).getNode();
4568 SDNode *N1 = N->getOperand(1).getNode();
4569 return N0->hasOneUse() && N1->hasOneUse() &&
4570 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4571 }
4572 return false;
4573}
4574
4575static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4576 unsigned Opcode = N->getOpcode();
4577 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4578 SDNode *N0 = N->getOperand(0).getNode();
4579 SDNode *N1 = N->getOperand(1).getNode();
4580 return N0->hasOneUse() && N1->hasOneUse() &&
4581 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4582 }
4583 return false;
4584}
4585
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004586static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4587 // Multiplications are only custom-lowered for 128-bit vectors so that
4588 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4589 EVT VT = Op.getValueType();
4590 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4591 SDNode *N0 = Op.getOperand(0).getNode();
4592 SDNode *N1 = Op.getOperand(1).getNode();
4593 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004594 bool isMLA = false;
4595 bool isN0SExt = isSignExtended(N0, DAG);
4596 bool isN1SExt = isSignExtended(N1, DAG);
4597 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004598 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004599 else {
4600 bool isN0ZExt = isZeroExtended(N0, DAG);
4601 bool isN1ZExt = isZeroExtended(N1, DAG);
4602 if (isN0ZExt && isN1ZExt)
4603 NewOpc = ARMISD::VMULLu;
4604 else if (isN1SExt || isN1ZExt) {
4605 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4606 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4607 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4608 NewOpc = ARMISD::VMULLs;
4609 isMLA = true;
4610 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4611 NewOpc = ARMISD::VMULLu;
4612 isMLA = true;
4613 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4614 std::swap(N0, N1);
4615 NewOpc = ARMISD::VMULLu;
4616 isMLA = true;
4617 }
4618 }
4619
4620 if (!NewOpc) {
4621 if (VT == MVT::v2i64)
4622 // Fall through to expand this. It is not legal.
4623 return SDValue();
4624 else
4625 // Other vector multiplications are legal.
4626 return Op;
4627 }
4628 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004629
4630 // Legalize to a VMULL instruction.
4631 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004632 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004633 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004634 if (!isMLA) {
4635 Op0 = SkipExtension(N0, DAG);
4636 assert(Op0.getValueType().is64BitVector() &&
4637 Op1.getValueType().is64BitVector() &&
4638 "unexpected types for extended operands to VMULL");
4639 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4640 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004641
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004642 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4643 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4644 // vmull q0, d4, d6
4645 // vmlal q0, d5, d6
4646 // is faster than
4647 // vaddl q0, d4, d5
4648 // vmovl q1, d6
4649 // vmul q0, q0, q1
4650 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4651 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4652 EVT Op1VT = Op1.getValueType();
4653 return DAG.getNode(N0->getOpcode(), DL, VT,
4654 DAG.getNode(NewOpc, DL, VT,
4655 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4656 DAG.getNode(NewOpc, DL, VT,
4657 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004658}
4659
Owen Anderson76706012011-04-05 21:48:57 +00004660static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004661LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4662 // Convert to float
4663 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4664 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4665 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4666 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4667 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4668 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4669 // Get reciprocal estimate.
4670 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004671 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004672 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4673 // Because char has a smaller range than uchar, we can actually get away
4674 // without any newton steps. This requires that we use a weird bias
4675 // of 0xb000, however (again, this has been exhaustively tested).
4676 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4677 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4678 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4679 Y = DAG.getConstant(0xb000, MVT::i32);
4680 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4681 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4682 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4683 // Convert back to short.
4684 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4685 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4686 return X;
4687}
4688
Owen Anderson76706012011-04-05 21:48:57 +00004689static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004690LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4691 SDValue N2;
4692 // Convert to float.
4693 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4694 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4695 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4696 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4697 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4698 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004699
Nate Begeman7973f352011-02-11 20:53:29 +00004700 // Use reciprocal estimate and one refinement step.
4701 // float4 recip = vrecpeq_f32(yf);
4702 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004703 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004704 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004705 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004706 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4707 N1, N2);
4708 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4709 // Because short has a smaller range than ushort, we can actually get away
4710 // with only a single newton step. This requires that we use a weird bias
4711 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004712 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004713 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4714 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004715 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004716 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4717 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4718 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4719 // Convert back to integer and return.
4720 // return vmovn_s32(vcvt_s32_f32(result));
4721 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4722 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4723 return N0;
4724}
4725
4726static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4727 EVT VT = Op.getValueType();
4728 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4729 "unexpected type for custom-lowering ISD::SDIV");
4730
4731 DebugLoc dl = Op.getDebugLoc();
4732 SDValue N0 = Op.getOperand(0);
4733 SDValue N1 = Op.getOperand(1);
4734 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004735
Nate Begeman7973f352011-02-11 20:53:29 +00004736 if (VT == MVT::v8i8) {
4737 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4738 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004739
Nate Begeman7973f352011-02-11 20:53:29 +00004740 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4741 DAG.getIntPtrConstant(4));
4742 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004743 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004744 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4745 DAG.getIntPtrConstant(0));
4746 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4747 DAG.getIntPtrConstant(0));
4748
4749 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4750 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4751
4752 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4753 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004754
Nate Begeman7973f352011-02-11 20:53:29 +00004755 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4756 return N0;
4757 }
4758 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4759}
4760
4761static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4762 EVT VT = Op.getValueType();
4763 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4764 "unexpected type for custom-lowering ISD::UDIV");
4765
4766 DebugLoc dl = Op.getDebugLoc();
4767 SDValue N0 = Op.getOperand(0);
4768 SDValue N1 = Op.getOperand(1);
4769 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004770
Nate Begeman7973f352011-02-11 20:53:29 +00004771 if (VT == MVT::v8i8) {
4772 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4773 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004774
Nate Begeman7973f352011-02-11 20:53:29 +00004775 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4776 DAG.getIntPtrConstant(4));
4777 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004778 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004779 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4780 DAG.getIntPtrConstant(0));
4781 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4782 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004783
Nate Begeman7973f352011-02-11 20:53:29 +00004784 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4785 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004786
Nate Begeman7973f352011-02-11 20:53:29 +00004787 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4788 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004789
4790 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004791 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4792 N0);
4793 return N0;
4794 }
Owen Anderson76706012011-04-05 21:48:57 +00004795
Nate Begeman7973f352011-02-11 20:53:29 +00004796 // v4i16 sdiv ... Convert to float.
4797 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4798 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4799 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4800 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4801 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004802 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004803
4804 // Use reciprocal estimate and two refinement steps.
4805 // float4 recip = vrecpeq_f32(yf);
4806 // recip *= vrecpsq_f32(yf, recip);
4807 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004808 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004809 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004810 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004811 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004812 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004813 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004814 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004815 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004816 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004817 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4818 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4819 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4820 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004821 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004822 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4823 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4824 N1 = DAG.getConstant(2, MVT::i32);
4825 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4826 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4827 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4828 // Convert back to integer and return.
4829 // return vmovn_u32(vcvt_s32_f32(result));
4830 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4831 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4832 return N0;
4833}
4834
Evan Cheng342e3162011-08-30 01:34:54 +00004835static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4836 EVT VT = Op.getNode()->getValueType(0);
4837 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4838
4839 unsigned Opc;
4840 bool ExtraOp = false;
4841 switch (Op.getOpcode()) {
4842 default: assert(0 && "Invalid code");
4843 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4844 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4845 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4846 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4847 }
4848
4849 if (!ExtraOp)
4850 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4851 Op.getOperand(1));
4852 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4853 Op.getOperand(1), Op.getOperand(2));
4854}
4855
Eli Friedman2bdffe42011-08-31 00:31:29 +00004856static void
4857ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4858 SelectionDAG &DAG, unsigned NewOp) {
4859 EVT T = Node->getValueType(0);
4860 DebugLoc dl = Node->getDebugLoc();
4861 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
4862
4863 SDValue Chain = Node->getOperand(0);
4864 SDValue In1 = Node->getOperand(1);
4865 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4866 Node->getOperand(2), DAG.getIntPtrConstant(0));
4867 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4868 Node->getOperand(2), DAG.getIntPtrConstant(1));
4869 SDValue Ops[] = { Chain, In1, In2L, In2H };
4870 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4871 SDValue Result =
4872 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
4873 cast<MemSDNode>(Node)->getMemOperand());
4874 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
4875 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
4876 Results.push_back(Result.getValue(2));
4877}
4878
Dan Gohmand858e902010-04-17 15:26:15 +00004879SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004880 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004881 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004882 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004883 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004884 case ISD::GlobalAddress:
4885 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4886 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004887 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004888 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004889 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4890 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004891 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004892 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004893 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00004894 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004895 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004896 case ISD::SINT_TO_FP:
4897 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4898 case ISD::FP_TO_SINT:
4899 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004900 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004901 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004902 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004903 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004904 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004905 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004906 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004907 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4908 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004909 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004910 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004911 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004912 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004913 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004914 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004915 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004916 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004917 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004918 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004919 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004920 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004921 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004922 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004923 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004924 case ISD::SDIV: return LowerSDIV(Op, DAG);
4925 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00004926 case ISD::ADDC:
4927 case ISD::ADDE:
4928 case ISD::SUBC:
4929 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004930 }
Dan Gohman475871a2008-07-27 21:46:04 +00004931 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004932}
4933
Duncan Sands1607f052008-12-01 11:39:25 +00004934/// ReplaceNodeResults - Replace the results of node with an illegal result
4935/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004936void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4937 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004938 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004939 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004940 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004941 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004942 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004943 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004944 case ISD::BITCAST:
4945 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004946 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004947 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004948 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004949 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004950 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00004951 case ISD::ATOMIC_LOAD_ADD:
4952 ReplaceATOMIC_BINARY_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
4953 return;
4954 case ISD::ATOMIC_LOAD_AND:
4955 ReplaceATOMIC_BINARY_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
4956 return;
4957 case ISD::ATOMIC_LOAD_NAND:
4958 ReplaceATOMIC_BINARY_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
4959 return;
4960 case ISD::ATOMIC_LOAD_OR:
4961 ReplaceATOMIC_BINARY_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
4962 return;
4963 case ISD::ATOMIC_LOAD_SUB:
4964 ReplaceATOMIC_BINARY_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
4965 return;
4966 case ISD::ATOMIC_LOAD_XOR:
4967 ReplaceATOMIC_BINARY_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
4968 return;
4969 case ISD::ATOMIC_SWAP:
4970 ReplaceATOMIC_BINARY_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
4971 return;
4972 //case ISD::ATOMIC_CMP_SWAP:
4973 // ReplaceATOMIC_CMPXCHG_64(N, Results, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00004974 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004975 if (Res.getNode())
4976 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004977}
Chris Lattner27a6c732007-11-24 07:07:01 +00004978
Evan Chenga8e29892007-01-19 07:51:42 +00004979//===----------------------------------------------------------------------===//
4980// ARM Scheduler Hooks
4981//===----------------------------------------------------------------------===//
4982
4983MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004984ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4985 MachineBasicBlock *BB,
4986 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004987 unsigned dest = MI->getOperand(0).getReg();
4988 unsigned ptr = MI->getOperand(1).getReg();
4989 unsigned oldval = MI->getOperand(2).getReg();
4990 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004991 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4992 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004993 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004994
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004995 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4996 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004997 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004998 : ARM::GPRRegisterClass);
4999
5000 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005001 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5002 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5003 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005004 }
5005
Jim Grosbach5278eb82009-12-11 01:42:04 +00005006 unsigned ldrOpc, strOpc;
5007 switch (Size) {
5008 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005009 case 1:
5010 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005011 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005012 break;
5013 case 2:
5014 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5015 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5016 break;
5017 case 4:
5018 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5019 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5020 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005021 }
5022
5023 MachineFunction *MF = BB->getParent();
5024 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5025 MachineFunction::iterator It = BB;
5026 ++It; // insert the new blocks after the current block
5027
5028 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5029 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5030 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5031 MF->insert(It, loop1MBB);
5032 MF->insert(It, loop2MBB);
5033 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005034
5035 // Transfer the remainder of BB and its successor edges to exitMBB.
5036 exitMBB->splice(exitMBB->begin(), BB,
5037 llvm::next(MachineBasicBlock::iterator(MI)),
5038 BB->end());
5039 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005040
5041 // thisMBB:
5042 // ...
5043 // fallthrough --> loop1MBB
5044 BB->addSuccessor(loop1MBB);
5045
5046 // loop1MBB:
5047 // ldrex dest, [ptr]
5048 // cmp dest, oldval
5049 // bne exitMBB
5050 BB = loop1MBB;
5051 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005052 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005053 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005054 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5055 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005056 BB->addSuccessor(loop2MBB);
5057 BB->addSuccessor(exitMBB);
5058
5059 // loop2MBB:
5060 // strex scratch, newval, [ptr]
5061 // cmp scratch, #0
5062 // bne loop1MBB
5063 BB = loop2MBB;
5064 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
5065 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005066 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005067 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005068 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5069 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005070 BB->addSuccessor(loop1MBB);
5071 BB->addSuccessor(exitMBB);
5072
5073 // exitMBB:
5074 // ...
5075 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005076
Dan Gohman14152b42010-07-06 20:24:04 +00005077 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005078
Jim Grosbach5278eb82009-12-11 01:42:04 +00005079 return BB;
5080}
5081
5082MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005083ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5084 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005085 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5086 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5087
5088 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005089 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005090 MachineFunction::iterator It = BB;
5091 ++It;
5092
5093 unsigned dest = MI->getOperand(0).getReg();
5094 unsigned ptr = MI->getOperand(1).getReg();
5095 unsigned incr = MI->getOperand(2).getReg();
5096 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005097 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005098
5099 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5100 if (isThumb2) {
5101 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5102 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5103 }
5104
Jim Grosbachc3c23542009-12-14 04:22:04 +00005105 unsigned ldrOpc, strOpc;
5106 switch (Size) {
5107 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005108 case 1:
5109 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005110 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005111 break;
5112 case 2:
5113 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5114 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5115 break;
5116 case 4:
5117 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5118 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5119 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005120 }
5121
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005122 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5123 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5124 MF->insert(It, loopMBB);
5125 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005126
5127 // Transfer the remainder of BB and its successor edges to exitMBB.
5128 exitMBB->splice(exitMBB->begin(), BB,
5129 llvm::next(MachineBasicBlock::iterator(MI)),
5130 BB->end());
5131 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005132
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005133 TargetRegisterClass *TRC =
5134 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5135 unsigned scratch = MRI.createVirtualRegister(TRC);
5136 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005137
5138 // thisMBB:
5139 // ...
5140 // fallthrough --> loopMBB
5141 BB->addSuccessor(loopMBB);
5142
5143 // loopMBB:
5144 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005145 // <binop> scratch2, dest, incr
5146 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005147 // cmp scratch, #0
5148 // bne- loopMBB
5149 // fallthrough --> exitMBB
5150 BB = loopMBB;
5151 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005152 if (BinOpcode) {
5153 // operand order needs to go the other way for NAND
5154 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5155 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5156 addReg(incr).addReg(dest)).addReg(0);
5157 else
5158 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5159 addReg(dest).addReg(incr)).addReg(0);
5160 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005161
5162 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5163 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005164 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005165 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005166 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5167 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005168
5169 BB->addSuccessor(loopMBB);
5170 BB->addSuccessor(exitMBB);
5171
5172 // exitMBB:
5173 // ...
5174 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005175
Dan Gohman14152b42010-07-06 20:24:04 +00005176 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005177
Jim Grosbachc3c23542009-12-14 04:22:04 +00005178 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005179}
5180
Jim Grosbachf7da8822011-04-26 19:44:18 +00005181MachineBasicBlock *
5182ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5183 MachineBasicBlock *BB,
5184 unsigned Size,
5185 bool signExtend,
5186 ARMCC::CondCodes Cond) const {
5187 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5188
5189 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5190 MachineFunction *MF = BB->getParent();
5191 MachineFunction::iterator It = BB;
5192 ++It;
5193
5194 unsigned dest = MI->getOperand(0).getReg();
5195 unsigned ptr = MI->getOperand(1).getReg();
5196 unsigned incr = MI->getOperand(2).getReg();
5197 unsigned oldval = dest;
5198 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005199 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005200
5201 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5202 if (isThumb2) {
5203 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5204 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5205 }
5206
Jim Grosbachf7da8822011-04-26 19:44:18 +00005207 unsigned ldrOpc, strOpc, extendOpc;
5208 switch (Size) {
5209 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5210 case 1:
5211 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5212 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005213 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005214 break;
5215 case 2:
5216 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5217 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005218 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005219 break;
5220 case 4:
5221 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5222 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5223 extendOpc = 0;
5224 break;
5225 }
5226
5227 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5228 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5229 MF->insert(It, loopMBB);
5230 MF->insert(It, exitMBB);
5231
5232 // Transfer the remainder of BB and its successor edges to exitMBB.
5233 exitMBB->splice(exitMBB->begin(), BB,
5234 llvm::next(MachineBasicBlock::iterator(MI)),
5235 BB->end());
5236 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5237
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005238 TargetRegisterClass *TRC =
5239 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5240 unsigned scratch = MRI.createVirtualRegister(TRC);
5241 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005242
5243 // thisMBB:
5244 // ...
5245 // fallthrough --> loopMBB
5246 BB->addSuccessor(loopMBB);
5247
5248 // loopMBB:
5249 // ldrex dest, ptr
5250 // (sign extend dest, if required)
5251 // cmp dest, incr
5252 // cmov.cond scratch2, dest, incr
5253 // strex scratch, scratch2, ptr
5254 // cmp scratch, #0
5255 // bne- loopMBB
5256 // fallthrough --> exitMBB
5257 BB = loopMBB;
5258 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5259
5260 // Sign extend the value, if necessary.
5261 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005262 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005263 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5264 .addReg(dest)
5265 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005266 }
5267
5268 // Build compare and cmov instructions.
5269 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5270 .addReg(oldval).addReg(incr));
5271 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5272 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5273
5274 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5275 .addReg(ptr));
5276 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5277 .addReg(scratch).addImm(0));
5278 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5279 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5280
5281 BB->addSuccessor(loopMBB);
5282 BB->addSuccessor(exitMBB);
5283
5284 // exitMBB:
5285 // ...
5286 BB = exitMBB;
5287
5288 MI->eraseFromParent(); // The instruction is gone now.
5289
5290 return BB;
5291}
5292
Eli Friedman2bdffe42011-08-31 00:31:29 +00005293MachineBasicBlock *
5294ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5295 unsigned Op1, unsigned Op2,
5296 bool NeedsCarry) const {
5297 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5298 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5299
5300 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5301 MachineFunction *MF = BB->getParent();
5302 MachineFunction::iterator It = BB;
5303 ++It;
5304
5305 unsigned destlo = MI->getOperand(0).getReg();
5306 unsigned desthi = MI->getOperand(1).getReg();
5307 unsigned ptr = MI->getOperand(2).getReg();
5308 unsigned vallo = MI->getOperand(3).getReg();
5309 unsigned valhi = MI->getOperand(4).getReg();
5310 DebugLoc dl = MI->getDebugLoc();
5311 bool isThumb2 = Subtarget->isThumb2();
5312
5313 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5314 if (isThumb2) {
5315 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5316 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5317 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5318 }
5319
5320 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5321 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5322
5323 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5324 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5325 MF->insert(It, loopMBB);
5326 MF->insert(It, exitMBB);
5327
5328 // Transfer the remainder of BB and its successor edges to exitMBB.
5329 exitMBB->splice(exitMBB->begin(), BB,
5330 llvm::next(MachineBasicBlock::iterator(MI)),
5331 BB->end());
5332 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5333
5334 TargetRegisterClass *TRC =
5335 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5336 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5337
5338 // thisMBB:
5339 // ...
5340 // fallthrough --> loopMBB
5341 BB->addSuccessor(loopMBB);
5342
5343 // loopMBB:
5344 // ldrexd r2, r3, ptr
5345 // <binopa> r0, r2, incr
5346 // <binopb> r1, r3, incr
5347 // strexd storesuccess, r0, r1, ptr
5348 // cmp storesuccess, #0
5349 // bne- loopMBB
5350 // fallthrough --> exitMBB
5351 //
5352 // Note that the registers are explicitly specified because there is not any
5353 // way to force the register allocator to allocate a register pair.
5354 //
5355 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
5356 // need to properly enforce the restriction that the two output registers
5357 // for ldrexd must be different.
5358 BB = loopMBB;
5359 // Load
5360 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5361 .addReg(ARM::R2, RegState::Define)
5362 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5363 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5364 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5365 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
5366 if (Op1) {
5367 // Perform binary operation
5368 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5369 .addReg(destlo).addReg(vallo))
5370 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5371 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5372 .addReg(desthi).addReg(valhi)).addReg(0);
5373 } else {
5374 // Copy to physregs for strexd
5375 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5376 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5377 }
5378
5379 // Store
5380 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5381 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5382 // Cmp+jump
5383 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5384 .addReg(storesuccess).addImm(0));
5385 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5386 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5387
5388 BB->addSuccessor(loopMBB);
5389 BB->addSuccessor(exitMBB);
5390
5391 // exitMBB:
5392 // ...
5393 BB = exitMBB;
5394
5395 MI->eraseFromParent(); // The instruction is gone now.
5396
5397 return BB;
5398}
5399
Evan Cheng218977b2010-07-13 19:27:42 +00005400static
5401MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5402 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5403 E = MBB->succ_end(); I != E; ++I)
5404 if (*I != Succ)
5405 return *I;
5406 llvm_unreachable("Expecting a BB with two successors!");
5407}
5408
Jim Grosbache801dc42009-12-12 01:40:06 +00005409MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005410ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005411 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005412 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005413 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005414 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005415 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005416 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005417 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005418 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005419 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005420 case ARM::STRi_preidx:
5421 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00005422 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00005423 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
5424 // Decode the offset.
5425 unsigned Offset = MI->getOperand(4).getImm();
5426 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
5427 Offset = ARM_AM::getAM2Offset(Offset);
5428 if (isSub)
5429 Offset = -Offset;
5430
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005431 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00005432 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00005433 .addOperand(MI->getOperand(0)) // Rn_wb
5434 .addOperand(MI->getOperand(1)) // Rt
5435 .addOperand(MI->getOperand(2)) // Rn
5436 .addImm(Offset) // offset (skip GPR==zero_reg)
5437 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005438 .addOperand(MI->getOperand(6))
5439 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00005440 MI->eraseFromParent();
5441 return BB;
5442 }
5443 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00005444 case ARM::STRBr_preidx:
5445 case ARM::STRH_preidx: {
5446 unsigned NewOpc;
5447 switch (MI->getOpcode()) {
5448 default: llvm_unreachable("unexpected opcode!");
5449 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
5450 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
5451 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
5452 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005453 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5454 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5455 MIB.addOperand(MI->getOperand(i));
5456 MI->eraseFromParent();
5457 return BB;
5458 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005459 case ARM::ATOMIC_LOAD_ADD_I8:
5460 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5461 case ARM::ATOMIC_LOAD_ADD_I16:
5462 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5463 case ARM::ATOMIC_LOAD_ADD_I32:
5464 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005465
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005466 case ARM::ATOMIC_LOAD_AND_I8:
5467 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5468 case ARM::ATOMIC_LOAD_AND_I16:
5469 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5470 case ARM::ATOMIC_LOAD_AND_I32:
5471 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005472
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005473 case ARM::ATOMIC_LOAD_OR_I8:
5474 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5475 case ARM::ATOMIC_LOAD_OR_I16:
5476 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5477 case ARM::ATOMIC_LOAD_OR_I32:
5478 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005479
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005480 case ARM::ATOMIC_LOAD_XOR_I8:
5481 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5482 case ARM::ATOMIC_LOAD_XOR_I16:
5483 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5484 case ARM::ATOMIC_LOAD_XOR_I32:
5485 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005486
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005487 case ARM::ATOMIC_LOAD_NAND_I8:
5488 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5489 case ARM::ATOMIC_LOAD_NAND_I16:
5490 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5491 case ARM::ATOMIC_LOAD_NAND_I32:
5492 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005493
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005494 case ARM::ATOMIC_LOAD_SUB_I8:
5495 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5496 case ARM::ATOMIC_LOAD_SUB_I16:
5497 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5498 case ARM::ATOMIC_LOAD_SUB_I32:
5499 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005500
Jim Grosbachf7da8822011-04-26 19:44:18 +00005501 case ARM::ATOMIC_LOAD_MIN_I8:
5502 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5503 case ARM::ATOMIC_LOAD_MIN_I16:
5504 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5505 case ARM::ATOMIC_LOAD_MIN_I32:
5506 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5507
5508 case ARM::ATOMIC_LOAD_MAX_I8:
5509 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5510 case ARM::ATOMIC_LOAD_MAX_I16:
5511 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5512 case ARM::ATOMIC_LOAD_MAX_I32:
5513 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5514
5515 case ARM::ATOMIC_LOAD_UMIN_I8:
5516 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5517 case ARM::ATOMIC_LOAD_UMIN_I16:
5518 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5519 case ARM::ATOMIC_LOAD_UMIN_I32:
5520 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5521
5522 case ARM::ATOMIC_LOAD_UMAX_I8:
5523 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5524 case ARM::ATOMIC_LOAD_UMAX_I16:
5525 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5526 case ARM::ATOMIC_LOAD_UMAX_I32:
5527 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5528
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005529 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5530 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5531 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005532
5533 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5534 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5535 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005536
Eli Friedman2bdffe42011-08-31 00:31:29 +00005537
5538 case ARM::ATOMADD6432:
5539 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
5540 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr, true);
5541 case ARM::ATOMSUB6432:
5542 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
5543 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr, true);
5544 case ARM::ATOMOR6432:
5545 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
5546 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr, false);
5547 case ARM::ATOMXOR6432:
5548 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
5549 isThumb2 ? ARM::t2EORrr : ARM::EORrr, false);
5550 case ARM::ATOMAND6432:
5551 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
5552 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr, false);
5553 case ARM::ATOMSWAP6432:
5554 return EmitAtomicBinary64(MI, BB, 0, 0, false);
5555
Evan Cheng007ea272009-08-12 05:17:19 +00005556 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005557 // To "insert" a SELECT_CC instruction, we actually have to insert the
5558 // diamond control-flow pattern. The incoming instruction knows the
5559 // destination vreg to set, the condition code register to branch on, the
5560 // true/false values to select between, and a branch opcode to use.
5561 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005562 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005563 ++It;
5564
5565 // thisMBB:
5566 // ...
5567 // TrueVal = ...
5568 // cmpTY ccX, r1, r2
5569 // bCC copy1MBB
5570 // fallthrough --> copy0MBB
5571 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005572 MachineFunction *F = BB->getParent();
5573 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5574 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005575 F->insert(It, copy0MBB);
5576 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005577
5578 // Transfer the remainder of BB and its successor edges to sinkMBB.
5579 sinkMBB->splice(sinkMBB->begin(), BB,
5580 llvm::next(MachineBasicBlock::iterator(MI)),
5581 BB->end());
5582 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5583
Dan Gohman258c58c2010-07-06 15:49:48 +00005584 BB->addSuccessor(copy0MBB);
5585 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005586
Dan Gohman14152b42010-07-06 20:24:04 +00005587 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5588 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5589
Evan Chenga8e29892007-01-19 07:51:42 +00005590 // copy0MBB:
5591 // %FalseValue = ...
5592 // # fallthrough to sinkMBB
5593 BB = copy0MBB;
5594
5595 // Update machine-CFG edges
5596 BB->addSuccessor(sinkMBB);
5597
5598 // sinkMBB:
5599 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5600 // ...
5601 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005602 BuildMI(*BB, BB->begin(), dl,
5603 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005604 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5605 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5606
Dan Gohman14152b42010-07-06 20:24:04 +00005607 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005608 return BB;
5609 }
Evan Cheng86198642009-08-07 00:34:42 +00005610
Evan Cheng218977b2010-07-13 19:27:42 +00005611 case ARM::BCCi64:
5612 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005613 // If there is an unconditional branch to the other successor, remove it.
5614 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005615
Evan Cheng218977b2010-07-13 19:27:42 +00005616 // Compare both parts that make up the double comparison separately for
5617 // equality.
5618 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5619
5620 unsigned LHS1 = MI->getOperand(1).getReg();
5621 unsigned LHS2 = MI->getOperand(2).getReg();
5622 if (RHSisZero) {
5623 AddDefaultPred(BuildMI(BB, dl,
5624 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5625 .addReg(LHS1).addImm(0));
5626 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5627 .addReg(LHS2).addImm(0)
5628 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5629 } else {
5630 unsigned RHS1 = MI->getOperand(3).getReg();
5631 unsigned RHS2 = MI->getOperand(4).getReg();
5632 AddDefaultPred(BuildMI(BB, dl,
5633 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5634 .addReg(LHS1).addReg(RHS1));
5635 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5636 .addReg(LHS2).addReg(RHS2)
5637 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5638 }
5639
5640 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5641 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5642 if (MI->getOperand(0).getImm() == ARMCC::NE)
5643 std::swap(destMBB, exitMBB);
5644
5645 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5646 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5647 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5648 .addMBB(exitMBB);
5649
5650 MI->eraseFromParent(); // The pseudo instruction is gone now.
5651 return BB;
5652 }
Evan Chenga8e29892007-01-19 07:51:42 +00005653 }
5654}
5655
Evan Cheng37fefc22011-08-30 19:09:48 +00005656void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
5657 SDNode *Node) const {
5658 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC,
5659 // RSB, RSC. Coming out of isel, they have an implicit CPSR def, but the
5660 // optional operand is not filled in. If the carry bit is used, then change
5661 // the optional operand to CPSR. Otherwise, remove the CPSR implicit def.
5662 const MCInstrDesc &MCID = MI->getDesc();
5663 if (Node->hasAnyUseOfValue(1)) {
5664 MachineOperand &MO = MI->getOperand(MCID.getNumOperands() - 2);
5665 MO.setReg(ARM::CPSR);
5666 MO.setIsDef(true);
5667 } else {
5668 for (unsigned i = MCID.getNumOperands(), e = MI->getNumOperands();
5669 i != e; ++i) {
5670 const MachineOperand &MO = MI->getOperand(i);
5671 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
5672 MI->RemoveOperand(i);
5673 break;
5674 }
5675 }
5676 }
5677}
5678
Evan Chenga8e29892007-01-19 07:51:42 +00005679//===----------------------------------------------------------------------===//
5680// ARM Optimization Hooks
5681//===----------------------------------------------------------------------===//
5682
Chris Lattnerd1980a52009-03-12 06:52:53 +00005683static
5684SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5685 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005686 SelectionDAG &DAG = DCI.DAG;
5687 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005688 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005689 unsigned Opc = N->getOpcode();
5690 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5691 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5692 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5693 ISD::CondCode CC = ISD::SETCC_INVALID;
5694
5695 if (isSlctCC) {
5696 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5697 } else {
5698 SDValue CCOp = Slct.getOperand(0);
5699 if (CCOp.getOpcode() == ISD::SETCC)
5700 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5701 }
5702
5703 bool DoXform = false;
5704 bool InvCC = false;
5705 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5706 "Bad input!");
5707
5708 if (LHS.getOpcode() == ISD::Constant &&
5709 cast<ConstantSDNode>(LHS)->isNullValue()) {
5710 DoXform = true;
5711 } else if (CC != ISD::SETCC_INVALID &&
5712 RHS.getOpcode() == ISD::Constant &&
5713 cast<ConstantSDNode>(RHS)->isNullValue()) {
5714 std::swap(LHS, RHS);
5715 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005716 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005717 Op0.getOperand(0).getValueType();
5718 bool isInt = OpVT.isInteger();
5719 CC = ISD::getSetCCInverse(CC, isInt);
5720
5721 if (!TLI.isCondCodeLegal(CC, OpVT))
5722 return SDValue(); // Inverse operator isn't legal.
5723
5724 DoXform = true;
5725 InvCC = true;
5726 }
5727
5728 if (DoXform) {
5729 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5730 if (isSlctCC)
5731 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5732 Slct.getOperand(0), Slct.getOperand(1), CC);
5733 SDValue CCOp = Slct.getOperand(0);
5734 if (InvCC)
5735 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5736 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5737 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5738 CCOp, OtherOp, Result);
5739 }
5740 return SDValue();
5741}
5742
Eric Christopherfa6f5912011-06-29 21:10:36 +00005743// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00005744// (only after legalization).
5745static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5746 TargetLowering::DAGCombinerInfo &DCI,
5747 const ARMSubtarget *Subtarget) {
5748
5749 // Only perform optimization if after legalize, and if NEON is available. We
5750 // also expected both operands to be BUILD_VECTORs.
5751 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5752 || N0.getOpcode() != ISD::BUILD_VECTOR
5753 || N1.getOpcode() != ISD::BUILD_VECTOR)
5754 return SDValue();
5755
5756 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5757 EVT VT = N->getValueType(0);
5758 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5759 return SDValue();
5760
5761 // Check that the vector operands are of the right form.
5762 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5763 // operands, where N is the size of the formed vector.
5764 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5765 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005766
5767 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005768 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005769 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005770 SDValue Vec = N0->getOperand(0)->getOperand(0);
5771 SDNode *V = Vec.getNode();
5772 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005773
Eric Christopherfa6f5912011-06-29 21:10:36 +00005774 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00005775 // check to see if each of their operands are an EXTRACT_VECTOR with
5776 // the same vector and appropriate index.
5777 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5778 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5779 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00005780
Tanya Lattner189531f2011-06-14 23:48:48 +00005781 SDValue ExtVec0 = N0->getOperand(i);
5782 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005783
Tanya Lattner189531f2011-06-14 23:48:48 +00005784 // First operand is the vector, verify its the same.
5785 if (V != ExtVec0->getOperand(0).getNode() ||
5786 V != ExtVec1->getOperand(0).getNode())
5787 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00005788
Tanya Lattner189531f2011-06-14 23:48:48 +00005789 // Second is the constant, verify its correct.
5790 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5791 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00005792
Tanya Lattner189531f2011-06-14 23:48:48 +00005793 // For the constant, we want to see all the even or all the odd.
5794 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5795 || C1->getZExtValue() != nextIndex+1)
5796 return SDValue();
5797
5798 // Increment index.
5799 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005800 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00005801 return SDValue();
5802 }
5803
5804 // Create VPADDL node.
5805 SelectionDAG &DAG = DCI.DAG;
5806 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00005807
5808 // Build operand list.
5809 SmallVector<SDValue, 8> Ops;
5810 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5811 TLI.getPointerTy()));
5812
5813 // Input is the vector.
5814 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005815
Tanya Lattner189531f2011-06-14 23:48:48 +00005816 // Get widened type and narrowed type.
5817 MVT widenType;
5818 unsigned numElem = VT.getVectorNumElements();
5819 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5820 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5821 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5822 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5823 default:
5824 assert(0 && "Invalid vector element type for padd optimization.");
5825 }
5826
5827 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5828 widenType, &Ops[0], Ops.size());
5829 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5830}
5831
Bob Wilson3d5792a2010-07-29 20:34:14 +00005832/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5833/// operands N0 and N1. This is a helper for PerformADDCombine that is
5834/// called with the default operands, and if that fails, with commuted
5835/// operands.
5836static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005837 TargetLowering::DAGCombinerInfo &DCI,
5838 const ARMSubtarget *Subtarget){
5839
5840 // Attempt to create vpaddl for this add.
5841 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5842 if (Result.getNode())
5843 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005844
Chris Lattnerd1980a52009-03-12 06:52:53 +00005845 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5846 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5847 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5848 if (Result.getNode()) return Result;
5849 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005850 return SDValue();
5851}
5852
Bob Wilson3d5792a2010-07-29 20:34:14 +00005853/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5854///
5855static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005856 TargetLowering::DAGCombinerInfo &DCI,
5857 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005858 SDValue N0 = N->getOperand(0);
5859 SDValue N1 = N->getOperand(1);
5860
5861 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005862 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005863 if (Result.getNode())
5864 return Result;
5865
5866 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005867 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005868}
5869
Chris Lattnerd1980a52009-03-12 06:52:53 +00005870/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005871///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005872static SDValue PerformSUBCombine(SDNode *N,
5873 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005874 SDValue N0 = N->getOperand(0);
5875 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005876
Chris Lattnerd1980a52009-03-12 06:52:53 +00005877 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5878 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5879 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5880 if (Result.getNode()) return Result;
5881 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005882
Chris Lattnerd1980a52009-03-12 06:52:53 +00005883 return SDValue();
5884}
5885
Evan Cheng463d3582011-03-31 19:38:48 +00005886/// PerformVMULCombine
5887/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5888/// special multiplier accumulator forwarding.
5889/// vmul d3, d0, d2
5890/// vmla d3, d1, d2
5891/// is faster than
5892/// vadd d3, d0, d1
5893/// vmul d3, d3, d2
5894static SDValue PerformVMULCombine(SDNode *N,
5895 TargetLowering::DAGCombinerInfo &DCI,
5896 const ARMSubtarget *Subtarget) {
5897 if (!Subtarget->hasVMLxForwarding())
5898 return SDValue();
5899
5900 SelectionDAG &DAG = DCI.DAG;
5901 SDValue N0 = N->getOperand(0);
5902 SDValue N1 = N->getOperand(1);
5903 unsigned Opcode = N0.getOpcode();
5904 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5905 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00005906 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00005907 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5908 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5909 return SDValue();
5910 std::swap(N0, N1);
5911 }
5912
5913 EVT VT = N->getValueType(0);
5914 DebugLoc DL = N->getDebugLoc();
5915 SDValue N00 = N0->getOperand(0);
5916 SDValue N01 = N0->getOperand(1);
5917 return DAG.getNode(Opcode, DL, VT,
5918 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5919 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5920}
5921
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005922static SDValue PerformMULCombine(SDNode *N,
5923 TargetLowering::DAGCombinerInfo &DCI,
5924 const ARMSubtarget *Subtarget) {
5925 SelectionDAG &DAG = DCI.DAG;
5926
5927 if (Subtarget->isThumb1Only())
5928 return SDValue();
5929
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005930 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5931 return SDValue();
5932
5933 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005934 if (VT.is64BitVector() || VT.is128BitVector())
5935 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005936 if (VT != MVT::i32)
5937 return SDValue();
5938
5939 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5940 if (!C)
5941 return SDValue();
5942
5943 uint64_t MulAmt = C->getZExtValue();
5944 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5945 ShiftAmt = ShiftAmt & (32 - 1);
5946 SDValue V = N->getOperand(0);
5947 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005948
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005949 SDValue Res;
5950 MulAmt >>= ShiftAmt;
5951 if (isPowerOf2_32(MulAmt - 1)) {
5952 // (mul x, 2^N + 1) => (add (shl x, N), x)
5953 Res = DAG.getNode(ISD::ADD, DL, VT,
5954 V, DAG.getNode(ISD::SHL, DL, VT,
5955 V, DAG.getConstant(Log2_32(MulAmt-1),
5956 MVT::i32)));
5957 } else if (isPowerOf2_32(MulAmt + 1)) {
5958 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5959 Res = DAG.getNode(ISD::SUB, DL, VT,
5960 DAG.getNode(ISD::SHL, DL, VT,
5961 V, DAG.getConstant(Log2_32(MulAmt+1),
5962 MVT::i32)),
5963 V);
5964 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005965 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005966
5967 if (ShiftAmt != 0)
5968 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5969 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005970
5971 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005972 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005973 return SDValue();
5974}
5975
Owen Anderson080c0922010-11-05 19:27:46 +00005976static SDValue PerformANDCombine(SDNode *N,
5977 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005978
Owen Anderson080c0922010-11-05 19:27:46 +00005979 // Attempt to use immediate-form VBIC
5980 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5981 DebugLoc dl = N->getDebugLoc();
5982 EVT VT = N->getValueType(0);
5983 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005984
Tanya Lattner0433b212011-04-07 15:24:20 +00005985 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5986 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005987
Owen Anderson080c0922010-11-05 19:27:46 +00005988 APInt SplatBits, SplatUndef;
5989 unsigned SplatBitSize;
5990 bool HasAnyUndefs;
5991 if (BVN &&
5992 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5993 if (SplatBitSize <= 64) {
5994 EVT VbicVT;
5995 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5996 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005997 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005998 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005999 if (Val.getNode()) {
6000 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006001 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006002 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006003 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006004 }
6005 }
6006 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006007
Owen Anderson080c0922010-11-05 19:27:46 +00006008 return SDValue();
6009}
6010
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006011/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6012static SDValue PerformORCombine(SDNode *N,
6013 TargetLowering::DAGCombinerInfo &DCI,
6014 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006015 // Attempt to use immediate-form VORR
6016 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6017 DebugLoc dl = N->getDebugLoc();
6018 EVT VT = N->getValueType(0);
6019 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006020
Tanya Lattner0433b212011-04-07 15:24:20 +00006021 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6022 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006023
Owen Anderson60f48702010-11-03 23:15:26 +00006024 APInt SplatBits, SplatUndef;
6025 unsigned SplatBitSize;
6026 bool HasAnyUndefs;
6027 if (BVN && Subtarget->hasNEON() &&
6028 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6029 if (SplatBitSize <= 64) {
6030 EVT VorrVT;
6031 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6032 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006033 DAG, VorrVT, VT.is128BitVector(),
6034 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006035 if (Val.getNode()) {
6036 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006037 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006038 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006039 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006040 }
6041 }
6042 }
6043
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006044 SDValue N0 = N->getOperand(0);
6045 if (N0.getOpcode() != ISD::AND)
6046 return SDValue();
6047 SDValue N1 = N->getOperand(1);
6048
6049 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6050 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6051 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6052 APInt SplatUndef;
6053 unsigned SplatBitSize;
6054 bool HasAnyUndefs;
6055
6056 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6057 APInt SplatBits0;
6058 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6059 HasAnyUndefs) && !HasAnyUndefs) {
6060 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6061 APInt SplatBits1;
6062 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6063 HasAnyUndefs) && !HasAnyUndefs &&
6064 SplatBits0 == ~SplatBits1) {
6065 // Canonicalize the vector type to make instruction selection simpler.
6066 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6067 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6068 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006069 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006070 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6071 }
6072 }
6073 }
6074
Jim Grosbach54238562010-07-17 03:30:54 +00006075 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6076 // reasonable.
6077
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006078 // BFI is only available on V6T2+
6079 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6080 return SDValue();
6081
Jim Grosbach54238562010-07-17 03:30:54 +00006082 DebugLoc DL = N->getDebugLoc();
6083 // 1) or (and A, mask), val => ARMbfi A, val, mask
6084 // iff (val & mask) == val
6085 //
6086 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6087 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006088 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006089 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006090 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006091 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006092
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006093 if (VT != MVT::i32)
6094 return SDValue();
6095
Evan Cheng30fb13f2010-12-13 20:32:54 +00006096 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006097
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006098 // The value and the mask need to be constants so we can verify this is
6099 // actually a bitfield set. If the mask is 0xffff, we can do better
6100 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006101 SDValue MaskOp = N0.getOperand(1);
6102 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6103 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006104 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006105 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006106 if (Mask == 0xffff)
6107 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006108 SDValue Res;
6109 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006110 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6111 if (N1C) {
6112 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006113 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006114 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006115
Evan Chenga9688c42010-12-11 04:11:38 +00006116 if (ARM::isBitFieldInvertedMask(Mask)) {
6117 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006118
Evan Cheng30fb13f2010-12-13 20:32:54 +00006119 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006120 DAG.getConstant(Val, MVT::i32),
6121 DAG.getConstant(Mask, MVT::i32));
6122
6123 // Do not add new nodes to DAG combiner worklist.
6124 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006125 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006126 }
Jim Grosbach54238562010-07-17 03:30:54 +00006127 } else if (N1.getOpcode() == ISD::AND) {
6128 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006129 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6130 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006131 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006132 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006133
Eric Christopher29aeed12011-03-26 01:21:03 +00006134 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6135 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006136 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006137 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006138 // The pack halfword instruction works better for masks that fit it,
6139 // so use that when it's available.
6140 if (Subtarget->hasT2ExtractPack() &&
6141 (Mask == 0xffff || Mask == 0xffff0000))
6142 return SDValue();
6143 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006144 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006145 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006146 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006147 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006148 DAG.getConstant(Mask, MVT::i32));
6149 // Do not add new nodes to DAG combiner worklist.
6150 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006151 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006152 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006153 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006154 // The pack halfword instruction works better for masks that fit it,
6155 // so use that when it's available.
6156 if (Subtarget->hasT2ExtractPack() &&
6157 (Mask2 == 0xffff || Mask2 == 0xffff0000))
6158 return SDValue();
6159 // 2b
6160 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006161 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00006162 DAG.getConstant(lsb, MVT::i32));
6163 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00006164 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00006165 // Do not add new nodes to DAG combiner worklist.
6166 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006167 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006168 }
6169 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006170
Evan Cheng30fb13f2010-12-13 20:32:54 +00006171 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
6172 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
6173 ARM::isBitFieldInvertedMask(~Mask)) {
6174 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
6175 // where lsb(mask) == #shamt and masked bits of B are known zero.
6176 SDValue ShAmt = N00.getOperand(1);
6177 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
6178 unsigned LSB = CountTrailingZeros_32(Mask);
6179 if (ShAmtC != LSB)
6180 return SDValue();
6181
6182 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
6183 DAG.getConstant(~Mask, MVT::i32));
6184
6185 // Do not add new nodes to DAG combiner worklist.
6186 DCI.CombineTo(N, Res, false);
6187 }
6188
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006189 return SDValue();
6190}
6191
Evan Chengbf188ae2011-06-15 01:12:31 +00006192/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
6193/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00006194static SDValue PerformBFICombine(SDNode *N,
6195 TargetLowering::DAGCombinerInfo &DCI) {
6196 SDValue N1 = N->getOperand(1);
6197 if (N1.getOpcode() == ISD::AND) {
6198 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6199 if (!N11C)
6200 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006201 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
6202 unsigned LSB = CountTrailingZeros_32(~InvMask);
6203 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
6204 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00006205 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006206 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00006207 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
6208 N->getOperand(0), N1.getOperand(0),
6209 N->getOperand(2));
6210 }
6211 return SDValue();
6212}
6213
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006214/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
6215/// ARMISD::VMOVRRD.
6216static SDValue PerformVMOVRRDCombine(SDNode *N,
6217 TargetLowering::DAGCombinerInfo &DCI) {
6218 // vmovrrd(vmovdrr x, y) -> x,y
6219 SDValue InDouble = N->getOperand(0);
6220 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6221 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006222
6223 // vmovrrd(load f64) -> (load i32), (load i32)
6224 SDNode *InNode = InDouble.getNode();
6225 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6226 InNode->getValueType(0) == MVT::f64 &&
6227 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6228 !cast<LoadSDNode>(InNode)->isVolatile()) {
6229 // TODO: Should this be done for non-FrameIndex operands?
6230 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6231
6232 SelectionDAG &DAG = DCI.DAG;
6233 DebugLoc DL = LD->getDebugLoc();
6234 SDValue BasePtr = LD->getBasePtr();
6235 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6236 LD->getPointerInfo(), LD->isVolatile(),
6237 LD->isNonTemporal(), LD->getAlignment());
6238
6239 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6240 DAG.getConstant(4, MVT::i32));
6241 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6242 LD->getPointerInfo(), LD->isVolatile(),
6243 LD->isNonTemporal(),
6244 std::min(4U, LD->getAlignment() / 2));
6245
6246 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6247 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6248 DCI.RemoveFromWorklist(LD);
6249 DAG.DeleteNode(LD);
6250 return Result;
6251 }
6252
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006253 return SDValue();
6254}
6255
6256/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6257/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6258static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6259 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6260 SDValue Op0 = N->getOperand(0);
6261 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006262 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006263 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006264 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006265 Op1 = Op1.getOperand(0);
6266 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6267 Op0.getNode() == Op1.getNode() &&
6268 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006269 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006270 N->getValueType(0), Op0.getOperand(0));
6271 return SDValue();
6272}
6273
Bob Wilson31600902010-12-21 06:43:19 +00006274/// PerformSTORECombine - Target-specific dag combine xforms for
6275/// ISD::STORE.
6276static SDValue PerformSTORECombine(SDNode *N,
6277 TargetLowering::DAGCombinerInfo &DCI) {
6278 // Bitcast an i64 store extracted from a vector to f64.
6279 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6280 StoreSDNode *St = cast<StoreSDNode>(N);
6281 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006282 if (!ISD::isNormalStore(St) || St->isVolatile())
6283 return SDValue();
6284
6285 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6286 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6287 SelectionDAG &DAG = DCI.DAG;
6288 DebugLoc DL = St->getDebugLoc();
6289 SDValue BasePtr = St->getBasePtr();
6290 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6291 StVal.getNode()->getOperand(0), BasePtr,
6292 St->getPointerInfo(), St->isVolatile(),
6293 St->isNonTemporal(), St->getAlignment());
6294
6295 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6296 DAG.getConstant(4, MVT::i32));
6297 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6298 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6299 St->isNonTemporal(),
6300 std::min(4U, St->getAlignment() / 2));
6301 }
6302
6303 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006304 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6305 return SDValue();
6306
6307 SelectionDAG &DAG = DCI.DAG;
6308 DebugLoc dl = StVal.getDebugLoc();
6309 SDValue IntVec = StVal.getOperand(0);
6310 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6311 IntVec.getValueType().getVectorNumElements());
6312 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6313 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6314 Vec, StVal.getOperand(1));
6315 dl = N->getDebugLoc();
6316 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6317 // Make the DAGCombiner fold the bitcasts.
6318 DCI.AddToWorklist(Vec.getNode());
6319 DCI.AddToWorklist(ExtElt.getNode());
6320 DCI.AddToWorklist(V.getNode());
6321 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6322 St->getPointerInfo(), St->isVolatile(),
6323 St->isNonTemporal(), St->getAlignment(),
6324 St->getTBAAInfo());
6325}
6326
6327/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6328/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6329/// i64 vector to have f64 elements, since the value can then be loaded
6330/// directly into a VFP register.
6331static bool hasNormalLoadOperand(SDNode *N) {
6332 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6333 for (unsigned i = 0; i < NumElts; ++i) {
6334 SDNode *Elt = N->getOperand(i).getNode();
6335 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6336 return true;
6337 }
6338 return false;
6339}
6340
Bob Wilson75f02882010-09-17 22:59:05 +00006341/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6342/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006343static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6344 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006345 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6346 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6347 // into a pair of GPRs, which is fine when the value is used as a scalar,
6348 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006349 SelectionDAG &DAG = DCI.DAG;
6350 if (N->getNumOperands() == 2) {
6351 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6352 if (RV.getNode())
6353 return RV;
6354 }
Bob Wilson75f02882010-09-17 22:59:05 +00006355
Bob Wilson31600902010-12-21 06:43:19 +00006356 // Load i64 elements as f64 values so that type legalization does not split
6357 // them up into i32 values.
6358 EVT VT = N->getValueType(0);
6359 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6360 return SDValue();
6361 DebugLoc dl = N->getDebugLoc();
6362 SmallVector<SDValue, 8> Ops;
6363 unsigned NumElts = VT.getVectorNumElements();
6364 for (unsigned i = 0; i < NumElts; ++i) {
6365 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6366 Ops.push_back(V);
6367 // Make the DAGCombiner fold the bitcast.
6368 DCI.AddToWorklist(V.getNode());
6369 }
6370 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6371 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6372 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6373}
6374
6375/// PerformInsertEltCombine - Target-specific dag combine xforms for
6376/// ISD::INSERT_VECTOR_ELT.
6377static SDValue PerformInsertEltCombine(SDNode *N,
6378 TargetLowering::DAGCombinerInfo &DCI) {
6379 // Bitcast an i64 load inserted into a vector to f64.
6380 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6381 EVT VT = N->getValueType(0);
6382 SDNode *Elt = N->getOperand(1).getNode();
6383 if (VT.getVectorElementType() != MVT::i64 ||
6384 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6385 return SDValue();
6386
6387 SelectionDAG &DAG = DCI.DAG;
6388 DebugLoc dl = N->getDebugLoc();
6389 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6390 VT.getVectorNumElements());
6391 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6392 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6393 // Make the DAGCombiner fold the bitcasts.
6394 DCI.AddToWorklist(Vec.getNode());
6395 DCI.AddToWorklist(V.getNode());
6396 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6397 Vec, V, N->getOperand(2));
6398 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006399}
6400
Bob Wilsonf20700c2010-10-27 20:38:28 +00006401/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6402/// ISD::VECTOR_SHUFFLE.
6403static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6404 // The LLVM shufflevector instruction does not require the shuffle mask
6405 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6406 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6407 // operands do not match the mask length, they are extended by concatenating
6408 // them with undef vectors. That is probably the right thing for other
6409 // targets, but for NEON it is better to concatenate two double-register
6410 // size vector operands into a single quad-register size vector. Do that
6411 // transformation here:
6412 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6413 // shuffle(concat(v1, v2), undef)
6414 SDValue Op0 = N->getOperand(0);
6415 SDValue Op1 = N->getOperand(1);
6416 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6417 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6418 Op0.getNumOperands() != 2 ||
6419 Op1.getNumOperands() != 2)
6420 return SDValue();
6421 SDValue Concat0Op1 = Op0.getOperand(1);
6422 SDValue Concat1Op1 = Op1.getOperand(1);
6423 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6424 Concat1Op1.getOpcode() != ISD::UNDEF)
6425 return SDValue();
6426 // Skip the transformation if any of the types are illegal.
6427 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6428 EVT VT = N->getValueType(0);
6429 if (!TLI.isTypeLegal(VT) ||
6430 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6431 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6432 return SDValue();
6433
6434 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6435 Op0.getOperand(0), Op1.getOperand(0));
6436 // Translate the shuffle mask.
6437 SmallVector<int, 16> NewMask;
6438 unsigned NumElts = VT.getVectorNumElements();
6439 unsigned HalfElts = NumElts/2;
6440 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6441 for (unsigned n = 0; n < NumElts; ++n) {
6442 int MaskElt = SVN->getMaskElt(n);
6443 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006444 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006445 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006446 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006447 NewElt = HalfElts + MaskElt - NumElts;
6448 NewMask.push_back(NewElt);
6449 }
6450 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6451 DAG.getUNDEF(VT), NewMask.data());
6452}
6453
Bob Wilson1c3ef902011-02-07 17:43:21 +00006454/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6455/// NEON load/store intrinsics to merge base address updates.
6456static SDValue CombineBaseUpdate(SDNode *N,
6457 TargetLowering::DAGCombinerInfo &DCI) {
6458 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6459 return SDValue();
6460
6461 SelectionDAG &DAG = DCI.DAG;
6462 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6463 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6464 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6465 SDValue Addr = N->getOperand(AddrOpIdx);
6466
6467 // Search for a use of the address operand that is an increment.
6468 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6469 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6470 SDNode *User = *UI;
6471 if (User->getOpcode() != ISD::ADD ||
6472 UI.getUse().getResNo() != Addr.getResNo())
6473 continue;
6474
6475 // Check that the add is independent of the load/store. Otherwise, folding
6476 // it would create a cycle.
6477 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6478 continue;
6479
6480 // Find the new opcode for the updating load/store.
6481 bool isLoad = true;
6482 bool isLaneOp = false;
6483 unsigned NewOpc = 0;
6484 unsigned NumVecs = 0;
6485 if (isIntrinsic) {
6486 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6487 switch (IntNo) {
6488 default: assert(0 && "unexpected intrinsic for Neon base update");
6489 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6490 NumVecs = 1; break;
6491 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6492 NumVecs = 2; break;
6493 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6494 NumVecs = 3; break;
6495 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6496 NumVecs = 4; break;
6497 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6498 NumVecs = 2; isLaneOp = true; break;
6499 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6500 NumVecs = 3; isLaneOp = true; break;
6501 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6502 NumVecs = 4; isLaneOp = true; break;
6503 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6504 NumVecs = 1; isLoad = false; break;
6505 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6506 NumVecs = 2; isLoad = false; break;
6507 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6508 NumVecs = 3; isLoad = false; break;
6509 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6510 NumVecs = 4; isLoad = false; break;
6511 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6512 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6513 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6514 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6515 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6516 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6517 }
6518 } else {
6519 isLaneOp = true;
6520 switch (N->getOpcode()) {
6521 default: assert(0 && "unexpected opcode for Neon base update");
6522 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6523 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6524 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6525 }
6526 }
6527
6528 // Find the size of memory referenced by the load/store.
6529 EVT VecTy;
6530 if (isLoad)
6531 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006532 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006533 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6534 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6535 if (isLaneOp)
6536 NumBytes /= VecTy.getVectorNumElements();
6537
6538 // If the increment is a constant, it must match the memory ref size.
6539 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6540 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6541 uint64_t IncVal = CInc->getZExtValue();
6542 if (IncVal != NumBytes)
6543 continue;
6544 } else if (NumBytes >= 3 * 16) {
6545 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6546 // separate instructions that make it harder to use a non-constant update.
6547 continue;
6548 }
6549
6550 // Create the new updating load/store node.
6551 EVT Tys[6];
6552 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6553 unsigned n;
6554 for (n = 0; n < NumResultVecs; ++n)
6555 Tys[n] = VecTy;
6556 Tys[n++] = MVT::i32;
6557 Tys[n] = MVT::Other;
6558 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6559 SmallVector<SDValue, 8> Ops;
6560 Ops.push_back(N->getOperand(0)); // incoming chain
6561 Ops.push_back(N->getOperand(AddrOpIdx));
6562 Ops.push_back(Inc);
6563 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6564 Ops.push_back(N->getOperand(i));
6565 }
6566 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6567 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6568 Ops.data(), Ops.size(),
6569 MemInt->getMemoryVT(),
6570 MemInt->getMemOperand());
6571
6572 // Update the uses.
6573 std::vector<SDValue> NewResults;
6574 for (unsigned i = 0; i < NumResultVecs; ++i) {
6575 NewResults.push_back(SDValue(UpdN.getNode(), i));
6576 }
6577 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6578 DCI.CombineTo(N, NewResults);
6579 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6580
6581 break;
Owen Anderson76706012011-04-05 21:48:57 +00006582 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006583 return SDValue();
6584}
6585
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006586/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6587/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6588/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6589/// return true.
6590static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6591 SelectionDAG &DAG = DCI.DAG;
6592 EVT VT = N->getValueType(0);
6593 // vldN-dup instructions only support 64-bit vectors for N > 1.
6594 if (!VT.is64BitVector())
6595 return false;
6596
6597 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6598 SDNode *VLD = N->getOperand(0).getNode();
6599 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6600 return false;
6601 unsigned NumVecs = 0;
6602 unsigned NewOpc = 0;
6603 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6604 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6605 NumVecs = 2;
6606 NewOpc = ARMISD::VLD2DUP;
6607 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6608 NumVecs = 3;
6609 NewOpc = ARMISD::VLD3DUP;
6610 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6611 NumVecs = 4;
6612 NewOpc = ARMISD::VLD4DUP;
6613 } else {
6614 return false;
6615 }
6616
6617 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6618 // numbers match the load.
6619 unsigned VLDLaneNo =
6620 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6621 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6622 UI != UE; ++UI) {
6623 // Ignore uses of the chain result.
6624 if (UI.getUse().getResNo() == NumVecs)
6625 continue;
6626 SDNode *User = *UI;
6627 if (User->getOpcode() != ARMISD::VDUPLANE ||
6628 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6629 return false;
6630 }
6631
6632 // Create the vldN-dup node.
6633 EVT Tys[5];
6634 unsigned n;
6635 for (n = 0; n < NumVecs; ++n)
6636 Tys[n] = VT;
6637 Tys[n] = MVT::Other;
6638 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6639 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6640 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6641 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6642 Ops, 2, VLDMemInt->getMemoryVT(),
6643 VLDMemInt->getMemOperand());
6644
6645 // Update the uses.
6646 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6647 UI != UE; ++UI) {
6648 unsigned ResNo = UI.getUse().getResNo();
6649 // Ignore uses of the chain result.
6650 if (ResNo == NumVecs)
6651 continue;
6652 SDNode *User = *UI;
6653 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6654 }
6655
6656 // Now the vldN-lane intrinsic is dead except for its chain result.
6657 // Update uses of the chain.
6658 std::vector<SDValue> VLDDupResults;
6659 for (unsigned n = 0; n < NumVecs; ++n)
6660 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6661 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6662 DCI.CombineTo(VLD, VLDDupResults);
6663
6664 return true;
6665}
6666
Bob Wilson9e82bf12010-07-14 01:22:12 +00006667/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6668/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006669static SDValue PerformVDUPLANECombine(SDNode *N,
6670 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006671 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006672
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006673 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6674 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6675 if (CombineVLDDUP(N, DCI))
6676 return SDValue(N, 0);
6677
6678 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6679 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006680 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006681 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006682 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006683 return SDValue();
6684
6685 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6686 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6687 // The canonical VMOV for a zero vector uses a 32-bit element size.
6688 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6689 unsigned EltBits;
6690 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6691 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006692 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006693 if (EltSize > VT.getVectorElementType().getSizeInBits())
6694 return SDValue();
6695
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006696 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006697}
6698
Eric Christopherfa6f5912011-06-29 21:10:36 +00006699// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00006700// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
6701static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
6702{
Chad Rosier118c9a02011-06-28 17:26:57 +00006703 integerPart cN;
6704 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00006705 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
6706 I != E; I++) {
6707 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
6708 if (!C)
6709 return false;
6710
Eric Christopherfa6f5912011-06-29 21:10:36 +00006711 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00006712 APFloat APF = C->getValueAPF();
6713 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
6714 != APFloat::opOK || !isExact)
6715 return false;
6716
6717 c0 = (I == 0) ? cN : c0;
6718 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
6719 return false;
6720 }
6721 C = c0;
6722 return true;
6723}
6724
6725/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
6726/// can replace combinations of VMUL and VCVT (floating-point to integer)
6727/// when the VMUL has a constant operand that is a power of 2.
6728///
6729/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6730/// vmul.f32 d16, d17, d16
6731/// vcvt.s32.f32 d16, d16
6732/// becomes:
6733/// vcvt.s32.f32 d16, d16, #3
6734static SDValue PerformVCVTCombine(SDNode *N,
6735 TargetLowering::DAGCombinerInfo &DCI,
6736 const ARMSubtarget *Subtarget) {
6737 SelectionDAG &DAG = DCI.DAG;
6738 SDValue Op = N->getOperand(0);
6739
6740 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
6741 Op.getOpcode() != ISD::FMUL)
6742 return SDValue();
6743
6744 uint64_t C;
6745 SDValue N0 = Op->getOperand(0);
6746 SDValue ConstVec = Op->getOperand(1);
6747 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
6748
Eric Christopherfa6f5912011-06-29 21:10:36 +00006749 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00006750 !isConstVecPow2(ConstVec, isSigned, C))
6751 return SDValue();
6752
6753 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
6754 Intrinsic::arm_neon_vcvtfp2fxu;
6755 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6756 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006757 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00006758 DAG.getConstant(Log2_64(C), MVT::i32));
6759}
6760
6761/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
6762/// can replace combinations of VCVT (integer to floating-point) and VDIV
6763/// when the VDIV has a constant operand that is a power of 2.
6764///
6765/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6766/// vcvt.f32.s32 d16, d16
6767/// vdiv.f32 d16, d17, d16
6768/// becomes:
6769/// vcvt.f32.s32 d16, d16, #3
6770static SDValue PerformVDIVCombine(SDNode *N,
6771 TargetLowering::DAGCombinerInfo &DCI,
6772 const ARMSubtarget *Subtarget) {
6773 SelectionDAG &DAG = DCI.DAG;
6774 SDValue Op = N->getOperand(0);
6775 unsigned OpOpcode = Op.getNode()->getOpcode();
6776
6777 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
6778 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
6779 return SDValue();
6780
6781 uint64_t C;
6782 SDValue ConstVec = N->getOperand(1);
6783 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
6784
6785 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
6786 !isConstVecPow2(ConstVec, isSigned, C))
6787 return SDValue();
6788
Eric Christopherfa6f5912011-06-29 21:10:36 +00006789 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00006790 Intrinsic::arm_neon_vcvtfxu2fp;
6791 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6792 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006793 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00006794 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
6795}
6796
6797/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00006798/// operand of a vector shift operation, where all the elements of the
6799/// build_vector must have the same constant integer value.
6800static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6801 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006802 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006803 Op = Op.getOperand(0);
6804 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6805 APInt SplatBits, SplatUndef;
6806 unsigned SplatBitSize;
6807 bool HasAnyUndefs;
6808 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6809 HasAnyUndefs, ElementBits) ||
6810 SplatBitSize > ElementBits)
6811 return false;
6812 Cnt = SplatBits.getSExtValue();
6813 return true;
6814}
6815
6816/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6817/// operand of a vector shift left operation. That value must be in the range:
6818/// 0 <= Value < ElementBits for a left shift; or
6819/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006820static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006821 assert(VT.isVector() && "vector shift count is not a vector type");
6822 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6823 if (! getVShiftImm(Op, ElementBits, Cnt))
6824 return false;
6825 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6826}
6827
6828/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6829/// operand of a vector shift right operation. For a shift opcode, the value
6830/// is positive, but for an intrinsic the value count must be negative. The
6831/// absolute value must be in the range:
6832/// 1 <= |Value| <= ElementBits for a right shift; or
6833/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006834static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006835 int64_t &Cnt) {
6836 assert(VT.isVector() && "vector shift count is not a vector type");
6837 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6838 if (! getVShiftImm(Op, ElementBits, Cnt))
6839 return false;
6840 if (isIntrinsic)
6841 Cnt = -Cnt;
6842 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6843}
6844
6845/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6846static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6847 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6848 switch (IntNo) {
6849 default:
6850 // Don't do anything for most intrinsics.
6851 break;
6852
6853 // Vector shifts: check for immediate versions and lower them.
6854 // Note: This is done during DAG combining instead of DAG legalizing because
6855 // the build_vectors for 64-bit vector element shift counts are generally
6856 // not legal, and it is hard to see their values after they get legalized to
6857 // loads from a constant pool.
6858 case Intrinsic::arm_neon_vshifts:
6859 case Intrinsic::arm_neon_vshiftu:
6860 case Intrinsic::arm_neon_vshiftls:
6861 case Intrinsic::arm_neon_vshiftlu:
6862 case Intrinsic::arm_neon_vshiftn:
6863 case Intrinsic::arm_neon_vrshifts:
6864 case Intrinsic::arm_neon_vrshiftu:
6865 case Intrinsic::arm_neon_vrshiftn:
6866 case Intrinsic::arm_neon_vqshifts:
6867 case Intrinsic::arm_neon_vqshiftu:
6868 case Intrinsic::arm_neon_vqshiftsu:
6869 case Intrinsic::arm_neon_vqshiftns:
6870 case Intrinsic::arm_neon_vqshiftnu:
6871 case Intrinsic::arm_neon_vqshiftnsu:
6872 case Intrinsic::arm_neon_vqrshiftns:
6873 case Intrinsic::arm_neon_vqrshiftnu:
6874 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006875 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006876 int64_t Cnt;
6877 unsigned VShiftOpc = 0;
6878
6879 switch (IntNo) {
6880 case Intrinsic::arm_neon_vshifts:
6881 case Intrinsic::arm_neon_vshiftu:
6882 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6883 VShiftOpc = ARMISD::VSHL;
6884 break;
6885 }
6886 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6887 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6888 ARMISD::VSHRs : ARMISD::VSHRu);
6889 break;
6890 }
6891 return SDValue();
6892
6893 case Intrinsic::arm_neon_vshiftls:
6894 case Intrinsic::arm_neon_vshiftlu:
6895 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6896 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006897 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006898
6899 case Intrinsic::arm_neon_vrshifts:
6900 case Intrinsic::arm_neon_vrshiftu:
6901 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6902 break;
6903 return SDValue();
6904
6905 case Intrinsic::arm_neon_vqshifts:
6906 case Intrinsic::arm_neon_vqshiftu:
6907 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6908 break;
6909 return SDValue();
6910
6911 case Intrinsic::arm_neon_vqshiftsu:
6912 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6913 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006914 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006915
6916 case Intrinsic::arm_neon_vshiftn:
6917 case Intrinsic::arm_neon_vrshiftn:
6918 case Intrinsic::arm_neon_vqshiftns:
6919 case Intrinsic::arm_neon_vqshiftnu:
6920 case Intrinsic::arm_neon_vqshiftnsu:
6921 case Intrinsic::arm_neon_vqrshiftns:
6922 case Intrinsic::arm_neon_vqrshiftnu:
6923 case Intrinsic::arm_neon_vqrshiftnsu:
6924 // Narrowing shifts require an immediate right shift.
6925 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6926 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006927 llvm_unreachable("invalid shift count for narrowing vector shift "
6928 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006929
6930 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006931 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006932 }
6933
6934 switch (IntNo) {
6935 case Intrinsic::arm_neon_vshifts:
6936 case Intrinsic::arm_neon_vshiftu:
6937 // Opcode already set above.
6938 break;
6939 case Intrinsic::arm_neon_vshiftls:
6940 case Intrinsic::arm_neon_vshiftlu:
6941 if (Cnt == VT.getVectorElementType().getSizeInBits())
6942 VShiftOpc = ARMISD::VSHLLi;
6943 else
6944 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6945 ARMISD::VSHLLs : ARMISD::VSHLLu);
6946 break;
6947 case Intrinsic::arm_neon_vshiftn:
6948 VShiftOpc = ARMISD::VSHRN; break;
6949 case Intrinsic::arm_neon_vrshifts:
6950 VShiftOpc = ARMISD::VRSHRs; break;
6951 case Intrinsic::arm_neon_vrshiftu:
6952 VShiftOpc = ARMISD::VRSHRu; break;
6953 case Intrinsic::arm_neon_vrshiftn:
6954 VShiftOpc = ARMISD::VRSHRN; break;
6955 case Intrinsic::arm_neon_vqshifts:
6956 VShiftOpc = ARMISD::VQSHLs; break;
6957 case Intrinsic::arm_neon_vqshiftu:
6958 VShiftOpc = ARMISD::VQSHLu; break;
6959 case Intrinsic::arm_neon_vqshiftsu:
6960 VShiftOpc = ARMISD::VQSHLsu; break;
6961 case Intrinsic::arm_neon_vqshiftns:
6962 VShiftOpc = ARMISD::VQSHRNs; break;
6963 case Intrinsic::arm_neon_vqshiftnu:
6964 VShiftOpc = ARMISD::VQSHRNu; break;
6965 case Intrinsic::arm_neon_vqshiftnsu:
6966 VShiftOpc = ARMISD::VQSHRNsu; break;
6967 case Intrinsic::arm_neon_vqrshiftns:
6968 VShiftOpc = ARMISD::VQRSHRNs; break;
6969 case Intrinsic::arm_neon_vqrshiftnu:
6970 VShiftOpc = ARMISD::VQRSHRNu; break;
6971 case Intrinsic::arm_neon_vqrshiftnsu:
6972 VShiftOpc = ARMISD::VQRSHRNsu; break;
6973 }
6974
6975 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006976 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006977 }
6978
6979 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006980 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006981 int64_t Cnt;
6982 unsigned VShiftOpc = 0;
6983
6984 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6985 VShiftOpc = ARMISD::VSLI;
6986 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6987 VShiftOpc = ARMISD::VSRI;
6988 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006989 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006990 }
6991
6992 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6993 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006994 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006995 }
6996
6997 case Intrinsic::arm_neon_vqrshifts:
6998 case Intrinsic::arm_neon_vqrshiftu:
6999 // No immediate versions of these to check for.
7000 break;
7001 }
7002
7003 return SDValue();
7004}
7005
7006/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7007/// lowers them. As with the vector shift intrinsics, this is done during DAG
7008/// combining instead of DAG legalizing because the build_vectors for 64-bit
7009/// vector element shift counts are generally not legal, and it is hard to see
7010/// their values after they get legalized to loads from a constant pool.
7011static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7012 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007013 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007014
7015 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007016 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7017 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007018 return SDValue();
7019
7020 assert(ST->hasNEON() && "unexpected vector shift");
7021 int64_t Cnt;
7022
7023 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007024 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007025
7026 case ISD::SHL:
7027 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7028 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007029 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007030 break;
7031
7032 case ISD::SRA:
7033 case ISD::SRL:
7034 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7035 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7036 ARMISD::VSHRs : ARMISD::VSHRu);
7037 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007038 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007039 }
7040 }
7041 return SDValue();
7042}
7043
7044/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7045/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7046static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7047 const ARMSubtarget *ST) {
7048 SDValue N0 = N->getOperand(0);
7049
7050 // Check for sign- and zero-extensions of vector extract operations of 8-
7051 // and 16-bit vector elements. NEON supports these directly. They are
7052 // handled during DAG combining because type legalization will promote them
7053 // to 32-bit types and it is messy to recognize the operations after that.
7054 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7055 SDValue Vec = N0.getOperand(0);
7056 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007057 EVT VT = N->getValueType(0);
7058 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007059 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7060
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 if (VT == MVT::i32 &&
7062 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007063 TLI.isTypeLegal(Vec.getValueType()) &&
7064 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007065
7066 unsigned Opc = 0;
7067 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007068 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007069 case ISD::SIGN_EXTEND:
7070 Opc = ARMISD::VGETLANEs;
7071 break;
7072 case ISD::ZERO_EXTEND:
7073 case ISD::ANY_EXTEND:
7074 Opc = ARMISD::VGETLANEu;
7075 break;
7076 }
7077 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7078 }
7079 }
7080
7081 return SDValue();
7082}
7083
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007084/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7085/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7086static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7087 const ARMSubtarget *ST) {
7088 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007089 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007090 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7091 // a NaN; only do the transformation when it matches that behavior.
7092
7093 // For now only do this when using NEON for FP operations; if using VFP, it
7094 // is not obvious that the benefit outweighs the cost of switching to the
7095 // NEON pipeline.
7096 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7097 N->getValueType(0) != MVT::f32)
7098 return SDValue();
7099
7100 SDValue CondLHS = N->getOperand(0);
7101 SDValue CondRHS = N->getOperand(1);
7102 SDValue LHS = N->getOperand(2);
7103 SDValue RHS = N->getOperand(3);
7104 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7105
7106 unsigned Opcode = 0;
7107 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007108 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007109 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007110 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007111 IsReversed = true ; // x CC y ? y : x
7112 } else {
7113 return SDValue();
7114 }
7115
Bob Wilsone742bb52010-02-24 22:15:53 +00007116 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007117 switch (CC) {
7118 default: break;
7119 case ISD::SETOLT:
7120 case ISD::SETOLE:
7121 case ISD::SETLT:
7122 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007123 case ISD::SETULT:
7124 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007125 // If LHS is NaN, an ordered comparison will be false and the result will
7126 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7127 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7128 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7129 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7130 break;
7131 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7132 // will return -0, so vmin can only be used for unsafe math or if one of
7133 // the operands is known to be nonzero.
7134 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
7135 !UnsafeFPMath &&
7136 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7137 break;
7138 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007139 break;
7140
7141 case ISD::SETOGT:
7142 case ISD::SETOGE:
7143 case ISD::SETGT:
7144 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007145 case ISD::SETUGT:
7146 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007147 // If LHS is NaN, an ordered comparison will be false and the result will
7148 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7149 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7150 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
7151 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7152 break;
7153 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
7154 // will return +0, so vmax can only be used for unsafe math or if one of
7155 // the operands is known to be nonzero.
7156 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
7157 !UnsafeFPMath &&
7158 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7159 break;
7160 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007161 break;
7162 }
7163
7164 if (!Opcode)
7165 return SDValue();
7166 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
7167}
7168
Evan Chenge721f5c2011-07-13 00:42:17 +00007169/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
7170SDValue
7171ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
7172 SDValue Cmp = N->getOperand(4);
7173 if (Cmp.getOpcode() != ARMISD::CMPZ)
7174 // Only looking at EQ and NE cases.
7175 return SDValue();
7176
7177 EVT VT = N->getValueType(0);
7178 DebugLoc dl = N->getDebugLoc();
7179 SDValue LHS = Cmp.getOperand(0);
7180 SDValue RHS = Cmp.getOperand(1);
7181 SDValue FalseVal = N->getOperand(0);
7182 SDValue TrueVal = N->getOperand(1);
7183 SDValue ARMcc = N->getOperand(2);
7184 ARMCC::CondCodes CC = (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
7185
7186 // Simplify
7187 // mov r1, r0
7188 // cmp r1, x
7189 // mov r0, y
7190 // moveq r0, x
7191 // to
7192 // cmp r0, x
7193 // movne r0, y
7194 //
7195 // mov r1, r0
7196 // cmp r1, x
7197 // mov r0, x
7198 // movne r0, y
7199 // to
7200 // cmp r0, x
7201 // movne r0, y
7202 /// FIXME: Turn this into a target neutral optimization?
7203 SDValue Res;
7204 if (CC == ARMCC::NE && FalseVal == RHS) {
7205 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
7206 N->getOperand(3), Cmp);
7207 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
7208 SDValue ARMcc;
7209 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
7210 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
7211 N->getOperand(3), NewCmp);
7212 }
7213
7214 if (Res.getNode()) {
7215 APInt KnownZero, KnownOne;
7216 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
7217 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
7218 // Capture demanded bits information that would be otherwise lost.
7219 if (KnownZero == 0xfffffffe)
7220 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7221 DAG.getValueType(MVT::i1));
7222 else if (KnownZero == 0xffffff00)
7223 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7224 DAG.getValueType(MVT::i8));
7225 else if (KnownZero == 0xffff0000)
7226 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7227 DAG.getValueType(MVT::i16));
7228 }
7229
7230 return Res;
7231}
7232
Dan Gohman475871a2008-07-27 21:46:04 +00007233SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007234 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007235 switch (N->getOpcode()) {
7236 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00007237 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007238 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007239 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007240 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00007241 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00007242 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00007243 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007244 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00007245 case ISD::STORE: return PerformSTORECombine(N, DCI);
7246 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
7247 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00007248 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007249 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00007250 case ISD::FP_TO_SINT:
7251 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
7252 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007253 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00007254 case ISD::SHL:
7255 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007256 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00007257 case ISD::SIGN_EXTEND:
7258 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007259 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
7260 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00007261 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00007262 case ARMISD::VLD2DUP:
7263 case ARMISD::VLD3DUP:
7264 case ARMISD::VLD4DUP:
7265 return CombineBaseUpdate(N, DCI);
7266 case ISD::INTRINSIC_VOID:
7267 case ISD::INTRINSIC_W_CHAIN:
7268 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7269 case Intrinsic::arm_neon_vld1:
7270 case Intrinsic::arm_neon_vld2:
7271 case Intrinsic::arm_neon_vld3:
7272 case Intrinsic::arm_neon_vld4:
7273 case Intrinsic::arm_neon_vld2lane:
7274 case Intrinsic::arm_neon_vld3lane:
7275 case Intrinsic::arm_neon_vld4lane:
7276 case Intrinsic::arm_neon_vst1:
7277 case Intrinsic::arm_neon_vst2:
7278 case Intrinsic::arm_neon_vst3:
7279 case Intrinsic::arm_neon_vst4:
7280 case Intrinsic::arm_neon_vst2lane:
7281 case Intrinsic::arm_neon_vst3lane:
7282 case Intrinsic::arm_neon_vst4lane:
7283 return CombineBaseUpdate(N, DCI);
7284 default: break;
7285 }
7286 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007287 }
Dan Gohman475871a2008-07-27 21:46:04 +00007288 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007289}
7290
Evan Cheng31959b12011-02-02 01:06:55 +00007291bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
7292 EVT VT) const {
7293 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
7294}
7295
Bill Wendlingaf566342009-08-15 21:21:19 +00007296bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00007297 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00007298 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00007299
7300 switch (VT.getSimpleVT().SimpleTy) {
7301 default:
7302 return false;
7303 case MVT::i8:
7304 case MVT::i16:
7305 case MVT::i32:
7306 return true;
7307 // FIXME: VLD1 etc with standard alignment is legal.
7308 }
7309}
7310
Evan Chenge6c835f2009-08-14 20:09:37 +00007311static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
7312 if (V < 0)
7313 return false;
7314
7315 unsigned Scale = 1;
7316 switch (VT.getSimpleVT().SimpleTy) {
7317 default: return false;
7318 case MVT::i1:
7319 case MVT::i8:
7320 // Scale == 1;
7321 break;
7322 case MVT::i16:
7323 // Scale == 2;
7324 Scale = 2;
7325 break;
7326 case MVT::i32:
7327 // Scale == 4;
7328 Scale = 4;
7329 break;
7330 }
7331
7332 if ((V & (Scale - 1)) != 0)
7333 return false;
7334 V /= Scale;
7335 return V == (V & ((1LL << 5) - 1));
7336}
7337
7338static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
7339 const ARMSubtarget *Subtarget) {
7340 bool isNeg = false;
7341 if (V < 0) {
7342 isNeg = true;
7343 V = - V;
7344 }
7345
7346 switch (VT.getSimpleVT().SimpleTy) {
7347 default: return false;
7348 case MVT::i1:
7349 case MVT::i8:
7350 case MVT::i16:
7351 case MVT::i32:
7352 // + imm12 or - imm8
7353 if (isNeg)
7354 return V == (V & ((1LL << 8) - 1));
7355 return V == (V & ((1LL << 12) - 1));
7356 case MVT::f32:
7357 case MVT::f64:
7358 // Same as ARM mode. FIXME: NEON?
7359 if (!Subtarget->hasVFP2())
7360 return false;
7361 if ((V & 3) != 0)
7362 return false;
7363 V >>= 2;
7364 return V == (V & ((1LL << 8) - 1));
7365 }
7366}
7367
Evan Chengb01fad62007-03-12 23:30:29 +00007368/// isLegalAddressImmediate - Return true if the integer value can be used
7369/// as the offset of the target addressing mode for load / store of the
7370/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00007371static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007372 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00007373 if (V == 0)
7374 return true;
7375
Evan Cheng65011532009-03-09 19:15:00 +00007376 if (!VT.isSimple())
7377 return false;
7378
Evan Chenge6c835f2009-08-14 20:09:37 +00007379 if (Subtarget->isThumb1Only())
7380 return isLegalT1AddressImmediate(V, VT);
7381 else if (Subtarget->isThumb2())
7382 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007383
Evan Chenge6c835f2009-08-14 20:09:37 +00007384 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007385 if (V < 0)
7386 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007387 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007388 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007389 case MVT::i1:
7390 case MVT::i8:
7391 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007392 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007393 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007394 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007395 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007396 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007397 case MVT::f32:
7398 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007399 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007400 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007401 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007402 return false;
7403 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007404 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007405 }
Evan Chenga8e29892007-01-19 07:51:42 +00007406}
7407
Evan Chenge6c835f2009-08-14 20:09:37 +00007408bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7409 EVT VT) const {
7410 int Scale = AM.Scale;
7411 if (Scale < 0)
7412 return false;
7413
7414 switch (VT.getSimpleVT().SimpleTy) {
7415 default: return false;
7416 case MVT::i1:
7417 case MVT::i8:
7418 case MVT::i16:
7419 case MVT::i32:
7420 if (Scale == 1)
7421 return true;
7422 // r + r << imm
7423 Scale = Scale & ~1;
7424 return Scale == 2 || Scale == 4 || Scale == 8;
7425 case MVT::i64:
7426 // r + r
7427 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7428 return true;
7429 return false;
7430 case MVT::isVoid:
7431 // Note, we allow "void" uses (basically, uses that aren't loads or
7432 // stores), because arm allows folding a scale into many arithmetic
7433 // operations. This should be made more precise and revisited later.
7434
7435 // Allow r << imm, but the imm has to be a multiple of two.
7436 if (Scale & 1) return false;
7437 return isPowerOf2_32(Scale);
7438 }
7439}
7440
Chris Lattner37caf8c2007-04-09 23:33:39 +00007441/// isLegalAddressingMode - Return true if the addressing mode represented
7442/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007443bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007444 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007445 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007446 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007447 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007448
Chris Lattner37caf8c2007-04-09 23:33:39 +00007449 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007450 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007451 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007452
Chris Lattner37caf8c2007-04-09 23:33:39 +00007453 switch (AM.Scale) {
7454 case 0: // no scale reg, must be "r+i" or "r", or "i".
7455 break;
7456 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007457 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007458 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007459 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007460 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007461 // ARM doesn't support any R+R*scale+imm addr modes.
7462 if (AM.BaseOffs)
7463 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007464
Bob Wilson2c7dab12009-04-08 17:55:28 +00007465 if (!VT.isSimple())
7466 return false;
7467
Evan Chenge6c835f2009-08-14 20:09:37 +00007468 if (Subtarget->isThumb2())
7469 return isLegalT2ScaledAddressingMode(AM, VT);
7470
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007471 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007472 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007473 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007474 case MVT::i1:
7475 case MVT::i8:
7476 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007477 if (Scale < 0) Scale = -Scale;
7478 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007479 return true;
7480 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007481 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007482 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007483 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007484 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007485 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007486 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007487 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007488
Owen Anderson825b72b2009-08-11 20:47:22 +00007489 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007490 // Note, we allow "void" uses (basically, uses that aren't loads or
7491 // stores), because arm allows folding a scale into many arithmetic
7492 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007493
Chris Lattner37caf8c2007-04-09 23:33:39 +00007494 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007495 if (Scale & 1) return false;
7496 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007497 }
7498 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007499 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007500 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007501}
7502
Evan Cheng77e47512009-11-11 19:05:52 +00007503/// isLegalICmpImmediate - Return true if the specified immediate is legal
7504/// icmp immediate, that is the target has icmp instructions which can compare
7505/// a register against the immediate without having to materialize the
7506/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007507bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007508 if (!Subtarget->isThumb())
7509 return ARM_AM::getSOImmVal(Imm) != -1;
7510 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007511 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007512 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007513}
7514
Dan Gohmancca82142011-05-03 00:46:49 +00007515/// isLegalAddImmediate - Return true if the specified immediate is legal
7516/// add immediate, that is the target has add instructions which can add
7517/// a register with the immediate without having to materialize the
7518/// immediate into a register.
7519bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7520 return ARM_AM::getSOImmVal(Imm) != -1;
7521}
7522
Owen Andersone50ed302009-08-10 22:56:29 +00007523static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007524 bool isSEXTLoad, SDValue &Base,
7525 SDValue &Offset, bool &isInc,
7526 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007527 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7528 return false;
7529
Owen Anderson825b72b2009-08-11 20:47:22 +00007530 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007531 // AddressingMode 3
7532 Base = Ptr->getOperand(0);
7533 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007534 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007535 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007536 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007537 isInc = false;
7538 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7539 return true;
7540 }
7541 }
7542 isInc = (Ptr->getOpcode() == ISD::ADD);
7543 Offset = Ptr->getOperand(1);
7544 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007545 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007546 // AddressingMode 2
7547 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007548 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007549 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007550 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007551 isInc = false;
7552 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7553 Base = Ptr->getOperand(0);
7554 return true;
7555 }
7556 }
7557
7558 if (Ptr->getOpcode() == ISD::ADD) {
7559 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00007560 ARM_AM::ShiftOpc ShOpcVal=
7561 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00007562 if (ShOpcVal != ARM_AM::no_shift) {
7563 Base = Ptr->getOperand(1);
7564 Offset = Ptr->getOperand(0);
7565 } else {
7566 Base = Ptr->getOperand(0);
7567 Offset = Ptr->getOperand(1);
7568 }
7569 return true;
7570 }
7571
7572 isInc = (Ptr->getOpcode() == ISD::ADD);
7573 Base = Ptr->getOperand(0);
7574 Offset = Ptr->getOperand(1);
7575 return true;
7576 }
7577
Jim Grosbache5165492009-11-09 00:11:35 +00007578 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007579 return false;
7580}
7581
Owen Andersone50ed302009-08-10 22:56:29 +00007582static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007583 bool isSEXTLoad, SDValue &Base,
7584 SDValue &Offset, bool &isInc,
7585 SelectionDAG &DAG) {
7586 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7587 return false;
7588
7589 Base = Ptr->getOperand(0);
7590 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7591 int RHSC = (int)RHS->getZExtValue();
7592 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7593 assert(Ptr->getOpcode() == ISD::ADD);
7594 isInc = false;
7595 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7596 return true;
7597 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7598 isInc = Ptr->getOpcode() == ISD::ADD;
7599 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7600 return true;
7601 }
7602 }
7603
7604 return false;
7605}
7606
Evan Chenga8e29892007-01-19 07:51:42 +00007607/// getPreIndexedAddressParts - returns true by value, base pointer and
7608/// offset pointer and addressing mode by reference if the node's address
7609/// can be legally represented as pre-indexed load / store address.
7610bool
Dan Gohman475871a2008-07-27 21:46:04 +00007611ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7612 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007613 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007614 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007615 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007616 return false;
7617
Owen Andersone50ed302009-08-10 22:56:29 +00007618 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007619 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007620 bool isSEXTLoad = false;
7621 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7622 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007623 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007624 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7625 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7626 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007627 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007628 } else
7629 return false;
7630
7631 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007632 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007633 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007634 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7635 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007636 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007637 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007638 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007639 if (!isLegal)
7640 return false;
7641
7642 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7643 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007644}
7645
7646/// getPostIndexedAddressParts - returns true by value, base pointer and
7647/// offset pointer and addressing mode by reference if this node can be
7648/// combined with a load / store to form a post-indexed load / store.
7649bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007650 SDValue &Base,
7651 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007652 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007653 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007654 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007655 return false;
7656
Owen Andersone50ed302009-08-10 22:56:29 +00007657 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007658 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007659 bool isSEXTLoad = false;
7660 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007661 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007662 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007663 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7664 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007665 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007666 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007667 } else
7668 return false;
7669
7670 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007671 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007672 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007673 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007674 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007675 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007676 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7677 isInc, DAG);
7678 if (!isLegal)
7679 return false;
7680
Evan Cheng28dad2a2010-05-18 21:31:17 +00007681 if (Ptr != Base) {
7682 // Swap base ptr and offset to catch more post-index load / store when
7683 // it's legal. In Thumb2 mode, offset must be an immediate.
7684 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7685 !Subtarget->isThumb2())
7686 std::swap(Base, Offset);
7687
7688 // Post-indexed load / store update the base pointer.
7689 if (Ptr != Base)
7690 return false;
7691 }
7692
Evan Chenge88d5ce2009-07-02 07:28:31 +00007693 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7694 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007695}
7696
Dan Gohman475871a2008-07-27 21:46:04 +00007697void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007698 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007699 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007700 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007701 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007702 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007703 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007704 switch (Op.getOpcode()) {
7705 default: break;
7706 case ARMISD::CMOV: {
7707 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007708 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007709 if (KnownZero == 0 && KnownOne == 0) return;
7710
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007711 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007712 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7713 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007714 KnownZero &= KnownZeroRHS;
7715 KnownOne &= KnownOneRHS;
7716 return;
7717 }
7718 }
7719}
7720
7721//===----------------------------------------------------------------------===//
7722// ARM Inline Assembly Support
7723//===----------------------------------------------------------------------===//
7724
Evan Cheng55d42002011-01-08 01:24:27 +00007725bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7726 // Looking for "rev" which is V6+.
7727 if (!Subtarget->hasV6Ops())
7728 return false;
7729
7730 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7731 std::string AsmStr = IA->getAsmString();
7732 SmallVector<StringRef, 4> AsmPieces;
7733 SplitString(AsmStr, AsmPieces, ";\n");
7734
7735 switch (AsmPieces.size()) {
7736 default: return false;
7737 case 1:
7738 AsmStr = AsmPieces[0];
7739 AsmPieces.clear();
7740 SplitString(AsmStr, AsmPieces, " \t,");
7741
7742 // rev $0, $1
7743 if (AsmPieces.size() == 3 &&
7744 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7745 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007746 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00007747 if (Ty && Ty->getBitWidth() == 32)
7748 return IntrinsicLowering::LowerToByteSwap(CI);
7749 }
7750 break;
7751 }
7752
7753 return false;
7754}
7755
Evan Chenga8e29892007-01-19 07:51:42 +00007756/// getConstraintType - Given a constraint letter, return the type of
7757/// constraint it is for this target.
7758ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007759ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7760 if (Constraint.size() == 1) {
7761 switch (Constraint[0]) {
7762 default: break;
7763 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007764 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00007765 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007766 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007767 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00007768 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00007769 // An address with a single base register. Due to the way we
7770 // currently handle addresses it is the same as an 'r' memory constraint.
7771 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007772 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007773 } else if (Constraint.size() == 2) {
7774 switch (Constraint[0]) {
7775 default: break;
7776 // All 'U+' constraints are addresses.
7777 case 'U': return C_Memory;
7778 }
Evan Chenga8e29892007-01-19 07:51:42 +00007779 }
Chris Lattner4234f572007-03-25 02:14:49 +00007780 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007781}
7782
John Thompson44ab89e2010-10-29 17:29:13 +00007783/// Examine constraint type and operand type and determine a weight value.
7784/// This object must already have been set up with the operand type
7785/// and the current alternative constraint selected.
7786TargetLowering::ConstraintWeight
7787ARMTargetLowering::getSingleConstraintMatchWeight(
7788 AsmOperandInfo &info, const char *constraint) const {
7789 ConstraintWeight weight = CW_Invalid;
7790 Value *CallOperandVal = info.CallOperandVal;
7791 // If we don't have a value, we can't do a match,
7792 // but allow it at the lowest weight.
7793 if (CallOperandVal == NULL)
7794 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007795 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007796 // Look at the constraint type.
7797 switch (*constraint) {
7798 default:
7799 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7800 break;
7801 case 'l':
7802 if (type->isIntegerTy()) {
7803 if (Subtarget->isThumb())
7804 weight = CW_SpecificReg;
7805 else
7806 weight = CW_Register;
7807 }
7808 break;
7809 case 'w':
7810 if (type->isFloatingPointTy())
7811 weight = CW_Register;
7812 break;
7813 }
7814 return weight;
7815}
7816
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007817typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7818RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007819ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007820 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007821 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007822 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007823 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007824 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007825 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007826 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007827 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007828 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007829 case 'h': // High regs or no regs.
7830 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007831 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007832 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007833 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007834 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007835 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007836 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007837 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007838 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007839 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007840 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007841 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007842 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007843 case 'x':
7844 if (VT == MVT::f32)
7845 return RCPair(0U, ARM::SPR_8RegisterClass);
7846 if (VT.getSizeInBits() == 64)
7847 return RCPair(0U, ARM::DPR_8RegisterClass);
7848 if (VT.getSizeInBits() == 128)
7849 return RCPair(0U, ARM::QPR_8RegisterClass);
7850 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007851 case 't':
7852 if (VT == MVT::f32)
7853 return RCPair(0U, ARM::SPRRegisterClass);
7854 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007855 }
7856 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007857 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007858 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007859
Evan Chenga8e29892007-01-19 07:51:42 +00007860 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7861}
7862
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007863/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7864/// vector. If it is invalid, don't add anything to Ops.
7865void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007866 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007867 std::vector<SDValue>&Ops,
7868 SelectionDAG &DAG) const {
7869 SDValue Result(0, 0);
7870
Eric Christopher100c8332011-06-02 23:16:42 +00007871 // Currently only support length 1 constraints.
7872 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007873
Eric Christopher100c8332011-06-02 23:16:42 +00007874 char ConstraintLetter = Constraint[0];
7875 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007876 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007877 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007878 case 'I': case 'J': case 'K': case 'L':
7879 case 'M': case 'N': case 'O':
7880 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7881 if (!C)
7882 return;
7883
7884 int64_t CVal64 = C->getSExtValue();
7885 int CVal = (int) CVal64;
7886 // None of these constraints allow values larger than 32 bits. Check
7887 // that the value fits in an int.
7888 if (CVal != CVal64)
7889 return;
7890
Eric Christopher100c8332011-06-02 23:16:42 +00007891 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007892 case 'j':
7893 // Constant suitable for movw, must be between 0 and
7894 // 65535.
7895 if (Subtarget->hasV6T2Ops())
7896 if (CVal >= 0 && CVal <= 65535)
7897 break;
7898 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007899 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007900 if (Subtarget->isThumb1Only()) {
7901 // This must be a constant between 0 and 255, for ADD
7902 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007903 if (CVal >= 0 && CVal <= 255)
7904 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007905 } else if (Subtarget->isThumb2()) {
7906 // A constant that can be used as an immediate value in a
7907 // data-processing instruction.
7908 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7909 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007910 } else {
7911 // A constant that can be used as an immediate value in a
7912 // data-processing instruction.
7913 if (ARM_AM::getSOImmVal(CVal) != -1)
7914 break;
7915 }
7916 return;
7917
7918 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007919 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007920 // This must be a constant between -255 and -1, for negated ADD
7921 // immediates. This can be used in GCC with an "n" modifier that
7922 // prints the negated value, for use with SUB instructions. It is
7923 // not useful otherwise but is implemented for compatibility.
7924 if (CVal >= -255 && CVal <= -1)
7925 break;
7926 } else {
7927 // This must be a constant between -4095 and 4095. It is not clear
7928 // what this constraint is intended for. Implemented for
7929 // compatibility with GCC.
7930 if (CVal >= -4095 && CVal <= 4095)
7931 break;
7932 }
7933 return;
7934
7935 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007936 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007937 // A 32-bit value where only one byte has a nonzero value. Exclude
7938 // zero to match GCC. This constraint is used by GCC internally for
7939 // constants that can be loaded with a move/shift combination.
7940 // It is not useful otherwise but is implemented for compatibility.
7941 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7942 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007943 } else if (Subtarget->isThumb2()) {
7944 // A constant whose bitwise inverse can be used as an immediate
7945 // value in a data-processing instruction. This can be used in GCC
7946 // with a "B" modifier that prints the inverted value, for use with
7947 // BIC and MVN instructions. It is not useful otherwise but is
7948 // implemented for compatibility.
7949 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7950 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007951 } else {
7952 // A constant whose bitwise inverse can be used as an immediate
7953 // value in a data-processing instruction. This can be used in GCC
7954 // with a "B" modifier that prints the inverted value, for use with
7955 // BIC and MVN instructions. It is not useful otherwise but is
7956 // implemented for compatibility.
7957 if (ARM_AM::getSOImmVal(~CVal) != -1)
7958 break;
7959 }
7960 return;
7961
7962 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007963 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007964 // This must be a constant between -7 and 7,
7965 // for 3-operand ADD/SUB immediate instructions.
7966 if (CVal >= -7 && CVal < 7)
7967 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007968 } else if (Subtarget->isThumb2()) {
7969 // A constant whose negation can be used as an immediate value in a
7970 // data-processing instruction. This can be used in GCC with an "n"
7971 // modifier that prints the negated value, for use with SUB
7972 // instructions. It is not useful otherwise but is implemented for
7973 // compatibility.
7974 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7975 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007976 } else {
7977 // A constant whose negation can be used as an immediate value in a
7978 // data-processing instruction. This can be used in GCC with an "n"
7979 // modifier that prints the negated value, for use with SUB
7980 // instructions. It is not useful otherwise but is implemented for
7981 // compatibility.
7982 if (ARM_AM::getSOImmVal(-CVal) != -1)
7983 break;
7984 }
7985 return;
7986
7987 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007988 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007989 // This must be a multiple of 4 between 0 and 1020, for
7990 // ADD sp + immediate.
7991 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7992 break;
7993 } else {
7994 // A power of two or a constant between 0 and 32. This is used in
7995 // GCC for the shift amount on shifted register operands, but it is
7996 // useful in general for any shift amounts.
7997 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7998 break;
7999 }
8000 return;
8001
8002 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008003 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008004 // This must be a constant between 0 and 31, for shift amounts.
8005 if (CVal >= 0 && CVal <= 31)
8006 break;
8007 }
8008 return;
8009
8010 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008011 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008012 // This must be a multiple of 4 between -508 and 508, for
8013 // ADD/SUB sp = sp + immediate.
8014 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8015 break;
8016 }
8017 return;
8018 }
8019 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8020 break;
8021 }
8022
8023 if (Result.getNode()) {
8024 Ops.push_back(Result);
8025 return;
8026 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008027 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008028}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008029
8030bool
8031ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8032 // The ARM target isn't yet aware of offsets.
8033 return false;
8034}
Evan Cheng39382422009-10-28 01:44:26 +00008035
8036int ARM::getVFPf32Imm(const APFloat &FPImm) {
8037 APInt Imm = FPImm.bitcastToAPInt();
8038 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
8039 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
8040 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
8041
8042 // We can handle 4 bits of mantissa.
8043 // mantissa = (16+UInt(e:f:g:h))/16.
8044 if (Mantissa & 0x7ffff)
8045 return -1;
8046 Mantissa >>= 19;
8047 if ((Mantissa & 0xf) != Mantissa)
8048 return -1;
8049
8050 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
8051 if (Exp < -3 || Exp > 4)
8052 return -1;
8053 Exp = ((Exp+3) & 0x7) ^ 4;
8054
8055 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
8056}
8057
8058int ARM::getVFPf64Imm(const APFloat &FPImm) {
8059 APInt Imm = FPImm.bitcastToAPInt();
8060 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
8061 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
8062 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
8063
8064 // We can handle 4 bits of mantissa.
8065 // mantissa = (16+UInt(e:f:g:h))/16.
8066 if (Mantissa & 0xffffffffffffLL)
8067 return -1;
8068 Mantissa >>= 48;
8069 if ((Mantissa & 0xf) != Mantissa)
8070 return -1;
8071
8072 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
8073 if (Exp < -3 || Exp > 4)
8074 return -1;
8075 Exp = ((Exp+3) & 0x7) ^ 4;
8076
8077 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
8078}
8079
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008080bool ARM::isBitFieldInvertedMask(unsigned v) {
8081 if (v == 0xffffffff)
8082 return 0;
8083 // there can be 1's on either or both "outsides", all the "inside"
8084 // bits must be 0's
8085 unsigned int lsb = 0, msb = 31;
8086 while (v & (1 << msb)) --msb;
8087 while (v & (1 << lsb)) ++lsb;
8088 for (unsigned int i = lsb; i <= msb; ++i) {
8089 if (v & (1 << i))
8090 return 0;
8091 }
8092 return 1;
8093}
8094
Evan Cheng39382422009-10-28 01:44:26 +00008095/// isFPImmLegal - Returns true if the target can instruction select the
8096/// specified FP immediate natively. If false, the legalizer will
8097/// materialize the FP immediate as a load from a constant pool.
8098bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8099 if (!Subtarget->hasVFP3())
8100 return false;
8101 if (VT == MVT::f32)
8102 return ARM::getVFPf32Imm(Imm) != -1;
8103 if (VT == MVT::f64)
8104 return ARM::getVFPf64Imm(Imm) != -1;
8105 return false;
8106}
Bob Wilson65ffec42010-09-21 17:56:22 +00008107
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008108/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008109/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8110/// specified in the intrinsic calls.
8111bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8112 const CallInst &I,
8113 unsigned Intrinsic) const {
8114 switch (Intrinsic) {
8115 case Intrinsic::arm_neon_vld1:
8116 case Intrinsic::arm_neon_vld2:
8117 case Intrinsic::arm_neon_vld3:
8118 case Intrinsic::arm_neon_vld4:
8119 case Intrinsic::arm_neon_vld2lane:
8120 case Intrinsic::arm_neon_vld3lane:
8121 case Intrinsic::arm_neon_vld4lane: {
8122 Info.opc = ISD::INTRINSIC_W_CHAIN;
8123 // Conservatively set memVT to the entire set of vectors loaded.
8124 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8125 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8126 Info.ptrVal = I.getArgOperand(0);
8127 Info.offset = 0;
8128 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8129 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8130 Info.vol = false; // volatile loads with NEON intrinsics not supported
8131 Info.readMem = true;
8132 Info.writeMem = false;
8133 return true;
8134 }
8135 case Intrinsic::arm_neon_vst1:
8136 case Intrinsic::arm_neon_vst2:
8137 case Intrinsic::arm_neon_vst3:
8138 case Intrinsic::arm_neon_vst4:
8139 case Intrinsic::arm_neon_vst2lane:
8140 case Intrinsic::arm_neon_vst3lane:
8141 case Intrinsic::arm_neon_vst4lane: {
8142 Info.opc = ISD::INTRINSIC_VOID;
8143 // Conservatively set memVT to the entire set of vectors stored.
8144 unsigned NumElts = 0;
8145 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008146 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008147 if (!ArgTy->isVectorTy())
8148 break;
8149 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8150 }
8151 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8152 Info.ptrVal = I.getArgOperand(0);
8153 Info.offset = 0;
8154 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8155 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8156 Info.vol = false; // volatile stores with NEON intrinsics not supported
8157 Info.readMem = false;
8158 Info.writeMem = true;
8159 return true;
8160 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008161 case Intrinsic::arm_strexd: {
8162 Info.opc = ISD::INTRINSIC_W_CHAIN;
8163 Info.memVT = MVT::i64;
8164 Info.ptrVal = I.getArgOperand(2);
8165 Info.offset = 0;
8166 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008167 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008168 Info.readMem = false;
8169 Info.writeMem = true;
8170 return true;
8171 }
8172 case Intrinsic::arm_ldrexd: {
8173 Info.opc = ISD::INTRINSIC_W_CHAIN;
8174 Info.memVT = MVT::i64;
8175 Info.ptrVal = I.getArgOperand(0);
8176 Info.offset = 0;
8177 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008178 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008179 Info.readMem = true;
8180 Info.writeMem = false;
8181 return true;
8182 }
Bob Wilson65ffec42010-09-21 17:56:22 +00008183 default:
8184 break;
8185 }
8186
8187 return false;
8188}