blob: faa861394247aa2b614569d9f804c9705524cb7f [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
20#include "ARMISelLowering.h"
21#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000022#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000023#include "ARMRegisterInfo.h"
24#include "ARMSubtarget.h"
25#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000026#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Cameron Zwaricha86686e2011-06-10 20:59:24 +000075namespace llvm {
76 class ARMCCState : public CCState {
77 public:
78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
80 LLVMContext &C, ParmContext PC)
81 : CCState(CC, isVarArg, MF, TM, locs, C) {
82 assert(((PC == Call) || (PC == Prologue)) &&
83 "ARMCCState users must specify whether their context is call"
84 "or prologue generation.");
85 CallOrPrologue = PC;
86 }
87 };
88}
89
Stuart Hastingsc7315872011-04-20 16:47:52 +000090// The APCS parameter registers.
91static const unsigned GPRArgRegs[] = {
92 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93};
94
Owen Andersone50ed302009-08-10 22:56:29 +000095void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
96 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000097 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
100 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 }
106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000109 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000110 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000111 if (ElemTy != MVT::i32) {
112 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
116 }
Owen Anderson70671842009-08-10 20:18:46 +0000117 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
118 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000119 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000120 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000121 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
122 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
125 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
126 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000127 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
128 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000129 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
130 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
131 setTruncStoreAction(VT.getSimpleVT(),
132 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000134 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135
136 // Promote all bit-wise operations.
137 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000139 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000146 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000147 }
Bob Wilson16330762009-09-16 00:17:28 +0000148
149 // Neon does not support vector divide/remainder operations.
150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Owen Andersone50ed302009-08-10 22:56:29 +0000158void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000159 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000161}
162
Owen Andersone50ed302009-08-10 22:56:29 +0000163void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000164 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000166}
167
Chris Lattnerf0144122009-07-28 03:13:23 +0000168static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000170 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000171
Chris Lattner80ec2792009-08-02 00:34:36 +0000172 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173}
174
Evan Chenga8e29892007-01-19 07:51:42 +0000175ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000176 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000178 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000179 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 // Uses VFP for Thumb libfuncs if available.
183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
184 // Single-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 // Double-precision floating-point arithmetic.
191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 // Single-precision comparisons.
197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 // Double-precision comparisons.
216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000224
Evan Chengb1df8f22007-04-27 08:15:43 +0000225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000233
Evan Chengb1df8f22007-04-27 08:15:43 +0000234 // Floating-point to integer conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000241
Evan Chengb1df8f22007-04-27 08:15:43 +0000242 // Conversions between floating types.
243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
245
246 // Integer to floating-point conversions.
247 // i64 conversions are done via library routines even when generating VFP
248 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000249 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
250 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256 }
257
Bob Wilson2f954612009-05-22 17:38:41 +0000258 // These libcalls are not available in 32-bit.
259 setLibcallName(RTLIB::SHL_I128, 0);
260 setLibcallName(RTLIB::SRL_I128, 0);
261 setLibcallName(RTLIB::SRA_I128, 0);
262
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000263 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000265 // RTABI chapter 4.1.2, Table 2
266 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
267 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
268 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
269 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
270 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
274
275 // Double-precision floating-point comparison helper functions
276 // RTABI chapter 4.1.2, Table 3
277 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
278 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
279 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
281 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
282 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
284 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
286 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
288 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
289 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
290 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
291 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
293 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
301
302 // Single-precision floating-point arithmetic helper functions
303 // RTABI chapter 4.1.2, Table 4
304 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
305 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
306 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
307 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
308 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
312
313 // Single-precision floating-point comparison helper functions
314 // RTABI chapter 4.1.2, Table 5
315 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
316 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
317 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
319 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
320 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
322 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
324 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
326 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
327 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
328 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
329 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
331 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
339
340 // Floating-point to integer conversions.
341 // RTABI chapter 4.1.2, Table 6
342 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
343 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
346 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
347 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
350 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
358
359 // Conversions between floating types.
360 // RTABI chapter 4.1.2, Table 7
361 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
362 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
363 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000365
366 // Integer to floating-point conversions.
367 // RTABI chapter 4.1.2, Table 8
368 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
369 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
370 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
371 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
372 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
373 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
374 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
375 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384
385 // Long long helper functions
386 // RTABI chapter 4.2, Table 9
387 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
388 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
389 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
390 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
391 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
392 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
393 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
399
400 // Integer division functions
401 // RTABI chapter 4.3.1
402 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
403 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
404 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
405 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
407 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
408 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000413 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000414
415 // Memory operations
416 // RTABI chapter 4.3.4
417 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
418 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
419 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000420 }
421
David Goodwinf1daf7d2009-07-08 23:10:31 +0000422 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000424 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000426 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000428 if (!Subtarget->isFPOnlySP())
429 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000430
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000432 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000433
434 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 addDRTypeForNEON(MVT::v2f32);
436 addDRTypeForNEON(MVT::v8i8);
437 addDRTypeForNEON(MVT::v4i16);
438 addDRTypeForNEON(MVT::v2i32);
439 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addQRTypeForNEON(MVT::v4f32);
442 addQRTypeForNEON(MVT::v2f64);
443 addQRTypeForNEON(MVT::v16i8);
444 addQRTypeForNEON(MVT::v8i16);
445 addQRTypeForNEON(MVT::v4i32);
446 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000447
Bob Wilson74dc72e2009-09-15 23:55:57 +0000448 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
449 // neither Neon nor VFP support any arithmetic operations on it.
450 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
451 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
452 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
453 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
454 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
455 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
456 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
457 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
458 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
459 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
460 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
461 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
462 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
463 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
464 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
465 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
467 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
468 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
469 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
470 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
471 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
472 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
473 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
474
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000475 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
476
Bob Wilson642b3292009-09-16 00:32:15 +0000477 // Neon does not support some operations on v1i64 and v2i64 types.
478 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000479 // Custom handling for some quad-vector types to detect VMULL.
480 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
481 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
482 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000483 // Custom handling for some vector types to avoid expensive expansions
484 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
485 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
486 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
487 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000488 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
489 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000490 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
491 // a destination type that is wider than the source.
492 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
493 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000494
Bob Wilson1c3ef902011-02-07 17:43:21 +0000495 setTargetDAGCombine(ISD::INTRINSIC_VOID);
496 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000497 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
498 setTargetDAGCombine(ISD::SHL);
499 setTargetDAGCombine(ISD::SRL);
500 setTargetDAGCombine(ISD::SRA);
501 setTargetDAGCombine(ISD::SIGN_EXTEND);
502 setTargetDAGCombine(ISD::ZERO_EXTEND);
503 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000504 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000505 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000506 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000507 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
508 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000509 }
510
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000511 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000512
513 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000515
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000516 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000518
Evan Chenga8e29892007-01-19 07:51:42 +0000519 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000520 if (!Subtarget->isThumb1Only()) {
521 for (unsigned im = (unsigned)ISD::PRE_INC;
522 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setIndexedLoadAction(im, MVT::i1, Legal);
524 setIndexedLoadAction(im, MVT::i8, Legal);
525 setIndexedLoadAction(im, MVT::i16, Legal);
526 setIndexedLoadAction(im, MVT::i32, Legal);
527 setIndexedStoreAction(im, MVT::i1, Legal);
528 setIndexedStoreAction(im, MVT::i8, Legal);
529 setIndexedStoreAction(im, MVT::i16, Legal);
530 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000531 }
Evan Chenga8e29892007-01-19 07:51:42 +0000532 }
533
534 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000535 setOperationAction(ISD::MUL, MVT::i64, Expand);
536 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000537 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
539 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000540 }
Eric Christopher2cc40132011-04-19 18:49:19 +0000541 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops())
542 setOperationAction(ISD::MULHS, MVT::i32, Expand);
543
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000544 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000545 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000546 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::SRL, MVT::i64, Custom);
548 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000549
550 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000552 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000554 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000556
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000557 // Only ARMv6 has BSWAP.
558 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000560
Evan Chenga8e29892007-01-19 07:51:42 +0000561 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000562 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000563 // v7M has a hardware divider
564 setOperationAction(ISD::SDIV, MVT::i32, Expand);
565 setOperationAction(ISD::UDIV, MVT::i32, Expand);
566 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 setOperationAction(ISD::SREM, MVT::i32, Expand);
568 setOperationAction(ISD::UREM, MVT::i32, Expand);
569 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
570 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000571
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
573 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
574 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
575 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000576 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000577
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000578 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000579
Evan Chenga8e29892007-01-19 07:51:42 +0000580 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setOperationAction(ISD::VASTART, MVT::Other, Custom);
582 setOperationAction(ISD::VAARG, MVT::Other, Expand);
583 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
584 setOperationAction(ISD::VAEND, MVT::Other, Expand);
585 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
586 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000587 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000588 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
589 setExceptionPointerRegister(ARM::R0);
590 setExceptionSelectorRegister(ARM::R1);
591
Evan Cheng3a1588a2010-04-15 22:20:34 +0000592 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000593 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
594 // the default expansion.
595 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000596 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000597 // membarrier needs custom lowering; the rest are legal and handled
598 // normally.
599 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
600 } else {
601 // Set them all for expansion, which will force libcalls.
602 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
603 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
604 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
605 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000606 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
607 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
608 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000609 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
610 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
612 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
613 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
616 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
617 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
618 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
624 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
625 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
626 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000627 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i8, Expand);
628 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i16, Expand);
629 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
630 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i8, Expand);
631 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i16, Expand);
632 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
633 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i8, Expand);
634 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i16, Expand);
635 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
636 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i8, Expand);
637 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i16, Expand);
638 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000639 // Since the libcalls include locking, fold in the fences
640 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000641 }
642 // 64-bit versions are always libcalls (for now)
643 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000644 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000645 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
646 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
647 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
648 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
649 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
650 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000651
Evan Cheng416941d2010-11-04 05:19:35 +0000652 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000653
Eli Friedmana2c6f452010-06-26 04:36:50 +0000654 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
655 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
657 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000658 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000660
Nate Begemand1fb5832010-08-03 21:31:55 +0000661 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000662 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
663 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000664 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000665 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
666 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000667
668 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000670 if (Subtarget->isTargetDarwin()) {
671 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
672 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000673 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000674 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000675 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000676
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::SETCC, MVT::i32, Expand);
678 setOperationAction(ISD::SETCC, MVT::f32, Expand);
679 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000680 setOperationAction(ISD::SELECT, MVT::i32, Custom);
681 setOperationAction(ISD::SELECT, MVT::f32, Custom);
682 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
684 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
685 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000686
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
688 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
689 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
690 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
691 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000692
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000693 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::FSIN, MVT::f64, Expand);
695 setOperationAction(ISD::FSIN, MVT::f32, Expand);
696 setOperationAction(ISD::FCOS, MVT::f32, Expand);
697 setOperationAction(ISD::FCOS, MVT::f64, Expand);
698 setOperationAction(ISD::FREM, MVT::f64, Expand);
699 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000700 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
702 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000703 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::FPOW, MVT::f64, Expand);
705 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000706
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000707 // Various VFP goodness
708 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000709 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
710 if (Subtarget->hasVFP2()) {
711 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
712 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
713 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
714 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
715 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000716 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000717 if (!Subtarget->hasFP16()) {
718 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
719 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000720 }
Evan Cheng110cf482008-04-01 01:50:16 +0000721 }
Evan Chenga8e29892007-01-19 07:51:42 +0000722
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000723 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000724 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000725 setTargetDAGCombine(ISD::ADD);
726 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000727 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000728
Owen Anderson080c0922010-11-05 19:27:46 +0000729 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000730 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000731 if (Subtarget->hasNEON())
732 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000733
Evan Chenga8e29892007-01-19 07:51:42 +0000734 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000735
Evan Chengf7d87ee2010-05-21 00:43:17 +0000736 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
737 setSchedulingPreference(Sched::RegPressure);
738 else
739 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000740
Evan Cheng05219282011-01-06 06:52:41 +0000741 //// temporary - rewrite interface to use type
742 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000743
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000744 // On ARM arguments smaller than 4 bytes are extended, so all arguments
745 // are at least 4 bytes aligned.
746 setMinStackArgumentAlignment(4);
747
Evan Chengfff606d2010-09-24 19:07:23 +0000748 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000749
750 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000751}
752
Andrew Trick32cec0a2011-01-19 02:35:27 +0000753// FIXME: It might make sense to define the representative register class as the
754// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
755// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
756// SPR's representative would be DPR_VFP2. This should work well if register
757// pressure tracking were modified such that a register use would increment the
758// pressure of the register class's representative and all of it's super
759// classes' representatives transitively. We have not implemented this because
760// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000761// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000762// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000763std::pair<const TargetRegisterClass*, uint8_t>
764ARMTargetLowering::findRepresentativeClass(EVT VT) const{
765 const TargetRegisterClass *RRC = 0;
766 uint8_t Cost = 1;
767 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000768 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000769 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000770 // Use DPR as representative register class for all floating point
771 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
772 // the cost is 1 for both f32 and f64.
773 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000774 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000775 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000776 // When NEON is used for SP, only half of the register file is available
777 // because operations that define both SP and DP results will be constrained
778 // to the VFP2 class (D0-D15). We currently model this constraint prior to
779 // coalescing by double-counting the SP regs. See the FIXME above.
780 if (Subtarget->useNEONForSinglePrecisionFP())
781 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000782 break;
783 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
784 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000785 RRC = ARM::DPRRegisterClass;
786 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000787 break;
788 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000789 RRC = ARM::DPRRegisterClass;
790 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000791 break;
792 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000793 RRC = ARM::DPRRegisterClass;
794 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000795 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000796 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000797 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000798}
799
Evan Chenga8e29892007-01-19 07:51:42 +0000800const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
801 switch (Opcode) {
802 default: return 0;
803 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000804 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000805 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000806 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
807 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000808 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000809 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
810 case ARMISD::tCALL: return "ARMISD::tCALL";
811 case ARMISD::BRCOND: return "ARMISD::BRCOND";
812 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000813 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000814 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
815 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
816 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000817 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000818 case ARMISD::CMPFP: return "ARMISD::CMPFP";
819 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000820 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000821 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
822 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000823
Jim Grosbach3482c802010-01-18 19:58:49 +0000824 case ARMISD::RBIT: return "ARMISD::RBIT";
825
Bob Wilson76a312b2010-03-19 22:51:32 +0000826 case ARMISD::FTOSI: return "ARMISD::FTOSI";
827 case ARMISD::FTOUI: return "ARMISD::FTOUI";
828 case ARMISD::SITOF: return "ARMISD::SITOF";
829 case ARMISD::UITOF: return "ARMISD::UITOF";
830
Evan Chenga8e29892007-01-19 07:51:42 +0000831 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
832 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
833 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000834
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000835 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
836 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000837
Evan Chengc5942082009-10-28 06:55:03 +0000838 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
839 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000840 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000841
Dale Johannesen51e28e62010-06-03 21:09:53 +0000842 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000843
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000844 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000845
Evan Cheng86198642009-08-07 00:34:42 +0000846 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
847
Jim Grosbach3728e962009-12-10 00:11:09 +0000848 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000849 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000850
Evan Chengdfed19f2010-11-03 06:34:55 +0000851 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
852
Bob Wilson5bafff32009-06-22 23:27:02 +0000853 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000854 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000855 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000856 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
857 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000858 case ARMISD::VCGEU: return "ARMISD::VCGEU";
859 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000860 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
861 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 case ARMISD::VCGTU: return "ARMISD::VCGTU";
863 case ARMISD::VTST: return "ARMISD::VTST";
864
865 case ARMISD::VSHL: return "ARMISD::VSHL";
866 case ARMISD::VSHRs: return "ARMISD::VSHRs";
867 case ARMISD::VSHRu: return "ARMISD::VSHRu";
868 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
869 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
870 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
871 case ARMISD::VSHRN: return "ARMISD::VSHRN";
872 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
873 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
874 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
875 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
876 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
877 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
878 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
879 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
880 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
881 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
882 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
883 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
884 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
885 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000886 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000887 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000888 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000889 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000890 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000891 case ARMISD::VREV64: return "ARMISD::VREV64";
892 case ARMISD::VREV32: return "ARMISD::VREV32";
893 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000894 case ARMISD::VZIP: return "ARMISD::VZIP";
895 case ARMISD::VUZP: return "ARMISD::VUZP";
896 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000897 case ARMISD::VTBL1: return "ARMISD::VTBL1";
898 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000899 case ARMISD::VMULLs: return "ARMISD::VMULLs";
900 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000901 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000902 case ARMISD::FMAX: return "ARMISD::FMAX";
903 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000904 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000905 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
906 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000907 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000908 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
909 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
910 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000911 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
912 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
913 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
914 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
915 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
916 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
917 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
918 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
919 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
920 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
921 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
922 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
923 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
924 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
925 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
926 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
927 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000928 }
929}
930
Evan Cheng06b666c2010-05-15 02:18:07 +0000931/// getRegClassFor - Return the register class that should be used for the
932/// specified value type.
933TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
934 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
935 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
936 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000937 if (Subtarget->hasNEON()) {
938 if (VT == MVT::v4i64)
939 return ARM::QQPRRegisterClass;
940 else if (VT == MVT::v8i64)
941 return ARM::QQQQPRRegisterClass;
942 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000943 return TargetLowering::getRegClassFor(VT);
944}
945
Eric Christopherab695882010-07-21 22:26:11 +0000946// Create a fast isel object.
947FastISel *
948ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
949 return ARM::createFastISel(funcInfo);
950}
951
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000952/// getMaximalGlobalOffset - Returns the maximal possible offset which can
953/// be used for loads / stores from the global.
954unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
955 return (Subtarget->isThumb1Only() ? 127 : 4095);
956}
957
Evan Cheng1cc39842010-05-20 23:26:43 +0000958Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000959 unsigned NumVals = N->getNumValues();
960 if (!NumVals)
961 return Sched::RegPressure;
962
963 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000964 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000965 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000966 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000967 if (VT.isFloatingPoint() || VT.isVector())
968 return Sched::Latency;
969 }
Evan Chengc10f5432010-05-28 23:25:23 +0000970
971 if (!N->isMachineOpcode())
972 return Sched::RegPressure;
973
974 // Load are scheduled for latency even if there instruction itinerary
975 // is not available.
976 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
977 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000978
979 if (TID.getNumDefs() == 0)
980 return Sched::RegPressure;
981 if (!Itins->isEmpty() &&
982 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000983 return Sched::Latency;
984
Evan Cheng1cc39842010-05-20 23:26:43 +0000985 return Sched::RegPressure;
986}
987
Evan Chenga8e29892007-01-19 07:51:42 +0000988//===----------------------------------------------------------------------===//
989// Lowering Code
990//===----------------------------------------------------------------------===//
991
Evan Chenga8e29892007-01-19 07:51:42 +0000992/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
993static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
994 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000995 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000996 case ISD::SETNE: return ARMCC::NE;
997 case ISD::SETEQ: return ARMCC::EQ;
998 case ISD::SETGT: return ARMCC::GT;
999 case ISD::SETGE: return ARMCC::GE;
1000 case ISD::SETLT: return ARMCC::LT;
1001 case ISD::SETLE: return ARMCC::LE;
1002 case ISD::SETUGT: return ARMCC::HI;
1003 case ISD::SETUGE: return ARMCC::HS;
1004 case ISD::SETULT: return ARMCC::LO;
1005 case ISD::SETULE: return ARMCC::LS;
1006 }
1007}
1008
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001009/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1010static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001011 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001012 CondCode2 = ARMCC::AL;
1013 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001014 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001015 case ISD::SETEQ:
1016 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1017 case ISD::SETGT:
1018 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1019 case ISD::SETGE:
1020 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1021 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001022 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001023 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1024 case ISD::SETO: CondCode = ARMCC::VC; break;
1025 case ISD::SETUO: CondCode = ARMCC::VS; break;
1026 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1027 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1028 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1029 case ISD::SETLT:
1030 case ISD::SETULT: CondCode = ARMCC::LT; break;
1031 case ISD::SETLE:
1032 case ISD::SETULE: CondCode = ARMCC::LE; break;
1033 case ISD::SETNE:
1034 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1035 }
Evan Chenga8e29892007-01-19 07:51:42 +00001036}
1037
Bob Wilson1f595bb2009-04-17 19:07:39 +00001038//===----------------------------------------------------------------------===//
1039// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001040//===----------------------------------------------------------------------===//
1041
1042#include "ARMGenCallingConv.inc"
1043
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001044/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1045/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001046CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001047 bool Return,
1048 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001049 switch (CC) {
1050 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001051 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001052 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001053 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001054 if (!Subtarget->isAAPCS_ABI())
1055 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1056 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1057 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1058 }
1059 // Fallthrough
1060 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001061 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001062 if (!Subtarget->isAAPCS_ABI())
1063 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1064 else if (Subtarget->hasVFP2() &&
1065 FloatABIType == FloatABI::Hard && !isVarArg)
1066 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1067 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1068 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001069 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001070 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001071 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001072 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001073 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001074 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001075 }
1076}
1077
Dan Gohman98ca4f22009-08-05 01:29:28 +00001078/// LowerCallResult - Lower the result values of a call into the
1079/// appropriate copies out of appropriate physical registers.
1080SDValue
1081ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001082 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001083 const SmallVectorImpl<ISD::InputArg> &Ins,
1084 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001085 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086
Bob Wilson1f595bb2009-04-17 19:07:39 +00001087 // Assign locations to each value returned by this call.
1088 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001089 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1090 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001091 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001092 CCAssignFnForNode(CallConv, /* Return*/ true,
1093 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001094
1095 // Copy all of the result registers out of their specified physreg.
1096 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1097 CCValAssign VA = RVLocs[i];
1098
Bob Wilson80915242009-04-25 00:33:20 +00001099 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001100 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001101 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001103 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001104 Chain = Lo.getValue(1);
1105 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001106 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001108 InFlag);
1109 Chain = Hi.getValue(1);
1110 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001111 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001112
Owen Anderson825b72b2009-08-11 20:47:22 +00001113 if (VA.getLocVT() == MVT::v2f64) {
1114 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1115 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1116 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001117
1118 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001119 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001120 Chain = Lo.getValue(1);
1121 InFlag = Lo.getValue(2);
1122 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001123 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001124 Chain = Hi.getValue(1);
1125 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001126 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1128 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001129 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001130 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001131 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1132 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001133 Chain = Val.getValue(1);
1134 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 }
Bob Wilson80915242009-04-25 00:33:20 +00001136
1137 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001138 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001139 case CCValAssign::Full: break;
1140 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001141 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001142 break;
1143 }
1144
Dan Gohman98ca4f22009-08-05 01:29:28 +00001145 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146 }
1147
Dan Gohman98ca4f22009-08-05 01:29:28 +00001148 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001149}
1150
Bob Wilsondee46d72009-04-17 20:35:10 +00001151/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001152SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001153ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1154 SDValue StackPtr, SDValue Arg,
1155 DebugLoc dl, SelectionDAG &DAG,
1156 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001157 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001158 unsigned LocMemOffset = VA.getLocMemOffset();
1159 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1160 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001161 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001162 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001163 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001164}
1165
Dan Gohman98ca4f22009-08-05 01:29:28 +00001166void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001167 SDValue Chain, SDValue &Arg,
1168 RegsToPassVector &RegsToPass,
1169 CCValAssign &VA, CCValAssign &NextVA,
1170 SDValue &StackPtr,
1171 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001172 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001173
Jim Grosbache5165492009-11-09 00:11:35 +00001174 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001175 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001176 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1177
1178 if (NextVA.isRegLoc())
1179 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1180 else {
1181 assert(NextVA.isMemLoc());
1182 if (StackPtr.getNode() == 0)
1183 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1184
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1186 dl, DAG, NextVA,
1187 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001188 }
1189}
1190
Dan Gohman98ca4f22009-08-05 01:29:28 +00001191/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001192/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1193/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001195ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001196 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001197 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001199 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001200 const SmallVectorImpl<ISD::InputArg> &Ins,
1201 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001202 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001203 MachineFunction &MF = DAG.getMachineFunction();
1204 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1205 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001206 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001207 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001208 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001209 if (isTailCall) {
1210 // Check if it's really possible to do a tail call.
1211 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1212 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001213 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001214 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1215 // detected sibcalls.
1216 if (isTailCall) {
1217 ++NumTailCalls;
1218 IsSibCall = true;
1219 }
1220 }
Evan Chenga8e29892007-01-19 07:51:42 +00001221
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222 // Analyze operands of the call, assigning locations to each operand.
1223 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001224 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1225 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001227 CCAssignFnForNode(CallConv, /* Return*/ false,
1228 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001229
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 // Get a count of how many bytes are to be pushed on the stack.
1231 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001232
Dale Johannesen51e28e62010-06-03 21:09:53 +00001233 // For tail calls, memory operands are available in our caller's stack.
1234 if (IsSibCall)
1235 NumBytes = 0;
1236
Evan Chenga8e29892007-01-19 07:51:42 +00001237 // Adjust the stack pointer for the new arguments...
1238 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001239 if (!IsSibCall)
1240 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001241
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001242 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001243
Bob Wilson5bafff32009-06-22 23:27:02 +00001244 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001246
Bob Wilson1f595bb2009-04-17 19:07:39 +00001247 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001248 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001249 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1250 i != e;
1251 ++i, ++realArgIdx) {
1252 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001253 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001254 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001255 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001256
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 // Promote the value if needed.
1258 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001259 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 case CCValAssign::Full: break;
1261 case CCValAssign::SExt:
1262 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1263 break;
1264 case CCValAssign::ZExt:
1265 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1266 break;
1267 case CCValAssign::AExt:
1268 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1269 break;
1270 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001271 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001272 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001273 }
1274
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001275 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001277 if (VA.getLocVT() == MVT::v2f64) {
1278 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1279 DAG.getConstant(0, MVT::i32));
1280 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1281 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001282
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001284 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1285
1286 VA = ArgLocs[++i]; // skip ahead to next loc
1287 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001289 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1290 } else {
1291 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001292
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1294 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001295 }
1296 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001297 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001298 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001299 }
1300 } else if (VA.isRegLoc()) {
1301 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001302 } else if (isByVal) {
1303 assert(VA.isMemLoc());
1304 unsigned offset = 0;
1305
1306 // True if this byval aggregate will be split between registers
1307 // and memory.
1308 if (CCInfo.isFirstByValRegValid()) {
1309 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1310 unsigned int i, j;
1311 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1312 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1313 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1314 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1315 MachinePointerInfo(),
1316 false, false, 0);
1317 MemOpChains.push_back(Load.getValue(1));
1318 RegsToPass.push_back(std::make_pair(j, Load));
1319 }
1320 offset = ARM::R4 - CCInfo.getFirstByValReg();
1321 CCInfo.clearFirstByValReg();
1322 }
1323
1324 unsigned LocMemOffset = VA.getLocMemOffset();
1325 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1326 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1327 StkPtrOff);
1328 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1329 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1330 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1331 MVT::i32);
1332 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1333 Flags.getByValAlign(),
1334 /*isVolatile=*/false,
1335 /*AlwaysInline=*/false,
1336 MachinePointerInfo(0),
1337 MachinePointerInfo(0)));
1338
1339 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001340 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001341
Dan Gohman98ca4f22009-08-05 01:29:28 +00001342 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1343 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001344 }
Evan Chenga8e29892007-01-19 07:51:42 +00001345 }
1346
1347 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001348 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001349 &MemOpChains[0], MemOpChains.size());
1350
1351 // Build a sequence of copy-to-reg nodes chained together with token chain
1352 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001354 // Tail call byval lowering might overwrite argument registers so in case of
1355 // tail call optimization the copies to registers are lowered later.
1356 if (!isTailCall)
1357 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1358 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1359 RegsToPass[i].second, InFlag);
1360 InFlag = Chain.getValue(1);
1361 }
Evan Chenga8e29892007-01-19 07:51:42 +00001362
Dale Johannesen51e28e62010-06-03 21:09:53 +00001363 // For tail calls lower the arguments to the 'real' stack slot.
1364 if (isTailCall) {
1365 // Force all the incoming stack arguments to be loaded from the stack
1366 // before any new outgoing arguments are stored to the stack, because the
1367 // outgoing stack slots may alias the incoming argument stack slots, and
1368 // the alias isn't otherwise explicit. This is slightly more conservative
1369 // than necessary, because it means that each store effectively depends
1370 // on every argument instead of just those arguments it would clobber.
1371
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001372 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001373 InFlag = SDValue();
1374 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1375 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1376 RegsToPass[i].second, InFlag);
1377 InFlag = Chain.getValue(1);
1378 }
1379 InFlag =SDValue();
1380 }
1381
Bill Wendling056292f2008-09-16 21:48:12 +00001382 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1383 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1384 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001385 bool isDirect = false;
1386 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001387 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001388 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001389
1390 if (EnableARMLongCalls) {
1391 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1392 && "long-calls with non-static relocation model!");
1393 // Handle a global address or an external symbol. If it's not one of
1394 // those, the target's already in a register, so we don't need to do
1395 // anything extra.
1396 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001397 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001398 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001399 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001400 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1401 ARMPCLabelIndex,
1402 ARMCP::CPValue, 0);
1403 // Get the address of the callee into a register
1404 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1405 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1406 Callee = DAG.getLoad(getPointerTy(), dl,
1407 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001408 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001409 false, false, 0);
1410 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1411 const char *Sym = S->getSymbol();
1412
1413 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001414 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001415 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1416 Sym, ARMPCLabelIndex, 0);
1417 // Get the address of the callee into a register
1418 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1419 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1420 Callee = DAG.getLoad(getPointerTy(), dl,
1421 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001422 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001423 false, false, 0);
1424 }
1425 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001426 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001427 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001428 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001429 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001430 getTargetMachine().getRelocationModel() != Reloc::Static;
1431 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001432 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001433 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001434 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001435 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001436 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001437 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001438 ARMPCLabelIndex,
1439 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001440 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001442 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001443 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001444 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001445 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001446 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001447 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001448 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001449 } else {
1450 // On ELF targets for PIC code, direct calls should go through the PLT
1451 unsigned OpFlags = 0;
1452 if (Subtarget->isTargetELF() &&
1453 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1454 OpFlags = ARMII::MO_PLT;
1455 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1456 }
Bill Wendling056292f2008-09-16 21:48:12 +00001457 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001458 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001459 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001460 getTargetMachine().getRelocationModel() != Reloc::Static;
1461 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001462 // tBX takes a register source operand.
1463 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001464 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001465 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001466 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001467 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001468 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001470 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001471 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001472 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001473 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001474 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001475 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001476 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001477 } else {
1478 unsigned OpFlags = 0;
1479 // On ELF targets for PIC code, direct calls should go through the PLT
1480 if (Subtarget->isTargetELF() &&
1481 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1482 OpFlags = ARMII::MO_PLT;
1483 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1484 }
Evan Chenga8e29892007-01-19 07:51:42 +00001485 }
1486
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001487 // FIXME: handle tail calls differently.
1488 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001489 if (Subtarget->isThumb()) {
1490 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001491 CallOpc = ARMISD::CALL_NOLINK;
1492 else
1493 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1494 } else {
1495 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001496 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1497 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001498 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001499
Dan Gohman475871a2008-07-27 21:46:04 +00001500 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001501 Ops.push_back(Chain);
1502 Ops.push_back(Callee);
1503
1504 // Add argument registers to the end of the list so that they are known live
1505 // into the call.
1506 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1507 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1508 RegsToPass[i].second.getValueType()));
1509
Gabor Greifba36cb52008-08-28 21:40:38 +00001510 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001511 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001512
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001513 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001514 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001515 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001516
Duncan Sands4bdcb612008-07-02 17:40:58 +00001517 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001518 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001519 InFlag = Chain.getValue(1);
1520
Chris Lattnere563bbc2008-10-11 22:08:30 +00001521 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1522 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001523 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001524 InFlag = Chain.getValue(1);
1525
Bob Wilson1f595bb2009-04-17 19:07:39 +00001526 // Handle result values, copying them out of physregs into vregs that we
1527 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001528 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1529 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001530}
1531
Stuart Hastingsf222e592011-02-28 17:17:53 +00001532/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001533/// on the stack. Remember the next parameter register to allocate,
1534/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001535/// this.
1536void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001537llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1538 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1539 assert((State->getCallOrPrologue() == Prologue ||
1540 State->getCallOrPrologue() == Call) &&
1541 "unhandled ParmContext");
1542 if ((!State->isFirstByValRegValid()) &&
1543 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1544 State->setFirstByValReg(reg);
1545 // At a call site, a byval parameter that is split between
1546 // registers and memory needs its size truncated here. In a
1547 // function prologue, such byval parameters are reassembled in
1548 // memory, and are not truncated.
1549 if (State->getCallOrPrologue() == Call) {
1550 unsigned excess = 4 * (ARM::R4 - reg);
1551 assert(size >= excess && "expected larger existing stack allocation");
1552 size -= excess;
1553 }
1554 }
1555 // Confiscate any remaining parameter registers to preclude their
1556 // assignment to subsequent parameters.
1557 while (State->AllocateReg(GPRArgRegs, 4))
1558 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001559}
1560
Dale Johannesen51e28e62010-06-03 21:09:53 +00001561/// MatchingStackOffset - Return true if the given stack call argument is
1562/// already available in the same position (relatively) of the caller's
1563/// incoming argument stack.
1564static
1565bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1566 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1567 const ARMInstrInfo *TII) {
1568 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1569 int FI = INT_MAX;
1570 if (Arg.getOpcode() == ISD::CopyFromReg) {
1571 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001572 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001573 return false;
1574 MachineInstr *Def = MRI->getVRegDef(VR);
1575 if (!Def)
1576 return false;
1577 if (!Flags.isByVal()) {
1578 if (!TII->isLoadFromStackSlot(Def, FI))
1579 return false;
1580 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001581 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001582 }
1583 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1584 if (Flags.isByVal())
1585 // ByVal argument is passed in as a pointer but it's now being
1586 // dereferenced. e.g.
1587 // define @foo(%struct.X* %A) {
1588 // tail call @bar(%struct.X* byval %A)
1589 // }
1590 return false;
1591 SDValue Ptr = Ld->getBasePtr();
1592 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1593 if (!FINode)
1594 return false;
1595 FI = FINode->getIndex();
1596 } else
1597 return false;
1598
1599 assert(FI != INT_MAX);
1600 if (!MFI->isFixedObjectIndex(FI))
1601 return false;
1602 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1603}
1604
1605/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1606/// for tail call optimization. Targets which want to do tail call
1607/// optimization should implement this function.
1608bool
1609ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1610 CallingConv::ID CalleeCC,
1611 bool isVarArg,
1612 bool isCalleeStructRet,
1613 bool isCallerStructRet,
1614 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001615 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001616 const SmallVectorImpl<ISD::InputArg> &Ins,
1617 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001618 const Function *CallerF = DAG.getMachineFunction().getFunction();
1619 CallingConv::ID CallerCC = CallerF->getCallingConv();
1620 bool CCMatch = CallerCC == CalleeCC;
1621
1622 // Look for obvious safe cases to perform tail call optimization that do not
1623 // require ABI changes. This is what gcc calls sibcall.
1624
Jim Grosbach7616b642010-06-16 23:45:49 +00001625 // Do not sibcall optimize vararg calls unless the call site is not passing
1626 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001627 if (isVarArg && !Outs.empty())
1628 return false;
1629
1630 // Also avoid sibcall optimization if either caller or callee uses struct
1631 // return semantics.
1632 if (isCalleeStructRet || isCallerStructRet)
1633 return false;
1634
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001635 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001636 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001637 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1638 // LR. This means if we need to reload LR, it takes an extra instructions,
1639 // which outweighs the value of the tail call; but here we don't know yet
1640 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001641 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001642 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001643
1644 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1645 // but we need to make sure there are enough registers; the only valid
1646 // registers are the 4 used for parameters. We don't currently do this
1647 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001648 if (Subtarget->isThumb1Only())
1649 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001650
Dale Johannesen51e28e62010-06-03 21:09:53 +00001651 // If the calling conventions do not match, then we'd better make sure the
1652 // results are returned in the same way as what the caller expects.
1653 if (!CCMatch) {
1654 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001655 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1656 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001657 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1658
1659 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001660 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1661 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001662 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1663
1664 if (RVLocs1.size() != RVLocs2.size())
1665 return false;
1666 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1667 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1668 return false;
1669 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1670 return false;
1671 if (RVLocs1[i].isRegLoc()) {
1672 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1673 return false;
1674 } else {
1675 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1676 return false;
1677 }
1678 }
1679 }
1680
1681 // If the callee takes no arguments then go on to check the results of the
1682 // call.
1683 if (!Outs.empty()) {
1684 // Check if stack adjustment is needed. For now, do not do this if any
1685 // argument is passed on the stack.
1686 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001687 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1688 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001689 CCInfo.AnalyzeCallOperands(Outs,
1690 CCAssignFnForNode(CalleeCC, false, isVarArg));
1691 if (CCInfo.getNextStackOffset()) {
1692 MachineFunction &MF = DAG.getMachineFunction();
1693
1694 // Check if the arguments are already laid out in the right way as
1695 // the caller's fixed stack objects.
1696 MachineFrameInfo *MFI = MF.getFrameInfo();
1697 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1698 const ARMInstrInfo *TII =
1699 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001700 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1701 i != e;
1702 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001703 CCValAssign &VA = ArgLocs[i];
1704 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001705 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001706 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001707 if (VA.getLocInfo() == CCValAssign::Indirect)
1708 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001709 if (VA.needsCustom()) {
1710 // f64 and vector types are split into multiple registers or
1711 // register/stack-slot combinations. The types will not match
1712 // the registers; give up on memory f64 refs until we figure
1713 // out what to do about this.
1714 if (!VA.isRegLoc())
1715 return false;
1716 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001717 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001718 if (RegVT == MVT::v2f64) {
1719 if (!ArgLocs[++i].isRegLoc())
1720 return false;
1721 if (!ArgLocs[++i].isRegLoc())
1722 return false;
1723 }
1724 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001725 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1726 MFI, MRI, TII))
1727 return false;
1728 }
1729 }
1730 }
1731 }
1732
1733 return true;
1734}
1735
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736SDValue
1737ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001738 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001739 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001740 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001741 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001742
Bob Wilsondee46d72009-04-17 20:35:10 +00001743 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001744 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001745
Bob Wilsondee46d72009-04-17 20:35:10 +00001746 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001747 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1748 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001749
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001751 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1752 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001753
1754 // If this is the first return lowered for this function, add
1755 // the regs to the liveout set for the function.
1756 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1757 for (unsigned i = 0; i != RVLocs.size(); ++i)
1758 if (RVLocs[i].isRegLoc())
1759 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001760 }
1761
Bob Wilson1f595bb2009-04-17 19:07:39 +00001762 SDValue Flag;
1763
1764 // Copy the result values into the output registers.
1765 for (unsigned i = 0, realRVLocIdx = 0;
1766 i != RVLocs.size();
1767 ++i, ++realRVLocIdx) {
1768 CCValAssign &VA = RVLocs[i];
1769 assert(VA.isRegLoc() && "Can only return in registers!");
1770
Dan Gohmanc9403652010-07-07 15:54:55 +00001771 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001772
1773 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001774 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001775 case CCValAssign::Full: break;
1776 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001777 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001778 break;
1779 }
1780
Bob Wilson1f595bb2009-04-17 19:07:39 +00001781 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001783 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1785 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001786 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001787 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001788
1789 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1790 Flag = Chain.getValue(1);
1791 VA = RVLocs[++i]; // skip ahead to next loc
1792 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1793 HalfGPRs.getValue(1), Flag);
1794 Flag = Chain.getValue(1);
1795 VA = RVLocs[++i]; // skip ahead to next loc
1796
1797 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1799 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001800 }
1801 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1802 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001803 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001804 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001805 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001806 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001807 VA = RVLocs[++i]; // skip ahead to next loc
1808 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1809 Flag);
1810 } else
1811 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1812
Bob Wilsondee46d72009-04-17 20:35:10 +00001813 // Guarantee that all emitted copies are
1814 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815 Flag = Chain.getValue(1);
1816 }
1817
1818 SDValue result;
1819 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001823
1824 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001825}
1826
Evan Cheng3d2125c2010-11-30 23:55:39 +00001827bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1828 if (N->getNumValues() != 1)
1829 return false;
1830 if (!N->hasNUsesOfValue(1, 0))
1831 return false;
1832
1833 unsigned NumCopies = 0;
1834 SDNode* Copies[2];
1835 SDNode *Use = *N->use_begin();
1836 if (Use->getOpcode() == ISD::CopyToReg) {
1837 Copies[NumCopies++] = Use;
1838 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1839 // f64 returned in a pair of GPRs.
1840 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1841 UI != UE; ++UI) {
1842 if (UI->getOpcode() != ISD::CopyToReg)
1843 return false;
1844 Copies[UI.getUse().getResNo()] = *UI;
1845 ++NumCopies;
1846 }
1847 } else if (Use->getOpcode() == ISD::BITCAST) {
1848 // f32 returned in a single GPR.
1849 if (!Use->hasNUsesOfValue(1, 0))
1850 return false;
1851 Use = *Use->use_begin();
1852 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1853 return false;
1854 Copies[NumCopies++] = Use;
1855 } else {
1856 return false;
1857 }
1858
1859 if (NumCopies != 1 && NumCopies != 2)
1860 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001861
1862 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001863 for (unsigned i = 0; i < NumCopies; ++i) {
1864 SDNode *Copy = Copies[i];
1865 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1866 UI != UE; ++UI) {
1867 if (UI->getOpcode() == ISD::CopyToReg) {
1868 SDNode *Use = *UI;
1869 if (Use == Copies[0] || Use == Copies[1])
1870 continue;
1871 return false;
1872 }
1873 if (UI->getOpcode() != ARMISD::RET_FLAG)
1874 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001875 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001876 }
1877 }
1878
Evan Cheng1bf891a2010-12-01 22:59:46 +00001879 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001880}
1881
Evan Cheng485fafc2011-03-21 01:19:09 +00001882bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1883 if (!EnableARMTailCalls)
1884 return false;
1885
1886 if (!CI->isTailCall())
1887 return false;
1888
1889 return !Subtarget->isThumb1Only();
1890}
1891
Bob Wilsonb62d2572009-11-03 00:02:05 +00001892// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1893// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1894// one of the above mentioned nodes. It has to be wrapped because otherwise
1895// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1896// be used to form addressing mode. These wrapped nodes will be selected
1897// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001898static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001899 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001900 // FIXME there is no actual debug info here
1901 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001902 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001903 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001904 if (CP->isMachineConstantPoolEntry())
1905 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1906 CP->getAlignment());
1907 else
1908 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1909 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001911}
1912
Jim Grosbache1102ca2010-07-19 17:20:38 +00001913unsigned ARMTargetLowering::getJumpTableEncoding() const {
1914 return MachineJumpTableInfo::EK_Inline;
1915}
1916
Dan Gohmand858e902010-04-17 15:26:15 +00001917SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1918 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001919 MachineFunction &MF = DAG.getMachineFunction();
1920 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1921 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001922 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001923 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001924 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001925 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1926 SDValue CPAddr;
1927 if (RelocM == Reloc::Static) {
1928 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1929 } else {
1930 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001931 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001932 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1933 ARMCP::CPBlockAddress,
1934 PCAdj);
1935 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1936 }
1937 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1938 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001939 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001940 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001941 if (RelocM == Reloc::Static)
1942 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001943 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001944 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001945}
1946
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001947// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001948SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001949ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001950 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001951 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001952 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001953 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001954 MachineFunction &MF = DAG.getMachineFunction();
1955 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001956 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001957 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001958 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001959 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001960 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001962 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001963 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001964 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001966
Evan Chenge7e0d622009-11-06 22:24:13 +00001967 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001968 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001969
1970 // call __tls_get_addr.
1971 ArgListTy Args;
1972 ArgListEntry Entry;
1973 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001974 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001975 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001976 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001977 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001978 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1979 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001981 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001982 return CallResult.first;
1983}
1984
1985// Lower ISD::GlobalTLSAddress using the "initial exec" or
1986// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001987SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001988ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001989 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001990 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001991 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue Offset;
1993 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001994 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001995 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001996 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001997
Chris Lattner4fb63d02009-07-15 04:12:33 +00001998 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001999 MachineFunction &MF = DAG.getMachineFunction();
2000 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002001 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002002 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002003 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2004 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002005 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002006 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002007 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002008 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002009 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002010 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002011 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002012 Chain = Offset.getValue(1);
2013
Evan Chenge7e0d622009-11-06 22:24:13 +00002014 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002015 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002016
Evan Cheng9eda6892009-10-31 03:39:36 +00002017 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002018 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002019 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002020 } else {
2021 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002022 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002023 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002024 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002025 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002026 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002027 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002028 }
2029
2030 // The address of the thread local variable is the add of the thread
2031 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002032 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002033}
2034
Dan Gohman475871a2008-07-27 21:46:04 +00002035SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002036ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002037 // TODO: implement the "local dynamic" model
2038 assert(Subtarget->isTargetELF() &&
2039 "TLS not implemented for non-ELF targets");
2040 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2041 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2042 // otherwise use the "Local Exec" TLS Model
2043 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2044 return LowerToTLSGeneralDynamicModel(GA, DAG);
2045 else
2046 return LowerToTLSExecModels(GA, DAG);
2047}
2048
Dan Gohman475871a2008-07-27 21:46:04 +00002049SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002050 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002051 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002052 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002053 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002054 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2055 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002056 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002057 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002058 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002059 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002060 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002061 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002062 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002063 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002064 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002065 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002066 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002067 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002068 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002069 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002070 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002071 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002072 }
2073
2074 // If we have T2 ops, we can materialize the address directly via movt/movw
2075 // pair. This is always cheaper.
2076 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002077 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002078 // FIXME: Once remat is capable of dealing with instructions with register
2079 // operands, expand this into two nodes.
2080 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2081 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002082 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002083 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2084 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2085 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2086 MachinePointerInfo::getConstantPool(),
2087 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002088 }
2089}
2090
Dan Gohman475871a2008-07-27 21:46:04 +00002091SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002092 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002093 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002094 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002095 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002096 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002097 MachineFunction &MF = DAG.getMachineFunction();
2098 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2099
Evan Cheng4abce0c2011-05-27 20:11:27 +00002100 // FIXME: Enable this for static codegen when tool issues are fixed.
2101 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002102 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002103 // FIXME: Once remat is capable of dealing with instructions with register
2104 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002105 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002106 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2107 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2108
Evan Cheng53519f02011-01-21 18:55:51 +00002109 unsigned Wrapper = (RelocM == Reloc::PIC_)
2110 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2111 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002112 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002113 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2114 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2115 MachinePointerInfo::getGOT(), false, false, 0);
2116 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002117 }
2118
2119 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002120 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002121 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002122 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002123 } else {
2124 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002125 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2126 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002127 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002128 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002129 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002130 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002131
Evan Cheng9eda6892009-10-31 03:39:36 +00002132 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002133 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002134 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002135 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002136
2137 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002138 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002139 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002140 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002141
Evan Cheng63476a82009-09-03 07:04:02 +00002142 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002143 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002144 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002145
2146 return Result;
2147}
2148
Dan Gohman475871a2008-07-27 21:46:04 +00002149SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002150 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002151 assert(Subtarget->isTargetELF() &&
2152 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002153 MachineFunction &MF = DAG.getMachineFunction();
2154 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002155 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002156 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002157 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002158 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002159 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2160 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002161 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002162 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002163 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002164 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002165 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002166 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002167 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002168 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002169}
2170
Jim Grosbach0e0da732009-05-12 23:59:14 +00002171SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002172ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2173 const {
2174 DebugLoc dl = Op.getDebugLoc();
2175 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002176 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002177}
2178
2179SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002180ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2181 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002182 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002183 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2184 Op.getOperand(1), Val);
2185}
2186
2187SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002188ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2189 DebugLoc dl = Op.getDebugLoc();
2190 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2191 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2192}
2193
2194SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002195ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002196 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002197 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002198 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002199 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002200 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002201 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002202 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002203 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2204 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002205 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002206 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002207 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002208 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002209 EVT PtrVT = getPointerTy();
2210 DebugLoc dl = Op.getDebugLoc();
2211 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2212 SDValue CPAddr;
2213 unsigned PCAdj = (RelocM != Reloc::PIC_)
2214 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002215 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002216 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2217 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002218 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002219 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002220 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002221 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002222 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002223 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002224
2225 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002226 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002227 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2228 }
2229 return Result;
2230 }
Evan Cheng92e39162011-03-29 23:06:19 +00002231 case Intrinsic::arm_neon_vmulls:
2232 case Intrinsic::arm_neon_vmullu: {
2233 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2234 ? ARMISD::VMULLs : ARMISD::VMULLu;
2235 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2236 Op.getOperand(1), Op.getOperand(2));
2237 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002238 }
2239}
2240
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002241static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002242 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002243 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002244 if (!Subtarget->hasDataBarrier()) {
2245 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2246 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2247 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002248 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002249 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002250 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002251 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002252 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002253
2254 SDValue Op5 = Op.getOperand(5);
2255 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2256 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2257 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2258 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2259
2260 ARM_MB::MemBOpt DMBOpt;
2261 if (isDeviceBarrier)
2262 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2263 else
2264 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2265 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2266 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002267}
2268
Evan Chengdfed19f2010-11-03 06:34:55 +00002269static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2270 const ARMSubtarget *Subtarget) {
2271 // ARM pre v5TE and Thumb1 does not have preload instructions.
2272 if (!(Subtarget->isThumb2() ||
2273 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2274 // Just preserve the chain.
2275 return Op.getOperand(0);
2276
2277 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002278 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2279 if (!isRead &&
2280 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2281 // ARMv7 with MP extension has PLDW.
2282 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002283
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002284 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2285 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002286 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002287 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002288 isData = ~isData & 1;
2289 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002290
2291 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002292 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2293 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002294}
2295
Dan Gohman1e93df62010-04-17 14:41:14 +00002296static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2297 MachineFunction &MF = DAG.getMachineFunction();
2298 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2299
Evan Chenga8e29892007-01-19 07:51:42 +00002300 // vastart just stores the address of the VarArgsFrameIndex slot into the
2301 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002302 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002303 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002304 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002305 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002306 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2307 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002308}
2309
Dan Gohman475871a2008-07-27 21:46:04 +00002310SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002311ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2312 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002313 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002314 MachineFunction &MF = DAG.getMachineFunction();
2315 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2316
2317 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002318 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002319 RC = ARM::tGPRRegisterClass;
2320 else
2321 RC = ARM::GPRRegisterClass;
2322
2323 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002324 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002325 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002326
2327 SDValue ArgValue2;
2328 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002329 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002330 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002331
2332 // Create load node to retrieve arguments from the stack.
2333 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002334 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002335 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002336 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002337 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002338 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002339 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002340 }
2341
Jim Grosbache5165492009-11-09 00:11:35 +00002342 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002343}
2344
Stuart Hastingsc7315872011-04-20 16:47:52 +00002345void
2346ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2347 unsigned &VARegSize, unsigned &VARegSaveSize)
2348 const {
2349 unsigned NumGPRs;
2350 if (CCInfo.isFirstByValRegValid())
2351 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2352 else {
2353 unsigned int firstUnalloced;
2354 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2355 sizeof(GPRArgRegs) /
2356 sizeof(GPRArgRegs[0]));
2357 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2358 }
2359
2360 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2361 VARegSize = NumGPRs * 4;
2362 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2363}
2364
2365// The remaining GPRs hold either the beginning of variable-argument
2366// data, or the beginning of an aggregate passed by value (usuall
2367// byval). Either way, we allocate stack slots adjacent to the data
2368// provided by our caller, and store the unallocated registers there.
2369// If this is a variadic function, the va_list pointer will begin with
2370// these values; otherwise, this reassembles a (byval) structure that
2371// was split between registers and memory.
2372void
2373ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2374 DebugLoc dl, SDValue &Chain,
2375 unsigned ArgOffset) const {
2376 MachineFunction &MF = DAG.getMachineFunction();
2377 MachineFrameInfo *MFI = MF.getFrameInfo();
2378 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2379 unsigned firstRegToSaveIndex;
2380 if (CCInfo.isFirstByValRegValid())
2381 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2382 else {
2383 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2384 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2385 }
2386
2387 unsigned VARegSize, VARegSaveSize;
2388 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2389 if (VARegSaveSize) {
2390 // If this function is vararg, store any remaining integer argument regs
2391 // to their spots on the stack so that they may be loaded by deferencing
2392 // the result of va_next.
2393 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002394 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2395 ArgOffset + VARegSaveSize
2396 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002397 false));
2398 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2399 getPointerTy());
2400
2401 SmallVector<SDValue, 4> MemOps;
2402 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2403 TargetRegisterClass *RC;
2404 if (AFI->isThumb1OnlyFunction())
2405 RC = ARM::tGPRRegisterClass;
2406 else
2407 RC = ARM::GPRRegisterClass;
2408
2409 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2410 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2411 SDValue Store =
2412 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002413 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002414 false, false, 0);
2415 MemOps.push_back(Store);
2416 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2417 DAG.getConstant(4, getPointerTy()));
2418 }
2419 if (!MemOps.empty())
2420 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2421 &MemOps[0], MemOps.size());
2422 } else
2423 // This will point to the next argument passed via stack.
2424 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2425}
2426
Bob Wilson5bafff32009-06-22 23:27:02 +00002427SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002428ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002429 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002430 const SmallVectorImpl<ISD::InputArg>
2431 &Ins,
2432 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002433 SmallVectorImpl<SDValue> &InVals)
2434 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002435 MachineFunction &MF = DAG.getMachineFunction();
2436 MachineFrameInfo *MFI = MF.getFrameInfo();
2437
Bob Wilson1f595bb2009-04-17 19:07:39 +00002438 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2439
2440 // Assign locations to all of the incoming arguments.
2441 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002442 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2443 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002444 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002445 CCAssignFnForNode(CallConv, /* Return*/ false,
2446 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002447
2448 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002449 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002450
Stuart Hastingsf222e592011-02-28 17:17:53 +00002451 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002452 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2453 CCValAssign &VA = ArgLocs[i];
2454
Bob Wilsondee46d72009-04-17 20:35:10 +00002455 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002456 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002457 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002458
Bob Wilson1f595bb2009-04-17 19:07:39 +00002459 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002460 // f64 and vector types are split up into multiple registers or
2461 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002462 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002463 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002464 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002465 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002466 SDValue ArgValue2;
2467 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002468 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002469 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2470 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002471 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002472 false, false, 0);
2473 } else {
2474 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2475 Chain, DAG, dl);
2476 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002477 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2478 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002479 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002480 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002481 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2482 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002483 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002484
Bob Wilson5bafff32009-06-22 23:27:02 +00002485 } else {
2486 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002487
Owen Anderson825b72b2009-08-11 20:47:22 +00002488 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002489 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002490 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002491 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002492 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002493 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002494 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002495 RC = (AFI->isThumb1OnlyFunction() ?
2496 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002497 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002498 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002499
2500 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002501 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002502 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002503 }
2504
2505 // If this is an 8 or 16-bit value, it is really passed promoted
2506 // to 32 bits. Insert an assert[sz]ext to capture this, then
2507 // truncate to the right size.
2508 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002509 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002510 case CCValAssign::Full: break;
2511 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002512 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002513 break;
2514 case CCValAssign::SExt:
2515 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2516 DAG.getValueType(VA.getValVT()));
2517 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2518 break;
2519 case CCValAssign::ZExt:
2520 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2521 DAG.getValueType(VA.getValVT()));
2522 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2523 break;
2524 }
2525
Dan Gohman98ca4f22009-08-05 01:29:28 +00002526 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002527
2528 } else { // VA.isRegLoc()
2529
2530 // sanity check
2531 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002532 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002533
Stuart Hastingsf222e592011-02-28 17:17:53 +00002534 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002535
Stuart Hastingsf222e592011-02-28 17:17:53 +00002536 // Some Ins[] entries become multiple ArgLoc[] entries.
2537 // Process them only once.
2538 if (index != lastInsIndex)
2539 {
2540 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002541 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002542 // This can be changed with more analysis.
2543 // In case of tail call optimization mark all arguments mutable.
2544 // Since they could be overwritten by lowering of arguments in case of
2545 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002546 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002547 unsigned VARegSize, VARegSaveSize;
2548 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2549 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2550 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002551 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002552 int FI = MFI->CreateFixedObject(Bytes,
2553 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002554 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2555 } else {
2556 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2557 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002558
Stuart Hastingsf222e592011-02-28 17:17:53 +00002559 // Create load nodes to retrieve arguments from the stack.
2560 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2561 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2562 MachinePointerInfo::getFixedStack(FI),
2563 false, false, 0));
2564 }
2565 lastInsIndex = index;
2566 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002567 }
2568 }
2569
2570 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002571 if (isVarArg)
2572 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002573
Dan Gohman98ca4f22009-08-05 01:29:28 +00002574 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002575}
2576
2577/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002578static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002579 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002580 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002581 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002582 // Maybe this has already been legalized into the constant pool?
2583 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002584 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002585 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002586 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002587 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002588 }
2589 }
2590 return false;
2591}
2592
Evan Chenga8e29892007-01-19 07:51:42 +00002593/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2594/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002595SDValue
2596ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002597 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002598 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002599 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002600 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002601 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002602 // Constant does not fit, try adjusting it by one?
2603 switch (CC) {
2604 default: break;
2605 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002606 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002607 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002608 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002609 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002610 }
2611 break;
2612 case ISD::SETULT:
2613 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002614 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002615 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002616 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002617 }
2618 break;
2619 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002620 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002621 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002622 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002623 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002624 }
2625 break;
2626 case ISD::SETULE:
2627 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002628 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002629 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002630 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002631 }
2632 break;
2633 }
2634 }
2635 }
2636
2637 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002638 ARMISD::NodeType CompareType;
2639 switch (CondCode) {
2640 default:
2641 CompareType = ARMISD::CMP;
2642 break;
2643 case ARMCC::EQ:
2644 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002645 // Uses only Z Flag
2646 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002647 break;
2648 }
Evan Cheng218977b2010-07-13 19:27:42 +00002649 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002650 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002651}
2652
2653/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002654SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002655ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002656 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002657 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002658 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002659 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002660 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002661 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2662 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002663}
2664
Bob Wilson79f56c92011-03-08 01:17:20 +00002665/// duplicateCmp - Glue values can have only one use, so this function
2666/// duplicates a comparison node.
2667SDValue
2668ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2669 unsigned Opc = Cmp.getOpcode();
2670 DebugLoc DL = Cmp.getDebugLoc();
2671 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2672 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2673
2674 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2675 Cmp = Cmp.getOperand(0);
2676 Opc = Cmp.getOpcode();
2677 if (Opc == ARMISD::CMPFP)
2678 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2679 else {
2680 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2681 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2682 }
2683 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2684}
2685
Bill Wendlingde2b1512010-08-11 08:43:16 +00002686SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2687 SDValue Cond = Op.getOperand(0);
2688 SDValue SelectTrue = Op.getOperand(1);
2689 SDValue SelectFalse = Op.getOperand(2);
2690 DebugLoc dl = Op.getDebugLoc();
2691
2692 // Convert:
2693 //
2694 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2695 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2696 //
2697 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2698 const ConstantSDNode *CMOVTrue =
2699 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2700 const ConstantSDNode *CMOVFalse =
2701 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2702
2703 if (CMOVTrue && CMOVFalse) {
2704 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2705 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2706
2707 SDValue True;
2708 SDValue False;
2709 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2710 True = SelectTrue;
2711 False = SelectFalse;
2712 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2713 True = SelectFalse;
2714 False = SelectTrue;
2715 }
2716
2717 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002718 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002719 SDValue ARMcc = Cond.getOperand(2);
2720 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002721 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002722 assert(True.getValueType() == VT);
2723 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002724 }
2725 }
2726 }
2727
2728 return DAG.getSelectCC(dl, Cond,
2729 DAG.getConstant(0, Cond.getValueType()),
2730 SelectTrue, SelectFalse, ISD::SETNE);
2731}
2732
Dan Gohmand858e902010-04-17 15:26:15 +00002733SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002734 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002735 SDValue LHS = Op.getOperand(0);
2736 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002737 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002738 SDValue TrueVal = Op.getOperand(2);
2739 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002740 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002741
Owen Anderson825b72b2009-08-11 20:47:22 +00002742 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002743 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002744 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002745 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2746 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002747 }
2748
2749 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002750 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002751
Evan Cheng218977b2010-07-13 19:27:42 +00002752 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2753 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002754 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002755 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002756 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002757 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002758 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002759 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002760 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002761 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002762 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002763 }
2764 return Result;
2765}
2766
Evan Cheng218977b2010-07-13 19:27:42 +00002767/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2768/// to morph to an integer compare sequence.
2769static bool canChangeToInt(SDValue Op, bool &SeenZero,
2770 const ARMSubtarget *Subtarget) {
2771 SDNode *N = Op.getNode();
2772 if (!N->hasOneUse())
2773 // Otherwise it requires moving the value from fp to integer registers.
2774 return false;
2775 if (!N->getNumValues())
2776 return false;
2777 EVT VT = Op.getValueType();
2778 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2779 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2780 // vmrs are very slow, e.g. cortex-a8.
2781 return false;
2782
2783 if (isFloatingPointZero(Op)) {
2784 SeenZero = true;
2785 return true;
2786 }
2787 return ISD::isNormalLoad(N);
2788}
2789
2790static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2791 if (isFloatingPointZero(Op))
2792 return DAG.getConstant(0, MVT::i32);
2793
2794 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2795 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002796 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002797 Ld->isVolatile(), Ld->isNonTemporal(),
2798 Ld->getAlignment());
2799
2800 llvm_unreachable("Unknown VFP cmp argument!");
2801}
2802
2803static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2804 SDValue &RetVal1, SDValue &RetVal2) {
2805 if (isFloatingPointZero(Op)) {
2806 RetVal1 = DAG.getConstant(0, MVT::i32);
2807 RetVal2 = DAG.getConstant(0, MVT::i32);
2808 return;
2809 }
2810
2811 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2812 SDValue Ptr = Ld->getBasePtr();
2813 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2814 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002815 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002816 Ld->isVolatile(), Ld->isNonTemporal(),
2817 Ld->getAlignment());
2818
2819 EVT PtrType = Ptr.getValueType();
2820 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2821 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2822 PtrType, Ptr, DAG.getConstant(4, PtrType));
2823 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2824 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002825 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002826 Ld->isVolatile(), Ld->isNonTemporal(),
2827 NewAlign);
2828 return;
2829 }
2830
2831 llvm_unreachable("Unknown VFP cmp argument!");
2832}
2833
2834/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2835/// f32 and even f64 comparisons to integer ones.
2836SDValue
2837ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2838 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002839 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002840 SDValue LHS = Op.getOperand(2);
2841 SDValue RHS = Op.getOperand(3);
2842 SDValue Dest = Op.getOperand(4);
2843 DebugLoc dl = Op.getDebugLoc();
2844
2845 bool SeenZero = false;
2846 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2847 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002848 // If one of the operand is zero, it's safe to ignore the NaN case since
2849 // we only care about equality comparisons.
2850 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002851 // If unsafe fp math optimization is enabled and there are no other uses of
2852 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002853 // to an integer comparison.
2854 if (CC == ISD::SETOEQ)
2855 CC = ISD::SETEQ;
2856 else if (CC == ISD::SETUNE)
2857 CC = ISD::SETNE;
2858
2859 SDValue ARMcc;
2860 if (LHS.getValueType() == MVT::f32) {
2861 LHS = bitcastf32Toi32(LHS, DAG);
2862 RHS = bitcastf32Toi32(RHS, DAG);
2863 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2864 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2865 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2866 Chain, Dest, ARMcc, CCR, Cmp);
2867 }
2868
2869 SDValue LHS1, LHS2;
2870 SDValue RHS1, RHS2;
2871 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2872 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2873 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2874 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002875 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002876 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2877 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2878 }
2879
2880 return SDValue();
2881}
2882
2883SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2884 SDValue Chain = Op.getOperand(0);
2885 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2886 SDValue LHS = Op.getOperand(2);
2887 SDValue RHS = Op.getOperand(3);
2888 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002889 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002890
Owen Anderson825b72b2009-08-11 20:47:22 +00002891 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002892 SDValue ARMcc;
2893 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002894 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002895 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002896 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002897 }
2898
Owen Anderson825b72b2009-08-11 20:47:22 +00002899 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002900
2901 if (UnsafeFPMath &&
2902 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2903 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2904 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2905 if (Result.getNode())
2906 return Result;
2907 }
2908
Evan Chenga8e29892007-01-19 07:51:42 +00002909 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002910 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002911
Evan Cheng218977b2010-07-13 19:27:42 +00002912 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2913 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002914 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002915 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002916 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002917 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002918 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002919 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2920 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002921 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002922 }
2923 return Res;
2924}
2925
Dan Gohmand858e902010-04-17 15:26:15 +00002926SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002927 SDValue Chain = Op.getOperand(0);
2928 SDValue Table = Op.getOperand(1);
2929 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002930 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002931
Owen Andersone50ed302009-08-10 22:56:29 +00002932 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002933 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2934 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002935 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002936 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002937 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002938 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2939 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002940 if (Subtarget->isThumb2()) {
2941 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2942 // which does another jump to the destination. This also makes it easier
2943 // to translate it to TBB / TBH later.
2944 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002945 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002946 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002947 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002948 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002949 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002950 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002951 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002952 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002953 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002954 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002955 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002956 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002957 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002958 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002959 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002960 }
Evan Chenga8e29892007-01-19 07:51:42 +00002961}
2962
Bob Wilson76a312b2010-03-19 22:51:32 +00002963static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2964 DebugLoc dl = Op.getDebugLoc();
2965 unsigned Opc;
2966
2967 switch (Op.getOpcode()) {
2968 default:
2969 assert(0 && "Invalid opcode!");
2970 case ISD::FP_TO_SINT:
2971 Opc = ARMISD::FTOSI;
2972 break;
2973 case ISD::FP_TO_UINT:
2974 Opc = ARMISD::FTOUI;
2975 break;
2976 }
2977 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002978 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002979}
2980
Cameron Zwarich3007d332011-03-29 21:41:55 +00002981static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2982 EVT VT = Op.getValueType();
2983 DebugLoc dl = Op.getDebugLoc();
2984
2985 EVT OperandVT = Op.getOperand(0).getValueType();
2986 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2987 if (VT != MVT::v4f32)
2988 return DAG.UnrollVectorOp(Op.getNode());
2989
2990 unsigned CastOpc;
2991 unsigned Opc;
2992 switch (Op.getOpcode()) {
2993 default:
2994 assert(0 && "Invalid opcode!");
2995 case ISD::SINT_TO_FP:
2996 CastOpc = ISD::SIGN_EXTEND;
2997 Opc = ISD::SINT_TO_FP;
2998 break;
2999 case ISD::UINT_TO_FP:
3000 CastOpc = ISD::ZERO_EXTEND;
3001 Opc = ISD::UINT_TO_FP;
3002 break;
3003 }
3004
3005 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3006 return DAG.getNode(Opc, dl, VT, Op);
3007}
3008
Bob Wilson76a312b2010-03-19 22:51:32 +00003009static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3010 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003011 if (VT.isVector())
3012 return LowerVectorINT_TO_FP(Op, DAG);
3013
Bob Wilson76a312b2010-03-19 22:51:32 +00003014 DebugLoc dl = Op.getDebugLoc();
3015 unsigned Opc;
3016
3017 switch (Op.getOpcode()) {
3018 default:
3019 assert(0 && "Invalid opcode!");
3020 case ISD::SINT_TO_FP:
3021 Opc = ARMISD::SITOF;
3022 break;
3023 case ISD::UINT_TO_FP:
3024 Opc = ARMISD::UITOF;
3025 break;
3026 }
3027
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003028 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003029 return DAG.getNode(Opc, dl, VT, Op);
3030}
3031
Evan Cheng515fe3a2010-07-08 02:08:50 +00003032SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003033 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003034 SDValue Tmp0 = Op.getOperand(0);
3035 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003036 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003037 EVT VT = Op.getValueType();
3038 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003039 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3040 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3041 bool UseNEON = !InGPR && Subtarget->hasNEON();
3042
3043 if (UseNEON) {
3044 // Use VBSL to copy the sign bit.
3045 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3046 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3047 DAG.getTargetConstant(EncodedVal, MVT::i32));
3048 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3049 if (VT == MVT::f64)
3050 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3051 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3052 DAG.getConstant(32, MVT::i32));
3053 else /*if (VT == MVT::f32)*/
3054 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3055 if (SrcVT == MVT::f32) {
3056 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3057 if (VT == MVT::f64)
3058 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3059 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3060 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003061 } else if (VT == MVT::f32)
3062 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3063 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3064 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003065 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3066 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3067
3068 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3069 MVT::i32);
3070 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3071 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3072 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003073
Evan Chenge573fb32011-02-23 02:24:55 +00003074 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3075 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3076 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003077 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003078 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3079 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3080 DAG.getConstant(0, MVT::i32));
3081 } else {
3082 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3083 }
3084
3085 return Res;
3086 }
Evan Chengc143dd42011-02-11 02:28:55 +00003087
3088 // Bitcast operand 1 to i32.
3089 if (SrcVT == MVT::f64)
3090 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3091 &Tmp1, 1).getValue(1);
3092 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3093
Evan Chenge573fb32011-02-23 02:24:55 +00003094 // Or in the signbit with integer operations.
3095 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3096 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3097 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3098 if (VT == MVT::f32) {
3099 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3100 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3101 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3102 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003103 }
3104
Evan Chenge573fb32011-02-23 02:24:55 +00003105 // f64: Or the high part with signbit and then combine two parts.
3106 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3107 &Tmp0, 1);
3108 SDValue Lo = Tmp0.getValue(0);
3109 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3110 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3111 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003112}
3113
Evan Cheng2457f2c2010-05-22 01:47:14 +00003114SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3115 MachineFunction &MF = DAG.getMachineFunction();
3116 MachineFrameInfo *MFI = MF.getFrameInfo();
3117 MFI->setReturnAddressIsTaken(true);
3118
3119 EVT VT = Op.getValueType();
3120 DebugLoc dl = Op.getDebugLoc();
3121 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3122 if (Depth) {
3123 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3124 SDValue Offset = DAG.getConstant(4, MVT::i32);
3125 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3126 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003127 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003128 }
3129
3130 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003131 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003132 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3133}
3134
Dan Gohmand858e902010-04-17 15:26:15 +00003135SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003136 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3137 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003138
Owen Andersone50ed302009-08-10 22:56:29 +00003139 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003140 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3141 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003142 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003143 ? ARM::R7 : ARM::R11;
3144 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3145 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003146 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3147 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003148 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003149 return FrameAddr;
3150}
3151
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003152/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003153/// expand a bit convert where either the source or destination type is i64 to
3154/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3155/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3156/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003157static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003158 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3159 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003160 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003161
Bob Wilson9f3f0612010-04-17 05:30:19 +00003162 // This function is only supposed to be called for i64 types, either as the
3163 // source or destination of the bit convert.
3164 EVT SrcVT = Op.getValueType();
3165 EVT DstVT = N->getValueType(0);
3166 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003167 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003168
Bob Wilson9f3f0612010-04-17 05:30:19 +00003169 // Turn i64->f64 into VMOVDRR.
3170 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003171 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3172 DAG.getConstant(0, MVT::i32));
3173 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3174 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003175 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003176 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003177 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003178
Jim Grosbache5165492009-11-09 00:11:35 +00003179 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003180 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3181 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3182 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3183 // Merge the pieces into a single i64 value.
3184 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3185 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003186
Bob Wilson9f3f0612010-04-17 05:30:19 +00003187 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003188}
3189
Bob Wilson5bafff32009-06-22 23:27:02 +00003190/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003191/// Zero vectors are used to represent vector negation and in those cases
3192/// will be implemented with the NEON VNEG instruction. However, VNEG does
3193/// not support i64 elements, so sometimes the zero vectors will need to be
3194/// explicitly constructed. Regardless, use a canonical VMOV to create the
3195/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003196static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003197 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003198 // The canonical modified immediate encoding of a zero vector is....0!
3199 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3200 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3201 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003202 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003203}
3204
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003205/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3206/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003207SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3208 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003209 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3210 EVT VT = Op.getValueType();
3211 unsigned VTBits = VT.getSizeInBits();
3212 DebugLoc dl = Op.getDebugLoc();
3213 SDValue ShOpLo = Op.getOperand(0);
3214 SDValue ShOpHi = Op.getOperand(1);
3215 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003216 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003217 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003218
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003219 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3220
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003221 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3222 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3223 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3224 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3225 DAG.getConstant(VTBits, MVT::i32));
3226 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3227 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003228 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003229
3230 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3231 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003232 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003233 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003234 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003235 CCR, Cmp);
3236
3237 SDValue Ops[2] = { Lo, Hi };
3238 return DAG.getMergeValues(Ops, 2, dl);
3239}
3240
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003241/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3242/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003243SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3244 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003245 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3246 EVT VT = Op.getValueType();
3247 unsigned VTBits = VT.getSizeInBits();
3248 DebugLoc dl = Op.getDebugLoc();
3249 SDValue ShOpLo = Op.getOperand(0);
3250 SDValue ShOpHi = Op.getOperand(1);
3251 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003252 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003253
3254 assert(Op.getOpcode() == ISD::SHL_PARTS);
3255 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3256 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3257 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3258 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3259 DAG.getConstant(VTBits, MVT::i32));
3260 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3261 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3262
3263 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3264 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3265 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003266 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003267 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003268 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003269 CCR, Cmp);
3270
3271 SDValue Ops[2] = { Lo, Hi };
3272 return DAG.getMergeValues(Ops, 2, dl);
3273}
3274
Jim Grosbach4725ca72010-09-08 03:54:02 +00003275SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003276 SelectionDAG &DAG) const {
3277 // The rounding mode is in bits 23:22 of the FPSCR.
3278 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3279 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3280 // so that the shift + and get folded into a bitfield extract.
3281 DebugLoc dl = Op.getDebugLoc();
3282 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3283 DAG.getConstant(Intrinsic::arm_get_fpscr,
3284 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003285 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003286 DAG.getConstant(1U << 22, MVT::i32));
3287 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3288 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003289 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003290 DAG.getConstant(3, MVT::i32));
3291}
3292
Jim Grosbach3482c802010-01-18 19:58:49 +00003293static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3294 const ARMSubtarget *ST) {
3295 EVT VT = N->getValueType(0);
3296 DebugLoc dl = N->getDebugLoc();
3297
3298 if (!ST->hasV6T2Ops())
3299 return SDValue();
3300
3301 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3302 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3303}
3304
Bob Wilson5bafff32009-06-22 23:27:02 +00003305static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3306 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003307 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003308 DebugLoc dl = N->getDebugLoc();
3309
Bob Wilsond5448bb2010-11-18 21:16:28 +00003310 if (!VT.isVector())
3311 return SDValue();
3312
Bob Wilson5bafff32009-06-22 23:27:02 +00003313 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003314 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003315
Bob Wilsond5448bb2010-11-18 21:16:28 +00003316 // Left shifts translate directly to the vshiftu intrinsic.
3317 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003318 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003319 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3320 N->getOperand(0), N->getOperand(1));
3321
3322 assert((N->getOpcode() == ISD::SRA ||
3323 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3324
3325 // NEON uses the same intrinsics for both left and right shifts. For
3326 // right shifts, the shift amounts are negative, so negate the vector of
3327 // shift amounts.
3328 EVT ShiftVT = N->getOperand(1).getValueType();
3329 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3330 getZeroVector(ShiftVT, DAG, dl),
3331 N->getOperand(1));
3332 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3333 Intrinsic::arm_neon_vshifts :
3334 Intrinsic::arm_neon_vshiftu);
3335 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3336 DAG.getConstant(vshiftInt, MVT::i32),
3337 N->getOperand(0), NegatedCount);
3338}
3339
3340static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3341 const ARMSubtarget *ST) {
3342 EVT VT = N->getValueType(0);
3343 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003344
Eli Friedmance392eb2009-08-22 03:13:10 +00003345 // We can get here for a node like i32 = ISD::SHL i32, i64
3346 if (VT != MVT::i64)
3347 return SDValue();
3348
3349 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003350 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003351
Chris Lattner27a6c732007-11-24 07:07:01 +00003352 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3353 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003354 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003355 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003356
Chris Lattner27a6c732007-11-24 07:07:01 +00003357 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003358 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003359
Chris Lattner27a6c732007-11-24 07:07:01 +00003360 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003361 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003362 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003363 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003364 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003365
Chris Lattner27a6c732007-11-24 07:07:01 +00003366 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3367 // captures the result into a carry flag.
3368 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003369 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003370
Chris Lattner27a6c732007-11-24 07:07:01 +00003371 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003373
Chris Lattner27a6c732007-11-24 07:07:01 +00003374 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003375 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003376}
3377
Bob Wilson5bafff32009-06-22 23:27:02 +00003378static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3379 SDValue TmpOp0, TmpOp1;
3380 bool Invert = false;
3381 bool Swap = false;
3382 unsigned Opc = 0;
3383
3384 SDValue Op0 = Op.getOperand(0);
3385 SDValue Op1 = Op.getOperand(1);
3386 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003387 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003388 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3389 DebugLoc dl = Op.getDebugLoc();
3390
3391 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3392 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003393 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003394 case ISD::SETUNE:
3395 case ISD::SETNE: Invert = true; // Fallthrough
3396 case ISD::SETOEQ:
3397 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3398 case ISD::SETOLT:
3399 case ISD::SETLT: Swap = true; // Fallthrough
3400 case ISD::SETOGT:
3401 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3402 case ISD::SETOLE:
3403 case ISD::SETLE: Swap = true; // Fallthrough
3404 case ISD::SETOGE:
3405 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3406 case ISD::SETUGE: Swap = true; // Fallthrough
3407 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3408 case ISD::SETUGT: Swap = true; // Fallthrough
3409 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3410 case ISD::SETUEQ: Invert = true; // Fallthrough
3411 case ISD::SETONE:
3412 // Expand this to (OLT | OGT).
3413 TmpOp0 = Op0;
3414 TmpOp1 = Op1;
3415 Opc = ISD::OR;
3416 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3417 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3418 break;
3419 case ISD::SETUO: Invert = true; // Fallthrough
3420 case ISD::SETO:
3421 // Expand this to (OLT | OGE).
3422 TmpOp0 = Op0;
3423 TmpOp1 = Op1;
3424 Opc = ISD::OR;
3425 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3426 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3427 break;
3428 }
3429 } else {
3430 // Integer comparisons.
3431 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003432 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003433 case ISD::SETNE: Invert = true;
3434 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3435 case ISD::SETLT: Swap = true;
3436 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3437 case ISD::SETLE: Swap = true;
3438 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3439 case ISD::SETULT: Swap = true;
3440 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3441 case ISD::SETULE: Swap = true;
3442 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3443 }
3444
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003445 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003446 if (Opc == ARMISD::VCEQ) {
3447
3448 SDValue AndOp;
3449 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3450 AndOp = Op0;
3451 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3452 AndOp = Op1;
3453
3454 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003455 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003456 AndOp = AndOp.getOperand(0);
3457
3458 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3459 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003460 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3461 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003462 Invert = !Invert;
3463 }
3464 }
3465 }
3466
3467 if (Swap)
3468 std::swap(Op0, Op1);
3469
Owen Andersonc24cb352010-11-08 23:21:22 +00003470 // If one of the operands is a constant vector zero, attempt to fold the
3471 // comparison to a specialized compare-against-zero form.
3472 SDValue SingleOp;
3473 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3474 SingleOp = Op0;
3475 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3476 if (Opc == ARMISD::VCGE)
3477 Opc = ARMISD::VCLEZ;
3478 else if (Opc == ARMISD::VCGT)
3479 Opc = ARMISD::VCLTZ;
3480 SingleOp = Op1;
3481 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003482
Owen Andersonc24cb352010-11-08 23:21:22 +00003483 SDValue Result;
3484 if (SingleOp.getNode()) {
3485 switch (Opc) {
3486 case ARMISD::VCEQ:
3487 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3488 case ARMISD::VCGE:
3489 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3490 case ARMISD::VCLEZ:
3491 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3492 case ARMISD::VCGT:
3493 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3494 case ARMISD::VCLTZ:
3495 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3496 default:
3497 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3498 }
3499 } else {
3500 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3501 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003502
3503 if (Invert)
3504 Result = DAG.getNOT(dl, Result, VT);
3505
3506 return Result;
3507}
3508
Bob Wilsond3c42842010-06-14 22:19:57 +00003509/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3510/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003511/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003512static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3513 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003514 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003515 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003516
Bob Wilson827b2102010-06-15 19:05:35 +00003517 // SplatBitSize is set to the smallest size that splats the vector, so a
3518 // zero vector will always have SplatBitSize == 8. However, NEON modified
3519 // immediate instructions others than VMOV do not support the 8-bit encoding
3520 // of a zero vector, and the default encoding of zero is supposed to be the
3521 // 32-bit version.
3522 if (SplatBits == 0)
3523 SplatBitSize = 32;
3524
Bob Wilson5bafff32009-06-22 23:27:02 +00003525 switch (SplatBitSize) {
3526 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003527 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003528 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003529 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003530 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003531 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003532 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003533 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003534 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003535
3536 case 16:
3537 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003538 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003539 if ((SplatBits & ~0xff) == 0) {
3540 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003541 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003542 Imm = SplatBits;
3543 break;
3544 }
3545 if ((SplatBits & ~0xff00) == 0) {
3546 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003547 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003548 Imm = SplatBits >> 8;
3549 break;
3550 }
3551 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003552
3553 case 32:
3554 // NEON's 32-bit VMOV supports splat values where:
3555 // * only one byte is nonzero, or
3556 // * the least significant byte is 0xff and the second byte is nonzero, or
3557 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003558 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003559 if ((SplatBits & ~0xff) == 0) {
3560 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003561 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003562 Imm = SplatBits;
3563 break;
3564 }
3565 if ((SplatBits & ~0xff00) == 0) {
3566 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003567 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003568 Imm = SplatBits >> 8;
3569 break;
3570 }
3571 if ((SplatBits & ~0xff0000) == 0) {
3572 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003573 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003574 Imm = SplatBits >> 16;
3575 break;
3576 }
3577 if ((SplatBits & ~0xff000000) == 0) {
3578 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003579 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003580 Imm = SplatBits >> 24;
3581 break;
3582 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003583
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003584 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3585 if (type == OtherModImm) return SDValue();
3586
Bob Wilson5bafff32009-06-22 23:27:02 +00003587 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003588 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3589 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003590 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003591 Imm = SplatBits >> 8;
3592 SplatBits |= 0xff;
3593 break;
3594 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003595
3596 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003597 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3598 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003599 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003600 Imm = SplatBits >> 16;
3601 SplatBits |= 0xffff;
3602 break;
3603 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003604
3605 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3606 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3607 // VMOV.I32. A (very) minor optimization would be to replicate the value
3608 // and fall through here to test for a valid 64-bit splat. But, then the
3609 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003610 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003611
3612 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003613 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003614 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003615 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003616 uint64_t BitMask = 0xff;
3617 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003618 unsigned ImmMask = 1;
3619 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003620 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003621 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003622 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003623 Imm |= ImmMask;
3624 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003625 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003626 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003627 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003628 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003629 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003630 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003631 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003632 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003633 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003634 break;
3635 }
3636
Bob Wilson1a913ed2010-06-11 21:34:50 +00003637 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003638 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003639 return SDValue();
3640 }
3641
Bob Wilsoncba270d2010-07-13 21:16:48 +00003642 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3643 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003644}
3645
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003646static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3647 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003648 unsigned NumElts = VT.getVectorNumElements();
3649 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003650
3651 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3652 if (M[0] < 0)
3653 return false;
3654
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003655 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003656
3657 // If this is a VEXT shuffle, the immediate value is the index of the first
3658 // element. The other shuffle indices must be the successive elements after
3659 // the first one.
3660 unsigned ExpectedElt = Imm;
3661 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003662 // Increment the expected index. If it wraps around, it may still be
3663 // a VEXT but the source vectors must be swapped.
3664 ExpectedElt += 1;
3665 if (ExpectedElt == NumElts * 2) {
3666 ExpectedElt = 0;
3667 ReverseVEXT = true;
3668 }
3669
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003670 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003671 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003672 return false;
3673 }
3674
3675 // Adjust the index value if the source operands will be swapped.
3676 if (ReverseVEXT)
3677 Imm -= NumElts;
3678
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003679 return true;
3680}
3681
Bob Wilson8bb9e482009-07-26 00:39:34 +00003682/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3683/// instruction with the specified blocksize. (The order of the elements
3684/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003685static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3686 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003687 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3688 "Only possible block sizes for VREV are: 16, 32, 64");
3689
Bob Wilson8bb9e482009-07-26 00:39:34 +00003690 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003691 if (EltSz == 64)
3692 return false;
3693
3694 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003695 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003696 // If the first shuffle index is UNDEF, be optimistic.
3697 if (M[0] < 0)
3698 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003699
3700 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3701 return false;
3702
3703 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003704 if (M[i] < 0) continue; // ignore UNDEF indices
3705 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003706 return false;
3707 }
3708
3709 return true;
3710}
3711
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003712static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3713 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3714 // range, then 0 is placed into the resulting vector. So pretty much any mask
3715 // of 8 elements can work here.
3716 return VT == MVT::v8i8 && M.size() == 8;
3717}
3718
Bob Wilsonc692cb72009-08-21 20:54:19 +00003719static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3720 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003721 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3722 if (EltSz == 64)
3723 return false;
3724
Bob Wilsonc692cb72009-08-21 20:54:19 +00003725 unsigned NumElts = VT.getVectorNumElements();
3726 WhichResult = (M[0] == 0 ? 0 : 1);
3727 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003728 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3729 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003730 return false;
3731 }
3732 return true;
3733}
3734
Bob Wilson324f4f12009-12-03 06:40:55 +00003735/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3736/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3737/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3738static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3739 unsigned &WhichResult) {
3740 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3741 if (EltSz == 64)
3742 return false;
3743
3744 unsigned NumElts = VT.getVectorNumElements();
3745 WhichResult = (M[0] == 0 ? 0 : 1);
3746 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003747 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3748 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003749 return false;
3750 }
3751 return true;
3752}
3753
Bob Wilsonc692cb72009-08-21 20:54:19 +00003754static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3755 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003756 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3757 if (EltSz == 64)
3758 return false;
3759
Bob Wilsonc692cb72009-08-21 20:54:19 +00003760 unsigned NumElts = VT.getVectorNumElements();
3761 WhichResult = (M[0] == 0 ? 0 : 1);
3762 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003763 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003764 if ((unsigned) M[i] != 2 * i + WhichResult)
3765 return false;
3766 }
3767
3768 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003769 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003770 return false;
3771
3772 return true;
3773}
3774
Bob Wilson324f4f12009-12-03 06:40:55 +00003775/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3776/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3777/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3778static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3779 unsigned &WhichResult) {
3780 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3781 if (EltSz == 64)
3782 return false;
3783
3784 unsigned Half = VT.getVectorNumElements() / 2;
3785 WhichResult = (M[0] == 0 ? 0 : 1);
3786 for (unsigned j = 0; j != 2; ++j) {
3787 unsigned Idx = WhichResult;
3788 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003789 int MIdx = M[i + j * Half];
3790 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003791 return false;
3792 Idx += 2;
3793 }
3794 }
3795
3796 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3797 if (VT.is64BitVector() && EltSz == 32)
3798 return false;
3799
3800 return true;
3801}
3802
Bob Wilsonc692cb72009-08-21 20:54:19 +00003803static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3804 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003805 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3806 if (EltSz == 64)
3807 return false;
3808
Bob Wilsonc692cb72009-08-21 20:54:19 +00003809 unsigned NumElts = VT.getVectorNumElements();
3810 WhichResult = (M[0] == 0 ? 0 : 1);
3811 unsigned Idx = WhichResult * NumElts / 2;
3812 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003813 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3814 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003815 return false;
3816 Idx += 1;
3817 }
3818
3819 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003820 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003821 return false;
3822
3823 return true;
3824}
3825
Bob Wilson324f4f12009-12-03 06:40:55 +00003826/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3827/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3828/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3829static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3830 unsigned &WhichResult) {
3831 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3832 if (EltSz == 64)
3833 return false;
3834
3835 unsigned NumElts = VT.getVectorNumElements();
3836 WhichResult = (M[0] == 0 ? 0 : 1);
3837 unsigned Idx = WhichResult * NumElts / 2;
3838 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003839 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3840 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003841 return false;
3842 Idx += 1;
3843 }
3844
3845 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3846 if (VT.is64BitVector() && EltSz == 32)
3847 return false;
3848
3849 return true;
3850}
3851
Dale Johannesenf630c712010-07-29 20:10:08 +00003852// If N is an integer constant that can be moved into a register in one
3853// instruction, return an SDValue of such a constant (will become a MOV
3854// instruction). Otherwise return null.
3855static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3856 const ARMSubtarget *ST, DebugLoc dl) {
3857 uint64_t Val;
3858 if (!isa<ConstantSDNode>(N))
3859 return SDValue();
3860 Val = cast<ConstantSDNode>(N)->getZExtValue();
3861
3862 if (ST->isThumb1Only()) {
3863 if (Val <= 255 || ~Val <= 255)
3864 return DAG.getConstant(Val, MVT::i32);
3865 } else {
3866 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3867 return DAG.getConstant(Val, MVT::i32);
3868 }
3869 return SDValue();
3870}
3871
Bob Wilson5bafff32009-06-22 23:27:02 +00003872// If this is a case we can't handle, return null and let the default
3873// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003874SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3875 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003876 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003877 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003878 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003879
3880 APInt SplatBits, SplatUndef;
3881 unsigned SplatBitSize;
3882 bool HasAnyUndefs;
3883 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003884 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003885 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003886 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003887 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003888 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003889 DAG, VmovVT, VT.is128BitVector(),
3890 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003891 if (Val.getNode()) {
3892 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003893 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003894 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003895
3896 // Try an immediate VMVN.
3897 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3898 ((1LL << SplatBitSize) - 1));
3899 Val = isNEONModifiedImm(NegatedImm,
3900 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003901 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003902 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003903 if (Val.getNode()) {
3904 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003905 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003906 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003907 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003908 }
3909
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003910 // Scan through the operands to see if only one value is used.
3911 unsigned NumElts = VT.getVectorNumElements();
3912 bool isOnlyLowElement = true;
3913 bool usesOnlyOneValue = true;
3914 bool isConstant = true;
3915 SDValue Value;
3916 for (unsigned i = 0; i < NumElts; ++i) {
3917 SDValue V = Op.getOperand(i);
3918 if (V.getOpcode() == ISD::UNDEF)
3919 continue;
3920 if (i > 0)
3921 isOnlyLowElement = false;
3922 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3923 isConstant = false;
3924
3925 if (!Value.getNode())
3926 Value = V;
3927 else if (V != Value)
3928 usesOnlyOneValue = false;
3929 }
3930
3931 if (!Value.getNode())
3932 return DAG.getUNDEF(VT);
3933
3934 if (isOnlyLowElement)
3935 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3936
Dale Johannesenf630c712010-07-29 20:10:08 +00003937 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3938
Dale Johannesen575cd142010-10-19 20:00:17 +00003939 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3940 // i32 and try again.
3941 if (usesOnlyOneValue && EltSize <= 32) {
3942 if (!isConstant)
3943 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3944 if (VT.getVectorElementType().isFloatingPoint()) {
3945 SmallVector<SDValue, 8> Ops;
3946 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003947 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003948 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003949 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3950 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003951 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3952 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003953 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003954 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003955 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3956 if (Val.getNode())
3957 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003958 }
3959
3960 // If all elements are constants and the case above didn't get hit, fall back
3961 // to the default expansion, which will generate a load from the constant
3962 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003963 if (isConstant)
3964 return SDValue();
3965
Bob Wilson11a1dff2011-01-07 21:37:30 +00003966 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3967 if (NumElts >= 4) {
3968 SDValue shuffle = ReconstructShuffle(Op, DAG);
3969 if (shuffle != SDValue())
3970 return shuffle;
3971 }
3972
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003973 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003974 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3975 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003976 if (EltSize >= 32) {
3977 // Do the expansion with floating-point types, since that is what the VFP
3978 // registers are defined to use, and since i64 is not legal.
3979 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3980 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003981 SmallVector<SDValue, 8> Ops;
3982 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003983 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003984 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003985 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003986 }
3987
3988 return SDValue();
3989}
3990
Bob Wilson11a1dff2011-01-07 21:37:30 +00003991// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003992// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003993SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3994 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003995 DebugLoc dl = Op.getDebugLoc();
3996 EVT VT = Op.getValueType();
3997 unsigned NumElts = VT.getVectorNumElements();
3998
3999 SmallVector<SDValue, 2> SourceVecs;
4000 SmallVector<unsigned, 2> MinElts;
4001 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004002
Bob Wilson11a1dff2011-01-07 21:37:30 +00004003 for (unsigned i = 0; i < NumElts; ++i) {
4004 SDValue V = Op.getOperand(i);
4005 if (V.getOpcode() == ISD::UNDEF)
4006 continue;
4007 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4008 // A shuffle can only come from building a vector from various
4009 // elements of other vectors.
4010 return SDValue();
4011 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004012
Bob Wilson11a1dff2011-01-07 21:37:30 +00004013 // Record this extraction against the appropriate vector if possible...
4014 SDValue SourceVec = V.getOperand(0);
4015 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4016 bool FoundSource = false;
4017 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4018 if (SourceVecs[j] == SourceVec) {
4019 if (MinElts[j] > EltNo)
4020 MinElts[j] = EltNo;
4021 if (MaxElts[j] < EltNo)
4022 MaxElts[j] = EltNo;
4023 FoundSource = true;
4024 break;
4025 }
4026 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004027
Bob Wilson11a1dff2011-01-07 21:37:30 +00004028 // Or record a new source if not...
4029 if (!FoundSource) {
4030 SourceVecs.push_back(SourceVec);
4031 MinElts.push_back(EltNo);
4032 MaxElts.push_back(EltNo);
4033 }
4034 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004035
Bob Wilson11a1dff2011-01-07 21:37:30 +00004036 // Currently only do something sane when at most two source vectors
4037 // involved.
4038 if (SourceVecs.size() > 2)
4039 return SDValue();
4040
4041 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4042 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004043
Bob Wilson11a1dff2011-01-07 21:37:30 +00004044 // This loop extracts the usage patterns of the source vectors
4045 // and prepares appropriate SDValues for a shuffle if possible.
4046 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4047 if (SourceVecs[i].getValueType() == VT) {
4048 // No VEXT necessary
4049 ShuffleSrcs[i] = SourceVecs[i];
4050 VEXTOffsets[i] = 0;
4051 continue;
4052 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4053 // It probably isn't worth padding out a smaller vector just to
4054 // break it down again in a shuffle.
4055 return SDValue();
4056 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004057
Bob Wilson11a1dff2011-01-07 21:37:30 +00004058 // Since only 64-bit and 128-bit vectors are legal on ARM and
4059 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004060 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4061 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004062
Bob Wilson11a1dff2011-01-07 21:37:30 +00004063 if (MaxElts[i] - MinElts[i] >= NumElts) {
4064 // Span too large for a VEXT to cope
4065 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004066 }
4067
Bob Wilson11a1dff2011-01-07 21:37:30 +00004068 if (MinElts[i] >= NumElts) {
4069 // The extraction can just take the second half
4070 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004071 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4072 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004073 DAG.getIntPtrConstant(NumElts));
4074 } else if (MaxElts[i] < NumElts) {
4075 // The extraction can just take the first half
4076 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004077 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4078 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004079 DAG.getIntPtrConstant(0));
4080 } else {
4081 // An actual VEXT is needed
4082 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004083 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4084 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004085 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004086 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4087 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004088 DAG.getIntPtrConstant(NumElts));
4089 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4090 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4091 }
4092 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004093
Bob Wilson11a1dff2011-01-07 21:37:30 +00004094 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004095
Bob Wilson11a1dff2011-01-07 21:37:30 +00004096 for (unsigned i = 0; i < NumElts; ++i) {
4097 SDValue Entry = Op.getOperand(i);
4098 if (Entry.getOpcode() == ISD::UNDEF) {
4099 Mask.push_back(-1);
4100 continue;
4101 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004102
Bob Wilson11a1dff2011-01-07 21:37:30 +00004103 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004104 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4105 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004106 if (ExtractVec == SourceVecs[0]) {
4107 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4108 } else {
4109 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4110 }
4111 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004112
Bob Wilson11a1dff2011-01-07 21:37:30 +00004113 // Final check before we try to produce nonsense...
4114 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004115 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4116 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004117
Bob Wilson11a1dff2011-01-07 21:37:30 +00004118 return SDValue();
4119}
4120
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004121/// isShuffleMaskLegal - Targets can use this to indicate that they only
4122/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4123/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4124/// are assumed to be legal.
4125bool
4126ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4127 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004128 if (VT.getVectorNumElements() == 4 &&
4129 (VT.is128BitVector() || VT.is64BitVector())) {
4130 unsigned PFIndexes[4];
4131 for (unsigned i = 0; i != 4; ++i) {
4132 if (M[i] < 0)
4133 PFIndexes[i] = 8;
4134 else
4135 PFIndexes[i] = M[i];
4136 }
4137
4138 // Compute the index in the perfect shuffle table.
4139 unsigned PFTableIndex =
4140 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4141 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4142 unsigned Cost = (PFEntry >> 30);
4143
4144 if (Cost <= 4)
4145 return true;
4146 }
4147
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004148 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004149 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004150
Bob Wilson53dd2452010-06-07 23:53:38 +00004151 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4152 return (EltSize >= 32 ||
4153 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004154 isVREVMask(M, VT, 64) ||
4155 isVREVMask(M, VT, 32) ||
4156 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004157 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004158 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004159 isVTRNMask(M, VT, WhichResult) ||
4160 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004161 isVZIPMask(M, VT, WhichResult) ||
4162 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4163 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4164 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004165}
4166
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004167/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4168/// the specified operations to build the shuffle.
4169static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4170 SDValue RHS, SelectionDAG &DAG,
4171 DebugLoc dl) {
4172 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4173 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4174 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4175
4176 enum {
4177 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4178 OP_VREV,
4179 OP_VDUP0,
4180 OP_VDUP1,
4181 OP_VDUP2,
4182 OP_VDUP3,
4183 OP_VEXT1,
4184 OP_VEXT2,
4185 OP_VEXT3,
4186 OP_VUZPL, // VUZP, left result
4187 OP_VUZPR, // VUZP, right result
4188 OP_VZIPL, // VZIP, left result
4189 OP_VZIPR, // VZIP, right result
4190 OP_VTRNL, // VTRN, left result
4191 OP_VTRNR // VTRN, right result
4192 };
4193
4194 if (OpNum == OP_COPY) {
4195 if (LHSID == (1*9+2)*9+3) return LHS;
4196 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4197 return RHS;
4198 }
4199
4200 SDValue OpLHS, OpRHS;
4201 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4202 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4203 EVT VT = OpLHS.getValueType();
4204
4205 switch (OpNum) {
4206 default: llvm_unreachable("Unknown shuffle opcode!");
4207 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004208 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004209 if (VT.getVectorElementType() == MVT::i32 ||
4210 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004211 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4212 // vrev <4 x i16> -> VREV32
4213 if (VT.getVectorElementType() == MVT::i16)
4214 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4215 // vrev <4 x i8> -> VREV16
4216 assert(VT.getVectorElementType() == MVT::i8);
4217 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004218 case OP_VDUP0:
4219 case OP_VDUP1:
4220 case OP_VDUP2:
4221 case OP_VDUP3:
4222 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004223 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004224 case OP_VEXT1:
4225 case OP_VEXT2:
4226 case OP_VEXT3:
4227 return DAG.getNode(ARMISD::VEXT, dl, VT,
4228 OpLHS, OpRHS,
4229 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4230 case OP_VUZPL:
4231 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004232 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004233 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4234 case OP_VZIPL:
4235 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004236 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004237 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4238 case OP_VTRNL:
4239 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004240 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4241 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004242 }
4243}
4244
Bill Wendling69a05a72011-03-14 23:02:38 +00004245static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4246 SmallVectorImpl<int> &ShuffleMask,
4247 SelectionDAG &DAG) {
4248 // Check to see if we can use the VTBL instruction.
4249 SDValue V1 = Op.getOperand(0);
4250 SDValue V2 = Op.getOperand(1);
4251 DebugLoc DL = Op.getDebugLoc();
4252
4253 SmallVector<SDValue, 8> VTBLMask;
4254 for (SmallVectorImpl<int>::iterator
4255 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4256 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4257
4258 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4259 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4260 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4261 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004262
Owen Anderson76706012011-04-05 21:48:57 +00004263 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004264 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4265 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004266}
4267
Bob Wilson5bafff32009-06-22 23:27:02 +00004268static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004269 SDValue V1 = Op.getOperand(0);
4270 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004271 DebugLoc dl = Op.getDebugLoc();
4272 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004273 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004274 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004275
Bob Wilson28865062009-08-13 02:13:04 +00004276 // Convert shuffles that are directly supported on NEON to target-specific
4277 // DAG nodes, instead of keeping them as shuffles and matching them again
4278 // during code selection. This is more efficient and avoids the possibility
4279 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004280 // FIXME: floating-point vectors should be canonicalized to integer vectors
4281 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004282 SVN->getMask(ShuffleMask);
4283
Bob Wilson53dd2452010-06-07 23:53:38 +00004284 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4285 if (EltSize <= 32) {
4286 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4287 int Lane = SVN->getSplatIndex();
4288 // If this is undef splat, generate it via "just" vdup, if possible.
4289 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004290
Bob Wilson53dd2452010-06-07 23:53:38 +00004291 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4292 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4293 }
4294 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4295 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004296 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004297
4298 bool ReverseVEXT;
4299 unsigned Imm;
4300 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4301 if (ReverseVEXT)
4302 std::swap(V1, V2);
4303 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4304 DAG.getConstant(Imm, MVT::i32));
4305 }
4306
4307 if (isVREVMask(ShuffleMask, VT, 64))
4308 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4309 if (isVREVMask(ShuffleMask, VT, 32))
4310 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4311 if (isVREVMask(ShuffleMask, VT, 16))
4312 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4313
4314 // Check for Neon shuffles that modify both input vectors in place.
4315 // If both results are used, i.e., if there are two shuffles with the same
4316 // source operands and with masks corresponding to both results of one of
4317 // these operations, DAG memoization will ensure that a single node is
4318 // used for both shuffles.
4319 unsigned WhichResult;
4320 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4321 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4322 V1, V2).getValue(WhichResult);
4323 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4324 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4325 V1, V2).getValue(WhichResult);
4326 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4327 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4328 V1, V2).getValue(WhichResult);
4329
4330 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4331 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4332 V1, V1).getValue(WhichResult);
4333 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4334 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4335 V1, V1).getValue(WhichResult);
4336 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4337 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4338 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004339 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004340
Bob Wilsonc692cb72009-08-21 20:54:19 +00004341 // If the shuffle is not directly supported and it has 4 elements, use
4342 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004343 unsigned NumElts = VT.getVectorNumElements();
4344 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004345 unsigned PFIndexes[4];
4346 for (unsigned i = 0; i != 4; ++i) {
4347 if (ShuffleMask[i] < 0)
4348 PFIndexes[i] = 8;
4349 else
4350 PFIndexes[i] = ShuffleMask[i];
4351 }
4352
4353 // Compute the index in the perfect shuffle table.
4354 unsigned PFTableIndex =
4355 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004356 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4357 unsigned Cost = (PFEntry >> 30);
4358
4359 if (Cost <= 4)
4360 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4361 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004362
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004363 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004364 if (EltSize >= 32) {
4365 // Do the expansion with floating-point types, since that is what the VFP
4366 // registers are defined to use, and since i64 is not legal.
4367 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4368 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004369 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4370 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004371 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004372 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004373 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004374 Ops.push_back(DAG.getUNDEF(EltVT));
4375 else
4376 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4377 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4378 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4379 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004380 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004381 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004382 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004383 }
4384
Bill Wendling69a05a72011-03-14 23:02:38 +00004385 if (VT == MVT::v8i8) {
4386 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4387 if (NewOp.getNode())
4388 return NewOp;
4389 }
4390
Bob Wilson22cac0d2009-08-14 05:16:33 +00004391 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004392}
4393
Bob Wilson5bafff32009-06-22 23:27:02 +00004394static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004395 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004396 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004397 if (!isa<ConstantSDNode>(Lane))
4398 return SDValue();
4399
4400 SDValue Vec = Op.getOperand(0);
4401 if (Op.getValueType() == MVT::i32 &&
4402 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4403 DebugLoc dl = Op.getDebugLoc();
4404 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4405 }
4406
4407 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004408}
4409
Bob Wilsona6d65862009-08-03 20:36:38 +00004410static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4411 // The only time a CONCAT_VECTORS operation can have legal types is when
4412 // two 64-bit vectors are concatenated to a 128-bit vector.
4413 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4414 "unexpected CONCAT_VECTORS");
4415 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004416 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004417 SDValue Op0 = Op.getOperand(0);
4418 SDValue Op1 = Op.getOperand(1);
4419 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004420 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004421 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004422 DAG.getIntPtrConstant(0));
4423 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004425 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004426 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004427 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004428}
4429
Bob Wilson626613d2010-11-23 19:38:38 +00004430/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4431/// element has been zero/sign-extended, depending on the isSigned parameter,
4432/// from an integer type half its size.
4433static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4434 bool isSigned) {
4435 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4436 EVT VT = N->getValueType(0);
4437 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4438 SDNode *BVN = N->getOperand(0).getNode();
4439 if (BVN->getValueType(0) != MVT::v4i32 ||
4440 BVN->getOpcode() != ISD::BUILD_VECTOR)
4441 return false;
4442 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4443 unsigned HiElt = 1 - LoElt;
4444 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4445 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4446 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4447 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4448 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4449 return false;
4450 if (isSigned) {
4451 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4452 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4453 return true;
4454 } else {
4455 if (Hi0->isNullValue() && Hi1->isNullValue())
4456 return true;
4457 }
4458 return false;
4459 }
4460
4461 if (N->getOpcode() != ISD::BUILD_VECTOR)
4462 return false;
4463
4464 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4465 SDNode *Elt = N->getOperand(i).getNode();
4466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4467 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4468 unsigned HalfSize = EltSize / 2;
4469 if (isSigned) {
4470 int64_t SExtVal = C->getSExtValue();
4471 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4472 return false;
4473 } else {
4474 if ((C->getZExtValue() >> HalfSize) != 0)
4475 return false;
4476 }
4477 continue;
4478 }
4479 return false;
4480 }
4481
4482 return true;
4483}
4484
4485/// isSignExtended - Check if a node is a vector value that is sign-extended
4486/// or a constant BUILD_VECTOR with sign-extended elements.
4487static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4488 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4489 return true;
4490 if (isExtendedBUILD_VECTOR(N, DAG, true))
4491 return true;
4492 return false;
4493}
4494
4495/// isZeroExtended - Check if a node is a vector value that is zero-extended
4496/// or a constant BUILD_VECTOR with zero-extended elements.
4497static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4498 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4499 return true;
4500 if (isExtendedBUILD_VECTOR(N, DAG, false))
4501 return true;
4502 return false;
4503}
4504
4505/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4506/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004507static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4508 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4509 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004510 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4511 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4512 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4513 LD->isNonTemporal(), LD->getAlignment());
4514 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4515 // have been legalized as a BITCAST from v4i32.
4516 if (N->getOpcode() == ISD::BITCAST) {
4517 SDNode *BVN = N->getOperand(0).getNode();
4518 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4519 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4520 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4521 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4522 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4523 }
4524 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4525 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4526 EVT VT = N->getValueType(0);
4527 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4528 unsigned NumElts = VT.getVectorNumElements();
4529 MVT TruncVT = MVT::getIntegerVT(EltSize);
4530 SmallVector<SDValue, 8> Ops;
4531 for (unsigned i = 0; i != NumElts; ++i) {
4532 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4533 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004534 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004535 }
4536 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4537 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004538}
4539
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004540static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4541 unsigned Opcode = N->getOpcode();
4542 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4543 SDNode *N0 = N->getOperand(0).getNode();
4544 SDNode *N1 = N->getOperand(1).getNode();
4545 return N0->hasOneUse() && N1->hasOneUse() &&
4546 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4547 }
4548 return false;
4549}
4550
4551static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4552 unsigned Opcode = N->getOpcode();
4553 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4554 SDNode *N0 = N->getOperand(0).getNode();
4555 SDNode *N1 = N->getOperand(1).getNode();
4556 return N0->hasOneUse() && N1->hasOneUse() &&
4557 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4558 }
4559 return false;
4560}
4561
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004562static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4563 // Multiplications are only custom-lowered for 128-bit vectors so that
4564 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4565 EVT VT = Op.getValueType();
4566 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4567 SDNode *N0 = Op.getOperand(0).getNode();
4568 SDNode *N1 = Op.getOperand(1).getNode();
4569 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004570 bool isMLA = false;
4571 bool isN0SExt = isSignExtended(N0, DAG);
4572 bool isN1SExt = isSignExtended(N1, DAG);
4573 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004574 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004575 else {
4576 bool isN0ZExt = isZeroExtended(N0, DAG);
4577 bool isN1ZExt = isZeroExtended(N1, DAG);
4578 if (isN0ZExt && isN1ZExt)
4579 NewOpc = ARMISD::VMULLu;
4580 else if (isN1SExt || isN1ZExt) {
4581 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4582 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4583 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4584 NewOpc = ARMISD::VMULLs;
4585 isMLA = true;
4586 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4587 NewOpc = ARMISD::VMULLu;
4588 isMLA = true;
4589 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4590 std::swap(N0, N1);
4591 NewOpc = ARMISD::VMULLu;
4592 isMLA = true;
4593 }
4594 }
4595
4596 if (!NewOpc) {
4597 if (VT == MVT::v2i64)
4598 // Fall through to expand this. It is not legal.
4599 return SDValue();
4600 else
4601 // Other vector multiplications are legal.
4602 return Op;
4603 }
4604 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004605
4606 // Legalize to a VMULL instruction.
4607 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004608 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004609 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004610 if (!isMLA) {
4611 Op0 = SkipExtension(N0, DAG);
4612 assert(Op0.getValueType().is64BitVector() &&
4613 Op1.getValueType().is64BitVector() &&
4614 "unexpected types for extended operands to VMULL");
4615 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4616 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004617
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004618 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4619 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4620 // vmull q0, d4, d6
4621 // vmlal q0, d5, d6
4622 // is faster than
4623 // vaddl q0, d4, d5
4624 // vmovl q1, d6
4625 // vmul q0, q0, q1
4626 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4627 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4628 EVT Op1VT = Op1.getValueType();
4629 return DAG.getNode(N0->getOpcode(), DL, VT,
4630 DAG.getNode(NewOpc, DL, VT,
4631 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4632 DAG.getNode(NewOpc, DL, VT,
4633 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004634}
4635
Owen Anderson76706012011-04-05 21:48:57 +00004636static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004637LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4638 // Convert to float
4639 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4640 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4641 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4642 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4643 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4644 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4645 // Get reciprocal estimate.
4646 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004647 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004648 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4649 // Because char has a smaller range than uchar, we can actually get away
4650 // without any newton steps. This requires that we use a weird bias
4651 // of 0xb000, however (again, this has been exhaustively tested).
4652 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4653 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4654 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4655 Y = DAG.getConstant(0xb000, MVT::i32);
4656 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4657 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4658 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4659 // Convert back to short.
4660 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4661 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4662 return X;
4663}
4664
Owen Anderson76706012011-04-05 21:48:57 +00004665static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004666LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4667 SDValue N2;
4668 // Convert to float.
4669 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4670 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4671 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4672 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4673 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4674 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004675
Nate Begeman7973f352011-02-11 20:53:29 +00004676 // Use reciprocal estimate and one refinement step.
4677 // float4 recip = vrecpeq_f32(yf);
4678 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004679 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004680 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004681 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004682 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4683 N1, N2);
4684 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4685 // Because short has a smaller range than ushort, we can actually get away
4686 // with only a single newton step. This requires that we use a weird bias
4687 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004688 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004689 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4690 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004691 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004692 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4693 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4694 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4695 // Convert back to integer and return.
4696 // return vmovn_s32(vcvt_s32_f32(result));
4697 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4698 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4699 return N0;
4700}
4701
4702static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4703 EVT VT = Op.getValueType();
4704 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4705 "unexpected type for custom-lowering ISD::SDIV");
4706
4707 DebugLoc dl = Op.getDebugLoc();
4708 SDValue N0 = Op.getOperand(0);
4709 SDValue N1 = Op.getOperand(1);
4710 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004711
Nate Begeman7973f352011-02-11 20:53:29 +00004712 if (VT == MVT::v8i8) {
4713 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4714 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004715
Nate Begeman7973f352011-02-11 20:53:29 +00004716 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4717 DAG.getIntPtrConstant(4));
4718 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004719 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004720 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4721 DAG.getIntPtrConstant(0));
4722 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4723 DAG.getIntPtrConstant(0));
4724
4725 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4726 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4727
4728 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4729 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004730
Nate Begeman7973f352011-02-11 20:53:29 +00004731 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4732 return N0;
4733 }
4734 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4735}
4736
4737static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4738 EVT VT = Op.getValueType();
4739 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4740 "unexpected type for custom-lowering ISD::UDIV");
4741
4742 DebugLoc dl = Op.getDebugLoc();
4743 SDValue N0 = Op.getOperand(0);
4744 SDValue N1 = Op.getOperand(1);
4745 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004746
Nate Begeman7973f352011-02-11 20:53:29 +00004747 if (VT == MVT::v8i8) {
4748 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4749 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004750
Nate Begeman7973f352011-02-11 20:53:29 +00004751 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4752 DAG.getIntPtrConstant(4));
4753 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004754 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004755 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4756 DAG.getIntPtrConstant(0));
4757 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4758 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004759
Nate Begeman7973f352011-02-11 20:53:29 +00004760 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4761 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004762
Nate Begeman7973f352011-02-11 20:53:29 +00004763 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4764 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004765
4766 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004767 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4768 N0);
4769 return N0;
4770 }
Owen Anderson76706012011-04-05 21:48:57 +00004771
Nate Begeman7973f352011-02-11 20:53:29 +00004772 // v4i16 sdiv ... Convert to float.
4773 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4774 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4775 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4776 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4777 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004778 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004779
4780 // Use reciprocal estimate and two refinement steps.
4781 // float4 recip = vrecpeq_f32(yf);
4782 // recip *= vrecpsq_f32(yf, recip);
4783 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004784 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004785 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004786 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004787 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004788 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004789 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004790 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004791 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004792 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004793 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4794 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4795 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4796 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004797 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004798 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4799 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4800 N1 = DAG.getConstant(2, MVT::i32);
4801 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4802 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4803 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4804 // Convert back to integer and return.
4805 // return vmovn_u32(vcvt_s32_f32(result));
4806 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4807 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4808 return N0;
4809}
4810
Dan Gohmand858e902010-04-17 15:26:15 +00004811SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004812 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004813 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004814 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004815 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004816 case ISD::GlobalAddress:
4817 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4818 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004819 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004820 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004821 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4822 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004823 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004824 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004825 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004826 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004827 case ISD::SINT_TO_FP:
4828 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4829 case ISD::FP_TO_SINT:
4830 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004831 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004832 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004833 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004834 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004835 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004836 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004837 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004838 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4839 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004840 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004841 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004842 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004843 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004844 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004845 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004846 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004847 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004848 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004849 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004850 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004851 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004852 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004853 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004854 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004855 case ISD::SDIV: return LowerSDIV(Op, DAG);
4856 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004857 }
Dan Gohman475871a2008-07-27 21:46:04 +00004858 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004859}
4860
Duncan Sands1607f052008-12-01 11:39:25 +00004861/// ReplaceNodeResults - Replace the results of node with an illegal result
4862/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004863void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4864 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004865 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004866 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004867 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004868 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004869 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004870 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004871 case ISD::BITCAST:
4872 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004873 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004874 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004875 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004876 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004877 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004878 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004879 if (Res.getNode())
4880 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004881}
Chris Lattner27a6c732007-11-24 07:07:01 +00004882
Evan Chenga8e29892007-01-19 07:51:42 +00004883//===----------------------------------------------------------------------===//
4884// ARM Scheduler Hooks
4885//===----------------------------------------------------------------------===//
4886
4887MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004888ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4889 MachineBasicBlock *BB,
4890 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004891 unsigned dest = MI->getOperand(0).getReg();
4892 unsigned ptr = MI->getOperand(1).getReg();
4893 unsigned oldval = MI->getOperand(2).getReg();
4894 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004895 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4896 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004897 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004898
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004899 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4900 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004901 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004902 : ARM::GPRRegisterClass);
4903
4904 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004905 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4906 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4907 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004908 }
4909
Jim Grosbach5278eb82009-12-11 01:42:04 +00004910 unsigned ldrOpc, strOpc;
4911 switch (Size) {
4912 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004913 case 1:
4914 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004915 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004916 break;
4917 case 2:
4918 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4919 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4920 break;
4921 case 4:
4922 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4923 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4924 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004925 }
4926
4927 MachineFunction *MF = BB->getParent();
4928 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4929 MachineFunction::iterator It = BB;
4930 ++It; // insert the new blocks after the current block
4931
4932 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4933 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4934 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4935 MF->insert(It, loop1MBB);
4936 MF->insert(It, loop2MBB);
4937 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004938
4939 // Transfer the remainder of BB and its successor edges to exitMBB.
4940 exitMBB->splice(exitMBB->begin(), BB,
4941 llvm::next(MachineBasicBlock::iterator(MI)),
4942 BB->end());
4943 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004944
4945 // thisMBB:
4946 // ...
4947 // fallthrough --> loop1MBB
4948 BB->addSuccessor(loop1MBB);
4949
4950 // loop1MBB:
4951 // ldrex dest, [ptr]
4952 // cmp dest, oldval
4953 // bne exitMBB
4954 BB = loop1MBB;
4955 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004956 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004957 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004958 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4959 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004960 BB->addSuccessor(loop2MBB);
4961 BB->addSuccessor(exitMBB);
4962
4963 // loop2MBB:
4964 // strex scratch, newval, [ptr]
4965 // cmp scratch, #0
4966 // bne loop1MBB
4967 BB = loop2MBB;
4968 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4969 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004970 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004971 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004972 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4973 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004974 BB->addSuccessor(loop1MBB);
4975 BB->addSuccessor(exitMBB);
4976
4977 // exitMBB:
4978 // ...
4979 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004980
Dan Gohman14152b42010-07-06 20:24:04 +00004981 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004982
Jim Grosbach5278eb82009-12-11 01:42:04 +00004983 return BB;
4984}
4985
4986MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004987ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4988 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004989 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4990 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4991
4992 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004993 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004994 MachineFunction::iterator It = BB;
4995 ++It;
4996
4997 unsigned dest = MI->getOperand(0).getReg();
4998 unsigned ptr = MI->getOperand(1).getReg();
4999 unsigned incr = MI->getOperand(2).getReg();
5000 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005001 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005002
5003 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5004 if (isThumb2) {
5005 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5006 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5007 }
5008
Jim Grosbachc3c23542009-12-14 04:22:04 +00005009 unsigned ldrOpc, strOpc;
5010 switch (Size) {
5011 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005012 case 1:
5013 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005014 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005015 break;
5016 case 2:
5017 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5018 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5019 break;
5020 case 4:
5021 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5022 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5023 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005024 }
5025
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005026 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5027 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5028 MF->insert(It, loopMBB);
5029 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005030
5031 // Transfer the remainder of BB and its successor edges to exitMBB.
5032 exitMBB->splice(exitMBB->begin(), BB,
5033 llvm::next(MachineBasicBlock::iterator(MI)),
5034 BB->end());
5035 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005036
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005037 TargetRegisterClass *TRC =
5038 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5039 unsigned scratch = MRI.createVirtualRegister(TRC);
5040 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005041
5042 // thisMBB:
5043 // ...
5044 // fallthrough --> loopMBB
5045 BB->addSuccessor(loopMBB);
5046
5047 // loopMBB:
5048 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005049 // <binop> scratch2, dest, incr
5050 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005051 // cmp scratch, #0
5052 // bne- loopMBB
5053 // fallthrough --> exitMBB
5054 BB = loopMBB;
5055 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005056 if (BinOpcode) {
5057 // operand order needs to go the other way for NAND
5058 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5059 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5060 addReg(incr).addReg(dest)).addReg(0);
5061 else
5062 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5063 addReg(dest).addReg(incr)).addReg(0);
5064 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005065
5066 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5067 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005068 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005069 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005070 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5071 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005072
5073 BB->addSuccessor(loopMBB);
5074 BB->addSuccessor(exitMBB);
5075
5076 // exitMBB:
5077 // ...
5078 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005079
Dan Gohman14152b42010-07-06 20:24:04 +00005080 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005081
Jim Grosbachc3c23542009-12-14 04:22:04 +00005082 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005083}
5084
Jim Grosbachf7da8822011-04-26 19:44:18 +00005085MachineBasicBlock *
5086ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5087 MachineBasicBlock *BB,
5088 unsigned Size,
5089 bool signExtend,
5090 ARMCC::CondCodes Cond) const {
5091 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5092
5093 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5094 MachineFunction *MF = BB->getParent();
5095 MachineFunction::iterator It = BB;
5096 ++It;
5097
5098 unsigned dest = MI->getOperand(0).getReg();
5099 unsigned ptr = MI->getOperand(1).getReg();
5100 unsigned incr = MI->getOperand(2).getReg();
5101 unsigned oldval = dest;
5102 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005103 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005104
5105 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5106 if (isThumb2) {
5107 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5108 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5109 }
5110
Jim Grosbachf7da8822011-04-26 19:44:18 +00005111 unsigned ldrOpc, strOpc, extendOpc;
5112 switch (Size) {
5113 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5114 case 1:
5115 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5116 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
5117 extendOpc = isThumb2 ? ARM::t2SXTBr : ARM::SXTBr;
5118 break;
5119 case 2:
5120 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5121 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5122 extendOpc = isThumb2 ? ARM::t2SXTHr : ARM::SXTHr;
5123 break;
5124 case 4:
5125 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5126 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5127 extendOpc = 0;
5128 break;
5129 }
5130
5131 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5132 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5133 MF->insert(It, loopMBB);
5134 MF->insert(It, exitMBB);
5135
5136 // Transfer the remainder of BB and its successor edges to exitMBB.
5137 exitMBB->splice(exitMBB->begin(), BB,
5138 llvm::next(MachineBasicBlock::iterator(MI)),
5139 BB->end());
5140 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5141
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005142 TargetRegisterClass *TRC =
5143 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5144 unsigned scratch = MRI.createVirtualRegister(TRC);
5145 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005146
5147 // thisMBB:
5148 // ...
5149 // fallthrough --> loopMBB
5150 BB->addSuccessor(loopMBB);
5151
5152 // loopMBB:
5153 // ldrex dest, ptr
5154 // (sign extend dest, if required)
5155 // cmp dest, incr
5156 // cmov.cond scratch2, dest, incr
5157 // strex scratch, scratch2, ptr
5158 // cmp scratch, #0
5159 // bne- loopMBB
5160 // fallthrough --> exitMBB
5161 BB = loopMBB;
5162 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5163
5164 // Sign extend the value, if necessary.
5165 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005166 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005167 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval).addReg(dest));
5168 }
5169
5170 // Build compare and cmov instructions.
5171 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5172 .addReg(oldval).addReg(incr));
5173 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5174 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5175
5176 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5177 .addReg(ptr));
5178 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5179 .addReg(scratch).addImm(0));
5180 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5181 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5182
5183 BB->addSuccessor(loopMBB);
5184 BB->addSuccessor(exitMBB);
5185
5186 // exitMBB:
5187 // ...
5188 BB = exitMBB;
5189
5190 MI->eraseFromParent(); // The instruction is gone now.
5191
5192 return BB;
5193}
5194
Evan Cheng218977b2010-07-13 19:27:42 +00005195static
5196MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5197 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5198 E = MBB->succ_end(); I != E; ++I)
5199 if (*I != Succ)
5200 return *I;
5201 llvm_unreachable("Expecting a BB with two successors!");
5202}
5203
Andrew Trick1c3af772011-04-23 03:55:32 +00005204// FIXME: This opcode table should obviously be expressed in the target
5205// description. We probably just need a "machine opcode" value in the pseudo
5206// instruction. But the ideal solution maybe to simply remove the "S" version
5207// of the opcode altogether.
5208struct AddSubFlagsOpcodePair {
5209 unsigned PseudoOpc;
5210 unsigned MachineOpc;
5211};
5212
5213static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5214 {ARM::ADCSri, ARM::ADCri},
5215 {ARM::ADCSrr, ARM::ADCrr},
5216 {ARM::ADCSrs, ARM::ADCrs},
5217 {ARM::SBCSri, ARM::SBCri},
5218 {ARM::SBCSrr, ARM::SBCrr},
5219 {ARM::SBCSrs, ARM::SBCrs},
5220 {ARM::RSBSri, ARM::RSBri},
5221 {ARM::RSBSrr, ARM::RSBrr},
5222 {ARM::RSBSrs, ARM::RSBrs},
5223 {ARM::RSCSri, ARM::RSCri},
5224 {ARM::RSCSrs, ARM::RSCrs},
5225 {ARM::t2ADCSri, ARM::t2ADCri},
5226 {ARM::t2ADCSrr, ARM::t2ADCrr},
5227 {ARM::t2ADCSrs, ARM::t2ADCrs},
5228 {ARM::t2SBCSri, ARM::t2SBCri},
5229 {ARM::t2SBCSrr, ARM::t2SBCrr},
5230 {ARM::t2SBCSrs, ARM::t2SBCrs},
5231 {ARM::t2RSBSri, ARM::t2RSBri},
5232 {ARM::t2RSBSrs, ARM::t2RSBrs},
5233};
5234
5235// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5236// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5237//
5238// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5239// position to be recognized by the target descrition as the 'S' bit.
5240bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5241 MachineBasicBlock *BB) const {
5242 unsigned OldOpc = MI->getOpcode();
5243 unsigned NewOpc = 0;
5244
5245 // This is only called for instructions that need remapping, so iterating over
5246 // the tiny opcode table is not costly.
5247 static const int NPairs =
5248 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5249 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5250 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5251 if (OldOpc == Pair->PseudoOpc) {
5252 NewOpc = Pair->MachineOpc;
5253 break;
5254 }
5255 }
5256 if (!NewOpc)
5257 return false;
5258
5259 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5260 DebugLoc dl = MI->getDebugLoc();
5261 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5262 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5263 MIB.addOperand(MI->getOperand(i));
5264 AddDefaultPred(MIB);
5265 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5266 MI->eraseFromParent();
5267 return true;
5268}
5269
Jim Grosbache801dc42009-12-12 01:40:06 +00005270MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005271ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005272 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005273 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005274 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005275 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005276 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005277 default: {
5278 if (RemapAddSubWithFlags(MI, BB))
5279 return BB;
5280
Jim Grosbach5278eb82009-12-11 01:42:04 +00005281 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005282 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005283 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005284 case ARM::ATOMIC_LOAD_ADD_I8:
5285 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5286 case ARM::ATOMIC_LOAD_ADD_I16:
5287 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5288 case ARM::ATOMIC_LOAD_ADD_I32:
5289 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005290
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005291 case ARM::ATOMIC_LOAD_AND_I8:
5292 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5293 case ARM::ATOMIC_LOAD_AND_I16:
5294 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5295 case ARM::ATOMIC_LOAD_AND_I32:
5296 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005297
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005298 case ARM::ATOMIC_LOAD_OR_I8:
5299 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5300 case ARM::ATOMIC_LOAD_OR_I16:
5301 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5302 case ARM::ATOMIC_LOAD_OR_I32:
5303 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005304
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005305 case ARM::ATOMIC_LOAD_XOR_I8:
5306 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5307 case ARM::ATOMIC_LOAD_XOR_I16:
5308 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5309 case ARM::ATOMIC_LOAD_XOR_I32:
5310 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005311
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005312 case ARM::ATOMIC_LOAD_NAND_I8:
5313 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5314 case ARM::ATOMIC_LOAD_NAND_I16:
5315 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5316 case ARM::ATOMIC_LOAD_NAND_I32:
5317 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005318
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005319 case ARM::ATOMIC_LOAD_SUB_I8:
5320 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5321 case ARM::ATOMIC_LOAD_SUB_I16:
5322 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5323 case ARM::ATOMIC_LOAD_SUB_I32:
5324 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005325
Jim Grosbachf7da8822011-04-26 19:44:18 +00005326 case ARM::ATOMIC_LOAD_MIN_I8:
5327 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5328 case ARM::ATOMIC_LOAD_MIN_I16:
5329 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5330 case ARM::ATOMIC_LOAD_MIN_I32:
5331 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5332
5333 case ARM::ATOMIC_LOAD_MAX_I8:
5334 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5335 case ARM::ATOMIC_LOAD_MAX_I16:
5336 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5337 case ARM::ATOMIC_LOAD_MAX_I32:
5338 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5339
5340 case ARM::ATOMIC_LOAD_UMIN_I8:
5341 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5342 case ARM::ATOMIC_LOAD_UMIN_I16:
5343 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5344 case ARM::ATOMIC_LOAD_UMIN_I32:
5345 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5346
5347 case ARM::ATOMIC_LOAD_UMAX_I8:
5348 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5349 case ARM::ATOMIC_LOAD_UMAX_I16:
5350 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5351 case ARM::ATOMIC_LOAD_UMAX_I32:
5352 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5353
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005354 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5355 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5356 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005357
5358 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5359 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5360 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005361
Evan Cheng007ea272009-08-12 05:17:19 +00005362 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005363 // To "insert" a SELECT_CC instruction, we actually have to insert the
5364 // diamond control-flow pattern. The incoming instruction knows the
5365 // destination vreg to set, the condition code register to branch on, the
5366 // true/false values to select between, and a branch opcode to use.
5367 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005368 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005369 ++It;
5370
5371 // thisMBB:
5372 // ...
5373 // TrueVal = ...
5374 // cmpTY ccX, r1, r2
5375 // bCC copy1MBB
5376 // fallthrough --> copy0MBB
5377 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005378 MachineFunction *F = BB->getParent();
5379 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5380 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005381 F->insert(It, copy0MBB);
5382 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005383
5384 // Transfer the remainder of BB and its successor edges to sinkMBB.
5385 sinkMBB->splice(sinkMBB->begin(), BB,
5386 llvm::next(MachineBasicBlock::iterator(MI)),
5387 BB->end());
5388 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5389
Dan Gohman258c58c2010-07-06 15:49:48 +00005390 BB->addSuccessor(copy0MBB);
5391 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005392
Dan Gohman14152b42010-07-06 20:24:04 +00005393 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5394 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5395
Evan Chenga8e29892007-01-19 07:51:42 +00005396 // copy0MBB:
5397 // %FalseValue = ...
5398 // # fallthrough to sinkMBB
5399 BB = copy0MBB;
5400
5401 // Update machine-CFG edges
5402 BB->addSuccessor(sinkMBB);
5403
5404 // sinkMBB:
5405 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5406 // ...
5407 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005408 BuildMI(*BB, BB->begin(), dl,
5409 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005410 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5411 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5412
Dan Gohman14152b42010-07-06 20:24:04 +00005413 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005414 return BB;
5415 }
Evan Cheng86198642009-08-07 00:34:42 +00005416
Evan Cheng218977b2010-07-13 19:27:42 +00005417 case ARM::BCCi64:
5418 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005419 // If there is an unconditional branch to the other successor, remove it.
5420 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005421
Evan Cheng218977b2010-07-13 19:27:42 +00005422 // Compare both parts that make up the double comparison separately for
5423 // equality.
5424 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5425
5426 unsigned LHS1 = MI->getOperand(1).getReg();
5427 unsigned LHS2 = MI->getOperand(2).getReg();
5428 if (RHSisZero) {
5429 AddDefaultPred(BuildMI(BB, dl,
5430 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5431 .addReg(LHS1).addImm(0));
5432 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5433 .addReg(LHS2).addImm(0)
5434 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5435 } else {
5436 unsigned RHS1 = MI->getOperand(3).getReg();
5437 unsigned RHS2 = MI->getOperand(4).getReg();
5438 AddDefaultPred(BuildMI(BB, dl,
5439 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5440 .addReg(LHS1).addReg(RHS1));
5441 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5442 .addReg(LHS2).addReg(RHS2)
5443 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5444 }
5445
5446 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5447 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5448 if (MI->getOperand(0).getImm() == ARMCC::NE)
5449 std::swap(destMBB, exitMBB);
5450
5451 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5452 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5453 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5454 .addMBB(exitMBB);
5455
5456 MI->eraseFromParent(); // The pseudo instruction is gone now.
5457 return BB;
5458 }
Evan Chenga8e29892007-01-19 07:51:42 +00005459 }
5460}
5461
5462//===----------------------------------------------------------------------===//
5463// ARM Optimization Hooks
5464//===----------------------------------------------------------------------===//
5465
Chris Lattnerd1980a52009-03-12 06:52:53 +00005466static
5467SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5468 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005469 SelectionDAG &DAG = DCI.DAG;
5470 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005471 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005472 unsigned Opc = N->getOpcode();
5473 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5474 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5475 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5476 ISD::CondCode CC = ISD::SETCC_INVALID;
5477
5478 if (isSlctCC) {
5479 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5480 } else {
5481 SDValue CCOp = Slct.getOperand(0);
5482 if (CCOp.getOpcode() == ISD::SETCC)
5483 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5484 }
5485
5486 bool DoXform = false;
5487 bool InvCC = false;
5488 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5489 "Bad input!");
5490
5491 if (LHS.getOpcode() == ISD::Constant &&
5492 cast<ConstantSDNode>(LHS)->isNullValue()) {
5493 DoXform = true;
5494 } else if (CC != ISD::SETCC_INVALID &&
5495 RHS.getOpcode() == ISD::Constant &&
5496 cast<ConstantSDNode>(RHS)->isNullValue()) {
5497 std::swap(LHS, RHS);
5498 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005499 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005500 Op0.getOperand(0).getValueType();
5501 bool isInt = OpVT.isInteger();
5502 CC = ISD::getSetCCInverse(CC, isInt);
5503
5504 if (!TLI.isCondCodeLegal(CC, OpVT))
5505 return SDValue(); // Inverse operator isn't legal.
5506
5507 DoXform = true;
5508 InvCC = true;
5509 }
5510
5511 if (DoXform) {
5512 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5513 if (isSlctCC)
5514 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5515 Slct.getOperand(0), Slct.getOperand(1), CC);
5516 SDValue CCOp = Slct.getOperand(0);
5517 if (InvCC)
5518 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5519 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5520 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5521 CCOp, OtherOp, Result);
5522 }
5523 return SDValue();
5524}
5525
Tanya Lattner189531f2011-06-14 23:48:48 +00005526// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
5527// (only after legalization).
5528static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5529 TargetLowering::DAGCombinerInfo &DCI,
5530 const ARMSubtarget *Subtarget) {
5531
5532 // Only perform optimization if after legalize, and if NEON is available. We
5533 // also expected both operands to be BUILD_VECTORs.
5534 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5535 || N0.getOpcode() != ISD::BUILD_VECTOR
5536 || N1.getOpcode() != ISD::BUILD_VECTOR)
5537 return SDValue();
5538
5539 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5540 EVT VT = N->getValueType(0);
5541 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5542 return SDValue();
5543
5544 // Check that the vector operands are of the right form.
5545 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5546 // operands, where N is the size of the formed vector.
5547 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5548 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005549
5550 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005551 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005552 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005553 SDValue Vec = N0->getOperand(0)->getOperand(0);
5554 SDNode *V = Vec.getNode();
5555 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005556
5557 // For each operands to the ADD which are BUILD_VECTORs,
5558 // check to see if each of their operands are an EXTRACT_VECTOR with
5559 // the same vector and appropriate index.
5560 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5561 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5562 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5563
5564 SDValue ExtVec0 = N0->getOperand(i);
5565 SDValue ExtVec1 = N1->getOperand(i);
5566
5567 // First operand is the vector, verify its the same.
5568 if (V != ExtVec0->getOperand(0).getNode() ||
5569 V != ExtVec1->getOperand(0).getNode())
5570 return SDValue();
5571
5572 // Second is the constant, verify its correct.
5573 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5574 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
5575
5576 // For the constant, we want to see all the even or all the odd.
5577 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5578 || C1->getZExtValue() != nextIndex+1)
5579 return SDValue();
5580
5581 // Increment index.
5582 nextIndex+=2;
5583 } else
5584 return SDValue();
5585 }
5586
5587 // Create VPADDL node.
5588 SelectionDAG &DAG = DCI.DAG;
5589 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5590 DebugLoc DL = N->getDebugLoc();
5591
5592 // Build operand list.
5593 SmallVector<SDValue, 8> Ops;
5594 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5595 TLI.getPointerTy()));
5596
5597 // Input is the vector.
5598 Ops.push_back(Vec);
5599
5600 // Get widened type and narrowed type.
5601 MVT widenType;
5602 unsigned numElem = VT.getVectorNumElements();
5603 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5604 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5605 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5606 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5607 default:
5608 assert(0 && "Invalid vector element type for padd optimization.");
5609 }
5610
5611 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5612 widenType, &Ops[0], Ops.size());
5613 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5614}
5615
Bob Wilson3d5792a2010-07-29 20:34:14 +00005616/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5617/// operands N0 and N1. This is a helper for PerformADDCombine that is
5618/// called with the default operands, and if that fails, with commuted
5619/// operands.
5620static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005621 TargetLowering::DAGCombinerInfo &DCI,
5622 const ARMSubtarget *Subtarget){
5623
5624 // Attempt to create vpaddl for this add.
5625 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5626 if (Result.getNode())
5627 return Result;
5628
Chris Lattnerd1980a52009-03-12 06:52:53 +00005629 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5630 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5631 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5632 if (Result.getNode()) return Result;
5633 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005634 return SDValue();
5635}
5636
Bob Wilson3d5792a2010-07-29 20:34:14 +00005637/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5638///
5639static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005640 TargetLowering::DAGCombinerInfo &DCI,
5641 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005642 SDValue N0 = N->getOperand(0);
5643 SDValue N1 = N->getOperand(1);
5644
5645 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005646 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005647 if (Result.getNode())
5648 return Result;
5649
5650 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005651 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005652}
5653
Chris Lattnerd1980a52009-03-12 06:52:53 +00005654/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005655///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005656static SDValue PerformSUBCombine(SDNode *N,
5657 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005658 SDValue N0 = N->getOperand(0);
5659 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005660
Chris Lattnerd1980a52009-03-12 06:52:53 +00005661 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5662 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5663 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5664 if (Result.getNode()) return Result;
5665 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005666
Chris Lattnerd1980a52009-03-12 06:52:53 +00005667 return SDValue();
5668}
5669
Evan Cheng463d3582011-03-31 19:38:48 +00005670/// PerformVMULCombine
5671/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5672/// special multiplier accumulator forwarding.
5673/// vmul d3, d0, d2
5674/// vmla d3, d1, d2
5675/// is faster than
5676/// vadd d3, d0, d1
5677/// vmul d3, d3, d2
5678static SDValue PerformVMULCombine(SDNode *N,
5679 TargetLowering::DAGCombinerInfo &DCI,
5680 const ARMSubtarget *Subtarget) {
5681 if (!Subtarget->hasVMLxForwarding())
5682 return SDValue();
5683
5684 SelectionDAG &DAG = DCI.DAG;
5685 SDValue N0 = N->getOperand(0);
5686 SDValue N1 = N->getOperand(1);
5687 unsigned Opcode = N0.getOpcode();
5688 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5689 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5690 Opcode = N0.getOpcode();
5691 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5692 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5693 return SDValue();
5694 std::swap(N0, N1);
5695 }
5696
5697 EVT VT = N->getValueType(0);
5698 DebugLoc DL = N->getDebugLoc();
5699 SDValue N00 = N0->getOperand(0);
5700 SDValue N01 = N0->getOperand(1);
5701 return DAG.getNode(Opcode, DL, VT,
5702 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5703 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5704}
5705
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005706static SDValue PerformMULCombine(SDNode *N,
5707 TargetLowering::DAGCombinerInfo &DCI,
5708 const ARMSubtarget *Subtarget) {
5709 SelectionDAG &DAG = DCI.DAG;
5710
5711 if (Subtarget->isThumb1Only())
5712 return SDValue();
5713
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005714 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5715 return SDValue();
5716
5717 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005718 if (VT.is64BitVector() || VT.is128BitVector())
5719 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005720 if (VT != MVT::i32)
5721 return SDValue();
5722
5723 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5724 if (!C)
5725 return SDValue();
5726
5727 uint64_t MulAmt = C->getZExtValue();
5728 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5729 ShiftAmt = ShiftAmt & (32 - 1);
5730 SDValue V = N->getOperand(0);
5731 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005732
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005733 SDValue Res;
5734 MulAmt >>= ShiftAmt;
5735 if (isPowerOf2_32(MulAmt - 1)) {
5736 // (mul x, 2^N + 1) => (add (shl x, N), x)
5737 Res = DAG.getNode(ISD::ADD, DL, VT,
5738 V, DAG.getNode(ISD::SHL, DL, VT,
5739 V, DAG.getConstant(Log2_32(MulAmt-1),
5740 MVT::i32)));
5741 } else if (isPowerOf2_32(MulAmt + 1)) {
5742 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5743 Res = DAG.getNode(ISD::SUB, DL, VT,
5744 DAG.getNode(ISD::SHL, DL, VT,
5745 V, DAG.getConstant(Log2_32(MulAmt+1),
5746 MVT::i32)),
5747 V);
5748 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005749 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005750
5751 if (ShiftAmt != 0)
5752 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5753 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005754
5755 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005756 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005757 return SDValue();
5758}
5759
Owen Anderson080c0922010-11-05 19:27:46 +00005760static SDValue PerformANDCombine(SDNode *N,
5761 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005762
Owen Anderson080c0922010-11-05 19:27:46 +00005763 // Attempt to use immediate-form VBIC
5764 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5765 DebugLoc dl = N->getDebugLoc();
5766 EVT VT = N->getValueType(0);
5767 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005768
Tanya Lattner0433b212011-04-07 15:24:20 +00005769 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5770 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005771
Owen Anderson080c0922010-11-05 19:27:46 +00005772 APInt SplatBits, SplatUndef;
5773 unsigned SplatBitSize;
5774 bool HasAnyUndefs;
5775 if (BVN &&
5776 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5777 if (SplatBitSize <= 64) {
5778 EVT VbicVT;
5779 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5780 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005781 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005782 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005783 if (Val.getNode()) {
5784 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005785 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005786 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005787 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005788 }
5789 }
5790 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005791
Owen Anderson080c0922010-11-05 19:27:46 +00005792 return SDValue();
5793}
5794
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005795/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5796static SDValue PerformORCombine(SDNode *N,
5797 TargetLowering::DAGCombinerInfo &DCI,
5798 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005799 // Attempt to use immediate-form VORR
5800 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5801 DebugLoc dl = N->getDebugLoc();
5802 EVT VT = N->getValueType(0);
5803 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005804
Tanya Lattner0433b212011-04-07 15:24:20 +00005805 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5806 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005807
Owen Anderson60f48702010-11-03 23:15:26 +00005808 APInt SplatBits, SplatUndef;
5809 unsigned SplatBitSize;
5810 bool HasAnyUndefs;
5811 if (BVN && Subtarget->hasNEON() &&
5812 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5813 if (SplatBitSize <= 64) {
5814 EVT VorrVT;
5815 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5816 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005817 DAG, VorrVT, VT.is128BitVector(),
5818 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005819 if (Val.getNode()) {
5820 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005821 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005822 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005823 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005824 }
5825 }
5826 }
5827
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005828 SDValue N0 = N->getOperand(0);
5829 if (N0.getOpcode() != ISD::AND)
5830 return SDValue();
5831 SDValue N1 = N->getOperand(1);
5832
5833 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5834 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5835 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5836 APInt SplatUndef;
5837 unsigned SplatBitSize;
5838 bool HasAnyUndefs;
5839
5840 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5841 APInt SplatBits0;
5842 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5843 HasAnyUndefs) && !HasAnyUndefs) {
5844 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5845 APInt SplatBits1;
5846 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5847 HasAnyUndefs) && !HasAnyUndefs &&
5848 SplatBits0 == ~SplatBits1) {
5849 // Canonicalize the vector type to make instruction selection simpler.
5850 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5851 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5852 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005853 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005854 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5855 }
5856 }
5857 }
5858
Jim Grosbach54238562010-07-17 03:30:54 +00005859 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5860 // reasonable.
5861
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005862 // BFI is only available on V6T2+
5863 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5864 return SDValue();
5865
Jim Grosbach54238562010-07-17 03:30:54 +00005866 DebugLoc DL = N->getDebugLoc();
5867 // 1) or (and A, mask), val => ARMbfi A, val, mask
5868 // iff (val & mask) == val
5869 //
5870 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5871 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005872 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005873 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005874 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005875 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005876
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005877 if (VT != MVT::i32)
5878 return SDValue();
5879
Evan Cheng30fb13f2010-12-13 20:32:54 +00005880 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005881
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005882 // The value and the mask need to be constants so we can verify this is
5883 // actually a bitfield set. If the mask is 0xffff, we can do better
5884 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005885 SDValue MaskOp = N0.getOperand(1);
5886 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5887 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005888 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005889 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005890 if (Mask == 0xffff)
5891 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005892 SDValue Res;
5893 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005894 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5895 if (N1C) {
5896 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005897 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005898 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005899
Evan Chenga9688c42010-12-11 04:11:38 +00005900 if (ARM::isBitFieldInvertedMask(Mask)) {
5901 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005902
Evan Cheng30fb13f2010-12-13 20:32:54 +00005903 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005904 DAG.getConstant(Val, MVT::i32),
5905 DAG.getConstant(Mask, MVT::i32));
5906
5907 // Do not add new nodes to DAG combiner worklist.
5908 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005909 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005910 }
Jim Grosbach54238562010-07-17 03:30:54 +00005911 } else if (N1.getOpcode() == ISD::AND) {
5912 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005913 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5914 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005915 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005916 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005917
Eric Christopher29aeed12011-03-26 01:21:03 +00005918 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5919 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005920 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005921 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005922 // The pack halfword instruction works better for masks that fit it,
5923 // so use that when it's available.
5924 if (Subtarget->hasT2ExtractPack() &&
5925 (Mask == 0xffff || Mask == 0xffff0000))
5926 return SDValue();
5927 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005928 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005929 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005930 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005931 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005932 DAG.getConstant(Mask, MVT::i32));
5933 // Do not add new nodes to DAG combiner worklist.
5934 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005935 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005936 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005937 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005938 // The pack halfword instruction works better for masks that fit it,
5939 // so use that when it's available.
5940 if (Subtarget->hasT2ExtractPack() &&
5941 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5942 return SDValue();
5943 // 2b
5944 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005945 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005946 DAG.getConstant(lsb, MVT::i32));
5947 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005948 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005949 // Do not add new nodes to DAG combiner worklist.
5950 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005951 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005952 }
5953 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005954
Evan Cheng30fb13f2010-12-13 20:32:54 +00005955 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5956 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5957 ARM::isBitFieldInvertedMask(~Mask)) {
5958 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5959 // where lsb(mask) == #shamt and masked bits of B are known zero.
5960 SDValue ShAmt = N00.getOperand(1);
5961 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5962 unsigned LSB = CountTrailingZeros_32(Mask);
5963 if (ShAmtC != LSB)
5964 return SDValue();
5965
5966 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5967 DAG.getConstant(~Mask, MVT::i32));
5968
5969 // Do not add new nodes to DAG combiner worklist.
5970 DCI.CombineTo(N, Res, false);
5971 }
5972
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005973 return SDValue();
5974}
5975
Evan Chengbf188ae2011-06-15 01:12:31 +00005976/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
5977/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00005978static SDValue PerformBFICombine(SDNode *N,
5979 TargetLowering::DAGCombinerInfo &DCI) {
5980 SDValue N1 = N->getOperand(1);
5981 if (N1.getOpcode() == ISD::AND) {
5982 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5983 if (!N11C)
5984 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00005985 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5986 unsigned LSB = CountTrailingZeros_32(~InvMask);
5987 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
5988 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00005989 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00005990 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00005991 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5992 N->getOperand(0), N1.getOperand(0),
5993 N->getOperand(2));
5994 }
5995 return SDValue();
5996}
5997
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005998/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5999/// ARMISD::VMOVRRD.
6000static SDValue PerformVMOVRRDCombine(SDNode *N,
6001 TargetLowering::DAGCombinerInfo &DCI) {
6002 // vmovrrd(vmovdrr x, y) -> x,y
6003 SDValue InDouble = N->getOperand(0);
6004 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6005 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006006
6007 // vmovrrd(load f64) -> (load i32), (load i32)
6008 SDNode *InNode = InDouble.getNode();
6009 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6010 InNode->getValueType(0) == MVT::f64 &&
6011 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6012 !cast<LoadSDNode>(InNode)->isVolatile()) {
6013 // TODO: Should this be done for non-FrameIndex operands?
6014 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6015
6016 SelectionDAG &DAG = DCI.DAG;
6017 DebugLoc DL = LD->getDebugLoc();
6018 SDValue BasePtr = LD->getBasePtr();
6019 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6020 LD->getPointerInfo(), LD->isVolatile(),
6021 LD->isNonTemporal(), LD->getAlignment());
6022
6023 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6024 DAG.getConstant(4, MVT::i32));
6025 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6026 LD->getPointerInfo(), LD->isVolatile(),
6027 LD->isNonTemporal(),
6028 std::min(4U, LD->getAlignment() / 2));
6029
6030 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6031 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6032 DCI.RemoveFromWorklist(LD);
6033 DAG.DeleteNode(LD);
6034 return Result;
6035 }
6036
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006037 return SDValue();
6038}
6039
6040/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6041/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6042static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6043 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6044 SDValue Op0 = N->getOperand(0);
6045 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006046 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006047 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006048 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006049 Op1 = Op1.getOperand(0);
6050 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6051 Op0.getNode() == Op1.getNode() &&
6052 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006053 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006054 N->getValueType(0), Op0.getOperand(0));
6055 return SDValue();
6056}
6057
Bob Wilson31600902010-12-21 06:43:19 +00006058/// PerformSTORECombine - Target-specific dag combine xforms for
6059/// ISD::STORE.
6060static SDValue PerformSTORECombine(SDNode *N,
6061 TargetLowering::DAGCombinerInfo &DCI) {
6062 // Bitcast an i64 store extracted from a vector to f64.
6063 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6064 StoreSDNode *St = cast<StoreSDNode>(N);
6065 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006066 if (!ISD::isNormalStore(St) || St->isVolatile())
6067 return SDValue();
6068
6069 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6070 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6071 SelectionDAG &DAG = DCI.DAG;
6072 DebugLoc DL = St->getDebugLoc();
6073 SDValue BasePtr = St->getBasePtr();
6074 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6075 StVal.getNode()->getOperand(0), BasePtr,
6076 St->getPointerInfo(), St->isVolatile(),
6077 St->isNonTemporal(), St->getAlignment());
6078
6079 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6080 DAG.getConstant(4, MVT::i32));
6081 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6082 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6083 St->isNonTemporal(),
6084 std::min(4U, St->getAlignment() / 2));
6085 }
6086
6087 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006088 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6089 return SDValue();
6090
6091 SelectionDAG &DAG = DCI.DAG;
6092 DebugLoc dl = StVal.getDebugLoc();
6093 SDValue IntVec = StVal.getOperand(0);
6094 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6095 IntVec.getValueType().getVectorNumElements());
6096 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6097 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6098 Vec, StVal.getOperand(1));
6099 dl = N->getDebugLoc();
6100 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6101 // Make the DAGCombiner fold the bitcasts.
6102 DCI.AddToWorklist(Vec.getNode());
6103 DCI.AddToWorklist(ExtElt.getNode());
6104 DCI.AddToWorklist(V.getNode());
6105 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6106 St->getPointerInfo(), St->isVolatile(),
6107 St->isNonTemporal(), St->getAlignment(),
6108 St->getTBAAInfo());
6109}
6110
6111/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6112/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6113/// i64 vector to have f64 elements, since the value can then be loaded
6114/// directly into a VFP register.
6115static bool hasNormalLoadOperand(SDNode *N) {
6116 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6117 for (unsigned i = 0; i < NumElts; ++i) {
6118 SDNode *Elt = N->getOperand(i).getNode();
6119 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6120 return true;
6121 }
6122 return false;
6123}
6124
Bob Wilson75f02882010-09-17 22:59:05 +00006125/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6126/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006127static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6128 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006129 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6130 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6131 // into a pair of GPRs, which is fine when the value is used as a scalar,
6132 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006133 SelectionDAG &DAG = DCI.DAG;
6134 if (N->getNumOperands() == 2) {
6135 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6136 if (RV.getNode())
6137 return RV;
6138 }
Bob Wilson75f02882010-09-17 22:59:05 +00006139
Bob Wilson31600902010-12-21 06:43:19 +00006140 // Load i64 elements as f64 values so that type legalization does not split
6141 // them up into i32 values.
6142 EVT VT = N->getValueType(0);
6143 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6144 return SDValue();
6145 DebugLoc dl = N->getDebugLoc();
6146 SmallVector<SDValue, 8> Ops;
6147 unsigned NumElts = VT.getVectorNumElements();
6148 for (unsigned i = 0; i < NumElts; ++i) {
6149 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6150 Ops.push_back(V);
6151 // Make the DAGCombiner fold the bitcast.
6152 DCI.AddToWorklist(V.getNode());
6153 }
6154 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6155 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6156 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6157}
6158
6159/// PerformInsertEltCombine - Target-specific dag combine xforms for
6160/// ISD::INSERT_VECTOR_ELT.
6161static SDValue PerformInsertEltCombine(SDNode *N,
6162 TargetLowering::DAGCombinerInfo &DCI) {
6163 // Bitcast an i64 load inserted into a vector to f64.
6164 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6165 EVT VT = N->getValueType(0);
6166 SDNode *Elt = N->getOperand(1).getNode();
6167 if (VT.getVectorElementType() != MVT::i64 ||
6168 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6169 return SDValue();
6170
6171 SelectionDAG &DAG = DCI.DAG;
6172 DebugLoc dl = N->getDebugLoc();
6173 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6174 VT.getVectorNumElements());
6175 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6176 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6177 // Make the DAGCombiner fold the bitcasts.
6178 DCI.AddToWorklist(Vec.getNode());
6179 DCI.AddToWorklist(V.getNode());
6180 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6181 Vec, V, N->getOperand(2));
6182 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006183}
6184
Bob Wilsonf20700c2010-10-27 20:38:28 +00006185/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6186/// ISD::VECTOR_SHUFFLE.
6187static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6188 // The LLVM shufflevector instruction does not require the shuffle mask
6189 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6190 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6191 // operands do not match the mask length, they are extended by concatenating
6192 // them with undef vectors. That is probably the right thing for other
6193 // targets, but for NEON it is better to concatenate two double-register
6194 // size vector operands into a single quad-register size vector. Do that
6195 // transformation here:
6196 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6197 // shuffle(concat(v1, v2), undef)
6198 SDValue Op0 = N->getOperand(0);
6199 SDValue Op1 = N->getOperand(1);
6200 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6201 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6202 Op0.getNumOperands() != 2 ||
6203 Op1.getNumOperands() != 2)
6204 return SDValue();
6205 SDValue Concat0Op1 = Op0.getOperand(1);
6206 SDValue Concat1Op1 = Op1.getOperand(1);
6207 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6208 Concat1Op1.getOpcode() != ISD::UNDEF)
6209 return SDValue();
6210 // Skip the transformation if any of the types are illegal.
6211 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6212 EVT VT = N->getValueType(0);
6213 if (!TLI.isTypeLegal(VT) ||
6214 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6215 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6216 return SDValue();
6217
6218 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6219 Op0.getOperand(0), Op1.getOperand(0));
6220 // Translate the shuffle mask.
6221 SmallVector<int, 16> NewMask;
6222 unsigned NumElts = VT.getVectorNumElements();
6223 unsigned HalfElts = NumElts/2;
6224 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6225 for (unsigned n = 0; n < NumElts; ++n) {
6226 int MaskElt = SVN->getMaskElt(n);
6227 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006228 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006229 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006230 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006231 NewElt = HalfElts + MaskElt - NumElts;
6232 NewMask.push_back(NewElt);
6233 }
6234 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6235 DAG.getUNDEF(VT), NewMask.data());
6236}
6237
Bob Wilson1c3ef902011-02-07 17:43:21 +00006238/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6239/// NEON load/store intrinsics to merge base address updates.
6240static SDValue CombineBaseUpdate(SDNode *N,
6241 TargetLowering::DAGCombinerInfo &DCI) {
6242 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6243 return SDValue();
6244
6245 SelectionDAG &DAG = DCI.DAG;
6246 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6247 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6248 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6249 SDValue Addr = N->getOperand(AddrOpIdx);
6250
6251 // Search for a use of the address operand that is an increment.
6252 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6253 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6254 SDNode *User = *UI;
6255 if (User->getOpcode() != ISD::ADD ||
6256 UI.getUse().getResNo() != Addr.getResNo())
6257 continue;
6258
6259 // Check that the add is independent of the load/store. Otherwise, folding
6260 // it would create a cycle.
6261 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6262 continue;
6263
6264 // Find the new opcode for the updating load/store.
6265 bool isLoad = true;
6266 bool isLaneOp = false;
6267 unsigned NewOpc = 0;
6268 unsigned NumVecs = 0;
6269 if (isIntrinsic) {
6270 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6271 switch (IntNo) {
6272 default: assert(0 && "unexpected intrinsic for Neon base update");
6273 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6274 NumVecs = 1; break;
6275 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6276 NumVecs = 2; break;
6277 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6278 NumVecs = 3; break;
6279 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6280 NumVecs = 4; break;
6281 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6282 NumVecs = 2; isLaneOp = true; break;
6283 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6284 NumVecs = 3; isLaneOp = true; break;
6285 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6286 NumVecs = 4; isLaneOp = true; break;
6287 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6288 NumVecs = 1; isLoad = false; break;
6289 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6290 NumVecs = 2; isLoad = false; break;
6291 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6292 NumVecs = 3; isLoad = false; break;
6293 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6294 NumVecs = 4; isLoad = false; break;
6295 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6296 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6297 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6298 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6299 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6300 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6301 }
6302 } else {
6303 isLaneOp = true;
6304 switch (N->getOpcode()) {
6305 default: assert(0 && "unexpected opcode for Neon base update");
6306 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6307 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6308 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6309 }
6310 }
6311
6312 // Find the size of memory referenced by the load/store.
6313 EVT VecTy;
6314 if (isLoad)
6315 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006316 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006317 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6318 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6319 if (isLaneOp)
6320 NumBytes /= VecTy.getVectorNumElements();
6321
6322 // If the increment is a constant, it must match the memory ref size.
6323 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6324 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6325 uint64_t IncVal = CInc->getZExtValue();
6326 if (IncVal != NumBytes)
6327 continue;
6328 } else if (NumBytes >= 3 * 16) {
6329 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6330 // separate instructions that make it harder to use a non-constant update.
6331 continue;
6332 }
6333
6334 // Create the new updating load/store node.
6335 EVT Tys[6];
6336 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6337 unsigned n;
6338 for (n = 0; n < NumResultVecs; ++n)
6339 Tys[n] = VecTy;
6340 Tys[n++] = MVT::i32;
6341 Tys[n] = MVT::Other;
6342 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6343 SmallVector<SDValue, 8> Ops;
6344 Ops.push_back(N->getOperand(0)); // incoming chain
6345 Ops.push_back(N->getOperand(AddrOpIdx));
6346 Ops.push_back(Inc);
6347 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6348 Ops.push_back(N->getOperand(i));
6349 }
6350 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6351 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6352 Ops.data(), Ops.size(),
6353 MemInt->getMemoryVT(),
6354 MemInt->getMemOperand());
6355
6356 // Update the uses.
6357 std::vector<SDValue> NewResults;
6358 for (unsigned i = 0; i < NumResultVecs; ++i) {
6359 NewResults.push_back(SDValue(UpdN.getNode(), i));
6360 }
6361 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6362 DCI.CombineTo(N, NewResults);
6363 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6364
6365 break;
Owen Anderson76706012011-04-05 21:48:57 +00006366 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006367 return SDValue();
6368}
6369
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006370/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6371/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6372/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6373/// return true.
6374static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6375 SelectionDAG &DAG = DCI.DAG;
6376 EVT VT = N->getValueType(0);
6377 // vldN-dup instructions only support 64-bit vectors for N > 1.
6378 if (!VT.is64BitVector())
6379 return false;
6380
6381 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6382 SDNode *VLD = N->getOperand(0).getNode();
6383 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6384 return false;
6385 unsigned NumVecs = 0;
6386 unsigned NewOpc = 0;
6387 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6388 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6389 NumVecs = 2;
6390 NewOpc = ARMISD::VLD2DUP;
6391 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6392 NumVecs = 3;
6393 NewOpc = ARMISD::VLD3DUP;
6394 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6395 NumVecs = 4;
6396 NewOpc = ARMISD::VLD4DUP;
6397 } else {
6398 return false;
6399 }
6400
6401 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6402 // numbers match the load.
6403 unsigned VLDLaneNo =
6404 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6405 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6406 UI != UE; ++UI) {
6407 // Ignore uses of the chain result.
6408 if (UI.getUse().getResNo() == NumVecs)
6409 continue;
6410 SDNode *User = *UI;
6411 if (User->getOpcode() != ARMISD::VDUPLANE ||
6412 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6413 return false;
6414 }
6415
6416 // Create the vldN-dup node.
6417 EVT Tys[5];
6418 unsigned n;
6419 for (n = 0; n < NumVecs; ++n)
6420 Tys[n] = VT;
6421 Tys[n] = MVT::Other;
6422 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6423 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6424 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6425 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6426 Ops, 2, VLDMemInt->getMemoryVT(),
6427 VLDMemInt->getMemOperand());
6428
6429 // Update the uses.
6430 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6431 UI != UE; ++UI) {
6432 unsigned ResNo = UI.getUse().getResNo();
6433 // Ignore uses of the chain result.
6434 if (ResNo == NumVecs)
6435 continue;
6436 SDNode *User = *UI;
6437 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6438 }
6439
6440 // Now the vldN-lane intrinsic is dead except for its chain result.
6441 // Update uses of the chain.
6442 std::vector<SDValue> VLDDupResults;
6443 for (unsigned n = 0; n < NumVecs; ++n)
6444 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6445 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6446 DCI.CombineTo(VLD, VLDDupResults);
6447
6448 return true;
6449}
6450
Bob Wilson9e82bf12010-07-14 01:22:12 +00006451/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6452/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006453static SDValue PerformVDUPLANECombine(SDNode *N,
6454 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006455 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006456
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006457 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6458 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6459 if (CombineVLDDUP(N, DCI))
6460 return SDValue(N, 0);
6461
6462 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6463 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006464 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006465 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006466 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006467 return SDValue();
6468
6469 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6470 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6471 // The canonical VMOV for a zero vector uses a 32-bit element size.
6472 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6473 unsigned EltBits;
6474 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6475 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006476 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006477 if (EltSize > VT.getVectorElementType().getSizeInBits())
6478 return SDValue();
6479
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006480 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006481}
6482
Bob Wilson5bafff32009-06-22 23:27:02 +00006483/// getVShiftImm - Check if this is a valid build_vector for the immediate
6484/// operand of a vector shift operation, where all the elements of the
6485/// build_vector must have the same constant integer value.
6486static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6487 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006488 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006489 Op = Op.getOperand(0);
6490 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6491 APInt SplatBits, SplatUndef;
6492 unsigned SplatBitSize;
6493 bool HasAnyUndefs;
6494 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6495 HasAnyUndefs, ElementBits) ||
6496 SplatBitSize > ElementBits)
6497 return false;
6498 Cnt = SplatBits.getSExtValue();
6499 return true;
6500}
6501
6502/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6503/// operand of a vector shift left operation. That value must be in the range:
6504/// 0 <= Value < ElementBits for a left shift; or
6505/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006506static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006507 assert(VT.isVector() && "vector shift count is not a vector type");
6508 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6509 if (! getVShiftImm(Op, ElementBits, Cnt))
6510 return false;
6511 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6512}
6513
6514/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6515/// operand of a vector shift right operation. For a shift opcode, the value
6516/// is positive, but for an intrinsic the value count must be negative. The
6517/// absolute value must be in the range:
6518/// 1 <= |Value| <= ElementBits for a right shift; or
6519/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006520static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006521 int64_t &Cnt) {
6522 assert(VT.isVector() && "vector shift count is not a vector type");
6523 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6524 if (! getVShiftImm(Op, ElementBits, Cnt))
6525 return false;
6526 if (isIntrinsic)
6527 Cnt = -Cnt;
6528 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6529}
6530
6531/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6532static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6533 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6534 switch (IntNo) {
6535 default:
6536 // Don't do anything for most intrinsics.
6537 break;
6538
6539 // Vector shifts: check for immediate versions and lower them.
6540 // Note: This is done during DAG combining instead of DAG legalizing because
6541 // the build_vectors for 64-bit vector element shift counts are generally
6542 // not legal, and it is hard to see their values after they get legalized to
6543 // loads from a constant pool.
6544 case Intrinsic::arm_neon_vshifts:
6545 case Intrinsic::arm_neon_vshiftu:
6546 case Intrinsic::arm_neon_vshiftls:
6547 case Intrinsic::arm_neon_vshiftlu:
6548 case Intrinsic::arm_neon_vshiftn:
6549 case Intrinsic::arm_neon_vrshifts:
6550 case Intrinsic::arm_neon_vrshiftu:
6551 case Intrinsic::arm_neon_vrshiftn:
6552 case Intrinsic::arm_neon_vqshifts:
6553 case Intrinsic::arm_neon_vqshiftu:
6554 case Intrinsic::arm_neon_vqshiftsu:
6555 case Intrinsic::arm_neon_vqshiftns:
6556 case Intrinsic::arm_neon_vqshiftnu:
6557 case Intrinsic::arm_neon_vqshiftnsu:
6558 case Intrinsic::arm_neon_vqrshiftns:
6559 case Intrinsic::arm_neon_vqrshiftnu:
6560 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006561 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006562 int64_t Cnt;
6563 unsigned VShiftOpc = 0;
6564
6565 switch (IntNo) {
6566 case Intrinsic::arm_neon_vshifts:
6567 case Intrinsic::arm_neon_vshiftu:
6568 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6569 VShiftOpc = ARMISD::VSHL;
6570 break;
6571 }
6572 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6573 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6574 ARMISD::VSHRs : ARMISD::VSHRu);
6575 break;
6576 }
6577 return SDValue();
6578
6579 case Intrinsic::arm_neon_vshiftls:
6580 case Intrinsic::arm_neon_vshiftlu:
6581 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6582 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006583 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006584
6585 case Intrinsic::arm_neon_vrshifts:
6586 case Intrinsic::arm_neon_vrshiftu:
6587 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6588 break;
6589 return SDValue();
6590
6591 case Intrinsic::arm_neon_vqshifts:
6592 case Intrinsic::arm_neon_vqshiftu:
6593 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6594 break;
6595 return SDValue();
6596
6597 case Intrinsic::arm_neon_vqshiftsu:
6598 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6599 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006600 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006601
6602 case Intrinsic::arm_neon_vshiftn:
6603 case Intrinsic::arm_neon_vrshiftn:
6604 case Intrinsic::arm_neon_vqshiftns:
6605 case Intrinsic::arm_neon_vqshiftnu:
6606 case Intrinsic::arm_neon_vqshiftnsu:
6607 case Intrinsic::arm_neon_vqrshiftns:
6608 case Intrinsic::arm_neon_vqrshiftnu:
6609 case Intrinsic::arm_neon_vqrshiftnsu:
6610 // Narrowing shifts require an immediate right shift.
6611 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6612 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006613 llvm_unreachable("invalid shift count for narrowing vector shift "
6614 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006615
6616 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006617 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006618 }
6619
6620 switch (IntNo) {
6621 case Intrinsic::arm_neon_vshifts:
6622 case Intrinsic::arm_neon_vshiftu:
6623 // Opcode already set above.
6624 break;
6625 case Intrinsic::arm_neon_vshiftls:
6626 case Intrinsic::arm_neon_vshiftlu:
6627 if (Cnt == VT.getVectorElementType().getSizeInBits())
6628 VShiftOpc = ARMISD::VSHLLi;
6629 else
6630 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6631 ARMISD::VSHLLs : ARMISD::VSHLLu);
6632 break;
6633 case Intrinsic::arm_neon_vshiftn:
6634 VShiftOpc = ARMISD::VSHRN; break;
6635 case Intrinsic::arm_neon_vrshifts:
6636 VShiftOpc = ARMISD::VRSHRs; break;
6637 case Intrinsic::arm_neon_vrshiftu:
6638 VShiftOpc = ARMISD::VRSHRu; break;
6639 case Intrinsic::arm_neon_vrshiftn:
6640 VShiftOpc = ARMISD::VRSHRN; break;
6641 case Intrinsic::arm_neon_vqshifts:
6642 VShiftOpc = ARMISD::VQSHLs; break;
6643 case Intrinsic::arm_neon_vqshiftu:
6644 VShiftOpc = ARMISD::VQSHLu; break;
6645 case Intrinsic::arm_neon_vqshiftsu:
6646 VShiftOpc = ARMISD::VQSHLsu; break;
6647 case Intrinsic::arm_neon_vqshiftns:
6648 VShiftOpc = ARMISD::VQSHRNs; break;
6649 case Intrinsic::arm_neon_vqshiftnu:
6650 VShiftOpc = ARMISD::VQSHRNu; break;
6651 case Intrinsic::arm_neon_vqshiftnsu:
6652 VShiftOpc = ARMISD::VQSHRNsu; break;
6653 case Intrinsic::arm_neon_vqrshiftns:
6654 VShiftOpc = ARMISD::VQRSHRNs; break;
6655 case Intrinsic::arm_neon_vqrshiftnu:
6656 VShiftOpc = ARMISD::VQRSHRNu; break;
6657 case Intrinsic::arm_neon_vqrshiftnsu:
6658 VShiftOpc = ARMISD::VQRSHRNsu; break;
6659 }
6660
6661 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006662 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006663 }
6664
6665 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006666 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006667 int64_t Cnt;
6668 unsigned VShiftOpc = 0;
6669
6670 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6671 VShiftOpc = ARMISD::VSLI;
6672 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6673 VShiftOpc = ARMISD::VSRI;
6674 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006675 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006676 }
6677
6678 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6679 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006680 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006681 }
6682
6683 case Intrinsic::arm_neon_vqrshifts:
6684 case Intrinsic::arm_neon_vqrshiftu:
6685 // No immediate versions of these to check for.
6686 break;
6687 }
6688
6689 return SDValue();
6690}
6691
6692/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6693/// lowers them. As with the vector shift intrinsics, this is done during DAG
6694/// combining instead of DAG legalizing because the build_vectors for 64-bit
6695/// vector element shift counts are generally not legal, and it is hard to see
6696/// their values after they get legalized to loads from a constant pool.
6697static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6698 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006699 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006700
6701 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006702 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6703 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006704 return SDValue();
6705
6706 assert(ST->hasNEON() && "unexpected vector shift");
6707 int64_t Cnt;
6708
6709 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006710 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006711
6712 case ISD::SHL:
6713 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6714 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006715 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006716 break;
6717
6718 case ISD::SRA:
6719 case ISD::SRL:
6720 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6721 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6722 ARMISD::VSHRs : ARMISD::VSHRu);
6723 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006724 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006725 }
6726 }
6727 return SDValue();
6728}
6729
6730/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6731/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6732static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6733 const ARMSubtarget *ST) {
6734 SDValue N0 = N->getOperand(0);
6735
6736 // Check for sign- and zero-extensions of vector extract operations of 8-
6737 // and 16-bit vector elements. NEON supports these directly. They are
6738 // handled during DAG combining because type legalization will promote them
6739 // to 32-bit types and it is messy to recognize the operations after that.
6740 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6741 SDValue Vec = N0.getOperand(0);
6742 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006743 EVT VT = N->getValueType(0);
6744 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006745 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6746
Owen Anderson825b72b2009-08-11 20:47:22 +00006747 if (VT == MVT::i32 &&
6748 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006749 TLI.isTypeLegal(Vec.getValueType()) &&
6750 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006751
6752 unsigned Opc = 0;
6753 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006754 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006755 case ISD::SIGN_EXTEND:
6756 Opc = ARMISD::VGETLANEs;
6757 break;
6758 case ISD::ZERO_EXTEND:
6759 case ISD::ANY_EXTEND:
6760 Opc = ARMISD::VGETLANEu;
6761 break;
6762 }
6763 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6764 }
6765 }
6766
6767 return SDValue();
6768}
6769
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006770/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6771/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6772static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6773 const ARMSubtarget *ST) {
6774 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006775 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006776 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6777 // a NaN; only do the transformation when it matches that behavior.
6778
6779 // For now only do this when using NEON for FP operations; if using VFP, it
6780 // is not obvious that the benefit outweighs the cost of switching to the
6781 // NEON pipeline.
6782 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6783 N->getValueType(0) != MVT::f32)
6784 return SDValue();
6785
6786 SDValue CondLHS = N->getOperand(0);
6787 SDValue CondRHS = N->getOperand(1);
6788 SDValue LHS = N->getOperand(2);
6789 SDValue RHS = N->getOperand(3);
6790 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6791
6792 unsigned Opcode = 0;
6793 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006794 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006795 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006796 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006797 IsReversed = true ; // x CC y ? y : x
6798 } else {
6799 return SDValue();
6800 }
6801
Bob Wilsone742bb52010-02-24 22:15:53 +00006802 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006803 switch (CC) {
6804 default: break;
6805 case ISD::SETOLT:
6806 case ISD::SETOLE:
6807 case ISD::SETLT:
6808 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006809 case ISD::SETULT:
6810 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006811 // If LHS is NaN, an ordered comparison will be false and the result will
6812 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6813 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6814 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6815 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6816 break;
6817 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6818 // will return -0, so vmin can only be used for unsafe math or if one of
6819 // the operands is known to be nonzero.
6820 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6821 !UnsafeFPMath &&
6822 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6823 break;
6824 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006825 break;
6826
6827 case ISD::SETOGT:
6828 case ISD::SETOGE:
6829 case ISD::SETGT:
6830 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006831 case ISD::SETUGT:
6832 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006833 // If LHS is NaN, an ordered comparison will be false and the result will
6834 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6835 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6836 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6837 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6838 break;
6839 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6840 // will return +0, so vmax can only be used for unsafe math or if one of
6841 // the operands is known to be nonzero.
6842 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6843 !UnsafeFPMath &&
6844 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6845 break;
6846 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006847 break;
6848 }
6849
6850 if (!Opcode)
6851 return SDValue();
6852 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6853}
6854
Dan Gohman475871a2008-07-27 21:46:04 +00006855SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006856 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006857 switch (N->getOpcode()) {
6858 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00006859 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006860 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006861 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006862 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006863 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006864 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006865 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006866 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006867 case ISD::STORE: return PerformSTORECombine(N, DCI);
6868 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6869 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006870 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006871 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006872 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006873 case ISD::SHL:
6874 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006875 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006876 case ISD::SIGN_EXTEND:
6877 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006878 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6879 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006880 case ARMISD::VLD2DUP:
6881 case ARMISD::VLD3DUP:
6882 case ARMISD::VLD4DUP:
6883 return CombineBaseUpdate(N, DCI);
6884 case ISD::INTRINSIC_VOID:
6885 case ISD::INTRINSIC_W_CHAIN:
6886 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6887 case Intrinsic::arm_neon_vld1:
6888 case Intrinsic::arm_neon_vld2:
6889 case Intrinsic::arm_neon_vld3:
6890 case Intrinsic::arm_neon_vld4:
6891 case Intrinsic::arm_neon_vld2lane:
6892 case Intrinsic::arm_neon_vld3lane:
6893 case Intrinsic::arm_neon_vld4lane:
6894 case Intrinsic::arm_neon_vst1:
6895 case Intrinsic::arm_neon_vst2:
6896 case Intrinsic::arm_neon_vst3:
6897 case Intrinsic::arm_neon_vst4:
6898 case Intrinsic::arm_neon_vst2lane:
6899 case Intrinsic::arm_neon_vst3lane:
6900 case Intrinsic::arm_neon_vst4lane:
6901 return CombineBaseUpdate(N, DCI);
6902 default: break;
6903 }
6904 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006905 }
Dan Gohman475871a2008-07-27 21:46:04 +00006906 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006907}
6908
Evan Cheng31959b12011-02-02 01:06:55 +00006909bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6910 EVT VT) const {
6911 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6912}
6913
Bill Wendlingaf566342009-08-15 21:21:19 +00006914bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006915 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006916 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006917
6918 switch (VT.getSimpleVT().SimpleTy) {
6919 default:
6920 return false;
6921 case MVT::i8:
6922 case MVT::i16:
6923 case MVT::i32:
6924 return true;
6925 // FIXME: VLD1 etc with standard alignment is legal.
6926 }
6927}
6928
Evan Chenge6c835f2009-08-14 20:09:37 +00006929static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6930 if (V < 0)
6931 return false;
6932
6933 unsigned Scale = 1;
6934 switch (VT.getSimpleVT().SimpleTy) {
6935 default: return false;
6936 case MVT::i1:
6937 case MVT::i8:
6938 // Scale == 1;
6939 break;
6940 case MVT::i16:
6941 // Scale == 2;
6942 Scale = 2;
6943 break;
6944 case MVT::i32:
6945 // Scale == 4;
6946 Scale = 4;
6947 break;
6948 }
6949
6950 if ((V & (Scale - 1)) != 0)
6951 return false;
6952 V /= Scale;
6953 return V == (V & ((1LL << 5) - 1));
6954}
6955
6956static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6957 const ARMSubtarget *Subtarget) {
6958 bool isNeg = false;
6959 if (V < 0) {
6960 isNeg = true;
6961 V = - V;
6962 }
6963
6964 switch (VT.getSimpleVT().SimpleTy) {
6965 default: return false;
6966 case MVT::i1:
6967 case MVT::i8:
6968 case MVT::i16:
6969 case MVT::i32:
6970 // + imm12 or - imm8
6971 if (isNeg)
6972 return V == (V & ((1LL << 8) - 1));
6973 return V == (V & ((1LL << 12) - 1));
6974 case MVT::f32:
6975 case MVT::f64:
6976 // Same as ARM mode. FIXME: NEON?
6977 if (!Subtarget->hasVFP2())
6978 return false;
6979 if ((V & 3) != 0)
6980 return false;
6981 V >>= 2;
6982 return V == (V & ((1LL << 8) - 1));
6983 }
6984}
6985
Evan Chengb01fad62007-03-12 23:30:29 +00006986/// isLegalAddressImmediate - Return true if the integer value can be used
6987/// as the offset of the target addressing mode for load / store of the
6988/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006989static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006990 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006991 if (V == 0)
6992 return true;
6993
Evan Cheng65011532009-03-09 19:15:00 +00006994 if (!VT.isSimple())
6995 return false;
6996
Evan Chenge6c835f2009-08-14 20:09:37 +00006997 if (Subtarget->isThumb1Only())
6998 return isLegalT1AddressImmediate(V, VT);
6999 else if (Subtarget->isThumb2())
7000 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007001
Evan Chenge6c835f2009-08-14 20:09:37 +00007002 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007003 if (V < 0)
7004 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007005 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007006 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007007 case MVT::i1:
7008 case MVT::i8:
7009 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007010 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007011 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007012 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007013 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007014 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007015 case MVT::f32:
7016 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007017 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007018 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007019 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007020 return false;
7021 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007022 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007023 }
Evan Chenga8e29892007-01-19 07:51:42 +00007024}
7025
Evan Chenge6c835f2009-08-14 20:09:37 +00007026bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7027 EVT VT) const {
7028 int Scale = AM.Scale;
7029 if (Scale < 0)
7030 return false;
7031
7032 switch (VT.getSimpleVT().SimpleTy) {
7033 default: return false;
7034 case MVT::i1:
7035 case MVT::i8:
7036 case MVT::i16:
7037 case MVT::i32:
7038 if (Scale == 1)
7039 return true;
7040 // r + r << imm
7041 Scale = Scale & ~1;
7042 return Scale == 2 || Scale == 4 || Scale == 8;
7043 case MVT::i64:
7044 // r + r
7045 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7046 return true;
7047 return false;
7048 case MVT::isVoid:
7049 // Note, we allow "void" uses (basically, uses that aren't loads or
7050 // stores), because arm allows folding a scale into many arithmetic
7051 // operations. This should be made more precise and revisited later.
7052
7053 // Allow r << imm, but the imm has to be a multiple of two.
7054 if (Scale & 1) return false;
7055 return isPowerOf2_32(Scale);
7056 }
7057}
7058
Chris Lattner37caf8c2007-04-09 23:33:39 +00007059/// isLegalAddressingMode - Return true if the addressing mode represented
7060/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007061bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007062 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007063 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007064 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007065 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007066
Chris Lattner37caf8c2007-04-09 23:33:39 +00007067 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007068 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007069 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007070
Chris Lattner37caf8c2007-04-09 23:33:39 +00007071 switch (AM.Scale) {
7072 case 0: // no scale reg, must be "r+i" or "r", or "i".
7073 break;
7074 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007075 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007076 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007077 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007078 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007079 // ARM doesn't support any R+R*scale+imm addr modes.
7080 if (AM.BaseOffs)
7081 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007082
Bob Wilson2c7dab12009-04-08 17:55:28 +00007083 if (!VT.isSimple())
7084 return false;
7085
Evan Chenge6c835f2009-08-14 20:09:37 +00007086 if (Subtarget->isThumb2())
7087 return isLegalT2ScaledAddressingMode(AM, VT);
7088
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007089 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007090 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007091 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007092 case MVT::i1:
7093 case MVT::i8:
7094 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007095 if (Scale < 0) Scale = -Scale;
7096 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007097 return true;
7098 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007099 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007100 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007101 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007102 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007103 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007104 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007105 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007106
Owen Anderson825b72b2009-08-11 20:47:22 +00007107 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007108 // Note, we allow "void" uses (basically, uses that aren't loads or
7109 // stores), because arm allows folding a scale into many arithmetic
7110 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007111
Chris Lattner37caf8c2007-04-09 23:33:39 +00007112 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007113 if (Scale & 1) return false;
7114 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007115 }
7116 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007117 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007118 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007119}
7120
Evan Cheng77e47512009-11-11 19:05:52 +00007121/// isLegalICmpImmediate - Return true if the specified immediate is legal
7122/// icmp immediate, that is the target has icmp instructions which can compare
7123/// a register against the immediate without having to materialize the
7124/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007125bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007126 if (!Subtarget->isThumb())
7127 return ARM_AM::getSOImmVal(Imm) != -1;
7128 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007129 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007130 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007131}
7132
Dan Gohmancca82142011-05-03 00:46:49 +00007133/// isLegalAddImmediate - Return true if the specified immediate is legal
7134/// add immediate, that is the target has add instructions which can add
7135/// a register with the immediate without having to materialize the
7136/// immediate into a register.
7137bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7138 return ARM_AM::getSOImmVal(Imm) != -1;
7139}
7140
Owen Andersone50ed302009-08-10 22:56:29 +00007141static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007142 bool isSEXTLoad, SDValue &Base,
7143 SDValue &Offset, bool &isInc,
7144 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007145 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7146 return false;
7147
Owen Anderson825b72b2009-08-11 20:47:22 +00007148 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007149 // AddressingMode 3
7150 Base = Ptr->getOperand(0);
7151 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007152 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007153 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007154 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007155 isInc = false;
7156 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7157 return true;
7158 }
7159 }
7160 isInc = (Ptr->getOpcode() == ISD::ADD);
7161 Offset = Ptr->getOperand(1);
7162 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007163 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007164 // AddressingMode 2
7165 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007166 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007167 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007168 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007169 isInc = false;
7170 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7171 Base = Ptr->getOperand(0);
7172 return true;
7173 }
7174 }
7175
7176 if (Ptr->getOpcode() == ISD::ADD) {
7177 isInc = true;
7178 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
7179 if (ShOpcVal != ARM_AM::no_shift) {
7180 Base = Ptr->getOperand(1);
7181 Offset = Ptr->getOperand(0);
7182 } else {
7183 Base = Ptr->getOperand(0);
7184 Offset = Ptr->getOperand(1);
7185 }
7186 return true;
7187 }
7188
7189 isInc = (Ptr->getOpcode() == ISD::ADD);
7190 Base = Ptr->getOperand(0);
7191 Offset = Ptr->getOperand(1);
7192 return true;
7193 }
7194
Jim Grosbache5165492009-11-09 00:11:35 +00007195 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007196 return false;
7197}
7198
Owen Andersone50ed302009-08-10 22:56:29 +00007199static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007200 bool isSEXTLoad, SDValue &Base,
7201 SDValue &Offset, bool &isInc,
7202 SelectionDAG &DAG) {
7203 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7204 return false;
7205
7206 Base = Ptr->getOperand(0);
7207 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7208 int RHSC = (int)RHS->getZExtValue();
7209 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7210 assert(Ptr->getOpcode() == ISD::ADD);
7211 isInc = false;
7212 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7213 return true;
7214 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7215 isInc = Ptr->getOpcode() == ISD::ADD;
7216 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7217 return true;
7218 }
7219 }
7220
7221 return false;
7222}
7223
Evan Chenga8e29892007-01-19 07:51:42 +00007224/// getPreIndexedAddressParts - returns true by value, base pointer and
7225/// offset pointer and addressing mode by reference if the node's address
7226/// can be legally represented as pre-indexed load / store address.
7227bool
Dan Gohman475871a2008-07-27 21:46:04 +00007228ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7229 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007230 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007231 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007232 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007233 return false;
7234
Owen Andersone50ed302009-08-10 22:56:29 +00007235 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007236 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007237 bool isSEXTLoad = false;
7238 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7239 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007240 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007241 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7242 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7243 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007244 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007245 } else
7246 return false;
7247
7248 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007249 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007250 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007251 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7252 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007253 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007254 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007255 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007256 if (!isLegal)
7257 return false;
7258
7259 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7260 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007261}
7262
7263/// getPostIndexedAddressParts - returns true by value, base pointer and
7264/// offset pointer and addressing mode by reference if this node can be
7265/// combined with a load / store to form a post-indexed load / store.
7266bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007267 SDValue &Base,
7268 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007269 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007270 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007271 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007272 return false;
7273
Owen Andersone50ed302009-08-10 22:56:29 +00007274 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007275 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007276 bool isSEXTLoad = false;
7277 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007278 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007279 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007280 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7281 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007282 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007283 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007284 } else
7285 return false;
7286
7287 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007288 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007289 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007290 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007291 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007292 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007293 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7294 isInc, DAG);
7295 if (!isLegal)
7296 return false;
7297
Evan Cheng28dad2a2010-05-18 21:31:17 +00007298 if (Ptr != Base) {
7299 // Swap base ptr and offset to catch more post-index load / store when
7300 // it's legal. In Thumb2 mode, offset must be an immediate.
7301 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7302 !Subtarget->isThumb2())
7303 std::swap(Base, Offset);
7304
7305 // Post-indexed load / store update the base pointer.
7306 if (Ptr != Base)
7307 return false;
7308 }
7309
Evan Chenge88d5ce2009-07-02 07:28:31 +00007310 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7311 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007312}
7313
Dan Gohman475871a2008-07-27 21:46:04 +00007314void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007315 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007316 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007317 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007318 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007319 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007320 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007321 switch (Op.getOpcode()) {
7322 default: break;
7323 case ARMISD::CMOV: {
7324 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007325 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007326 if (KnownZero == 0 && KnownOne == 0) return;
7327
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007328 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007329 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7330 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007331 KnownZero &= KnownZeroRHS;
7332 KnownOne &= KnownOneRHS;
7333 return;
7334 }
7335 }
7336}
7337
7338//===----------------------------------------------------------------------===//
7339// ARM Inline Assembly Support
7340//===----------------------------------------------------------------------===//
7341
Evan Cheng55d42002011-01-08 01:24:27 +00007342bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7343 // Looking for "rev" which is V6+.
7344 if (!Subtarget->hasV6Ops())
7345 return false;
7346
7347 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7348 std::string AsmStr = IA->getAsmString();
7349 SmallVector<StringRef, 4> AsmPieces;
7350 SplitString(AsmStr, AsmPieces, ";\n");
7351
7352 switch (AsmPieces.size()) {
7353 default: return false;
7354 case 1:
7355 AsmStr = AsmPieces[0];
7356 AsmPieces.clear();
7357 SplitString(AsmStr, AsmPieces, " \t,");
7358
7359 // rev $0, $1
7360 if (AsmPieces.size() == 3 &&
7361 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7362 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7363 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7364 if (Ty && Ty->getBitWidth() == 32)
7365 return IntrinsicLowering::LowerToByteSwap(CI);
7366 }
7367 break;
7368 }
7369
7370 return false;
7371}
7372
Evan Chenga8e29892007-01-19 07:51:42 +00007373/// getConstraintType - Given a constraint letter, return the type of
7374/// constraint it is for this target.
7375ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007376ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7377 if (Constraint.size() == 1) {
7378 switch (Constraint[0]) {
7379 default: break;
7380 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007381 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00007382 }
Eric Christophercf714d42011-06-03 17:24:37 +00007383 } else {
7384 if (Constraint == "Uv")
7385 return C_Memory;
Evan Chenga8e29892007-01-19 07:51:42 +00007386 }
Chris Lattner4234f572007-03-25 02:14:49 +00007387 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007388}
7389
John Thompson44ab89e2010-10-29 17:29:13 +00007390/// Examine constraint type and operand type and determine a weight value.
7391/// This object must already have been set up with the operand type
7392/// and the current alternative constraint selected.
7393TargetLowering::ConstraintWeight
7394ARMTargetLowering::getSingleConstraintMatchWeight(
7395 AsmOperandInfo &info, const char *constraint) const {
7396 ConstraintWeight weight = CW_Invalid;
7397 Value *CallOperandVal = info.CallOperandVal;
7398 // If we don't have a value, we can't do a match,
7399 // but allow it at the lowest weight.
7400 if (CallOperandVal == NULL)
7401 return CW_Default;
7402 const Type *type = CallOperandVal->getType();
7403 // Look at the constraint type.
7404 switch (*constraint) {
7405 default:
7406 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7407 break;
7408 case 'l':
7409 if (type->isIntegerTy()) {
7410 if (Subtarget->isThumb())
7411 weight = CW_SpecificReg;
7412 else
7413 weight = CW_Register;
7414 }
7415 break;
7416 case 'w':
7417 if (type->isFloatingPointTy())
7418 weight = CW_Register;
7419 break;
7420 }
7421 return weight;
7422}
7423
Bob Wilson2dc4f542009-03-20 22:42:55 +00007424std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00007425ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007426 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007427 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007428 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007429 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007430 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007431 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007432 return std::make_pair(0U, ARM::tGPRRegisterClass);
7433 else
7434 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007435 case 'r':
7436 return std::make_pair(0U, ARM::GPRRegisterClass);
7437 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007438 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007439 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007440 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007441 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007442 if (VT.getSizeInBits() == 128)
7443 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007444 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007445 }
7446 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007447 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007448 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007449
Evan Chenga8e29892007-01-19 07:51:42 +00007450 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7451}
7452
7453std::vector<unsigned> ARMTargetLowering::
7454getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007455 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007456 if (Constraint.size() != 1)
7457 return std::vector<unsigned>();
7458
7459 switch (Constraint[0]) { // GCC ARM Constraint Letters
7460 default: break;
7461 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007462 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7463 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7464 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007465 case 'r':
7466 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7467 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7468 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
7469 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007470 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007471 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007472 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
7473 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
7474 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
7475 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
7476 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
7477 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
7478 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
7479 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00007480 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007481 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
7482 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
7483 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
7484 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00007485 if (VT.getSizeInBits() == 128)
7486 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
7487 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007488 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007489 }
7490
7491 return std::vector<unsigned>();
7492}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007493
7494/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7495/// vector. If it is invalid, don't add anything to Ops.
7496void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007497 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007498 std::vector<SDValue>&Ops,
7499 SelectionDAG &DAG) const {
7500 SDValue Result(0, 0);
7501
Eric Christopher100c8332011-06-02 23:16:42 +00007502 // Currently only support length 1 constraints.
7503 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007504
Eric Christopher100c8332011-06-02 23:16:42 +00007505 char ConstraintLetter = Constraint[0];
7506 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007507 default: break;
7508 case 'I': case 'J': case 'K': case 'L':
7509 case 'M': case 'N': case 'O':
7510 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7511 if (!C)
7512 return;
7513
7514 int64_t CVal64 = C->getSExtValue();
7515 int CVal = (int) CVal64;
7516 // None of these constraints allow values larger than 32 bits. Check
7517 // that the value fits in an int.
7518 if (CVal != CVal64)
7519 return;
7520
Eric Christopher100c8332011-06-02 23:16:42 +00007521 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007522 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007523 if (Subtarget->isThumb1Only()) {
7524 // This must be a constant between 0 and 255, for ADD
7525 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007526 if (CVal >= 0 && CVal <= 255)
7527 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007528 } else if (Subtarget->isThumb2()) {
7529 // A constant that can be used as an immediate value in a
7530 // data-processing instruction.
7531 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7532 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007533 } else {
7534 // A constant that can be used as an immediate value in a
7535 // data-processing instruction.
7536 if (ARM_AM::getSOImmVal(CVal) != -1)
7537 break;
7538 }
7539 return;
7540
7541 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007542 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007543 // This must be a constant between -255 and -1, for negated ADD
7544 // immediates. This can be used in GCC with an "n" modifier that
7545 // prints the negated value, for use with SUB instructions. It is
7546 // not useful otherwise but is implemented for compatibility.
7547 if (CVal >= -255 && CVal <= -1)
7548 break;
7549 } else {
7550 // This must be a constant between -4095 and 4095. It is not clear
7551 // what this constraint is intended for. Implemented for
7552 // compatibility with GCC.
7553 if (CVal >= -4095 && CVal <= 4095)
7554 break;
7555 }
7556 return;
7557
7558 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007559 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007560 // A 32-bit value where only one byte has a nonzero value. Exclude
7561 // zero to match GCC. This constraint is used by GCC internally for
7562 // constants that can be loaded with a move/shift combination.
7563 // It is not useful otherwise but is implemented for compatibility.
7564 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7565 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007566 } else if (Subtarget->isThumb2()) {
7567 // A constant whose bitwise inverse can be used as an immediate
7568 // value in a data-processing instruction. This can be used in GCC
7569 // with a "B" modifier that prints the inverted value, for use with
7570 // BIC and MVN instructions. It is not useful otherwise but is
7571 // implemented for compatibility.
7572 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7573 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007574 } else {
7575 // A constant whose bitwise inverse can be used as an immediate
7576 // value in a data-processing instruction. This can be used in GCC
7577 // with a "B" modifier that prints the inverted value, for use with
7578 // BIC and MVN instructions. It is not useful otherwise but is
7579 // implemented for compatibility.
7580 if (ARM_AM::getSOImmVal(~CVal) != -1)
7581 break;
7582 }
7583 return;
7584
7585 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007586 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007587 // This must be a constant between -7 and 7,
7588 // for 3-operand ADD/SUB immediate instructions.
7589 if (CVal >= -7 && CVal < 7)
7590 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007591 } else if (Subtarget->isThumb2()) {
7592 // A constant whose negation can be used as an immediate value in a
7593 // data-processing instruction. This can be used in GCC with an "n"
7594 // modifier that prints the negated value, for use with SUB
7595 // instructions. It is not useful otherwise but is implemented for
7596 // compatibility.
7597 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7598 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007599 } else {
7600 // A constant whose negation can be used as an immediate value in a
7601 // data-processing instruction. This can be used in GCC with an "n"
7602 // modifier that prints the negated value, for use with SUB
7603 // instructions. It is not useful otherwise but is implemented for
7604 // compatibility.
7605 if (ARM_AM::getSOImmVal(-CVal) != -1)
7606 break;
7607 }
7608 return;
7609
7610 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007611 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007612 // This must be a multiple of 4 between 0 and 1020, for
7613 // ADD sp + immediate.
7614 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7615 break;
7616 } else {
7617 // A power of two or a constant between 0 and 32. This is used in
7618 // GCC for the shift amount on shifted register operands, but it is
7619 // useful in general for any shift amounts.
7620 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7621 break;
7622 }
7623 return;
7624
7625 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007626 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007627 // This must be a constant between 0 and 31, for shift amounts.
7628 if (CVal >= 0 && CVal <= 31)
7629 break;
7630 }
7631 return;
7632
7633 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007634 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007635 // This must be a multiple of 4 between -508 and 508, for
7636 // ADD/SUB sp = sp + immediate.
7637 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7638 break;
7639 }
7640 return;
7641 }
7642 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7643 break;
7644 }
7645
7646 if (Result.getNode()) {
7647 Ops.push_back(Result);
7648 return;
7649 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007650 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007651}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007652
7653bool
7654ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7655 // The ARM target isn't yet aware of offsets.
7656 return false;
7657}
Evan Cheng39382422009-10-28 01:44:26 +00007658
7659int ARM::getVFPf32Imm(const APFloat &FPImm) {
7660 APInt Imm = FPImm.bitcastToAPInt();
7661 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7662 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7663 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7664
7665 // We can handle 4 bits of mantissa.
7666 // mantissa = (16+UInt(e:f:g:h))/16.
7667 if (Mantissa & 0x7ffff)
7668 return -1;
7669 Mantissa >>= 19;
7670 if ((Mantissa & 0xf) != Mantissa)
7671 return -1;
7672
7673 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7674 if (Exp < -3 || Exp > 4)
7675 return -1;
7676 Exp = ((Exp+3) & 0x7) ^ 4;
7677
7678 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7679}
7680
7681int ARM::getVFPf64Imm(const APFloat &FPImm) {
7682 APInt Imm = FPImm.bitcastToAPInt();
7683 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7684 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7685 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7686
7687 // We can handle 4 bits of mantissa.
7688 // mantissa = (16+UInt(e:f:g:h))/16.
7689 if (Mantissa & 0xffffffffffffLL)
7690 return -1;
7691 Mantissa >>= 48;
7692 if ((Mantissa & 0xf) != Mantissa)
7693 return -1;
7694
7695 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7696 if (Exp < -3 || Exp > 4)
7697 return -1;
7698 Exp = ((Exp+3) & 0x7) ^ 4;
7699
7700 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7701}
7702
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007703bool ARM::isBitFieldInvertedMask(unsigned v) {
7704 if (v == 0xffffffff)
7705 return 0;
7706 // there can be 1's on either or both "outsides", all the "inside"
7707 // bits must be 0's
7708 unsigned int lsb = 0, msb = 31;
7709 while (v & (1 << msb)) --msb;
7710 while (v & (1 << lsb)) ++lsb;
7711 for (unsigned int i = lsb; i <= msb; ++i) {
7712 if (v & (1 << i))
7713 return 0;
7714 }
7715 return 1;
7716}
7717
Evan Cheng39382422009-10-28 01:44:26 +00007718/// isFPImmLegal - Returns true if the target can instruction select the
7719/// specified FP immediate natively. If false, the legalizer will
7720/// materialize the FP immediate as a load from a constant pool.
7721bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7722 if (!Subtarget->hasVFP3())
7723 return false;
7724 if (VT == MVT::f32)
7725 return ARM::getVFPf32Imm(Imm) != -1;
7726 if (VT == MVT::f64)
7727 return ARM::getVFPf64Imm(Imm) != -1;
7728 return false;
7729}
Bob Wilson65ffec42010-09-21 17:56:22 +00007730
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007731/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007732/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7733/// specified in the intrinsic calls.
7734bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7735 const CallInst &I,
7736 unsigned Intrinsic) const {
7737 switch (Intrinsic) {
7738 case Intrinsic::arm_neon_vld1:
7739 case Intrinsic::arm_neon_vld2:
7740 case Intrinsic::arm_neon_vld3:
7741 case Intrinsic::arm_neon_vld4:
7742 case Intrinsic::arm_neon_vld2lane:
7743 case Intrinsic::arm_neon_vld3lane:
7744 case Intrinsic::arm_neon_vld4lane: {
7745 Info.opc = ISD::INTRINSIC_W_CHAIN;
7746 // Conservatively set memVT to the entire set of vectors loaded.
7747 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7748 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7749 Info.ptrVal = I.getArgOperand(0);
7750 Info.offset = 0;
7751 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7752 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7753 Info.vol = false; // volatile loads with NEON intrinsics not supported
7754 Info.readMem = true;
7755 Info.writeMem = false;
7756 return true;
7757 }
7758 case Intrinsic::arm_neon_vst1:
7759 case Intrinsic::arm_neon_vst2:
7760 case Intrinsic::arm_neon_vst3:
7761 case Intrinsic::arm_neon_vst4:
7762 case Intrinsic::arm_neon_vst2lane:
7763 case Intrinsic::arm_neon_vst3lane:
7764 case Intrinsic::arm_neon_vst4lane: {
7765 Info.opc = ISD::INTRINSIC_VOID;
7766 // Conservatively set memVT to the entire set of vectors stored.
7767 unsigned NumElts = 0;
7768 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7769 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7770 if (!ArgTy->isVectorTy())
7771 break;
7772 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7773 }
7774 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7775 Info.ptrVal = I.getArgOperand(0);
7776 Info.offset = 0;
7777 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7778 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7779 Info.vol = false; // volatile stores with NEON intrinsics not supported
7780 Info.readMem = false;
7781 Info.writeMem = true;
7782 return true;
7783 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007784 case Intrinsic::arm_strexd: {
7785 Info.opc = ISD::INTRINSIC_W_CHAIN;
7786 Info.memVT = MVT::i64;
7787 Info.ptrVal = I.getArgOperand(2);
7788 Info.offset = 0;
7789 Info.align = 8;
7790 Info.vol = false;
7791 Info.readMem = false;
7792 Info.writeMem = true;
7793 return true;
7794 }
7795 case Intrinsic::arm_ldrexd: {
7796 Info.opc = ISD::INTRINSIC_W_CHAIN;
7797 Info.memVT = MVT::i64;
7798 Info.ptrVal = I.getArgOperand(0);
7799 Info.offset = 0;
7800 Info.align = 8;
7801 Info.vol = false;
7802 Info.readMem = true;
7803 Info.writeMem = false;
7804 return true;
7805 }
Bob Wilson65ffec42010-09-21 17:56:22 +00007806 default:
7807 break;
7808 }
7809
7810 return false;
7811}