blob: d9bb3e65fa4da0a94e091408f7764115552423f8 [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);
Chad Rosieref01edf2011-06-24 19:23:04 +0000509 setTargetDAGCombine(ISD::FP_TO_SINT);
510 setTargetDAGCombine(ISD::FP_TO_UINT);
511 setTargetDAGCombine(ISD::FDIV);
Bob Wilson5bafff32009-06-22 23:27:02 +0000512 }
513
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000514 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000515
516 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000518
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000519 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000521
Evan Chenga8e29892007-01-19 07:51:42 +0000522 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000523 if (!Subtarget->isThumb1Only()) {
524 for (unsigned im = (unsigned)ISD::PRE_INC;
525 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setIndexedLoadAction(im, MVT::i1, Legal);
527 setIndexedLoadAction(im, MVT::i8, Legal);
528 setIndexedLoadAction(im, MVT::i16, Legal);
529 setIndexedLoadAction(im, MVT::i32, Legal);
530 setIndexedStoreAction(im, MVT::i1, Legal);
531 setIndexedStoreAction(im, MVT::i8, Legal);
532 setIndexedStoreAction(im, MVT::i16, Legal);
533 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000534 }
Evan Chenga8e29892007-01-19 07:51:42 +0000535 }
536
537 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000538 setOperationAction(ISD::MUL, MVT::i64, Expand);
539 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000540 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
542 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000543 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000544 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
545 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000546 setOperationAction(ISD::MULHS, MVT::i32, Expand);
547
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000548 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000549 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000550 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::SRL, MVT::i64, Custom);
552 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000553
554 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000556 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000558 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000560
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000561 // Only ARMv6 has BSWAP.
562 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000564
Evan Chenga8e29892007-01-19 07:51:42 +0000565 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000566 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000567 // v7M has a hardware divider
568 setOperationAction(ISD::SDIV, MVT::i32, Expand);
569 setOperationAction(ISD::UDIV, MVT::i32, Expand);
570 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::SREM, MVT::i32, Expand);
572 setOperationAction(ISD::UREM, MVT::i32, Expand);
573 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
574 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000575
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
577 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
578 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
579 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000580 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000581
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000582 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000583
Evan Chenga8e29892007-01-19 07:51:42 +0000584 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::VASTART, MVT::Other, Custom);
586 setOperationAction(ISD::VAARG, MVT::Other, Expand);
587 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
588 setOperationAction(ISD::VAEND, MVT::Other, Expand);
589 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
590 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000591 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000592 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
593 setExceptionPointerRegister(ARM::R0);
594 setExceptionSelectorRegister(ARM::R1);
595
Evan Cheng3a1588a2010-04-15 22:20:34 +0000596 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000597 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
598 // the default expansion.
599 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000600 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000601 // membarrier needs custom lowering; the rest are legal and handled
602 // normally.
603 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
604 } else {
605 // Set them all for expansion, which will force libcalls.
606 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
607 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
608 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
609 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000610 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
611 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
612 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000613 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
616 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
617 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
618 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
624 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
625 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
626 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
627 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
628 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
629 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
630 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000631 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i8, Expand);
632 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i16, Expand);
633 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
634 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i8, Expand);
635 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i16, Expand);
636 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
637 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i8, Expand);
638 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i16, Expand);
639 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
640 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i8, Expand);
641 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i16, Expand);
642 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000643 // Since the libcalls include locking, fold in the fences
644 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000645 }
646 // 64-bit versions are always libcalls (for now)
647 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000648 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000649 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
650 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
651 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
652 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
653 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
654 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000655
Evan Cheng416941d2010-11-04 05:19:35 +0000656 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000657
Eli Friedmana2c6f452010-06-26 04:36:50 +0000658 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
659 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
661 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000662 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000664
Nate Begemand1fb5832010-08-03 21:31:55 +0000665 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000666 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
667 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000668 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000669 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
670 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000671
672 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000674 if (Subtarget->isTargetDarwin()) {
675 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
676 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000677 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000678 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000679 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::SETCC, MVT::i32, Expand);
682 setOperationAction(ISD::SETCC, MVT::f32, Expand);
683 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000684 setOperationAction(ISD::SELECT, MVT::i32, Custom);
685 setOperationAction(ISD::SELECT, MVT::f32, Custom);
686 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
688 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
689 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000690
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
692 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
693 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
694 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
695 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000696
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000697 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::FSIN, MVT::f64, Expand);
699 setOperationAction(ISD::FSIN, MVT::f32, Expand);
700 setOperationAction(ISD::FCOS, MVT::f32, Expand);
701 setOperationAction(ISD::FCOS, MVT::f64, Expand);
702 setOperationAction(ISD::FREM, MVT::f64, Expand);
703 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000704 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
706 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000707 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 setOperationAction(ISD::FPOW, MVT::f64, Expand);
709 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000710
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000711 // Various VFP goodness
712 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000713 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
714 if (Subtarget->hasVFP2()) {
715 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
716 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
717 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
718 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
719 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000720 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000721 if (!Subtarget->hasFP16()) {
722 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
723 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000724 }
Evan Cheng110cf482008-04-01 01:50:16 +0000725 }
Evan Chenga8e29892007-01-19 07:51:42 +0000726
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000727 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000728 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000729 setTargetDAGCombine(ISD::ADD);
730 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000731 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000732
Owen Anderson080c0922010-11-05 19:27:46 +0000733 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000734 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000735 if (Subtarget->hasNEON())
736 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000737
Evan Chenga8e29892007-01-19 07:51:42 +0000738 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000739
Evan Chengf7d87ee2010-05-21 00:43:17 +0000740 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
741 setSchedulingPreference(Sched::RegPressure);
742 else
743 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000744
Evan Cheng05219282011-01-06 06:52:41 +0000745 //// temporary - rewrite interface to use type
746 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000747
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000748 // On ARM arguments smaller than 4 bytes are extended, so all arguments
749 // are at least 4 bytes aligned.
750 setMinStackArgumentAlignment(4);
751
Evan Chengfff606d2010-09-24 19:07:23 +0000752 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000753
754 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000755}
756
Andrew Trick32cec0a2011-01-19 02:35:27 +0000757// FIXME: It might make sense to define the representative register class as the
758// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
759// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
760// SPR's representative would be DPR_VFP2. This should work well if register
761// pressure tracking were modified such that a register use would increment the
762// pressure of the register class's representative and all of it's super
763// classes' representatives transitively. We have not implemented this because
764// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000765// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000766// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000767std::pair<const TargetRegisterClass*, uint8_t>
768ARMTargetLowering::findRepresentativeClass(EVT VT) const{
769 const TargetRegisterClass *RRC = 0;
770 uint8_t Cost = 1;
771 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000772 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000773 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000774 // Use DPR as representative register class for all floating point
775 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
776 // the cost is 1 for both f32 and f64.
777 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000778 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000779 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000780 // When NEON is used for SP, only half of the register file is available
781 // because operations that define both SP and DP results will be constrained
782 // to the VFP2 class (D0-D15). We currently model this constraint prior to
783 // coalescing by double-counting the SP regs. See the FIXME above.
784 if (Subtarget->useNEONForSinglePrecisionFP())
785 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000786 break;
787 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
788 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000789 RRC = ARM::DPRRegisterClass;
790 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000791 break;
792 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000793 RRC = ARM::DPRRegisterClass;
794 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000795 break;
796 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000797 RRC = ARM::DPRRegisterClass;
798 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000799 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000800 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000801 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000802}
803
Evan Chenga8e29892007-01-19 07:51:42 +0000804const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
805 switch (Opcode) {
806 default: return 0;
807 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000808 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000809 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000810 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
811 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000812 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000813 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
814 case ARMISD::tCALL: return "ARMISD::tCALL";
815 case ARMISD::BRCOND: return "ARMISD::BRCOND";
816 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000817 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000818 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
819 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
820 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000821 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000822 case ARMISD::CMPFP: return "ARMISD::CMPFP";
823 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000824 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000825 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
826 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000827
Jim Grosbach3482c802010-01-18 19:58:49 +0000828 case ARMISD::RBIT: return "ARMISD::RBIT";
829
Bob Wilson76a312b2010-03-19 22:51:32 +0000830 case ARMISD::FTOSI: return "ARMISD::FTOSI";
831 case ARMISD::FTOUI: return "ARMISD::FTOUI";
832 case ARMISD::SITOF: return "ARMISD::SITOF";
833 case ARMISD::UITOF: return "ARMISD::UITOF";
834
Evan Chenga8e29892007-01-19 07:51:42 +0000835 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
836 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
837 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000838
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000839 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
840 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000841
Evan Chengc5942082009-10-28 06:55:03 +0000842 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
843 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000844 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000845
Dale Johannesen51e28e62010-06-03 21:09:53 +0000846 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000847
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000848 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000849
Evan Cheng86198642009-08-07 00:34:42 +0000850 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
851
Jim Grosbach3728e962009-12-10 00:11:09 +0000852 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000853 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000854
Evan Chengdfed19f2010-11-03 06:34:55 +0000855 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
856
Bob Wilson5bafff32009-06-22 23:27:02 +0000857 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000858 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000859 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000860 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
861 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 case ARMISD::VCGEU: return "ARMISD::VCGEU";
863 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000864 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
865 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000866 case ARMISD::VCGTU: return "ARMISD::VCGTU";
867 case ARMISD::VTST: return "ARMISD::VTST";
868
869 case ARMISD::VSHL: return "ARMISD::VSHL";
870 case ARMISD::VSHRs: return "ARMISD::VSHRs";
871 case ARMISD::VSHRu: return "ARMISD::VSHRu";
872 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
873 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
874 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
875 case ARMISD::VSHRN: return "ARMISD::VSHRN";
876 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
877 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
878 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
879 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
880 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
881 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
882 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
883 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
884 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
885 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
886 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
887 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
888 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
889 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000890 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000891 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000892 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000893 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000894 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000895 case ARMISD::VREV64: return "ARMISD::VREV64";
896 case ARMISD::VREV32: return "ARMISD::VREV32";
897 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000898 case ARMISD::VZIP: return "ARMISD::VZIP";
899 case ARMISD::VUZP: return "ARMISD::VUZP";
900 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000901 case ARMISD::VTBL1: return "ARMISD::VTBL1";
902 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000903 case ARMISD::VMULLs: return "ARMISD::VMULLs";
904 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000905 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000906 case ARMISD::FMAX: return "ARMISD::FMAX";
907 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000908 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000909 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
910 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000911 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000912 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
913 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
914 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000915 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
916 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
917 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
918 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
919 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
920 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
921 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
922 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
923 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
924 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
925 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
926 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
927 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
928 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
929 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
930 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
931 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000932 }
933}
934
Evan Cheng06b666c2010-05-15 02:18:07 +0000935/// getRegClassFor - Return the register class that should be used for the
936/// specified value type.
937TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
938 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
939 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
940 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000941 if (Subtarget->hasNEON()) {
942 if (VT == MVT::v4i64)
943 return ARM::QQPRRegisterClass;
944 else if (VT == MVT::v8i64)
945 return ARM::QQQQPRRegisterClass;
946 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000947 return TargetLowering::getRegClassFor(VT);
948}
949
Eric Christopherab695882010-07-21 22:26:11 +0000950// Create a fast isel object.
951FastISel *
952ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
953 return ARM::createFastISel(funcInfo);
954}
955
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000956/// getMaximalGlobalOffset - Returns the maximal possible offset which can
957/// be used for loads / stores from the global.
958unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
959 return (Subtarget->isThumb1Only() ? 127 : 4095);
960}
961
Evan Cheng1cc39842010-05-20 23:26:43 +0000962Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000963 unsigned NumVals = N->getNumValues();
964 if (!NumVals)
965 return Sched::RegPressure;
966
967 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000968 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000969 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000970 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000971 if (VT.isFloatingPoint() || VT.isVector())
972 return Sched::Latency;
973 }
Evan Chengc10f5432010-05-28 23:25:23 +0000974
975 if (!N->isMachineOpcode())
976 return Sched::RegPressure;
977
978 // Load are scheduled for latency even if there instruction itinerary
979 // is not available.
980 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000981 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000982
Evan Chenge837dea2011-06-28 19:10:37 +0000983 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +0000984 return Sched::RegPressure;
985 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +0000986 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000987 return Sched::Latency;
988
Evan Cheng1cc39842010-05-20 23:26:43 +0000989 return Sched::RegPressure;
990}
991
Evan Chenga8e29892007-01-19 07:51:42 +0000992//===----------------------------------------------------------------------===//
993// Lowering Code
994//===----------------------------------------------------------------------===//
995
Evan Chenga8e29892007-01-19 07:51:42 +0000996/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
997static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
998 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000999 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001000 case ISD::SETNE: return ARMCC::NE;
1001 case ISD::SETEQ: return ARMCC::EQ;
1002 case ISD::SETGT: return ARMCC::GT;
1003 case ISD::SETGE: return ARMCC::GE;
1004 case ISD::SETLT: return ARMCC::LT;
1005 case ISD::SETLE: return ARMCC::LE;
1006 case ISD::SETUGT: return ARMCC::HI;
1007 case ISD::SETUGE: return ARMCC::HS;
1008 case ISD::SETULT: return ARMCC::LO;
1009 case ISD::SETULE: return ARMCC::LS;
1010 }
1011}
1012
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001013/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1014static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001015 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001016 CondCode2 = ARMCC::AL;
1017 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001018 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001019 case ISD::SETEQ:
1020 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1021 case ISD::SETGT:
1022 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1023 case ISD::SETGE:
1024 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1025 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001026 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001027 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1028 case ISD::SETO: CondCode = ARMCC::VC; break;
1029 case ISD::SETUO: CondCode = ARMCC::VS; break;
1030 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1031 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1032 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1033 case ISD::SETLT:
1034 case ISD::SETULT: CondCode = ARMCC::LT; break;
1035 case ISD::SETLE:
1036 case ISD::SETULE: CondCode = ARMCC::LE; break;
1037 case ISD::SETNE:
1038 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1039 }
Evan Chenga8e29892007-01-19 07:51:42 +00001040}
1041
Bob Wilson1f595bb2009-04-17 19:07:39 +00001042//===----------------------------------------------------------------------===//
1043// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001044//===----------------------------------------------------------------------===//
1045
1046#include "ARMGenCallingConv.inc"
1047
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001048/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1049/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001050CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001051 bool Return,
1052 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001053 switch (CC) {
1054 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001055 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001056 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001057 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001058 if (!Subtarget->isAAPCS_ABI())
1059 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1060 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1061 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1062 }
1063 // Fallthrough
1064 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001065 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001066 if (!Subtarget->isAAPCS_ABI())
1067 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1068 else if (Subtarget->hasVFP2() &&
1069 FloatABIType == FloatABI::Hard && !isVarArg)
1070 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1071 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1072 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001073 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001074 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001075 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001076 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001077 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001078 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001079 }
1080}
1081
Dan Gohman98ca4f22009-08-05 01:29:28 +00001082/// LowerCallResult - Lower the result values of a call into the
1083/// appropriate copies out of appropriate physical registers.
1084SDValue
1085ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001086 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087 const SmallVectorImpl<ISD::InputArg> &Ins,
1088 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001089 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001090
Bob Wilson1f595bb2009-04-17 19:07:39 +00001091 // Assign locations to each value returned by this call.
1092 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001093 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1094 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001095 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001096 CCAssignFnForNode(CallConv, /* Return*/ true,
1097 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001098
1099 // Copy all of the result registers out of their specified physreg.
1100 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1101 CCValAssign VA = RVLocs[i];
1102
Bob Wilson80915242009-04-25 00:33:20 +00001103 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001105 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001106 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001108 Chain = Lo.getValue(1);
1109 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001110 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001111 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001112 InFlag);
1113 Chain = Hi.getValue(1);
1114 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001115 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001116
Owen Anderson825b72b2009-08-11 20:47:22 +00001117 if (VA.getLocVT() == MVT::v2f64) {
1118 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1119 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1120 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001121
1122 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001123 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001124 Chain = Lo.getValue(1);
1125 InFlag = Lo.getValue(2);
1126 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001128 Chain = Hi.getValue(1);
1129 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001130 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001131 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1132 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001133 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001135 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1136 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001137 Chain = Val.getValue(1);
1138 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001139 }
Bob Wilson80915242009-04-25 00:33:20 +00001140
1141 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001142 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001143 case CCValAssign::Full: break;
1144 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001145 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001146 break;
1147 }
1148
Dan Gohman98ca4f22009-08-05 01:29:28 +00001149 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001150 }
1151
Dan Gohman98ca4f22009-08-05 01:29:28 +00001152 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001153}
1154
Bob Wilsondee46d72009-04-17 20:35:10 +00001155/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001156SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001157ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1158 SDValue StackPtr, SDValue Arg,
1159 DebugLoc dl, SelectionDAG &DAG,
1160 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001161 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001162 unsigned LocMemOffset = VA.getLocMemOffset();
1163 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1164 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001166 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001167 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001168}
1169
Dan Gohman98ca4f22009-08-05 01:29:28 +00001170void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001171 SDValue Chain, SDValue &Arg,
1172 RegsToPassVector &RegsToPass,
1173 CCValAssign &VA, CCValAssign &NextVA,
1174 SDValue &StackPtr,
1175 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001176 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001177
Jim Grosbache5165492009-11-09 00:11:35 +00001178 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001180 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1181
1182 if (NextVA.isRegLoc())
1183 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1184 else {
1185 assert(NextVA.isMemLoc());
1186 if (StackPtr.getNode() == 0)
1187 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1188
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1190 dl, DAG, NextVA,
1191 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001192 }
1193}
1194
Dan Gohman98ca4f22009-08-05 01:29:28 +00001195/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001196/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1197/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001199ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001200 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001201 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001203 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001204 const SmallVectorImpl<ISD::InputArg> &Ins,
1205 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001206 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001207 MachineFunction &MF = DAG.getMachineFunction();
1208 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1209 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001210 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001211 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001212 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001213 if (isTailCall) {
1214 // Check if it's really possible to do a tail call.
1215 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1216 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001217 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001218 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1219 // detected sibcalls.
1220 if (isTailCall) {
1221 ++NumTailCalls;
1222 IsSibCall = true;
1223 }
1224 }
Evan Chenga8e29892007-01-19 07:51:42 +00001225
Bob Wilson1f595bb2009-04-17 19:07:39 +00001226 // Analyze operands of the call, assigning locations to each operand.
1227 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001228 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1229 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001230 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001231 CCAssignFnForNode(CallConv, /* Return*/ false,
1232 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001233
Bob Wilson1f595bb2009-04-17 19:07:39 +00001234 // Get a count of how many bytes are to be pushed on the stack.
1235 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001236
Dale Johannesen51e28e62010-06-03 21:09:53 +00001237 // For tail calls, memory operands are available in our caller's stack.
1238 if (IsSibCall)
1239 NumBytes = 0;
1240
Evan Chenga8e29892007-01-19 07:51:42 +00001241 // Adjust the stack pointer for the new arguments...
1242 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001243 if (!IsSibCall)
1244 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001245
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001246 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001247
Bob Wilson5bafff32009-06-22 23:27:02 +00001248 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001249 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001250
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001252 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001253 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1254 i != e;
1255 ++i, ++realArgIdx) {
1256 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001257 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001258 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001259 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001260
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261 // Promote the value if needed.
1262 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001263 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001264 case CCValAssign::Full: break;
1265 case CCValAssign::SExt:
1266 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1267 break;
1268 case CCValAssign::ZExt:
1269 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1270 break;
1271 case CCValAssign::AExt:
1272 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1273 break;
1274 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001275 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001277 }
1278
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001279 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001280 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001281 if (VA.getLocVT() == MVT::v2f64) {
1282 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1283 DAG.getConstant(0, MVT::i32));
1284 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1285 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001286
Dan Gohman98ca4f22009-08-05 01:29:28 +00001287 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001288 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1289
1290 VA = ArgLocs[++i]; // skip ahead to next loc
1291 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001293 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1294 } else {
1295 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001296
Dan Gohman98ca4f22009-08-05 01:29:28 +00001297 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1298 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001299 }
1300 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001301 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001302 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001303 }
1304 } else if (VA.isRegLoc()) {
1305 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001306 } else if (isByVal) {
1307 assert(VA.isMemLoc());
1308 unsigned offset = 0;
1309
1310 // True if this byval aggregate will be split between registers
1311 // and memory.
1312 if (CCInfo.isFirstByValRegValid()) {
1313 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1314 unsigned int i, j;
1315 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1316 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1317 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1318 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1319 MachinePointerInfo(),
1320 false, false, 0);
1321 MemOpChains.push_back(Load.getValue(1));
1322 RegsToPass.push_back(std::make_pair(j, Load));
1323 }
1324 offset = ARM::R4 - CCInfo.getFirstByValReg();
1325 CCInfo.clearFirstByValReg();
1326 }
1327
1328 unsigned LocMemOffset = VA.getLocMemOffset();
1329 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1330 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1331 StkPtrOff);
1332 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1333 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1334 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1335 MVT::i32);
1336 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1337 Flags.getByValAlign(),
1338 /*isVolatile=*/false,
1339 /*AlwaysInline=*/false,
1340 MachinePointerInfo(0),
1341 MachinePointerInfo(0)));
1342
1343 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001344 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001345
Dan Gohman98ca4f22009-08-05 01:29:28 +00001346 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1347 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001348 }
Evan Chenga8e29892007-01-19 07:51:42 +00001349 }
1350
1351 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001352 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001353 &MemOpChains[0], MemOpChains.size());
1354
1355 // Build a sequence of copy-to-reg nodes chained together with token chain
1356 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001357 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001358 // Tail call byval lowering might overwrite argument registers so in case of
1359 // tail call optimization the copies to registers are lowered later.
1360 if (!isTailCall)
1361 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1362 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1363 RegsToPass[i].second, InFlag);
1364 InFlag = Chain.getValue(1);
1365 }
Evan Chenga8e29892007-01-19 07:51:42 +00001366
Dale Johannesen51e28e62010-06-03 21:09:53 +00001367 // For tail calls lower the arguments to the 'real' stack slot.
1368 if (isTailCall) {
1369 // Force all the incoming stack arguments to be loaded from the stack
1370 // before any new outgoing arguments are stored to the stack, because the
1371 // outgoing stack slots may alias the incoming argument stack slots, and
1372 // the alias isn't otherwise explicit. This is slightly more conservative
1373 // than necessary, because it means that each store effectively depends
1374 // on every argument instead of just those arguments it would clobber.
1375
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001376 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001377 InFlag = SDValue();
1378 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1379 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1380 RegsToPass[i].second, InFlag);
1381 InFlag = Chain.getValue(1);
1382 }
1383 InFlag =SDValue();
1384 }
1385
Bill Wendling056292f2008-09-16 21:48:12 +00001386 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1387 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1388 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001389 bool isDirect = false;
1390 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001391 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001392 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001393
1394 if (EnableARMLongCalls) {
1395 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1396 && "long-calls with non-static relocation model!");
1397 // Handle a global address or an external symbol. If it's not one of
1398 // those, the target's already in a register, so we don't need to do
1399 // anything extra.
1400 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001401 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001402 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001403 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001404 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1405 ARMPCLabelIndex,
1406 ARMCP::CPValue, 0);
1407 // Get the address of the callee into a register
1408 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1409 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1410 Callee = DAG.getLoad(getPointerTy(), dl,
1411 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001412 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001413 false, false, 0);
1414 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1415 const char *Sym = S->getSymbol();
1416
1417 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001418 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001419 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1420 Sym, ARMPCLabelIndex, 0);
1421 // Get the address of the callee into a register
1422 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1423 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1424 Callee = DAG.getLoad(getPointerTy(), dl,
1425 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001426 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001427 false, false, 0);
1428 }
1429 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001430 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001431 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001432 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001433 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001434 getTargetMachine().getRelocationModel() != Reloc::Static;
1435 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001436 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001437 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001438 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001439 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001440 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001441 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001442 ARMPCLabelIndex,
1443 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001444 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001445 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001446 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001447 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001448 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001449 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001450 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001451 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001452 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001453 } else {
1454 // On ELF targets for PIC code, direct calls should go through the PLT
1455 unsigned OpFlags = 0;
1456 if (Subtarget->isTargetELF() &&
1457 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1458 OpFlags = ARMII::MO_PLT;
1459 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1460 }
Bill Wendling056292f2008-09-16 21:48:12 +00001461 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001462 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001463 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001464 getTargetMachine().getRelocationModel() != Reloc::Static;
1465 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001466 // tBX takes a register source operand.
1467 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001468 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001469 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001470 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001471 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001472 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001473 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001474 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001475 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001476 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001477 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001478 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001479 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001480 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001481 } else {
1482 unsigned OpFlags = 0;
1483 // On ELF targets for PIC code, direct calls should go through the PLT
1484 if (Subtarget->isTargetELF() &&
1485 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1486 OpFlags = ARMII::MO_PLT;
1487 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1488 }
Evan Chenga8e29892007-01-19 07:51:42 +00001489 }
1490
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001491 // FIXME: handle tail calls differently.
1492 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001493 if (Subtarget->isThumb()) {
1494 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001495 CallOpc = ARMISD::CALL_NOLINK;
1496 else
1497 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1498 } else {
1499 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001500 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1501 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001502 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001503
Dan Gohman475871a2008-07-27 21:46:04 +00001504 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001505 Ops.push_back(Chain);
1506 Ops.push_back(Callee);
1507
1508 // Add argument registers to the end of the list so that they are known live
1509 // into the call.
1510 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1511 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1512 RegsToPass[i].second.getValueType()));
1513
Gabor Greifba36cb52008-08-28 21:40:38 +00001514 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001515 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001516
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001517 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001518 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001519 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001520
Duncan Sands4bdcb612008-07-02 17:40:58 +00001521 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001522 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001523 InFlag = Chain.getValue(1);
1524
Chris Lattnere563bbc2008-10-11 22:08:30 +00001525 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1526 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001527 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001528 InFlag = Chain.getValue(1);
1529
Bob Wilson1f595bb2009-04-17 19:07:39 +00001530 // Handle result values, copying them out of physregs into vregs that we
1531 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001532 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1533 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001534}
1535
Stuart Hastingsf222e592011-02-28 17:17:53 +00001536/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001537/// on the stack. Remember the next parameter register to allocate,
1538/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001539/// this.
1540void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001541llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1542 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1543 assert((State->getCallOrPrologue() == Prologue ||
1544 State->getCallOrPrologue() == Call) &&
1545 "unhandled ParmContext");
1546 if ((!State->isFirstByValRegValid()) &&
1547 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1548 State->setFirstByValReg(reg);
1549 // At a call site, a byval parameter that is split between
1550 // registers and memory needs its size truncated here. In a
1551 // function prologue, such byval parameters are reassembled in
1552 // memory, and are not truncated.
1553 if (State->getCallOrPrologue() == Call) {
1554 unsigned excess = 4 * (ARM::R4 - reg);
1555 assert(size >= excess && "expected larger existing stack allocation");
1556 size -= excess;
1557 }
1558 }
1559 // Confiscate any remaining parameter registers to preclude their
1560 // assignment to subsequent parameters.
1561 while (State->AllocateReg(GPRArgRegs, 4))
1562 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001563}
1564
Dale Johannesen51e28e62010-06-03 21:09:53 +00001565/// MatchingStackOffset - Return true if the given stack call argument is
1566/// already available in the same position (relatively) of the caller's
1567/// incoming argument stack.
1568static
1569bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1570 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1571 const ARMInstrInfo *TII) {
1572 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1573 int FI = INT_MAX;
1574 if (Arg.getOpcode() == ISD::CopyFromReg) {
1575 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001576 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001577 return false;
1578 MachineInstr *Def = MRI->getVRegDef(VR);
1579 if (!Def)
1580 return false;
1581 if (!Flags.isByVal()) {
1582 if (!TII->isLoadFromStackSlot(Def, FI))
1583 return false;
1584 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001585 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001586 }
1587 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1588 if (Flags.isByVal())
1589 // ByVal argument is passed in as a pointer but it's now being
1590 // dereferenced. e.g.
1591 // define @foo(%struct.X* %A) {
1592 // tail call @bar(%struct.X* byval %A)
1593 // }
1594 return false;
1595 SDValue Ptr = Ld->getBasePtr();
1596 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1597 if (!FINode)
1598 return false;
1599 FI = FINode->getIndex();
1600 } else
1601 return false;
1602
1603 assert(FI != INT_MAX);
1604 if (!MFI->isFixedObjectIndex(FI))
1605 return false;
1606 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1607}
1608
1609/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1610/// for tail call optimization. Targets which want to do tail call
1611/// optimization should implement this function.
1612bool
1613ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1614 CallingConv::ID CalleeCC,
1615 bool isVarArg,
1616 bool isCalleeStructRet,
1617 bool isCallerStructRet,
1618 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001619 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001620 const SmallVectorImpl<ISD::InputArg> &Ins,
1621 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001622 const Function *CallerF = DAG.getMachineFunction().getFunction();
1623 CallingConv::ID CallerCC = CallerF->getCallingConv();
1624 bool CCMatch = CallerCC == CalleeCC;
1625
1626 // Look for obvious safe cases to perform tail call optimization that do not
1627 // require ABI changes. This is what gcc calls sibcall.
1628
Jim Grosbach7616b642010-06-16 23:45:49 +00001629 // Do not sibcall optimize vararg calls unless the call site is not passing
1630 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001631 if (isVarArg && !Outs.empty())
1632 return false;
1633
1634 // Also avoid sibcall optimization if either caller or callee uses struct
1635 // return semantics.
1636 if (isCalleeStructRet || isCallerStructRet)
1637 return false;
1638
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001639 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001640 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1641 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1642 // support in the assembler and linker to be used. This would need to be
1643 // fixed to fully support tail calls in Thumb1.
1644 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001645 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1646 // LR. This means if we need to reload LR, it takes an extra instructions,
1647 // which outweighs the value of the tail call; but here we don't know yet
1648 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001649 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001650 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001651
1652 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1653 // but we need to make sure there are enough registers; the only valid
1654 // registers are the 4 used for parameters. We don't currently do this
1655 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001656 if (Subtarget->isThumb1Only())
1657 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001658
Dale Johannesen51e28e62010-06-03 21:09:53 +00001659 // If the calling conventions do not match, then we'd better make sure the
1660 // results are returned in the same way as what the caller expects.
1661 if (!CCMatch) {
1662 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001663 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1664 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001665 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1666
1667 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001668 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1669 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001670 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1671
1672 if (RVLocs1.size() != RVLocs2.size())
1673 return false;
1674 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1675 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1676 return false;
1677 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1678 return false;
1679 if (RVLocs1[i].isRegLoc()) {
1680 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1681 return false;
1682 } else {
1683 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1684 return false;
1685 }
1686 }
1687 }
1688
1689 // If the callee takes no arguments then go on to check the results of the
1690 // call.
1691 if (!Outs.empty()) {
1692 // Check if stack adjustment is needed. For now, do not do this if any
1693 // argument is passed on the stack.
1694 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001695 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1696 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001697 CCInfo.AnalyzeCallOperands(Outs,
1698 CCAssignFnForNode(CalleeCC, false, isVarArg));
1699 if (CCInfo.getNextStackOffset()) {
1700 MachineFunction &MF = DAG.getMachineFunction();
1701
1702 // Check if the arguments are already laid out in the right way as
1703 // the caller's fixed stack objects.
1704 MachineFrameInfo *MFI = MF.getFrameInfo();
1705 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1706 const ARMInstrInfo *TII =
1707 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001708 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1709 i != e;
1710 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001711 CCValAssign &VA = ArgLocs[i];
1712 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001713 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001714 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001715 if (VA.getLocInfo() == CCValAssign::Indirect)
1716 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001717 if (VA.needsCustom()) {
1718 // f64 and vector types are split into multiple registers or
1719 // register/stack-slot combinations. The types will not match
1720 // the registers; give up on memory f64 refs until we figure
1721 // out what to do about this.
1722 if (!VA.isRegLoc())
1723 return false;
1724 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001725 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001726 if (RegVT == MVT::v2f64) {
1727 if (!ArgLocs[++i].isRegLoc())
1728 return false;
1729 if (!ArgLocs[++i].isRegLoc())
1730 return false;
1731 }
1732 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001733 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1734 MFI, MRI, TII))
1735 return false;
1736 }
1737 }
1738 }
1739 }
1740
1741 return true;
1742}
1743
Dan Gohman98ca4f22009-08-05 01:29:28 +00001744SDValue
1745ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001746 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001748 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001749 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001750
Bob Wilsondee46d72009-04-17 20:35:10 +00001751 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001752 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001753
Bob Wilsondee46d72009-04-17 20:35:10 +00001754 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001755 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1756 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757
Dan Gohman98ca4f22009-08-05 01:29:28 +00001758 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001759 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1760 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001761
1762 // If this is the first return lowered for this function, add
1763 // the regs to the liveout set for the function.
1764 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1765 for (unsigned i = 0; i != RVLocs.size(); ++i)
1766 if (RVLocs[i].isRegLoc())
1767 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001768 }
1769
Bob Wilson1f595bb2009-04-17 19:07:39 +00001770 SDValue Flag;
1771
1772 // Copy the result values into the output registers.
1773 for (unsigned i = 0, realRVLocIdx = 0;
1774 i != RVLocs.size();
1775 ++i, ++realRVLocIdx) {
1776 CCValAssign &VA = RVLocs[i];
1777 assert(VA.isRegLoc() && "Can only return in registers!");
1778
Dan Gohmanc9403652010-07-07 15:54:55 +00001779 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001780
1781 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001782 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001783 case CCValAssign::Full: break;
1784 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001785 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001786 break;
1787 }
1788
Bob Wilson1f595bb2009-04-17 19:07:39 +00001789 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001790 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001791 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001792 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1793 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001794 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001796
1797 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1798 Flag = Chain.getValue(1);
1799 VA = RVLocs[++i]; // skip ahead to next loc
1800 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1801 HalfGPRs.getValue(1), Flag);
1802 Flag = Chain.getValue(1);
1803 VA = RVLocs[++i]; // skip ahead to next loc
1804
1805 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1807 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001808 }
1809 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1810 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001811 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001813 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001814 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815 VA = RVLocs[++i]; // skip ahead to next loc
1816 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1817 Flag);
1818 } else
1819 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1820
Bob Wilsondee46d72009-04-17 20:35:10 +00001821 // Guarantee that all emitted copies are
1822 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001823 Flag = Chain.getValue(1);
1824 }
1825
1826 SDValue result;
1827 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001829 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001830 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001831
1832 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001833}
1834
Evan Cheng3d2125c2010-11-30 23:55:39 +00001835bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1836 if (N->getNumValues() != 1)
1837 return false;
1838 if (!N->hasNUsesOfValue(1, 0))
1839 return false;
1840
1841 unsigned NumCopies = 0;
1842 SDNode* Copies[2];
1843 SDNode *Use = *N->use_begin();
1844 if (Use->getOpcode() == ISD::CopyToReg) {
1845 Copies[NumCopies++] = Use;
1846 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1847 // f64 returned in a pair of GPRs.
1848 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1849 UI != UE; ++UI) {
1850 if (UI->getOpcode() != ISD::CopyToReg)
1851 return false;
1852 Copies[UI.getUse().getResNo()] = *UI;
1853 ++NumCopies;
1854 }
1855 } else if (Use->getOpcode() == ISD::BITCAST) {
1856 // f32 returned in a single GPR.
1857 if (!Use->hasNUsesOfValue(1, 0))
1858 return false;
1859 Use = *Use->use_begin();
1860 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1861 return false;
1862 Copies[NumCopies++] = Use;
1863 } else {
1864 return false;
1865 }
1866
1867 if (NumCopies != 1 && NumCopies != 2)
1868 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001869
1870 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001871 for (unsigned i = 0; i < NumCopies; ++i) {
1872 SDNode *Copy = Copies[i];
1873 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1874 UI != UE; ++UI) {
1875 if (UI->getOpcode() == ISD::CopyToReg) {
1876 SDNode *Use = *UI;
1877 if (Use == Copies[0] || Use == Copies[1])
1878 continue;
1879 return false;
1880 }
1881 if (UI->getOpcode() != ARMISD::RET_FLAG)
1882 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001883 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001884 }
1885 }
1886
Evan Cheng1bf891a2010-12-01 22:59:46 +00001887 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001888}
1889
Evan Cheng485fafc2011-03-21 01:19:09 +00001890bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1891 if (!EnableARMTailCalls)
1892 return false;
1893
1894 if (!CI->isTailCall())
1895 return false;
1896
1897 return !Subtarget->isThumb1Only();
1898}
1899
Bob Wilsonb62d2572009-11-03 00:02:05 +00001900// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1901// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1902// one of the above mentioned nodes. It has to be wrapped because otherwise
1903// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1904// be used to form addressing mode. These wrapped nodes will be selected
1905// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001906static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001907 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001908 // FIXME there is no actual debug info here
1909 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001910 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001911 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001912 if (CP->isMachineConstantPoolEntry())
1913 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1914 CP->getAlignment());
1915 else
1916 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1917 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001918 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001919}
1920
Jim Grosbache1102ca2010-07-19 17:20:38 +00001921unsigned ARMTargetLowering::getJumpTableEncoding() const {
1922 return MachineJumpTableInfo::EK_Inline;
1923}
1924
Dan Gohmand858e902010-04-17 15:26:15 +00001925SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1926 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001927 MachineFunction &MF = DAG.getMachineFunction();
1928 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1929 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001930 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001931 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001932 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001933 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1934 SDValue CPAddr;
1935 if (RelocM == Reloc::Static) {
1936 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1937 } else {
1938 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001939 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001940 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1941 ARMCP::CPBlockAddress,
1942 PCAdj);
1943 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1944 }
1945 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1946 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001947 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001948 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001949 if (RelocM == Reloc::Static)
1950 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001951 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001952 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001953}
1954
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001955// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001956SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001957ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001958 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001959 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001960 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001961 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001962 MachineFunction &MF = DAG.getMachineFunction();
1963 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001964 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001965 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001966 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001967 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001968 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001969 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001970 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001971 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001972 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001973 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001974
Evan Chenge7e0d622009-11-06 22:24:13 +00001975 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001976 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001977
1978 // call __tls_get_addr.
1979 ArgListTy Args;
1980 ArgListEntry Entry;
1981 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001982 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001983 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001984 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001985 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001986 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1987 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001988 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001989 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001990 return CallResult.first;
1991}
1992
1993// Lower ISD::GlobalTLSAddress using the "initial exec" or
1994// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001995SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001996ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001997 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001998 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001999 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002000 SDValue Offset;
2001 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002002 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002003 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002004 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002005
Chris Lattner4fb63d02009-07-15 04:12:33 +00002006 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002007 MachineFunction &MF = DAG.getMachineFunction();
2008 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002009 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002010 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002011 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2012 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002013 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002014 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002015 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002016 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
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 Chain = Offset.getValue(1);
2021
Evan Chenge7e0d622009-11-06 22:24:13 +00002022 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002023 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002024
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 } else {
2029 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002030 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002031 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002033 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002034 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002035 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002036 }
2037
2038 // The address of the thread local variable is the add of the thread
2039 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002040 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002041}
2042
Dan Gohman475871a2008-07-27 21:46:04 +00002043SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002044ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002045 // TODO: implement the "local dynamic" model
2046 assert(Subtarget->isTargetELF() &&
2047 "TLS not implemented for non-ELF targets");
2048 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2049 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2050 // otherwise use the "Local Exec" TLS Model
2051 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2052 return LowerToTLSGeneralDynamicModel(GA, DAG);
2053 else
2054 return LowerToTLSExecModels(GA, DAG);
2055}
2056
Dan Gohman475871a2008-07-27 21:46:04 +00002057SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002058 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002059 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002060 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002061 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002062 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2063 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002064 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002065 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002066 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002067 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002068 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002069 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002070 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002071 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002072 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002073 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002074 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002075 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002076 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002077 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002078 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002079 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002080 }
2081
2082 // If we have T2 ops, we can materialize the address directly via movt/movw
2083 // pair. This is always cheaper.
2084 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002085 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002086 // FIXME: Once remat is capable of dealing with instructions with register
2087 // operands, expand this into two nodes.
2088 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2089 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002090 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002091 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2092 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2093 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2094 MachinePointerInfo::getConstantPool(),
2095 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002096 }
2097}
2098
Dan Gohman475871a2008-07-27 21:46:04 +00002099SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002100 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002101 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002102 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002103 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002104 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002105 MachineFunction &MF = DAG.getMachineFunction();
2106 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2107
Evan Cheng4abce0c2011-05-27 20:11:27 +00002108 // FIXME: Enable this for static codegen when tool issues are fixed.
2109 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002110 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002111 // FIXME: Once remat is capable of dealing with instructions with register
2112 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002113 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002114 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2115 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2116
Evan Cheng53519f02011-01-21 18:55:51 +00002117 unsigned Wrapper = (RelocM == Reloc::PIC_)
2118 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2119 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002120 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002121 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2122 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2123 MachinePointerInfo::getGOT(), false, false, 0);
2124 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002125 }
2126
2127 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002128 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002129 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002130 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002131 } else {
2132 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002133 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2134 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002135 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002136 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002137 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002138 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002139
Evan Cheng9eda6892009-10-31 03:39:36 +00002140 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002141 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002142 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002143 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002144
2145 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002146 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002147 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002148 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002149
Evan Cheng63476a82009-09-03 07:04:02 +00002150 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002151 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002152 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002153
2154 return Result;
2155}
2156
Dan Gohman475871a2008-07-27 21:46:04 +00002157SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002158 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002159 assert(Subtarget->isTargetELF() &&
2160 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002161 MachineFunction &MF = DAG.getMachineFunction();
2162 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002163 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002164 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002165 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002166 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002167 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2168 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002169 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002170 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002171 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002172 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002173 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002174 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002175 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002176 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002177}
2178
Jim Grosbach0e0da732009-05-12 23:59:14 +00002179SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002180ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2181 const {
2182 DebugLoc dl = Op.getDebugLoc();
2183 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002184 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002185}
2186
2187SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002188ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2189 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002190 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002191 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2192 Op.getOperand(1), Val);
2193}
2194
2195SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002196ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2197 DebugLoc dl = Op.getDebugLoc();
2198 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2199 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2200}
2201
2202SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002203ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002204 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002205 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002206 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002207 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002208 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002209 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002210 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002211 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2212 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002213 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002214 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002215 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002216 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002217 EVT PtrVT = getPointerTy();
2218 DebugLoc dl = Op.getDebugLoc();
2219 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2220 SDValue CPAddr;
2221 unsigned PCAdj = (RelocM != Reloc::PIC_)
2222 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002223 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002224 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2225 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002226 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002227 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002228 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002229 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002230 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002231 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002232
2233 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002234 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002235 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2236 }
2237 return Result;
2238 }
Evan Cheng92e39162011-03-29 23:06:19 +00002239 case Intrinsic::arm_neon_vmulls:
2240 case Intrinsic::arm_neon_vmullu: {
2241 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2242 ? ARMISD::VMULLs : ARMISD::VMULLu;
2243 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2244 Op.getOperand(1), Op.getOperand(2));
2245 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002246 }
2247}
2248
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002249static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002250 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002251 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002252 if (!Subtarget->hasDataBarrier()) {
2253 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2254 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2255 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002256 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002257 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002258 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002259 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002260 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002261
2262 SDValue Op5 = Op.getOperand(5);
2263 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2264 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2265 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2266 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2267
2268 ARM_MB::MemBOpt DMBOpt;
2269 if (isDeviceBarrier)
2270 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2271 else
2272 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2273 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2274 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002275}
2276
Evan Chengdfed19f2010-11-03 06:34:55 +00002277static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2278 const ARMSubtarget *Subtarget) {
2279 // ARM pre v5TE and Thumb1 does not have preload instructions.
2280 if (!(Subtarget->isThumb2() ||
2281 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2282 // Just preserve the chain.
2283 return Op.getOperand(0);
2284
2285 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002286 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2287 if (!isRead &&
2288 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2289 // ARMv7 with MP extension has PLDW.
2290 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002291
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002292 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2293 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002294 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002295 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002296 isData = ~isData & 1;
2297 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002298
2299 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002300 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2301 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002302}
2303
Dan Gohman1e93df62010-04-17 14:41:14 +00002304static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2305 MachineFunction &MF = DAG.getMachineFunction();
2306 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2307
Evan Chenga8e29892007-01-19 07:51:42 +00002308 // vastart just stores the address of the VarArgsFrameIndex slot into the
2309 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002310 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002311 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002312 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002313 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002314 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2315 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002316}
2317
Dan Gohman475871a2008-07-27 21:46:04 +00002318SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002319ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2320 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002321 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002322 MachineFunction &MF = DAG.getMachineFunction();
2323 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2324
2325 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002326 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002327 RC = ARM::tGPRRegisterClass;
2328 else
2329 RC = ARM::GPRRegisterClass;
2330
2331 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002332 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002333 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002334
2335 SDValue ArgValue2;
2336 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002337 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002338 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002339
2340 // Create load node to retrieve arguments from the stack.
2341 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002342 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002343 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002344 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002345 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002346 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002347 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002348 }
2349
Jim Grosbache5165492009-11-09 00:11:35 +00002350 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002351}
2352
Stuart Hastingsc7315872011-04-20 16:47:52 +00002353void
2354ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2355 unsigned &VARegSize, unsigned &VARegSaveSize)
2356 const {
2357 unsigned NumGPRs;
2358 if (CCInfo.isFirstByValRegValid())
2359 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2360 else {
2361 unsigned int firstUnalloced;
2362 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2363 sizeof(GPRArgRegs) /
2364 sizeof(GPRArgRegs[0]));
2365 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2366 }
2367
2368 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2369 VARegSize = NumGPRs * 4;
2370 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2371}
2372
2373// The remaining GPRs hold either the beginning of variable-argument
2374// data, or the beginning of an aggregate passed by value (usuall
2375// byval). Either way, we allocate stack slots adjacent to the data
2376// provided by our caller, and store the unallocated registers there.
2377// If this is a variadic function, the va_list pointer will begin with
2378// these values; otherwise, this reassembles a (byval) structure that
2379// was split between registers and memory.
2380void
2381ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2382 DebugLoc dl, SDValue &Chain,
2383 unsigned ArgOffset) const {
2384 MachineFunction &MF = DAG.getMachineFunction();
2385 MachineFrameInfo *MFI = MF.getFrameInfo();
2386 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2387 unsigned firstRegToSaveIndex;
2388 if (CCInfo.isFirstByValRegValid())
2389 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2390 else {
2391 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2392 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2393 }
2394
2395 unsigned VARegSize, VARegSaveSize;
2396 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2397 if (VARegSaveSize) {
2398 // If this function is vararg, store any remaining integer argument regs
2399 // to their spots on the stack so that they may be loaded by deferencing
2400 // the result of va_next.
2401 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002402 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2403 ArgOffset + VARegSaveSize
2404 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002405 false));
2406 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2407 getPointerTy());
2408
2409 SmallVector<SDValue, 4> MemOps;
2410 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2411 TargetRegisterClass *RC;
2412 if (AFI->isThumb1OnlyFunction())
2413 RC = ARM::tGPRRegisterClass;
2414 else
2415 RC = ARM::GPRRegisterClass;
2416
2417 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2418 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2419 SDValue Store =
2420 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002421 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002422 false, false, 0);
2423 MemOps.push_back(Store);
2424 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2425 DAG.getConstant(4, getPointerTy()));
2426 }
2427 if (!MemOps.empty())
2428 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2429 &MemOps[0], MemOps.size());
2430 } else
2431 // This will point to the next argument passed via stack.
2432 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2433}
2434
Bob Wilson5bafff32009-06-22 23:27:02 +00002435SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002436ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002437 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002438 const SmallVectorImpl<ISD::InputArg>
2439 &Ins,
2440 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002441 SmallVectorImpl<SDValue> &InVals)
2442 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002443 MachineFunction &MF = DAG.getMachineFunction();
2444 MachineFrameInfo *MFI = MF.getFrameInfo();
2445
Bob Wilson1f595bb2009-04-17 19:07:39 +00002446 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2447
2448 // Assign locations to all of the incoming arguments.
2449 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002450 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2451 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002452 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002453 CCAssignFnForNode(CallConv, /* Return*/ false,
2454 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002455
2456 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002457 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002458
Stuart Hastingsf222e592011-02-28 17:17:53 +00002459 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002460 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2461 CCValAssign &VA = ArgLocs[i];
2462
Bob Wilsondee46d72009-04-17 20:35:10 +00002463 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002464 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002465 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002466
Bob Wilson1f595bb2009-04-17 19:07:39 +00002467 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002468 // f64 and vector types are split up into multiple registers or
2469 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002470 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002471 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002473 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002474 SDValue ArgValue2;
2475 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002476 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002477 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2478 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002479 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002480 false, false, 0);
2481 } else {
2482 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2483 Chain, DAG, dl);
2484 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002485 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2486 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002487 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002488 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002489 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2490 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002491 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002492
Bob Wilson5bafff32009-06-22 23:27:02 +00002493 } else {
2494 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002495
Owen Anderson825b72b2009-08-11 20:47:22 +00002496 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002497 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002498 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002499 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002500 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002501 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002502 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002503 RC = (AFI->isThumb1OnlyFunction() ?
2504 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002505 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002506 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002507
2508 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002509 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002510 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002511 }
2512
2513 // If this is an 8 or 16-bit value, it is really passed promoted
2514 // to 32 bits. Insert an assert[sz]ext to capture this, then
2515 // truncate to the right size.
2516 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002517 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002518 case CCValAssign::Full: break;
2519 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002520 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002521 break;
2522 case CCValAssign::SExt:
2523 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2524 DAG.getValueType(VA.getValVT()));
2525 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2526 break;
2527 case CCValAssign::ZExt:
2528 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2529 DAG.getValueType(VA.getValVT()));
2530 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2531 break;
2532 }
2533
Dan Gohman98ca4f22009-08-05 01:29:28 +00002534 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002535
2536 } else { // VA.isRegLoc()
2537
2538 // sanity check
2539 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002540 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002541
Stuart Hastingsf222e592011-02-28 17:17:53 +00002542 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002543
Stuart Hastingsf222e592011-02-28 17:17:53 +00002544 // Some Ins[] entries become multiple ArgLoc[] entries.
2545 // Process them only once.
2546 if (index != lastInsIndex)
2547 {
2548 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002549 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002550 // This can be changed with more analysis.
2551 // In case of tail call optimization mark all arguments mutable.
2552 // Since they could be overwritten by lowering of arguments in case of
2553 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002554 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002555 unsigned VARegSize, VARegSaveSize;
2556 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2557 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2558 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002559 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002560 int FI = MFI->CreateFixedObject(Bytes,
2561 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002562 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2563 } else {
2564 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2565 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002566
Stuart Hastingsf222e592011-02-28 17:17:53 +00002567 // Create load nodes to retrieve arguments from the stack.
2568 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2569 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2570 MachinePointerInfo::getFixedStack(FI),
2571 false, false, 0));
2572 }
2573 lastInsIndex = index;
2574 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002575 }
2576 }
2577
2578 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002579 if (isVarArg)
2580 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002581
Dan Gohman98ca4f22009-08-05 01:29:28 +00002582 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002583}
2584
2585/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002586static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002587 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002588 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002589 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002590 // Maybe this has already been legalized into the constant pool?
2591 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002592 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002593 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002594 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002595 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002596 }
2597 }
2598 return false;
2599}
2600
Evan Chenga8e29892007-01-19 07:51:42 +00002601/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2602/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002603SDValue
2604ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002605 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002606 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002607 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002608 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002609 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002610 // Constant does not fit, try adjusting it by one?
2611 switch (CC) {
2612 default: break;
2613 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002614 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002615 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002616 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002617 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002618 }
2619 break;
2620 case ISD::SETULT:
2621 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002622 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002623 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002624 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002625 }
2626 break;
2627 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002628 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002629 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002630 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002631 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002632 }
2633 break;
2634 case ISD::SETULE:
2635 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002636 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002637 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002638 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002639 }
2640 break;
2641 }
2642 }
2643 }
2644
2645 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002646 ARMISD::NodeType CompareType;
2647 switch (CondCode) {
2648 default:
2649 CompareType = ARMISD::CMP;
2650 break;
2651 case ARMCC::EQ:
2652 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002653 // Uses only Z Flag
2654 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002655 break;
2656 }
Evan Cheng218977b2010-07-13 19:27:42 +00002657 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002658 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002659}
2660
2661/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002662SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002663ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002664 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002665 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002666 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002667 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002668 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002669 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2670 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002671}
2672
Bob Wilson79f56c92011-03-08 01:17:20 +00002673/// duplicateCmp - Glue values can have only one use, so this function
2674/// duplicates a comparison node.
2675SDValue
2676ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2677 unsigned Opc = Cmp.getOpcode();
2678 DebugLoc DL = Cmp.getDebugLoc();
2679 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2680 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2681
2682 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2683 Cmp = Cmp.getOperand(0);
2684 Opc = Cmp.getOpcode();
2685 if (Opc == ARMISD::CMPFP)
2686 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2687 else {
2688 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2689 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2690 }
2691 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2692}
2693
Bill Wendlingde2b1512010-08-11 08:43:16 +00002694SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2695 SDValue Cond = Op.getOperand(0);
2696 SDValue SelectTrue = Op.getOperand(1);
2697 SDValue SelectFalse = Op.getOperand(2);
2698 DebugLoc dl = Op.getDebugLoc();
2699
2700 // Convert:
2701 //
2702 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2703 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2704 //
2705 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2706 const ConstantSDNode *CMOVTrue =
2707 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2708 const ConstantSDNode *CMOVFalse =
2709 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2710
2711 if (CMOVTrue && CMOVFalse) {
2712 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2713 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2714
2715 SDValue True;
2716 SDValue False;
2717 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2718 True = SelectTrue;
2719 False = SelectFalse;
2720 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2721 True = SelectFalse;
2722 False = SelectTrue;
2723 }
2724
2725 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002726 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002727 SDValue ARMcc = Cond.getOperand(2);
2728 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002729 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002730 assert(True.getValueType() == VT);
2731 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002732 }
2733 }
2734 }
2735
2736 return DAG.getSelectCC(dl, Cond,
2737 DAG.getConstant(0, Cond.getValueType()),
2738 SelectTrue, SelectFalse, ISD::SETNE);
2739}
2740
Dan Gohmand858e902010-04-17 15:26:15 +00002741SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002742 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002743 SDValue LHS = Op.getOperand(0);
2744 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002745 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002746 SDValue TrueVal = Op.getOperand(2);
2747 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002748 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002749
Owen Anderson825b72b2009-08-11 20:47:22 +00002750 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002751 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002752 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002753 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2754 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002755 }
2756
2757 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002758 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002759
Evan Cheng218977b2010-07-13 19:27:42 +00002760 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2761 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002762 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002763 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002764 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002765 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002766 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002767 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002768 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002769 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002770 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002771 }
2772 return Result;
2773}
2774
Evan Cheng218977b2010-07-13 19:27:42 +00002775/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2776/// to morph to an integer compare sequence.
2777static bool canChangeToInt(SDValue Op, bool &SeenZero,
2778 const ARMSubtarget *Subtarget) {
2779 SDNode *N = Op.getNode();
2780 if (!N->hasOneUse())
2781 // Otherwise it requires moving the value from fp to integer registers.
2782 return false;
2783 if (!N->getNumValues())
2784 return false;
2785 EVT VT = Op.getValueType();
2786 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2787 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2788 // vmrs are very slow, e.g. cortex-a8.
2789 return false;
2790
2791 if (isFloatingPointZero(Op)) {
2792 SeenZero = true;
2793 return true;
2794 }
2795 return ISD::isNormalLoad(N);
2796}
2797
2798static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2799 if (isFloatingPointZero(Op))
2800 return DAG.getConstant(0, MVT::i32);
2801
2802 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2803 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002804 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002805 Ld->isVolatile(), Ld->isNonTemporal(),
2806 Ld->getAlignment());
2807
2808 llvm_unreachable("Unknown VFP cmp argument!");
2809}
2810
2811static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2812 SDValue &RetVal1, SDValue &RetVal2) {
2813 if (isFloatingPointZero(Op)) {
2814 RetVal1 = DAG.getConstant(0, MVT::i32);
2815 RetVal2 = DAG.getConstant(0, MVT::i32);
2816 return;
2817 }
2818
2819 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2820 SDValue Ptr = Ld->getBasePtr();
2821 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2822 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002823 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002824 Ld->isVolatile(), Ld->isNonTemporal(),
2825 Ld->getAlignment());
2826
2827 EVT PtrType = Ptr.getValueType();
2828 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2829 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2830 PtrType, Ptr, DAG.getConstant(4, PtrType));
2831 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2832 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002833 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002834 Ld->isVolatile(), Ld->isNonTemporal(),
2835 NewAlign);
2836 return;
2837 }
2838
2839 llvm_unreachable("Unknown VFP cmp argument!");
2840}
2841
2842/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2843/// f32 and even f64 comparisons to integer ones.
2844SDValue
2845ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2846 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002847 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002848 SDValue LHS = Op.getOperand(2);
2849 SDValue RHS = Op.getOperand(3);
2850 SDValue Dest = Op.getOperand(4);
2851 DebugLoc dl = Op.getDebugLoc();
2852
2853 bool SeenZero = false;
2854 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2855 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002856 // If one of the operand is zero, it's safe to ignore the NaN case since
2857 // we only care about equality comparisons.
2858 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002859 // If unsafe fp math optimization is enabled and there are no other uses of
2860 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002861 // to an integer comparison.
2862 if (CC == ISD::SETOEQ)
2863 CC = ISD::SETEQ;
2864 else if (CC == ISD::SETUNE)
2865 CC = ISD::SETNE;
2866
2867 SDValue ARMcc;
2868 if (LHS.getValueType() == MVT::f32) {
2869 LHS = bitcastf32Toi32(LHS, DAG);
2870 RHS = bitcastf32Toi32(RHS, DAG);
2871 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2872 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2873 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2874 Chain, Dest, ARMcc, CCR, Cmp);
2875 }
2876
2877 SDValue LHS1, LHS2;
2878 SDValue RHS1, RHS2;
2879 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2880 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2881 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2882 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002883 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002884 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2885 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2886 }
2887
2888 return SDValue();
2889}
2890
2891SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2892 SDValue Chain = Op.getOperand(0);
2893 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2894 SDValue LHS = Op.getOperand(2);
2895 SDValue RHS = Op.getOperand(3);
2896 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002897 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002898
Owen Anderson825b72b2009-08-11 20:47:22 +00002899 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002900 SDValue ARMcc;
2901 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002902 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002903 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002904 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002905 }
2906
Owen Anderson825b72b2009-08-11 20:47:22 +00002907 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002908
2909 if (UnsafeFPMath &&
2910 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2911 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2912 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2913 if (Result.getNode())
2914 return Result;
2915 }
2916
Evan Chenga8e29892007-01-19 07:51:42 +00002917 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002918 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002919
Evan Cheng218977b2010-07-13 19:27:42 +00002920 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2921 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002922 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002923 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002924 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002925 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002926 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002927 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2928 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002929 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002930 }
2931 return Res;
2932}
2933
Dan Gohmand858e902010-04-17 15:26:15 +00002934SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002935 SDValue Chain = Op.getOperand(0);
2936 SDValue Table = Op.getOperand(1);
2937 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002938 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002939
Owen Andersone50ed302009-08-10 22:56:29 +00002940 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002941 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2942 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002943 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002944 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002945 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002946 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2947 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002948 if (Subtarget->isThumb2()) {
2949 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2950 // which does another jump to the destination. This also makes it easier
2951 // to translate it to TBB / TBH later.
2952 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002953 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002954 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002955 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002956 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002957 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002958 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002959 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002960 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002961 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002962 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002963 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002964 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002965 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002966 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002967 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002968 }
Evan Chenga8e29892007-01-19 07:51:42 +00002969}
2970
Bob Wilson76a312b2010-03-19 22:51:32 +00002971static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2972 DebugLoc dl = Op.getDebugLoc();
2973 unsigned Opc;
2974
2975 switch (Op.getOpcode()) {
2976 default:
2977 assert(0 && "Invalid opcode!");
2978 case ISD::FP_TO_SINT:
2979 Opc = ARMISD::FTOSI;
2980 break;
2981 case ISD::FP_TO_UINT:
2982 Opc = ARMISD::FTOUI;
2983 break;
2984 }
2985 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002986 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002987}
2988
Cameron Zwarich3007d332011-03-29 21:41:55 +00002989static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2990 EVT VT = Op.getValueType();
2991 DebugLoc dl = Op.getDebugLoc();
2992
2993 EVT OperandVT = Op.getOperand(0).getValueType();
2994 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2995 if (VT != MVT::v4f32)
2996 return DAG.UnrollVectorOp(Op.getNode());
2997
2998 unsigned CastOpc;
2999 unsigned Opc;
3000 switch (Op.getOpcode()) {
3001 default:
3002 assert(0 && "Invalid opcode!");
3003 case ISD::SINT_TO_FP:
3004 CastOpc = ISD::SIGN_EXTEND;
3005 Opc = ISD::SINT_TO_FP;
3006 break;
3007 case ISD::UINT_TO_FP:
3008 CastOpc = ISD::ZERO_EXTEND;
3009 Opc = ISD::UINT_TO_FP;
3010 break;
3011 }
3012
3013 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3014 return DAG.getNode(Opc, dl, VT, Op);
3015}
3016
Bob Wilson76a312b2010-03-19 22:51:32 +00003017static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3018 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003019 if (VT.isVector())
3020 return LowerVectorINT_TO_FP(Op, DAG);
3021
Bob Wilson76a312b2010-03-19 22:51:32 +00003022 DebugLoc dl = Op.getDebugLoc();
3023 unsigned Opc;
3024
3025 switch (Op.getOpcode()) {
3026 default:
3027 assert(0 && "Invalid opcode!");
3028 case ISD::SINT_TO_FP:
3029 Opc = ARMISD::SITOF;
3030 break;
3031 case ISD::UINT_TO_FP:
3032 Opc = ARMISD::UITOF;
3033 break;
3034 }
3035
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003036 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003037 return DAG.getNode(Opc, dl, VT, Op);
3038}
3039
Evan Cheng515fe3a2010-07-08 02:08:50 +00003040SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003041 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003042 SDValue Tmp0 = Op.getOperand(0);
3043 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003044 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003045 EVT VT = Op.getValueType();
3046 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003047 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3048 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3049 bool UseNEON = !InGPR && Subtarget->hasNEON();
3050
3051 if (UseNEON) {
3052 // Use VBSL to copy the sign bit.
3053 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3054 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3055 DAG.getTargetConstant(EncodedVal, MVT::i32));
3056 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3057 if (VT == MVT::f64)
3058 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3059 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3060 DAG.getConstant(32, MVT::i32));
3061 else /*if (VT == MVT::f32)*/
3062 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3063 if (SrcVT == MVT::f32) {
3064 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3065 if (VT == MVT::f64)
3066 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3067 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3068 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003069 } else if (VT == MVT::f32)
3070 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3071 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3072 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003073 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3074 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3075
3076 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3077 MVT::i32);
3078 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3079 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3080 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003081
Evan Chenge573fb32011-02-23 02:24:55 +00003082 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3083 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3084 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003085 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003086 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3087 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3088 DAG.getConstant(0, MVT::i32));
3089 } else {
3090 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3091 }
3092
3093 return Res;
3094 }
Evan Chengc143dd42011-02-11 02:28:55 +00003095
3096 // Bitcast operand 1 to i32.
3097 if (SrcVT == MVT::f64)
3098 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3099 &Tmp1, 1).getValue(1);
3100 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3101
Evan Chenge573fb32011-02-23 02:24:55 +00003102 // Or in the signbit with integer operations.
3103 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3104 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3105 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3106 if (VT == MVT::f32) {
3107 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3108 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3109 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3110 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003111 }
3112
Evan Chenge573fb32011-02-23 02:24:55 +00003113 // f64: Or the high part with signbit and then combine two parts.
3114 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3115 &Tmp0, 1);
3116 SDValue Lo = Tmp0.getValue(0);
3117 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3118 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3119 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003120}
3121
Evan Cheng2457f2c2010-05-22 01:47:14 +00003122SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3123 MachineFunction &MF = DAG.getMachineFunction();
3124 MachineFrameInfo *MFI = MF.getFrameInfo();
3125 MFI->setReturnAddressIsTaken(true);
3126
3127 EVT VT = Op.getValueType();
3128 DebugLoc dl = Op.getDebugLoc();
3129 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3130 if (Depth) {
3131 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3132 SDValue Offset = DAG.getConstant(4, MVT::i32);
3133 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3134 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003135 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003136 }
3137
3138 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003139 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003140 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3141}
3142
Dan Gohmand858e902010-04-17 15:26:15 +00003143SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003144 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3145 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003146
Owen Andersone50ed302009-08-10 22:56:29 +00003147 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003148 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3149 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003150 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003151 ? ARM::R7 : ARM::R11;
3152 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3153 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003154 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3155 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003156 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003157 return FrameAddr;
3158}
3159
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003160/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003161/// expand a bit convert where either the source or destination type is i64 to
3162/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3163/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3164/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003165static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003166 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3167 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003168 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003169
Bob Wilson9f3f0612010-04-17 05:30:19 +00003170 // This function is only supposed to be called for i64 types, either as the
3171 // source or destination of the bit convert.
3172 EVT SrcVT = Op.getValueType();
3173 EVT DstVT = N->getValueType(0);
3174 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003175 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003176
Bob Wilson9f3f0612010-04-17 05:30:19 +00003177 // Turn i64->f64 into VMOVDRR.
3178 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003179 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3180 DAG.getConstant(0, MVT::i32));
3181 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3182 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003183 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003184 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003185 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003186
Jim Grosbache5165492009-11-09 00:11:35 +00003187 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003188 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3189 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3190 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3191 // Merge the pieces into a single i64 value.
3192 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3193 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003194
Bob Wilson9f3f0612010-04-17 05:30:19 +00003195 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003196}
3197
Bob Wilson5bafff32009-06-22 23:27:02 +00003198/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003199/// Zero vectors are used to represent vector negation and in those cases
3200/// will be implemented with the NEON VNEG instruction. However, VNEG does
3201/// not support i64 elements, so sometimes the zero vectors will need to be
3202/// explicitly constructed. Regardless, use a canonical VMOV to create the
3203/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003204static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003205 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003206 // The canonical modified immediate encoding of a zero vector is....0!
3207 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3208 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3209 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003210 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003211}
3212
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003213/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3214/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003215SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3216 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003217 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3218 EVT VT = Op.getValueType();
3219 unsigned VTBits = VT.getSizeInBits();
3220 DebugLoc dl = Op.getDebugLoc();
3221 SDValue ShOpLo = Op.getOperand(0);
3222 SDValue ShOpHi = Op.getOperand(1);
3223 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003224 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003225 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003226
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003227 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3228
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003229 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3230 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3231 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3232 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3233 DAG.getConstant(VTBits, MVT::i32));
3234 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3235 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003236 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003237
3238 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3239 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003240 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003241 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003242 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003243 CCR, Cmp);
3244
3245 SDValue Ops[2] = { Lo, Hi };
3246 return DAG.getMergeValues(Ops, 2, dl);
3247}
3248
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003249/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3250/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003251SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3252 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003253 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3254 EVT VT = Op.getValueType();
3255 unsigned VTBits = VT.getSizeInBits();
3256 DebugLoc dl = Op.getDebugLoc();
3257 SDValue ShOpLo = Op.getOperand(0);
3258 SDValue ShOpHi = Op.getOperand(1);
3259 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003260 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003261
3262 assert(Op.getOpcode() == ISD::SHL_PARTS);
3263 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3264 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3265 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3266 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3267 DAG.getConstant(VTBits, MVT::i32));
3268 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3269 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3270
3271 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3272 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3273 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003274 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003275 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003276 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003277 CCR, Cmp);
3278
3279 SDValue Ops[2] = { Lo, Hi };
3280 return DAG.getMergeValues(Ops, 2, dl);
3281}
3282
Jim Grosbach4725ca72010-09-08 03:54:02 +00003283SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003284 SelectionDAG &DAG) const {
3285 // The rounding mode is in bits 23:22 of the FPSCR.
3286 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3287 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3288 // so that the shift + and get folded into a bitfield extract.
3289 DebugLoc dl = Op.getDebugLoc();
3290 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3291 DAG.getConstant(Intrinsic::arm_get_fpscr,
3292 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003293 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003294 DAG.getConstant(1U << 22, MVT::i32));
3295 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3296 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003297 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003298 DAG.getConstant(3, MVT::i32));
3299}
3300
Jim Grosbach3482c802010-01-18 19:58:49 +00003301static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3302 const ARMSubtarget *ST) {
3303 EVT VT = N->getValueType(0);
3304 DebugLoc dl = N->getDebugLoc();
3305
3306 if (!ST->hasV6T2Ops())
3307 return SDValue();
3308
3309 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3310 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3311}
3312
Bob Wilson5bafff32009-06-22 23:27:02 +00003313static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3314 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003315 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003316 DebugLoc dl = N->getDebugLoc();
3317
Bob Wilsond5448bb2010-11-18 21:16:28 +00003318 if (!VT.isVector())
3319 return SDValue();
3320
Bob Wilson5bafff32009-06-22 23:27:02 +00003321 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003322 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003323
Bob Wilsond5448bb2010-11-18 21:16:28 +00003324 // Left shifts translate directly to the vshiftu intrinsic.
3325 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003326 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003327 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3328 N->getOperand(0), N->getOperand(1));
3329
3330 assert((N->getOpcode() == ISD::SRA ||
3331 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3332
3333 // NEON uses the same intrinsics for both left and right shifts. For
3334 // right shifts, the shift amounts are negative, so negate the vector of
3335 // shift amounts.
3336 EVT ShiftVT = N->getOperand(1).getValueType();
3337 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3338 getZeroVector(ShiftVT, DAG, dl),
3339 N->getOperand(1));
3340 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3341 Intrinsic::arm_neon_vshifts :
3342 Intrinsic::arm_neon_vshiftu);
3343 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3344 DAG.getConstant(vshiftInt, MVT::i32),
3345 N->getOperand(0), NegatedCount);
3346}
3347
3348static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3349 const ARMSubtarget *ST) {
3350 EVT VT = N->getValueType(0);
3351 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003352
Eli Friedmance392eb2009-08-22 03:13:10 +00003353 // We can get here for a node like i32 = ISD::SHL i32, i64
3354 if (VT != MVT::i64)
3355 return SDValue();
3356
3357 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003358 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003359
Chris Lattner27a6c732007-11-24 07:07:01 +00003360 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3361 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003362 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003363 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003364
Chris Lattner27a6c732007-11-24 07:07:01 +00003365 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003366 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003367
Chris Lattner27a6c732007-11-24 07:07:01 +00003368 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003370 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003371 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003372 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003373
Chris Lattner27a6c732007-11-24 07:07:01 +00003374 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3375 // captures the result into a carry flag.
3376 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003377 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003378
Chris Lattner27a6c732007-11-24 07:07:01 +00003379 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003380 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003381
Chris Lattner27a6c732007-11-24 07:07:01 +00003382 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003383 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003384}
3385
Bob Wilson5bafff32009-06-22 23:27:02 +00003386static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3387 SDValue TmpOp0, TmpOp1;
3388 bool Invert = false;
3389 bool Swap = false;
3390 unsigned Opc = 0;
3391
3392 SDValue Op0 = Op.getOperand(0);
3393 SDValue Op1 = Op.getOperand(1);
3394 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003395 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003396 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3397 DebugLoc dl = Op.getDebugLoc();
3398
3399 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3400 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003401 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003402 case ISD::SETUNE:
3403 case ISD::SETNE: Invert = true; // Fallthrough
3404 case ISD::SETOEQ:
3405 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3406 case ISD::SETOLT:
3407 case ISD::SETLT: Swap = true; // Fallthrough
3408 case ISD::SETOGT:
3409 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3410 case ISD::SETOLE:
3411 case ISD::SETLE: Swap = true; // Fallthrough
3412 case ISD::SETOGE:
3413 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3414 case ISD::SETUGE: Swap = true; // Fallthrough
3415 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3416 case ISD::SETUGT: Swap = true; // Fallthrough
3417 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3418 case ISD::SETUEQ: Invert = true; // Fallthrough
3419 case ISD::SETONE:
3420 // Expand this to (OLT | OGT).
3421 TmpOp0 = Op0;
3422 TmpOp1 = Op1;
3423 Opc = ISD::OR;
3424 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3425 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3426 break;
3427 case ISD::SETUO: Invert = true; // Fallthrough
3428 case ISD::SETO:
3429 // Expand this to (OLT | OGE).
3430 TmpOp0 = Op0;
3431 TmpOp1 = Op1;
3432 Opc = ISD::OR;
3433 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3434 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3435 break;
3436 }
3437 } else {
3438 // Integer comparisons.
3439 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003440 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003441 case ISD::SETNE: Invert = true;
3442 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3443 case ISD::SETLT: Swap = true;
3444 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3445 case ISD::SETLE: Swap = true;
3446 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3447 case ISD::SETULT: Swap = true;
3448 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3449 case ISD::SETULE: Swap = true;
3450 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3451 }
3452
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003453 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003454 if (Opc == ARMISD::VCEQ) {
3455
3456 SDValue AndOp;
3457 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3458 AndOp = Op0;
3459 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3460 AndOp = Op1;
3461
3462 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003463 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003464 AndOp = AndOp.getOperand(0);
3465
3466 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3467 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003468 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3469 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003470 Invert = !Invert;
3471 }
3472 }
3473 }
3474
3475 if (Swap)
3476 std::swap(Op0, Op1);
3477
Owen Andersonc24cb352010-11-08 23:21:22 +00003478 // If one of the operands is a constant vector zero, attempt to fold the
3479 // comparison to a specialized compare-against-zero form.
3480 SDValue SingleOp;
3481 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3482 SingleOp = Op0;
3483 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3484 if (Opc == ARMISD::VCGE)
3485 Opc = ARMISD::VCLEZ;
3486 else if (Opc == ARMISD::VCGT)
3487 Opc = ARMISD::VCLTZ;
3488 SingleOp = Op1;
3489 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003490
Owen Andersonc24cb352010-11-08 23:21:22 +00003491 SDValue Result;
3492 if (SingleOp.getNode()) {
3493 switch (Opc) {
3494 case ARMISD::VCEQ:
3495 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3496 case ARMISD::VCGE:
3497 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3498 case ARMISD::VCLEZ:
3499 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3500 case ARMISD::VCGT:
3501 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3502 case ARMISD::VCLTZ:
3503 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3504 default:
3505 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3506 }
3507 } else {
3508 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3509 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003510
3511 if (Invert)
3512 Result = DAG.getNOT(dl, Result, VT);
3513
3514 return Result;
3515}
3516
Bob Wilsond3c42842010-06-14 22:19:57 +00003517/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3518/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003519/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003520static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3521 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003522 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003523 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003524
Bob Wilson827b2102010-06-15 19:05:35 +00003525 // SplatBitSize is set to the smallest size that splats the vector, so a
3526 // zero vector will always have SplatBitSize == 8. However, NEON modified
3527 // immediate instructions others than VMOV do not support the 8-bit encoding
3528 // of a zero vector, and the default encoding of zero is supposed to be the
3529 // 32-bit version.
3530 if (SplatBits == 0)
3531 SplatBitSize = 32;
3532
Bob Wilson5bafff32009-06-22 23:27:02 +00003533 switch (SplatBitSize) {
3534 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003535 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003536 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003537 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003538 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003539 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003540 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003541 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003542 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003543
3544 case 16:
3545 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003546 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003547 if ((SplatBits & ~0xff) == 0) {
3548 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003549 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003550 Imm = SplatBits;
3551 break;
3552 }
3553 if ((SplatBits & ~0xff00) == 0) {
3554 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003555 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003556 Imm = SplatBits >> 8;
3557 break;
3558 }
3559 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003560
3561 case 32:
3562 // NEON's 32-bit VMOV supports splat values where:
3563 // * only one byte is nonzero, or
3564 // * the least significant byte is 0xff and the second byte is nonzero, or
3565 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003566 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003567 if ((SplatBits & ~0xff) == 0) {
3568 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003569 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003570 Imm = SplatBits;
3571 break;
3572 }
3573 if ((SplatBits & ~0xff00) == 0) {
3574 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003575 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003576 Imm = SplatBits >> 8;
3577 break;
3578 }
3579 if ((SplatBits & ~0xff0000) == 0) {
3580 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003581 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003582 Imm = SplatBits >> 16;
3583 break;
3584 }
3585 if ((SplatBits & ~0xff000000) == 0) {
3586 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003587 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003588 Imm = SplatBits >> 24;
3589 break;
3590 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003591
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003592 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3593 if (type == OtherModImm) return SDValue();
3594
Bob Wilson5bafff32009-06-22 23:27:02 +00003595 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003596 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3597 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003598 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003599 Imm = SplatBits >> 8;
3600 SplatBits |= 0xff;
3601 break;
3602 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003603
3604 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003605 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3606 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003607 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003608 Imm = SplatBits >> 16;
3609 SplatBits |= 0xffff;
3610 break;
3611 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003612
3613 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3614 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3615 // VMOV.I32. A (very) minor optimization would be to replicate the value
3616 // and fall through here to test for a valid 64-bit splat. But, then the
3617 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003618 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003619
3620 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003621 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003622 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003623 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003624 uint64_t BitMask = 0xff;
3625 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003626 unsigned ImmMask = 1;
3627 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003628 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003629 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003630 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003631 Imm |= ImmMask;
3632 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003633 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003634 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003635 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003636 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003637 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003638 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003639 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003640 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003641 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003642 break;
3643 }
3644
Bob Wilson1a913ed2010-06-11 21:34:50 +00003645 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003646 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003647 return SDValue();
3648 }
3649
Bob Wilsoncba270d2010-07-13 21:16:48 +00003650 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3651 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003652}
3653
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003654static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3655 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003656 unsigned NumElts = VT.getVectorNumElements();
3657 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003658
3659 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3660 if (M[0] < 0)
3661 return false;
3662
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003663 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003664
3665 // If this is a VEXT shuffle, the immediate value is the index of the first
3666 // element. The other shuffle indices must be the successive elements after
3667 // the first one.
3668 unsigned ExpectedElt = Imm;
3669 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003670 // Increment the expected index. If it wraps around, it may still be
3671 // a VEXT but the source vectors must be swapped.
3672 ExpectedElt += 1;
3673 if (ExpectedElt == NumElts * 2) {
3674 ExpectedElt = 0;
3675 ReverseVEXT = true;
3676 }
3677
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003678 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003679 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003680 return false;
3681 }
3682
3683 // Adjust the index value if the source operands will be swapped.
3684 if (ReverseVEXT)
3685 Imm -= NumElts;
3686
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003687 return true;
3688}
3689
Bob Wilson8bb9e482009-07-26 00:39:34 +00003690/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3691/// instruction with the specified blocksize. (The order of the elements
3692/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003693static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3694 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003695 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3696 "Only possible block sizes for VREV are: 16, 32, 64");
3697
Bob Wilson8bb9e482009-07-26 00:39:34 +00003698 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003699 if (EltSz == 64)
3700 return false;
3701
3702 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003703 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003704 // If the first shuffle index is UNDEF, be optimistic.
3705 if (M[0] < 0)
3706 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003707
3708 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3709 return false;
3710
3711 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003712 if (M[i] < 0) continue; // ignore UNDEF indices
3713 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003714 return false;
3715 }
3716
3717 return true;
3718}
3719
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003720static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3721 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3722 // range, then 0 is placed into the resulting vector. So pretty much any mask
3723 // of 8 elements can work here.
3724 return VT == MVT::v8i8 && M.size() == 8;
3725}
3726
Bob Wilsonc692cb72009-08-21 20:54:19 +00003727static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3728 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003729 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3730 if (EltSz == 64)
3731 return false;
3732
Bob Wilsonc692cb72009-08-21 20:54:19 +00003733 unsigned NumElts = VT.getVectorNumElements();
3734 WhichResult = (M[0] == 0 ? 0 : 1);
3735 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003736 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3737 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003738 return false;
3739 }
3740 return true;
3741}
3742
Bob Wilson324f4f12009-12-03 06:40:55 +00003743/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3744/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3745/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3746static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3747 unsigned &WhichResult) {
3748 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3749 if (EltSz == 64)
3750 return false;
3751
3752 unsigned NumElts = VT.getVectorNumElements();
3753 WhichResult = (M[0] == 0 ? 0 : 1);
3754 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003755 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3756 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003757 return false;
3758 }
3759 return true;
3760}
3761
Bob Wilsonc692cb72009-08-21 20:54:19 +00003762static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3763 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003764 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3765 if (EltSz == 64)
3766 return false;
3767
Bob Wilsonc692cb72009-08-21 20:54:19 +00003768 unsigned NumElts = VT.getVectorNumElements();
3769 WhichResult = (M[0] == 0 ? 0 : 1);
3770 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003771 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003772 if ((unsigned) M[i] != 2 * i + WhichResult)
3773 return false;
3774 }
3775
3776 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003777 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003778 return false;
3779
3780 return true;
3781}
3782
Bob Wilson324f4f12009-12-03 06:40:55 +00003783/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3784/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3785/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3786static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3787 unsigned &WhichResult) {
3788 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3789 if (EltSz == 64)
3790 return false;
3791
3792 unsigned Half = VT.getVectorNumElements() / 2;
3793 WhichResult = (M[0] == 0 ? 0 : 1);
3794 for (unsigned j = 0; j != 2; ++j) {
3795 unsigned Idx = WhichResult;
3796 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003797 int MIdx = M[i + j * Half];
3798 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003799 return false;
3800 Idx += 2;
3801 }
3802 }
3803
3804 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3805 if (VT.is64BitVector() && EltSz == 32)
3806 return false;
3807
3808 return true;
3809}
3810
Bob Wilsonc692cb72009-08-21 20:54:19 +00003811static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3812 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003813 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3814 if (EltSz == 64)
3815 return false;
3816
Bob Wilsonc692cb72009-08-21 20:54:19 +00003817 unsigned NumElts = VT.getVectorNumElements();
3818 WhichResult = (M[0] == 0 ? 0 : 1);
3819 unsigned Idx = WhichResult * NumElts / 2;
3820 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003821 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3822 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003823 return false;
3824 Idx += 1;
3825 }
3826
3827 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003828 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003829 return false;
3830
3831 return true;
3832}
3833
Bob Wilson324f4f12009-12-03 06:40:55 +00003834/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3835/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3836/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3837static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3838 unsigned &WhichResult) {
3839 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3840 if (EltSz == 64)
3841 return false;
3842
3843 unsigned NumElts = VT.getVectorNumElements();
3844 WhichResult = (M[0] == 0 ? 0 : 1);
3845 unsigned Idx = WhichResult * NumElts / 2;
3846 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003847 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3848 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003849 return false;
3850 Idx += 1;
3851 }
3852
3853 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3854 if (VT.is64BitVector() && EltSz == 32)
3855 return false;
3856
3857 return true;
3858}
3859
Dale Johannesenf630c712010-07-29 20:10:08 +00003860// If N is an integer constant that can be moved into a register in one
3861// instruction, return an SDValue of such a constant (will become a MOV
3862// instruction). Otherwise return null.
3863static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3864 const ARMSubtarget *ST, DebugLoc dl) {
3865 uint64_t Val;
3866 if (!isa<ConstantSDNode>(N))
3867 return SDValue();
3868 Val = cast<ConstantSDNode>(N)->getZExtValue();
3869
3870 if (ST->isThumb1Only()) {
3871 if (Val <= 255 || ~Val <= 255)
3872 return DAG.getConstant(Val, MVT::i32);
3873 } else {
3874 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3875 return DAG.getConstant(Val, MVT::i32);
3876 }
3877 return SDValue();
3878}
3879
Bob Wilson5bafff32009-06-22 23:27:02 +00003880// If this is a case we can't handle, return null and let the default
3881// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003882SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3883 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003884 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003885 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003886 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003887
3888 APInt SplatBits, SplatUndef;
3889 unsigned SplatBitSize;
3890 bool HasAnyUndefs;
3891 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003892 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003893 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003894 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003895 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003896 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003897 DAG, VmovVT, VT.is128BitVector(),
3898 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003899 if (Val.getNode()) {
3900 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003901 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003902 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003903
3904 // Try an immediate VMVN.
3905 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3906 ((1LL << SplatBitSize) - 1));
3907 Val = isNEONModifiedImm(NegatedImm,
3908 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003909 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003910 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003911 if (Val.getNode()) {
3912 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003913 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003914 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003915 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003916 }
3917
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003918 // Scan through the operands to see if only one value is used.
3919 unsigned NumElts = VT.getVectorNumElements();
3920 bool isOnlyLowElement = true;
3921 bool usesOnlyOneValue = true;
3922 bool isConstant = true;
3923 SDValue Value;
3924 for (unsigned i = 0; i < NumElts; ++i) {
3925 SDValue V = Op.getOperand(i);
3926 if (V.getOpcode() == ISD::UNDEF)
3927 continue;
3928 if (i > 0)
3929 isOnlyLowElement = false;
3930 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3931 isConstant = false;
3932
3933 if (!Value.getNode())
3934 Value = V;
3935 else if (V != Value)
3936 usesOnlyOneValue = false;
3937 }
3938
3939 if (!Value.getNode())
3940 return DAG.getUNDEF(VT);
3941
3942 if (isOnlyLowElement)
3943 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3944
Dale Johannesenf630c712010-07-29 20:10:08 +00003945 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3946
Dale Johannesen575cd142010-10-19 20:00:17 +00003947 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3948 // i32 and try again.
3949 if (usesOnlyOneValue && EltSize <= 32) {
3950 if (!isConstant)
3951 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3952 if (VT.getVectorElementType().isFloatingPoint()) {
3953 SmallVector<SDValue, 8> Ops;
3954 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003955 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003956 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003957 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3958 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003959 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3960 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003961 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003962 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003963 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3964 if (Val.getNode())
3965 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003966 }
3967
3968 // If all elements are constants and the case above didn't get hit, fall back
3969 // to the default expansion, which will generate a load from the constant
3970 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003971 if (isConstant)
3972 return SDValue();
3973
Bob Wilson11a1dff2011-01-07 21:37:30 +00003974 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3975 if (NumElts >= 4) {
3976 SDValue shuffle = ReconstructShuffle(Op, DAG);
3977 if (shuffle != SDValue())
3978 return shuffle;
3979 }
3980
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003981 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003982 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3983 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003984 if (EltSize >= 32) {
3985 // Do the expansion with floating-point types, since that is what the VFP
3986 // registers are defined to use, and since i64 is not legal.
3987 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3988 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003989 SmallVector<SDValue, 8> Ops;
3990 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003991 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003992 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003993 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003994 }
3995
3996 return SDValue();
3997}
3998
Bob Wilson11a1dff2011-01-07 21:37:30 +00003999// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004000// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004001SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4002 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004003 DebugLoc dl = Op.getDebugLoc();
4004 EVT VT = Op.getValueType();
4005 unsigned NumElts = VT.getVectorNumElements();
4006
4007 SmallVector<SDValue, 2> SourceVecs;
4008 SmallVector<unsigned, 2> MinElts;
4009 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004010
Bob Wilson11a1dff2011-01-07 21:37:30 +00004011 for (unsigned i = 0; i < NumElts; ++i) {
4012 SDValue V = Op.getOperand(i);
4013 if (V.getOpcode() == ISD::UNDEF)
4014 continue;
4015 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4016 // A shuffle can only come from building a vector from various
4017 // elements of other vectors.
4018 return SDValue();
4019 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004020
Bob Wilson11a1dff2011-01-07 21:37:30 +00004021 // Record this extraction against the appropriate vector if possible...
4022 SDValue SourceVec = V.getOperand(0);
4023 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4024 bool FoundSource = false;
4025 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4026 if (SourceVecs[j] == SourceVec) {
4027 if (MinElts[j] > EltNo)
4028 MinElts[j] = EltNo;
4029 if (MaxElts[j] < EltNo)
4030 MaxElts[j] = EltNo;
4031 FoundSource = true;
4032 break;
4033 }
4034 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004035
Bob Wilson11a1dff2011-01-07 21:37:30 +00004036 // Or record a new source if not...
4037 if (!FoundSource) {
4038 SourceVecs.push_back(SourceVec);
4039 MinElts.push_back(EltNo);
4040 MaxElts.push_back(EltNo);
4041 }
4042 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004043
Bob Wilson11a1dff2011-01-07 21:37:30 +00004044 // Currently only do something sane when at most two source vectors
4045 // involved.
4046 if (SourceVecs.size() > 2)
4047 return SDValue();
4048
4049 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4050 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004051
Bob Wilson11a1dff2011-01-07 21:37:30 +00004052 // This loop extracts the usage patterns of the source vectors
4053 // and prepares appropriate SDValues for a shuffle if possible.
4054 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4055 if (SourceVecs[i].getValueType() == VT) {
4056 // No VEXT necessary
4057 ShuffleSrcs[i] = SourceVecs[i];
4058 VEXTOffsets[i] = 0;
4059 continue;
4060 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4061 // It probably isn't worth padding out a smaller vector just to
4062 // break it down again in a shuffle.
4063 return SDValue();
4064 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004065
Bob Wilson11a1dff2011-01-07 21:37:30 +00004066 // Since only 64-bit and 128-bit vectors are legal on ARM and
4067 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004068 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4069 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004070
Bob Wilson11a1dff2011-01-07 21:37:30 +00004071 if (MaxElts[i] - MinElts[i] >= NumElts) {
4072 // Span too large for a VEXT to cope
4073 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004074 }
4075
Bob Wilson11a1dff2011-01-07 21:37:30 +00004076 if (MinElts[i] >= NumElts) {
4077 // The extraction can just take the second half
4078 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004079 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4080 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004081 DAG.getIntPtrConstant(NumElts));
4082 } else if (MaxElts[i] < NumElts) {
4083 // The extraction can just take the first half
4084 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004085 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4086 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004087 DAG.getIntPtrConstant(0));
4088 } else {
4089 // An actual VEXT is needed
4090 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004091 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4092 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004093 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004094 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4095 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004096 DAG.getIntPtrConstant(NumElts));
4097 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4098 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4099 }
4100 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004101
Bob Wilson11a1dff2011-01-07 21:37:30 +00004102 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004103
Bob Wilson11a1dff2011-01-07 21:37:30 +00004104 for (unsigned i = 0; i < NumElts; ++i) {
4105 SDValue Entry = Op.getOperand(i);
4106 if (Entry.getOpcode() == ISD::UNDEF) {
4107 Mask.push_back(-1);
4108 continue;
4109 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004110
Bob Wilson11a1dff2011-01-07 21:37:30 +00004111 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004112 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4113 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004114 if (ExtractVec == SourceVecs[0]) {
4115 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4116 } else {
4117 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4118 }
4119 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004120
Bob Wilson11a1dff2011-01-07 21:37:30 +00004121 // Final check before we try to produce nonsense...
4122 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004123 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4124 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004125
Bob Wilson11a1dff2011-01-07 21:37:30 +00004126 return SDValue();
4127}
4128
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004129/// isShuffleMaskLegal - Targets can use this to indicate that they only
4130/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4131/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4132/// are assumed to be legal.
4133bool
4134ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4135 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004136 if (VT.getVectorNumElements() == 4 &&
4137 (VT.is128BitVector() || VT.is64BitVector())) {
4138 unsigned PFIndexes[4];
4139 for (unsigned i = 0; i != 4; ++i) {
4140 if (M[i] < 0)
4141 PFIndexes[i] = 8;
4142 else
4143 PFIndexes[i] = M[i];
4144 }
4145
4146 // Compute the index in the perfect shuffle table.
4147 unsigned PFTableIndex =
4148 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4149 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4150 unsigned Cost = (PFEntry >> 30);
4151
4152 if (Cost <= 4)
4153 return true;
4154 }
4155
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004156 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004157 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004158
Bob Wilson53dd2452010-06-07 23:53:38 +00004159 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4160 return (EltSize >= 32 ||
4161 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004162 isVREVMask(M, VT, 64) ||
4163 isVREVMask(M, VT, 32) ||
4164 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004165 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004166 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004167 isVTRNMask(M, VT, WhichResult) ||
4168 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004169 isVZIPMask(M, VT, WhichResult) ||
4170 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4171 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4172 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004173}
4174
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004175/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4176/// the specified operations to build the shuffle.
4177static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4178 SDValue RHS, SelectionDAG &DAG,
4179 DebugLoc dl) {
4180 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4181 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4182 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4183
4184 enum {
4185 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4186 OP_VREV,
4187 OP_VDUP0,
4188 OP_VDUP1,
4189 OP_VDUP2,
4190 OP_VDUP3,
4191 OP_VEXT1,
4192 OP_VEXT2,
4193 OP_VEXT3,
4194 OP_VUZPL, // VUZP, left result
4195 OP_VUZPR, // VUZP, right result
4196 OP_VZIPL, // VZIP, left result
4197 OP_VZIPR, // VZIP, right result
4198 OP_VTRNL, // VTRN, left result
4199 OP_VTRNR // VTRN, right result
4200 };
4201
4202 if (OpNum == OP_COPY) {
4203 if (LHSID == (1*9+2)*9+3) return LHS;
4204 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4205 return RHS;
4206 }
4207
4208 SDValue OpLHS, OpRHS;
4209 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4210 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4211 EVT VT = OpLHS.getValueType();
4212
4213 switch (OpNum) {
4214 default: llvm_unreachable("Unknown shuffle opcode!");
4215 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004216 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004217 if (VT.getVectorElementType() == MVT::i32 ||
4218 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004219 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4220 // vrev <4 x i16> -> VREV32
4221 if (VT.getVectorElementType() == MVT::i16)
4222 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4223 // vrev <4 x i8> -> VREV16
4224 assert(VT.getVectorElementType() == MVT::i8);
4225 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004226 case OP_VDUP0:
4227 case OP_VDUP1:
4228 case OP_VDUP2:
4229 case OP_VDUP3:
4230 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004231 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004232 case OP_VEXT1:
4233 case OP_VEXT2:
4234 case OP_VEXT3:
4235 return DAG.getNode(ARMISD::VEXT, dl, VT,
4236 OpLHS, OpRHS,
4237 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4238 case OP_VUZPL:
4239 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004240 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004241 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4242 case OP_VZIPL:
4243 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004244 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004245 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4246 case OP_VTRNL:
4247 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004248 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4249 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004250 }
4251}
4252
Bill Wendling69a05a72011-03-14 23:02:38 +00004253static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4254 SmallVectorImpl<int> &ShuffleMask,
4255 SelectionDAG &DAG) {
4256 // Check to see if we can use the VTBL instruction.
4257 SDValue V1 = Op.getOperand(0);
4258 SDValue V2 = Op.getOperand(1);
4259 DebugLoc DL = Op.getDebugLoc();
4260
4261 SmallVector<SDValue, 8> VTBLMask;
4262 for (SmallVectorImpl<int>::iterator
4263 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4264 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4265
4266 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4267 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4268 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4269 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004270
Owen Anderson76706012011-04-05 21:48:57 +00004271 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004272 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4273 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004274}
4275
Bob Wilson5bafff32009-06-22 23:27:02 +00004276static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004277 SDValue V1 = Op.getOperand(0);
4278 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004279 DebugLoc dl = Op.getDebugLoc();
4280 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004281 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004282 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004283
Bob Wilson28865062009-08-13 02:13:04 +00004284 // Convert shuffles that are directly supported on NEON to target-specific
4285 // DAG nodes, instead of keeping them as shuffles and matching them again
4286 // during code selection. This is more efficient and avoids the possibility
4287 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004288 // FIXME: floating-point vectors should be canonicalized to integer vectors
4289 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004290 SVN->getMask(ShuffleMask);
4291
Bob Wilson53dd2452010-06-07 23:53:38 +00004292 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4293 if (EltSize <= 32) {
4294 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4295 int Lane = SVN->getSplatIndex();
4296 // If this is undef splat, generate it via "just" vdup, if possible.
4297 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004298
Bob Wilson53dd2452010-06-07 23:53:38 +00004299 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4300 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4301 }
4302 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4303 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004304 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004305
4306 bool ReverseVEXT;
4307 unsigned Imm;
4308 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4309 if (ReverseVEXT)
4310 std::swap(V1, V2);
4311 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4312 DAG.getConstant(Imm, MVT::i32));
4313 }
4314
4315 if (isVREVMask(ShuffleMask, VT, 64))
4316 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4317 if (isVREVMask(ShuffleMask, VT, 32))
4318 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4319 if (isVREVMask(ShuffleMask, VT, 16))
4320 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4321
4322 // Check for Neon shuffles that modify both input vectors in place.
4323 // If both results are used, i.e., if there are two shuffles with the same
4324 // source operands and with masks corresponding to both results of one of
4325 // these operations, DAG memoization will ensure that a single node is
4326 // used for both shuffles.
4327 unsigned WhichResult;
4328 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4329 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4330 V1, V2).getValue(WhichResult);
4331 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4332 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4333 V1, V2).getValue(WhichResult);
4334 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4335 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4336 V1, V2).getValue(WhichResult);
4337
4338 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4339 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4340 V1, V1).getValue(WhichResult);
4341 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4342 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4343 V1, V1).getValue(WhichResult);
4344 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4345 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4346 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004347 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004348
Bob Wilsonc692cb72009-08-21 20:54:19 +00004349 // If the shuffle is not directly supported and it has 4 elements, use
4350 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004351 unsigned NumElts = VT.getVectorNumElements();
4352 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004353 unsigned PFIndexes[4];
4354 for (unsigned i = 0; i != 4; ++i) {
4355 if (ShuffleMask[i] < 0)
4356 PFIndexes[i] = 8;
4357 else
4358 PFIndexes[i] = ShuffleMask[i];
4359 }
4360
4361 // Compute the index in the perfect shuffle table.
4362 unsigned PFTableIndex =
4363 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004364 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4365 unsigned Cost = (PFEntry >> 30);
4366
4367 if (Cost <= 4)
4368 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4369 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004370
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004371 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004372 if (EltSize >= 32) {
4373 // Do the expansion with floating-point types, since that is what the VFP
4374 // registers are defined to use, and since i64 is not legal.
4375 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4376 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004377 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4378 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004379 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004380 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004381 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004382 Ops.push_back(DAG.getUNDEF(EltVT));
4383 else
4384 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4385 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4386 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4387 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004388 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004389 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004390 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004391 }
4392
Bill Wendling69a05a72011-03-14 23:02:38 +00004393 if (VT == MVT::v8i8) {
4394 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4395 if (NewOp.getNode())
4396 return NewOp;
4397 }
4398
Bob Wilson22cac0d2009-08-14 05:16:33 +00004399 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004400}
4401
Bob Wilson5bafff32009-06-22 23:27:02 +00004402static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004403 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004404 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004405 if (!isa<ConstantSDNode>(Lane))
4406 return SDValue();
4407
4408 SDValue Vec = Op.getOperand(0);
4409 if (Op.getValueType() == MVT::i32 &&
4410 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4411 DebugLoc dl = Op.getDebugLoc();
4412 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4413 }
4414
4415 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004416}
4417
Bob Wilsona6d65862009-08-03 20:36:38 +00004418static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4419 // The only time a CONCAT_VECTORS operation can have legal types is when
4420 // two 64-bit vectors are concatenated to a 128-bit vector.
4421 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4422 "unexpected CONCAT_VECTORS");
4423 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004425 SDValue Op0 = Op.getOperand(0);
4426 SDValue Op1 = Op.getOperand(1);
4427 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004428 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004429 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004430 DAG.getIntPtrConstant(0));
4431 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004432 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004433 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004434 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004435 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004436}
4437
Bob Wilson626613d2010-11-23 19:38:38 +00004438/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4439/// element has been zero/sign-extended, depending on the isSigned parameter,
4440/// from an integer type half its size.
4441static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4442 bool isSigned) {
4443 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4444 EVT VT = N->getValueType(0);
4445 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4446 SDNode *BVN = N->getOperand(0).getNode();
4447 if (BVN->getValueType(0) != MVT::v4i32 ||
4448 BVN->getOpcode() != ISD::BUILD_VECTOR)
4449 return false;
4450 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4451 unsigned HiElt = 1 - LoElt;
4452 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4453 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4454 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4455 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4456 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4457 return false;
4458 if (isSigned) {
4459 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4460 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4461 return true;
4462 } else {
4463 if (Hi0->isNullValue() && Hi1->isNullValue())
4464 return true;
4465 }
4466 return false;
4467 }
4468
4469 if (N->getOpcode() != ISD::BUILD_VECTOR)
4470 return false;
4471
4472 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4473 SDNode *Elt = N->getOperand(i).getNode();
4474 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4475 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4476 unsigned HalfSize = EltSize / 2;
4477 if (isSigned) {
4478 int64_t SExtVal = C->getSExtValue();
4479 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4480 return false;
4481 } else {
4482 if ((C->getZExtValue() >> HalfSize) != 0)
4483 return false;
4484 }
4485 continue;
4486 }
4487 return false;
4488 }
4489
4490 return true;
4491}
4492
4493/// isSignExtended - Check if a node is a vector value that is sign-extended
4494/// or a constant BUILD_VECTOR with sign-extended elements.
4495static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4496 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4497 return true;
4498 if (isExtendedBUILD_VECTOR(N, DAG, true))
4499 return true;
4500 return false;
4501}
4502
4503/// isZeroExtended - Check if a node is a vector value that is zero-extended
4504/// or a constant BUILD_VECTOR with zero-extended elements.
4505static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4506 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4507 return true;
4508 if (isExtendedBUILD_VECTOR(N, DAG, false))
4509 return true;
4510 return false;
4511}
4512
4513/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4514/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004515static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4516 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4517 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004518 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4519 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4520 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4521 LD->isNonTemporal(), LD->getAlignment());
4522 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4523 // have been legalized as a BITCAST from v4i32.
4524 if (N->getOpcode() == ISD::BITCAST) {
4525 SDNode *BVN = N->getOperand(0).getNode();
4526 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4527 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4528 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4529 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4530 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4531 }
4532 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4533 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4534 EVT VT = N->getValueType(0);
4535 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4536 unsigned NumElts = VT.getVectorNumElements();
4537 MVT TruncVT = MVT::getIntegerVT(EltSize);
4538 SmallVector<SDValue, 8> Ops;
4539 for (unsigned i = 0; i != NumElts; ++i) {
4540 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4541 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004542 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004543 }
4544 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4545 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004546}
4547
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004548static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4549 unsigned Opcode = N->getOpcode();
4550 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4551 SDNode *N0 = N->getOperand(0).getNode();
4552 SDNode *N1 = N->getOperand(1).getNode();
4553 return N0->hasOneUse() && N1->hasOneUse() &&
4554 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4555 }
4556 return false;
4557}
4558
4559static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4560 unsigned Opcode = N->getOpcode();
4561 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4562 SDNode *N0 = N->getOperand(0).getNode();
4563 SDNode *N1 = N->getOperand(1).getNode();
4564 return N0->hasOneUse() && N1->hasOneUse() &&
4565 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4566 }
4567 return false;
4568}
4569
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004570static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4571 // Multiplications are only custom-lowered for 128-bit vectors so that
4572 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4573 EVT VT = Op.getValueType();
4574 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4575 SDNode *N0 = Op.getOperand(0).getNode();
4576 SDNode *N1 = Op.getOperand(1).getNode();
4577 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004578 bool isMLA = false;
4579 bool isN0SExt = isSignExtended(N0, DAG);
4580 bool isN1SExt = isSignExtended(N1, DAG);
4581 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004582 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004583 else {
4584 bool isN0ZExt = isZeroExtended(N0, DAG);
4585 bool isN1ZExt = isZeroExtended(N1, DAG);
4586 if (isN0ZExt && isN1ZExt)
4587 NewOpc = ARMISD::VMULLu;
4588 else if (isN1SExt || isN1ZExt) {
4589 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4590 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4591 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4592 NewOpc = ARMISD::VMULLs;
4593 isMLA = true;
4594 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4595 NewOpc = ARMISD::VMULLu;
4596 isMLA = true;
4597 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4598 std::swap(N0, N1);
4599 NewOpc = ARMISD::VMULLu;
4600 isMLA = true;
4601 }
4602 }
4603
4604 if (!NewOpc) {
4605 if (VT == MVT::v2i64)
4606 // Fall through to expand this. It is not legal.
4607 return SDValue();
4608 else
4609 // Other vector multiplications are legal.
4610 return Op;
4611 }
4612 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004613
4614 // Legalize to a VMULL instruction.
4615 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004616 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004617 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004618 if (!isMLA) {
4619 Op0 = SkipExtension(N0, DAG);
4620 assert(Op0.getValueType().is64BitVector() &&
4621 Op1.getValueType().is64BitVector() &&
4622 "unexpected types for extended operands to VMULL");
4623 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4624 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004625
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004626 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4627 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4628 // vmull q0, d4, d6
4629 // vmlal q0, d5, d6
4630 // is faster than
4631 // vaddl q0, d4, d5
4632 // vmovl q1, d6
4633 // vmul q0, q0, q1
4634 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4635 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4636 EVT Op1VT = Op1.getValueType();
4637 return DAG.getNode(N0->getOpcode(), DL, VT,
4638 DAG.getNode(NewOpc, DL, VT,
4639 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4640 DAG.getNode(NewOpc, DL, VT,
4641 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004642}
4643
Owen Anderson76706012011-04-05 21:48:57 +00004644static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004645LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4646 // Convert to float
4647 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4648 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4649 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4650 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4651 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4652 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4653 // Get reciprocal estimate.
4654 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004655 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004656 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4657 // Because char has a smaller range than uchar, we can actually get away
4658 // without any newton steps. This requires that we use a weird bias
4659 // of 0xb000, however (again, this has been exhaustively tested).
4660 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4661 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4662 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4663 Y = DAG.getConstant(0xb000, MVT::i32);
4664 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4665 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4666 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4667 // Convert back to short.
4668 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4669 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4670 return X;
4671}
4672
Owen Anderson76706012011-04-05 21:48:57 +00004673static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004674LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4675 SDValue N2;
4676 // Convert to float.
4677 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4678 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4679 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4680 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4681 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4682 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004683
Nate Begeman7973f352011-02-11 20:53:29 +00004684 // Use reciprocal estimate and one refinement step.
4685 // float4 recip = vrecpeq_f32(yf);
4686 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004687 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004688 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004689 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004690 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4691 N1, N2);
4692 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4693 // Because short has a smaller range than ushort, we can actually get away
4694 // with only a single newton step. This requires that we use a weird bias
4695 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004696 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004697 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4698 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004699 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004700 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4701 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4702 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4703 // Convert back to integer and return.
4704 // return vmovn_s32(vcvt_s32_f32(result));
4705 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4706 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4707 return N0;
4708}
4709
4710static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4711 EVT VT = Op.getValueType();
4712 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4713 "unexpected type for custom-lowering ISD::SDIV");
4714
4715 DebugLoc dl = Op.getDebugLoc();
4716 SDValue N0 = Op.getOperand(0);
4717 SDValue N1 = Op.getOperand(1);
4718 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004719
Nate Begeman7973f352011-02-11 20:53:29 +00004720 if (VT == MVT::v8i8) {
4721 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4722 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004723
Nate Begeman7973f352011-02-11 20:53:29 +00004724 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4725 DAG.getIntPtrConstant(4));
4726 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004727 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004728 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4729 DAG.getIntPtrConstant(0));
4730 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4731 DAG.getIntPtrConstant(0));
4732
4733 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4734 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4735
4736 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4737 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004738
Nate Begeman7973f352011-02-11 20:53:29 +00004739 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4740 return N0;
4741 }
4742 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4743}
4744
4745static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4746 EVT VT = Op.getValueType();
4747 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4748 "unexpected type for custom-lowering ISD::UDIV");
4749
4750 DebugLoc dl = Op.getDebugLoc();
4751 SDValue N0 = Op.getOperand(0);
4752 SDValue N1 = Op.getOperand(1);
4753 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004754
Nate Begeman7973f352011-02-11 20:53:29 +00004755 if (VT == MVT::v8i8) {
4756 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4757 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004758
Nate Begeman7973f352011-02-11 20:53:29 +00004759 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4760 DAG.getIntPtrConstant(4));
4761 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004762 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004763 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4764 DAG.getIntPtrConstant(0));
4765 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4766 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004767
Nate Begeman7973f352011-02-11 20:53:29 +00004768 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4769 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004770
Nate Begeman7973f352011-02-11 20:53:29 +00004771 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4772 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004773
4774 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004775 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4776 N0);
4777 return N0;
4778 }
Owen Anderson76706012011-04-05 21:48:57 +00004779
Nate Begeman7973f352011-02-11 20:53:29 +00004780 // v4i16 sdiv ... Convert to float.
4781 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4782 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4783 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4784 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4785 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004786 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004787
4788 // Use reciprocal estimate and two refinement steps.
4789 // float4 recip = vrecpeq_f32(yf);
4790 // recip *= vrecpsq_f32(yf, recip);
4791 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004792 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004793 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004794 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004795 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004796 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004797 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004798 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004799 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004800 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004801 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4802 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4803 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4804 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004805 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004806 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4807 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4808 N1 = DAG.getConstant(2, MVT::i32);
4809 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4810 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4811 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4812 // Convert back to integer and return.
4813 // return vmovn_u32(vcvt_s32_f32(result));
4814 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4815 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4816 return N0;
4817}
4818
Dan Gohmand858e902010-04-17 15:26:15 +00004819SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004820 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004821 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004822 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004823 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004824 case ISD::GlobalAddress:
4825 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4826 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004827 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004828 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004829 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4830 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004831 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004832 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004833 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004834 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004835 case ISD::SINT_TO_FP:
4836 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4837 case ISD::FP_TO_SINT:
4838 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004839 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004840 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004841 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004842 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004843 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004844 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004845 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004846 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4847 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004848 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004849 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004850 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004851 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004852 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004853 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004854 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004855 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004856 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004857 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004858 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004859 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004860 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004861 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004862 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004863 case ISD::SDIV: return LowerSDIV(Op, DAG);
4864 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004865 }
Dan Gohman475871a2008-07-27 21:46:04 +00004866 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004867}
4868
Duncan Sands1607f052008-12-01 11:39:25 +00004869/// ReplaceNodeResults - Replace the results of node with an illegal result
4870/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004871void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4872 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004873 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004874 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004875 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004876 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004877 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004878 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004879 case ISD::BITCAST:
4880 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004881 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004882 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004883 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004884 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004885 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004886 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004887 if (Res.getNode())
4888 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004889}
Chris Lattner27a6c732007-11-24 07:07:01 +00004890
Evan Chenga8e29892007-01-19 07:51:42 +00004891//===----------------------------------------------------------------------===//
4892// ARM Scheduler Hooks
4893//===----------------------------------------------------------------------===//
4894
4895MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004896ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4897 MachineBasicBlock *BB,
4898 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004899 unsigned dest = MI->getOperand(0).getReg();
4900 unsigned ptr = MI->getOperand(1).getReg();
4901 unsigned oldval = MI->getOperand(2).getReg();
4902 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004903 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4904 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004905 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004906
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004907 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4908 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004909 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004910 : ARM::GPRRegisterClass);
4911
4912 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004913 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4914 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4915 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004916 }
4917
Jim Grosbach5278eb82009-12-11 01:42:04 +00004918 unsigned ldrOpc, strOpc;
4919 switch (Size) {
4920 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004921 case 1:
4922 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004923 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004924 break;
4925 case 2:
4926 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4927 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4928 break;
4929 case 4:
4930 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4931 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4932 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004933 }
4934
4935 MachineFunction *MF = BB->getParent();
4936 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4937 MachineFunction::iterator It = BB;
4938 ++It; // insert the new blocks after the current block
4939
4940 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4941 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4942 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4943 MF->insert(It, loop1MBB);
4944 MF->insert(It, loop2MBB);
4945 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004946
4947 // Transfer the remainder of BB and its successor edges to exitMBB.
4948 exitMBB->splice(exitMBB->begin(), BB,
4949 llvm::next(MachineBasicBlock::iterator(MI)),
4950 BB->end());
4951 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004952
4953 // thisMBB:
4954 // ...
4955 // fallthrough --> loop1MBB
4956 BB->addSuccessor(loop1MBB);
4957
4958 // loop1MBB:
4959 // ldrex dest, [ptr]
4960 // cmp dest, oldval
4961 // bne exitMBB
4962 BB = loop1MBB;
4963 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004964 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004965 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004966 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4967 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004968 BB->addSuccessor(loop2MBB);
4969 BB->addSuccessor(exitMBB);
4970
4971 // loop2MBB:
4972 // strex scratch, newval, [ptr]
4973 // cmp scratch, #0
4974 // bne loop1MBB
4975 BB = loop2MBB;
4976 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4977 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004978 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004979 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004980 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4981 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004982 BB->addSuccessor(loop1MBB);
4983 BB->addSuccessor(exitMBB);
4984
4985 // exitMBB:
4986 // ...
4987 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004988
Dan Gohman14152b42010-07-06 20:24:04 +00004989 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004990
Jim Grosbach5278eb82009-12-11 01:42:04 +00004991 return BB;
4992}
4993
4994MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004995ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4996 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004997 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4998 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4999
5000 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005001 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005002 MachineFunction::iterator It = BB;
5003 ++It;
5004
5005 unsigned dest = MI->getOperand(0).getReg();
5006 unsigned ptr = MI->getOperand(1).getReg();
5007 unsigned incr = MI->getOperand(2).getReg();
5008 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005009 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005010
5011 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5012 if (isThumb2) {
5013 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5014 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5015 }
5016
Jim Grosbachc3c23542009-12-14 04:22:04 +00005017 unsigned ldrOpc, strOpc;
5018 switch (Size) {
5019 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005020 case 1:
5021 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005022 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005023 break;
5024 case 2:
5025 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5026 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5027 break;
5028 case 4:
5029 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5030 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5031 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005032 }
5033
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005034 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5035 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5036 MF->insert(It, loopMBB);
5037 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005038
5039 // Transfer the remainder of BB and its successor edges to exitMBB.
5040 exitMBB->splice(exitMBB->begin(), BB,
5041 llvm::next(MachineBasicBlock::iterator(MI)),
5042 BB->end());
5043 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005044
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005045 TargetRegisterClass *TRC =
5046 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5047 unsigned scratch = MRI.createVirtualRegister(TRC);
5048 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005049
5050 // thisMBB:
5051 // ...
5052 // fallthrough --> loopMBB
5053 BB->addSuccessor(loopMBB);
5054
5055 // loopMBB:
5056 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005057 // <binop> scratch2, dest, incr
5058 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005059 // cmp scratch, #0
5060 // bne- loopMBB
5061 // fallthrough --> exitMBB
5062 BB = loopMBB;
5063 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005064 if (BinOpcode) {
5065 // operand order needs to go the other way for NAND
5066 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5067 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5068 addReg(incr).addReg(dest)).addReg(0);
5069 else
5070 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5071 addReg(dest).addReg(incr)).addReg(0);
5072 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005073
5074 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5075 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005076 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005077 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005078 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5079 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005080
5081 BB->addSuccessor(loopMBB);
5082 BB->addSuccessor(exitMBB);
5083
5084 // exitMBB:
5085 // ...
5086 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005087
Dan Gohman14152b42010-07-06 20:24:04 +00005088 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005089
Jim Grosbachc3c23542009-12-14 04:22:04 +00005090 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005091}
5092
Jim Grosbachf7da8822011-04-26 19:44:18 +00005093MachineBasicBlock *
5094ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5095 MachineBasicBlock *BB,
5096 unsigned Size,
5097 bool signExtend,
5098 ARMCC::CondCodes Cond) const {
5099 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5100
5101 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5102 MachineFunction *MF = BB->getParent();
5103 MachineFunction::iterator It = BB;
5104 ++It;
5105
5106 unsigned dest = MI->getOperand(0).getReg();
5107 unsigned ptr = MI->getOperand(1).getReg();
5108 unsigned incr = MI->getOperand(2).getReg();
5109 unsigned oldval = dest;
5110 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005111 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005112
5113 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5114 if (isThumb2) {
5115 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5116 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5117 }
5118
Jim Grosbachf7da8822011-04-26 19:44:18 +00005119 unsigned ldrOpc, strOpc, extendOpc;
5120 switch (Size) {
5121 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5122 case 1:
5123 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5124 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
5125 extendOpc = isThumb2 ? ARM::t2SXTBr : ARM::SXTBr;
5126 break;
5127 case 2:
5128 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5129 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5130 extendOpc = isThumb2 ? ARM::t2SXTHr : ARM::SXTHr;
5131 break;
5132 case 4:
5133 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5134 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5135 extendOpc = 0;
5136 break;
5137 }
5138
5139 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5140 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5141 MF->insert(It, loopMBB);
5142 MF->insert(It, exitMBB);
5143
5144 // Transfer the remainder of BB and its successor edges to exitMBB.
5145 exitMBB->splice(exitMBB->begin(), BB,
5146 llvm::next(MachineBasicBlock::iterator(MI)),
5147 BB->end());
5148 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5149
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005150 TargetRegisterClass *TRC =
5151 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5152 unsigned scratch = MRI.createVirtualRegister(TRC);
5153 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005154
5155 // thisMBB:
5156 // ...
5157 // fallthrough --> loopMBB
5158 BB->addSuccessor(loopMBB);
5159
5160 // loopMBB:
5161 // ldrex dest, ptr
5162 // (sign extend dest, if required)
5163 // cmp dest, incr
5164 // cmov.cond scratch2, dest, incr
5165 // strex scratch, scratch2, ptr
5166 // cmp scratch, #0
5167 // bne- loopMBB
5168 // fallthrough --> exitMBB
5169 BB = loopMBB;
5170 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5171
5172 // Sign extend the value, if necessary.
5173 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005174 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005175 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval).addReg(dest));
5176 }
5177
5178 // Build compare and cmov instructions.
5179 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5180 .addReg(oldval).addReg(incr));
5181 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5182 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5183
5184 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5185 .addReg(ptr));
5186 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5187 .addReg(scratch).addImm(0));
5188 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5189 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5190
5191 BB->addSuccessor(loopMBB);
5192 BB->addSuccessor(exitMBB);
5193
5194 // exitMBB:
5195 // ...
5196 BB = exitMBB;
5197
5198 MI->eraseFromParent(); // The instruction is gone now.
5199
5200 return BB;
5201}
5202
Evan Cheng218977b2010-07-13 19:27:42 +00005203static
5204MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5205 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5206 E = MBB->succ_end(); I != E; ++I)
5207 if (*I != Succ)
5208 return *I;
5209 llvm_unreachable("Expecting a BB with two successors!");
5210}
5211
Andrew Trick1c3af772011-04-23 03:55:32 +00005212// FIXME: This opcode table should obviously be expressed in the target
5213// description. We probably just need a "machine opcode" value in the pseudo
5214// instruction. But the ideal solution maybe to simply remove the "S" version
5215// of the opcode altogether.
5216struct AddSubFlagsOpcodePair {
5217 unsigned PseudoOpc;
5218 unsigned MachineOpc;
5219};
5220
5221static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5222 {ARM::ADCSri, ARM::ADCri},
5223 {ARM::ADCSrr, ARM::ADCrr},
5224 {ARM::ADCSrs, ARM::ADCrs},
5225 {ARM::SBCSri, ARM::SBCri},
5226 {ARM::SBCSrr, ARM::SBCrr},
5227 {ARM::SBCSrs, ARM::SBCrs},
5228 {ARM::RSBSri, ARM::RSBri},
5229 {ARM::RSBSrr, ARM::RSBrr},
5230 {ARM::RSBSrs, ARM::RSBrs},
5231 {ARM::RSCSri, ARM::RSCri},
5232 {ARM::RSCSrs, ARM::RSCrs},
5233 {ARM::t2ADCSri, ARM::t2ADCri},
5234 {ARM::t2ADCSrr, ARM::t2ADCrr},
5235 {ARM::t2ADCSrs, ARM::t2ADCrs},
5236 {ARM::t2SBCSri, ARM::t2SBCri},
5237 {ARM::t2SBCSrr, ARM::t2SBCrr},
5238 {ARM::t2SBCSrs, ARM::t2SBCrs},
5239 {ARM::t2RSBSri, ARM::t2RSBri},
5240 {ARM::t2RSBSrs, ARM::t2RSBrs},
5241};
5242
5243// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5244// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5245//
5246// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5247// position to be recognized by the target descrition as the 'S' bit.
5248bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5249 MachineBasicBlock *BB) const {
5250 unsigned OldOpc = MI->getOpcode();
5251 unsigned NewOpc = 0;
5252
5253 // This is only called for instructions that need remapping, so iterating over
5254 // the tiny opcode table is not costly.
5255 static const int NPairs =
5256 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5257 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5258 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5259 if (OldOpc == Pair->PseudoOpc) {
5260 NewOpc = Pair->MachineOpc;
5261 break;
5262 }
5263 }
5264 if (!NewOpc)
5265 return false;
5266
5267 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5268 DebugLoc dl = MI->getDebugLoc();
5269 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5270 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5271 MIB.addOperand(MI->getOperand(i));
5272 AddDefaultPred(MIB);
5273 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5274 MI->eraseFromParent();
5275 return true;
5276}
5277
Jim Grosbache801dc42009-12-12 01:40:06 +00005278MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005279ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005280 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005281 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005282 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005283 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005284 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005285 default: {
5286 if (RemapAddSubWithFlags(MI, BB))
5287 return BB;
5288
Jim Grosbach5278eb82009-12-11 01:42:04 +00005289 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005290 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005291 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005292 case ARM::ATOMIC_LOAD_ADD_I8:
5293 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5294 case ARM::ATOMIC_LOAD_ADD_I16:
5295 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5296 case ARM::ATOMIC_LOAD_ADD_I32:
5297 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005298
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005299 case ARM::ATOMIC_LOAD_AND_I8:
5300 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5301 case ARM::ATOMIC_LOAD_AND_I16:
5302 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5303 case ARM::ATOMIC_LOAD_AND_I32:
5304 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005305
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005306 case ARM::ATOMIC_LOAD_OR_I8:
5307 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5308 case ARM::ATOMIC_LOAD_OR_I16:
5309 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5310 case ARM::ATOMIC_LOAD_OR_I32:
5311 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005312
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005313 case ARM::ATOMIC_LOAD_XOR_I8:
5314 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5315 case ARM::ATOMIC_LOAD_XOR_I16:
5316 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5317 case ARM::ATOMIC_LOAD_XOR_I32:
5318 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005319
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005320 case ARM::ATOMIC_LOAD_NAND_I8:
5321 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5322 case ARM::ATOMIC_LOAD_NAND_I16:
5323 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5324 case ARM::ATOMIC_LOAD_NAND_I32:
5325 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005326
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005327 case ARM::ATOMIC_LOAD_SUB_I8:
5328 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5329 case ARM::ATOMIC_LOAD_SUB_I16:
5330 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5331 case ARM::ATOMIC_LOAD_SUB_I32:
5332 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005333
Jim Grosbachf7da8822011-04-26 19:44:18 +00005334 case ARM::ATOMIC_LOAD_MIN_I8:
5335 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5336 case ARM::ATOMIC_LOAD_MIN_I16:
5337 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5338 case ARM::ATOMIC_LOAD_MIN_I32:
5339 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5340
5341 case ARM::ATOMIC_LOAD_MAX_I8:
5342 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5343 case ARM::ATOMIC_LOAD_MAX_I16:
5344 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5345 case ARM::ATOMIC_LOAD_MAX_I32:
5346 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5347
5348 case ARM::ATOMIC_LOAD_UMIN_I8:
5349 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5350 case ARM::ATOMIC_LOAD_UMIN_I16:
5351 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5352 case ARM::ATOMIC_LOAD_UMIN_I32:
5353 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5354
5355 case ARM::ATOMIC_LOAD_UMAX_I8:
5356 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5357 case ARM::ATOMIC_LOAD_UMAX_I16:
5358 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5359 case ARM::ATOMIC_LOAD_UMAX_I32:
5360 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5361
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005362 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5363 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5364 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005365
5366 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5367 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5368 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005369
Evan Cheng007ea272009-08-12 05:17:19 +00005370 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005371 // To "insert" a SELECT_CC instruction, we actually have to insert the
5372 // diamond control-flow pattern. The incoming instruction knows the
5373 // destination vreg to set, the condition code register to branch on, the
5374 // true/false values to select between, and a branch opcode to use.
5375 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005376 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005377 ++It;
5378
5379 // thisMBB:
5380 // ...
5381 // TrueVal = ...
5382 // cmpTY ccX, r1, r2
5383 // bCC copy1MBB
5384 // fallthrough --> copy0MBB
5385 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005386 MachineFunction *F = BB->getParent();
5387 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5388 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005389 F->insert(It, copy0MBB);
5390 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005391
5392 // Transfer the remainder of BB and its successor edges to sinkMBB.
5393 sinkMBB->splice(sinkMBB->begin(), BB,
5394 llvm::next(MachineBasicBlock::iterator(MI)),
5395 BB->end());
5396 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5397
Dan Gohman258c58c2010-07-06 15:49:48 +00005398 BB->addSuccessor(copy0MBB);
5399 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005400
Dan Gohman14152b42010-07-06 20:24:04 +00005401 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5402 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5403
Evan Chenga8e29892007-01-19 07:51:42 +00005404 // copy0MBB:
5405 // %FalseValue = ...
5406 // # fallthrough to sinkMBB
5407 BB = copy0MBB;
5408
5409 // Update machine-CFG edges
5410 BB->addSuccessor(sinkMBB);
5411
5412 // sinkMBB:
5413 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5414 // ...
5415 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005416 BuildMI(*BB, BB->begin(), dl,
5417 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005418 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5419 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5420
Dan Gohman14152b42010-07-06 20:24:04 +00005421 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005422 return BB;
5423 }
Evan Cheng86198642009-08-07 00:34:42 +00005424
Evan Cheng218977b2010-07-13 19:27:42 +00005425 case ARM::BCCi64:
5426 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005427 // If there is an unconditional branch to the other successor, remove it.
5428 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005429
Evan Cheng218977b2010-07-13 19:27:42 +00005430 // Compare both parts that make up the double comparison separately for
5431 // equality.
5432 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5433
5434 unsigned LHS1 = MI->getOperand(1).getReg();
5435 unsigned LHS2 = MI->getOperand(2).getReg();
5436 if (RHSisZero) {
5437 AddDefaultPred(BuildMI(BB, dl,
5438 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5439 .addReg(LHS1).addImm(0));
5440 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5441 .addReg(LHS2).addImm(0)
5442 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5443 } else {
5444 unsigned RHS1 = MI->getOperand(3).getReg();
5445 unsigned RHS2 = MI->getOperand(4).getReg();
5446 AddDefaultPred(BuildMI(BB, dl,
5447 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5448 .addReg(LHS1).addReg(RHS1));
5449 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5450 .addReg(LHS2).addReg(RHS2)
5451 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5452 }
5453
5454 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5455 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5456 if (MI->getOperand(0).getImm() == ARMCC::NE)
5457 std::swap(destMBB, exitMBB);
5458
5459 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5460 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5461 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5462 .addMBB(exitMBB);
5463
5464 MI->eraseFromParent(); // The pseudo instruction is gone now.
5465 return BB;
5466 }
Evan Chenga8e29892007-01-19 07:51:42 +00005467 }
5468}
5469
5470//===----------------------------------------------------------------------===//
5471// ARM Optimization Hooks
5472//===----------------------------------------------------------------------===//
5473
Chris Lattnerd1980a52009-03-12 06:52:53 +00005474static
5475SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5476 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005477 SelectionDAG &DAG = DCI.DAG;
5478 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005479 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005480 unsigned Opc = N->getOpcode();
5481 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5482 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5483 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5484 ISD::CondCode CC = ISD::SETCC_INVALID;
5485
5486 if (isSlctCC) {
5487 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5488 } else {
5489 SDValue CCOp = Slct.getOperand(0);
5490 if (CCOp.getOpcode() == ISD::SETCC)
5491 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5492 }
5493
5494 bool DoXform = false;
5495 bool InvCC = false;
5496 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5497 "Bad input!");
5498
5499 if (LHS.getOpcode() == ISD::Constant &&
5500 cast<ConstantSDNode>(LHS)->isNullValue()) {
5501 DoXform = true;
5502 } else if (CC != ISD::SETCC_INVALID &&
5503 RHS.getOpcode() == ISD::Constant &&
5504 cast<ConstantSDNode>(RHS)->isNullValue()) {
5505 std::swap(LHS, RHS);
5506 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005507 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005508 Op0.getOperand(0).getValueType();
5509 bool isInt = OpVT.isInteger();
5510 CC = ISD::getSetCCInverse(CC, isInt);
5511
5512 if (!TLI.isCondCodeLegal(CC, OpVT))
5513 return SDValue(); // Inverse operator isn't legal.
5514
5515 DoXform = true;
5516 InvCC = true;
5517 }
5518
5519 if (DoXform) {
5520 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5521 if (isSlctCC)
5522 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5523 Slct.getOperand(0), Slct.getOperand(1), CC);
5524 SDValue CCOp = Slct.getOperand(0);
5525 if (InvCC)
5526 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5527 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5528 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5529 CCOp, OtherOp, Result);
5530 }
5531 return SDValue();
5532}
5533
Eric Christopherfa6f5912011-06-29 21:10:36 +00005534// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00005535// (only after legalization).
5536static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5537 TargetLowering::DAGCombinerInfo &DCI,
5538 const ARMSubtarget *Subtarget) {
5539
5540 // Only perform optimization if after legalize, and if NEON is available. We
5541 // also expected both operands to be BUILD_VECTORs.
5542 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5543 || N0.getOpcode() != ISD::BUILD_VECTOR
5544 || N1.getOpcode() != ISD::BUILD_VECTOR)
5545 return SDValue();
5546
5547 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5548 EVT VT = N->getValueType(0);
5549 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5550 return SDValue();
5551
5552 // Check that the vector operands are of the right form.
5553 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5554 // operands, where N is the size of the formed vector.
5555 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5556 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005557
5558 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005559 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005560 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005561 SDValue Vec = N0->getOperand(0)->getOperand(0);
5562 SDNode *V = Vec.getNode();
5563 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005564
Eric Christopherfa6f5912011-06-29 21:10:36 +00005565 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00005566 // check to see if each of their operands are an EXTRACT_VECTOR with
5567 // the same vector and appropriate index.
5568 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5569 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5570 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00005571
Tanya Lattner189531f2011-06-14 23:48:48 +00005572 SDValue ExtVec0 = N0->getOperand(i);
5573 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005574
Tanya Lattner189531f2011-06-14 23:48:48 +00005575 // First operand is the vector, verify its the same.
5576 if (V != ExtVec0->getOperand(0).getNode() ||
5577 V != ExtVec1->getOperand(0).getNode())
5578 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00005579
Tanya Lattner189531f2011-06-14 23:48:48 +00005580 // Second is the constant, verify its correct.
5581 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5582 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00005583
Tanya Lattner189531f2011-06-14 23:48:48 +00005584 // For the constant, we want to see all the even or all the odd.
5585 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5586 || C1->getZExtValue() != nextIndex+1)
5587 return SDValue();
5588
5589 // Increment index.
5590 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005591 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00005592 return SDValue();
5593 }
5594
5595 // Create VPADDL node.
5596 SelectionDAG &DAG = DCI.DAG;
5597 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00005598
5599 // Build operand list.
5600 SmallVector<SDValue, 8> Ops;
5601 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5602 TLI.getPointerTy()));
5603
5604 // Input is the vector.
5605 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005606
Tanya Lattner189531f2011-06-14 23:48:48 +00005607 // Get widened type and narrowed type.
5608 MVT widenType;
5609 unsigned numElem = VT.getVectorNumElements();
5610 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5611 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5612 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5613 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5614 default:
5615 assert(0 && "Invalid vector element type for padd optimization.");
5616 }
5617
5618 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5619 widenType, &Ops[0], Ops.size());
5620 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5621}
5622
Bob Wilson3d5792a2010-07-29 20:34:14 +00005623/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5624/// operands N0 and N1. This is a helper for PerformADDCombine that is
5625/// called with the default operands, and if that fails, with commuted
5626/// operands.
5627static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005628 TargetLowering::DAGCombinerInfo &DCI,
5629 const ARMSubtarget *Subtarget){
5630
5631 // Attempt to create vpaddl for this add.
5632 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5633 if (Result.getNode())
5634 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005635
Chris Lattnerd1980a52009-03-12 06:52:53 +00005636 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5637 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5638 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5639 if (Result.getNode()) return Result;
5640 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005641 return SDValue();
5642}
5643
Bob Wilson3d5792a2010-07-29 20:34:14 +00005644/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5645///
5646static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005647 TargetLowering::DAGCombinerInfo &DCI,
5648 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005649 SDValue N0 = N->getOperand(0);
5650 SDValue N1 = N->getOperand(1);
5651
5652 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005653 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005654 if (Result.getNode())
5655 return Result;
5656
5657 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005658 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005659}
5660
Chris Lattnerd1980a52009-03-12 06:52:53 +00005661/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005662///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005663static SDValue PerformSUBCombine(SDNode *N,
5664 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005665 SDValue N0 = N->getOperand(0);
5666 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005667
Chris Lattnerd1980a52009-03-12 06:52:53 +00005668 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5669 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5670 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5671 if (Result.getNode()) return Result;
5672 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005673
Chris Lattnerd1980a52009-03-12 06:52:53 +00005674 return SDValue();
5675}
5676
Evan Cheng463d3582011-03-31 19:38:48 +00005677/// PerformVMULCombine
5678/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5679/// special multiplier accumulator forwarding.
5680/// vmul d3, d0, d2
5681/// vmla d3, d1, d2
5682/// is faster than
5683/// vadd d3, d0, d1
5684/// vmul d3, d3, d2
5685static SDValue PerformVMULCombine(SDNode *N,
5686 TargetLowering::DAGCombinerInfo &DCI,
5687 const ARMSubtarget *Subtarget) {
5688 if (!Subtarget->hasVMLxForwarding())
5689 return SDValue();
5690
5691 SelectionDAG &DAG = DCI.DAG;
5692 SDValue N0 = N->getOperand(0);
5693 SDValue N1 = N->getOperand(1);
5694 unsigned Opcode = N0.getOpcode();
5695 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5696 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00005697 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00005698 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5699 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5700 return SDValue();
5701 std::swap(N0, N1);
5702 }
5703
5704 EVT VT = N->getValueType(0);
5705 DebugLoc DL = N->getDebugLoc();
5706 SDValue N00 = N0->getOperand(0);
5707 SDValue N01 = N0->getOperand(1);
5708 return DAG.getNode(Opcode, DL, VT,
5709 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5710 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5711}
5712
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005713static SDValue PerformMULCombine(SDNode *N,
5714 TargetLowering::DAGCombinerInfo &DCI,
5715 const ARMSubtarget *Subtarget) {
5716 SelectionDAG &DAG = DCI.DAG;
5717
5718 if (Subtarget->isThumb1Only())
5719 return SDValue();
5720
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005721 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5722 return SDValue();
5723
5724 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005725 if (VT.is64BitVector() || VT.is128BitVector())
5726 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005727 if (VT != MVT::i32)
5728 return SDValue();
5729
5730 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5731 if (!C)
5732 return SDValue();
5733
5734 uint64_t MulAmt = C->getZExtValue();
5735 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5736 ShiftAmt = ShiftAmt & (32 - 1);
5737 SDValue V = N->getOperand(0);
5738 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005739
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005740 SDValue Res;
5741 MulAmt >>= ShiftAmt;
5742 if (isPowerOf2_32(MulAmt - 1)) {
5743 // (mul x, 2^N + 1) => (add (shl x, N), x)
5744 Res = DAG.getNode(ISD::ADD, DL, VT,
5745 V, DAG.getNode(ISD::SHL, DL, VT,
5746 V, DAG.getConstant(Log2_32(MulAmt-1),
5747 MVT::i32)));
5748 } else if (isPowerOf2_32(MulAmt + 1)) {
5749 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5750 Res = DAG.getNode(ISD::SUB, DL, VT,
5751 DAG.getNode(ISD::SHL, DL, VT,
5752 V, DAG.getConstant(Log2_32(MulAmt+1),
5753 MVT::i32)),
5754 V);
5755 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005756 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005757
5758 if (ShiftAmt != 0)
5759 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5760 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005761
5762 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005763 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005764 return SDValue();
5765}
5766
Owen Anderson080c0922010-11-05 19:27:46 +00005767static SDValue PerformANDCombine(SDNode *N,
5768 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005769
Owen Anderson080c0922010-11-05 19:27:46 +00005770 // Attempt to use immediate-form VBIC
5771 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5772 DebugLoc dl = N->getDebugLoc();
5773 EVT VT = N->getValueType(0);
5774 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005775
Tanya Lattner0433b212011-04-07 15:24:20 +00005776 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5777 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005778
Owen Anderson080c0922010-11-05 19:27:46 +00005779 APInt SplatBits, SplatUndef;
5780 unsigned SplatBitSize;
5781 bool HasAnyUndefs;
5782 if (BVN &&
5783 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5784 if (SplatBitSize <= 64) {
5785 EVT VbicVT;
5786 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5787 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005788 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005789 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005790 if (Val.getNode()) {
5791 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005792 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005793 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005794 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005795 }
5796 }
5797 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005798
Owen Anderson080c0922010-11-05 19:27:46 +00005799 return SDValue();
5800}
5801
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005802/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5803static SDValue PerformORCombine(SDNode *N,
5804 TargetLowering::DAGCombinerInfo &DCI,
5805 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005806 // Attempt to use immediate-form VORR
5807 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5808 DebugLoc dl = N->getDebugLoc();
5809 EVT VT = N->getValueType(0);
5810 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005811
Tanya Lattner0433b212011-04-07 15:24:20 +00005812 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5813 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005814
Owen Anderson60f48702010-11-03 23:15:26 +00005815 APInt SplatBits, SplatUndef;
5816 unsigned SplatBitSize;
5817 bool HasAnyUndefs;
5818 if (BVN && Subtarget->hasNEON() &&
5819 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5820 if (SplatBitSize <= 64) {
5821 EVT VorrVT;
5822 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5823 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005824 DAG, VorrVT, VT.is128BitVector(),
5825 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005826 if (Val.getNode()) {
5827 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005828 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005829 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005830 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005831 }
5832 }
5833 }
5834
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005835 SDValue N0 = N->getOperand(0);
5836 if (N0.getOpcode() != ISD::AND)
5837 return SDValue();
5838 SDValue N1 = N->getOperand(1);
5839
5840 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5841 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5842 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5843 APInt SplatUndef;
5844 unsigned SplatBitSize;
5845 bool HasAnyUndefs;
5846
5847 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5848 APInt SplatBits0;
5849 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5850 HasAnyUndefs) && !HasAnyUndefs) {
5851 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5852 APInt SplatBits1;
5853 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5854 HasAnyUndefs) && !HasAnyUndefs &&
5855 SplatBits0 == ~SplatBits1) {
5856 // Canonicalize the vector type to make instruction selection simpler.
5857 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5858 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5859 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005860 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005861 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5862 }
5863 }
5864 }
5865
Jim Grosbach54238562010-07-17 03:30:54 +00005866 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5867 // reasonable.
5868
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005869 // BFI is only available on V6T2+
5870 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5871 return SDValue();
5872
Jim Grosbach54238562010-07-17 03:30:54 +00005873 DebugLoc DL = N->getDebugLoc();
5874 // 1) or (and A, mask), val => ARMbfi A, val, mask
5875 // iff (val & mask) == val
5876 //
5877 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5878 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005879 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005880 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005881 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005882 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005883
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005884 if (VT != MVT::i32)
5885 return SDValue();
5886
Evan Cheng30fb13f2010-12-13 20:32:54 +00005887 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005888
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005889 // The value and the mask need to be constants so we can verify this is
5890 // actually a bitfield set. If the mask is 0xffff, we can do better
5891 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005892 SDValue MaskOp = N0.getOperand(1);
5893 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5894 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005895 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005896 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005897 if (Mask == 0xffff)
5898 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005899 SDValue Res;
5900 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005901 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5902 if (N1C) {
5903 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005904 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005905 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005906
Evan Chenga9688c42010-12-11 04:11:38 +00005907 if (ARM::isBitFieldInvertedMask(Mask)) {
5908 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005909
Evan Cheng30fb13f2010-12-13 20:32:54 +00005910 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005911 DAG.getConstant(Val, MVT::i32),
5912 DAG.getConstant(Mask, MVT::i32));
5913
5914 // Do not add new nodes to DAG combiner worklist.
5915 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005916 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005917 }
Jim Grosbach54238562010-07-17 03:30:54 +00005918 } else if (N1.getOpcode() == ISD::AND) {
5919 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005920 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5921 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005922 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005923 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005924
Eric Christopher29aeed12011-03-26 01:21:03 +00005925 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5926 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005927 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005928 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005929 // The pack halfword instruction works better for masks that fit it,
5930 // so use that when it's available.
5931 if (Subtarget->hasT2ExtractPack() &&
5932 (Mask == 0xffff || Mask == 0xffff0000))
5933 return SDValue();
5934 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005935 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005936 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005937 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005938 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005939 DAG.getConstant(Mask, MVT::i32));
5940 // Do not add new nodes to DAG combiner worklist.
5941 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005942 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005943 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005944 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005945 // The pack halfword instruction works better for masks that fit it,
5946 // so use that when it's available.
5947 if (Subtarget->hasT2ExtractPack() &&
5948 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5949 return SDValue();
5950 // 2b
5951 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005952 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005953 DAG.getConstant(lsb, MVT::i32));
5954 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005955 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005956 // Do not add new nodes to DAG combiner worklist.
5957 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005958 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005959 }
5960 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005961
Evan Cheng30fb13f2010-12-13 20:32:54 +00005962 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5963 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5964 ARM::isBitFieldInvertedMask(~Mask)) {
5965 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5966 // where lsb(mask) == #shamt and masked bits of B are known zero.
5967 SDValue ShAmt = N00.getOperand(1);
5968 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5969 unsigned LSB = CountTrailingZeros_32(Mask);
5970 if (ShAmtC != LSB)
5971 return SDValue();
5972
5973 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5974 DAG.getConstant(~Mask, MVT::i32));
5975
5976 // Do not add new nodes to DAG combiner worklist.
5977 DCI.CombineTo(N, Res, false);
5978 }
5979
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005980 return SDValue();
5981}
5982
Evan Chengbf188ae2011-06-15 01:12:31 +00005983/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
5984/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00005985static SDValue PerformBFICombine(SDNode *N,
5986 TargetLowering::DAGCombinerInfo &DCI) {
5987 SDValue N1 = N->getOperand(1);
5988 if (N1.getOpcode() == ISD::AND) {
5989 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5990 if (!N11C)
5991 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00005992 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5993 unsigned LSB = CountTrailingZeros_32(~InvMask);
5994 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
5995 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00005996 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00005997 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00005998 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5999 N->getOperand(0), N1.getOperand(0),
6000 N->getOperand(2));
6001 }
6002 return SDValue();
6003}
6004
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006005/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
6006/// ARMISD::VMOVRRD.
6007static SDValue PerformVMOVRRDCombine(SDNode *N,
6008 TargetLowering::DAGCombinerInfo &DCI) {
6009 // vmovrrd(vmovdrr x, y) -> x,y
6010 SDValue InDouble = N->getOperand(0);
6011 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6012 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006013
6014 // vmovrrd(load f64) -> (load i32), (load i32)
6015 SDNode *InNode = InDouble.getNode();
6016 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6017 InNode->getValueType(0) == MVT::f64 &&
6018 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6019 !cast<LoadSDNode>(InNode)->isVolatile()) {
6020 // TODO: Should this be done for non-FrameIndex operands?
6021 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6022
6023 SelectionDAG &DAG = DCI.DAG;
6024 DebugLoc DL = LD->getDebugLoc();
6025 SDValue BasePtr = LD->getBasePtr();
6026 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6027 LD->getPointerInfo(), LD->isVolatile(),
6028 LD->isNonTemporal(), LD->getAlignment());
6029
6030 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6031 DAG.getConstant(4, MVT::i32));
6032 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6033 LD->getPointerInfo(), LD->isVolatile(),
6034 LD->isNonTemporal(),
6035 std::min(4U, LD->getAlignment() / 2));
6036
6037 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6038 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6039 DCI.RemoveFromWorklist(LD);
6040 DAG.DeleteNode(LD);
6041 return Result;
6042 }
6043
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006044 return SDValue();
6045}
6046
6047/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6048/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6049static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6050 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6051 SDValue Op0 = N->getOperand(0);
6052 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006053 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006054 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006055 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006056 Op1 = Op1.getOperand(0);
6057 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6058 Op0.getNode() == Op1.getNode() &&
6059 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006060 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006061 N->getValueType(0), Op0.getOperand(0));
6062 return SDValue();
6063}
6064
Bob Wilson31600902010-12-21 06:43:19 +00006065/// PerformSTORECombine - Target-specific dag combine xforms for
6066/// ISD::STORE.
6067static SDValue PerformSTORECombine(SDNode *N,
6068 TargetLowering::DAGCombinerInfo &DCI) {
6069 // Bitcast an i64 store extracted from a vector to f64.
6070 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6071 StoreSDNode *St = cast<StoreSDNode>(N);
6072 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006073 if (!ISD::isNormalStore(St) || St->isVolatile())
6074 return SDValue();
6075
6076 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6077 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6078 SelectionDAG &DAG = DCI.DAG;
6079 DebugLoc DL = St->getDebugLoc();
6080 SDValue BasePtr = St->getBasePtr();
6081 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6082 StVal.getNode()->getOperand(0), BasePtr,
6083 St->getPointerInfo(), St->isVolatile(),
6084 St->isNonTemporal(), St->getAlignment());
6085
6086 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6087 DAG.getConstant(4, MVT::i32));
6088 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6089 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6090 St->isNonTemporal(),
6091 std::min(4U, St->getAlignment() / 2));
6092 }
6093
6094 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006095 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6096 return SDValue();
6097
6098 SelectionDAG &DAG = DCI.DAG;
6099 DebugLoc dl = StVal.getDebugLoc();
6100 SDValue IntVec = StVal.getOperand(0);
6101 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6102 IntVec.getValueType().getVectorNumElements());
6103 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6104 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6105 Vec, StVal.getOperand(1));
6106 dl = N->getDebugLoc();
6107 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6108 // Make the DAGCombiner fold the bitcasts.
6109 DCI.AddToWorklist(Vec.getNode());
6110 DCI.AddToWorklist(ExtElt.getNode());
6111 DCI.AddToWorklist(V.getNode());
6112 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6113 St->getPointerInfo(), St->isVolatile(),
6114 St->isNonTemporal(), St->getAlignment(),
6115 St->getTBAAInfo());
6116}
6117
6118/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6119/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6120/// i64 vector to have f64 elements, since the value can then be loaded
6121/// directly into a VFP register.
6122static bool hasNormalLoadOperand(SDNode *N) {
6123 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6124 for (unsigned i = 0; i < NumElts; ++i) {
6125 SDNode *Elt = N->getOperand(i).getNode();
6126 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6127 return true;
6128 }
6129 return false;
6130}
6131
Bob Wilson75f02882010-09-17 22:59:05 +00006132/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6133/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006134static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6135 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006136 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6137 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6138 // into a pair of GPRs, which is fine when the value is used as a scalar,
6139 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006140 SelectionDAG &DAG = DCI.DAG;
6141 if (N->getNumOperands() == 2) {
6142 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6143 if (RV.getNode())
6144 return RV;
6145 }
Bob Wilson75f02882010-09-17 22:59:05 +00006146
Bob Wilson31600902010-12-21 06:43:19 +00006147 // Load i64 elements as f64 values so that type legalization does not split
6148 // them up into i32 values.
6149 EVT VT = N->getValueType(0);
6150 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6151 return SDValue();
6152 DebugLoc dl = N->getDebugLoc();
6153 SmallVector<SDValue, 8> Ops;
6154 unsigned NumElts = VT.getVectorNumElements();
6155 for (unsigned i = 0; i < NumElts; ++i) {
6156 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6157 Ops.push_back(V);
6158 // Make the DAGCombiner fold the bitcast.
6159 DCI.AddToWorklist(V.getNode());
6160 }
6161 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6162 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6163 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6164}
6165
6166/// PerformInsertEltCombine - Target-specific dag combine xforms for
6167/// ISD::INSERT_VECTOR_ELT.
6168static SDValue PerformInsertEltCombine(SDNode *N,
6169 TargetLowering::DAGCombinerInfo &DCI) {
6170 // Bitcast an i64 load inserted into a vector to f64.
6171 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6172 EVT VT = N->getValueType(0);
6173 SDNode *Elt = N->getOperand(1).getNode();
6174 if (VT.getVectorElementType() != MVT::i64 ||
6175 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6176 return SDValue();
6177
6178 SelectionDAG &DAG = DCI.DAG;
6179 DebugLoc dl = N->getDebugLoc();
6180 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6181 VT.getVectorNumElements());
6182 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6183 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6184 // Make the DAGCombiner fold the bitcasts.
6185 DCI.AddToWorklist(Vec.getNode());
6186 DCI.AddToWorklist(V.getNode());
6187 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6188 Vec, V, N->getOperand(2));
6189 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006190}
6191
Bob Wilsonf20700c2010-10-27 20:38:28 +00006192/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6193/// ISD::VECTOR_SHUFFLE.
6194static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6195 // The LLVM shufflevector instruction does not require the shuffle mask
6196 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6197 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6198 // operands do not match the mask length, they are extended by concatenating
6199 // them with undef vectors. That is probably the right thing for other
6200 // targets, but for NEON it is better to concatenate two double-register
6201 // size vector operands into a single quad-register size vector. Do that
6202 // transformation here:
6203 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6204 // shuffle(concat(v1, v2), undef)
6205 SDValue Op0 = N->getOperand(0);
6206 SDValue Op1 = N->getOperand(1);
6207 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6208 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6209 Op0.getNumOperands() != 2 ||
6210 Op1.getNumOperands() != 2)
6211 return SDValue();
6212 SDValue Concat0Op1 = Op0.getOperand(1);
6213 SDValue Concat1Op1 = Op1.getOperand(1);
6214 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6215 Concat1Op1.getOpcode() != ISD::UNDEF)
6216 return SDValue();
6217 // Skip the transformation if any of the types are illegal.
6218 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6219 EVT VT = N->getValueType(0);
6220 if (!TLI.isTypeLegal(VT) ||
6221 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6222 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6223 return SDValue();
6224
6225 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6226 Op0.getOperand(0), Op1.getOperand(0));
6227 // Translate the shuffle mask.
6228 SmallVector<int, 16> NewMask;
6229 unsigned NumElts = VT.getVectorNumElements();
6230 unsigned HalfElts = NumElts/2;
6231 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6232 for (unsigned n = 0; n < NumElts; ++n) {
6233 int MaskElt = SVN->getMaskElt(n);
6234 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006235 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006236 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006237 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006238 NewElt = HalfElts + MaskElt - NumElts;
6239 NewMask.push_back(NewElt);
6240 }
6241 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6242 DAG.getUNDEF(VT), NewMask.data());
6243}
6244
Bob Wilson1c3ef902011-02-07 17:43:21 +00006245/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6246/// NEON load/store intrinsics to merge base address updates.
6247static SDValue CombineBaseUpdate(SDNode *N,
6248 TargetLowering::DAGCombinerInfo &DCI) {
6249 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6250 return SDValue();
6251
6252 SelectionDAG &DAG = DCI.DAG;
6253 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6254 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6255 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6256 SDValue Addr = N->getOperand(AddrOpIdx);
6257
6258 // Search for a use of the address operand that is an increment.
6259 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6260 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6261 SDNode *User = *UI;
6262 if (User->getOpcode() != ISD::ADD ||
6263 UI.getUse().getResNo() != Addr.getResNo())
6264 continue;
6265
6266 // Check that the add is independent of the load/store. Otherwise, folding
6267 // it would create a cycle.
6268 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6269 continue;
6270
6271 // Find the new opcode for the updating load/store.
6272 bool isLoad = true;
6273 bool isLaneOp = false;
6274 unsigned NewOpc = 0;
6275 unsigned NumVecs = 0;
6276 if (isIntrinsic) {
6277 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6278 switch (IntNo) {
6279 default: assert(0 && "unexpected intrinsic for Neon base update");
6280 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6281 NumVecs = 1; break;
6282 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6283 NumVecs = 2; break;
6284 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6285 NumVecs = 3; break;
6286 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6287 NumVecs = 4; break;
6288 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6289 NumVecs = 2; isLaneOp = true; break;
6290 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6291 NumVecs = 3; isLaneOp = true; break;
6292 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6293 NumVecs = 4; isLaneOp = true; break;
6294 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6295 NumVecs = 1; isLoad = false; break;
6296 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6297 NumVecs = 2; isLoad = false; break;
6298 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6299 NumVecs = 3; isLoad = false; break;
6300 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6301 NumVecs = 4; isLoad = false; break;
6302 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6303 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6304 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6305 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6306 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6307 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6308 }
6309 } else {
6310 isLaneOp = true;
6311 switch (N->getOpcode()) {
6312 default: assert(0 && "unexpected opcode for Neon base update");
6313 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6314 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6315 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6316 }
6317 }
6318
6319 // Find the size of memory referenced by the load/store.
6320 EVT VecTy;
6321 if (isLoad)
6322 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006323 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006324 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6325 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6326 if (isLaneOp)
6327 NumBytes /= VecTy.getVectorNumElements();
6328
6329 // If the increment is a constant, it must match the memory ref size.
6330 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6331 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6332 uint64_t IncVal = CInc->getZExtValue();
6333 if (IncVal != NumBytes)
6334 continue;
6335 } else if (NumBytes >= 3 * 16) {
6336 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6337 // separate instructions that make it harder to use a non-constant update.
6338 continue;
6339 }
6340
6341 // Create the new updating load/store node.
6342 EVT Tys[6];
6343 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6344 unsigned n;
6345 for (n = 0; n < NumResultVecs; ++n)
6346 Tys[n] = VecTy;
6347 Tys[n++] = MVT::i32;
6348 Tys[n] = MVT::Other;
6349 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6350 SmallVector<SDValue, 8> Ops;
6351 Ops.push_back(N->getOperand(0)); // incoming chain
6352 Ops.push_back(N->getOperand(AddrOpIdx));
6353 Ops.push_back(Inc);
6354 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6355 Ops.push_back(N->getOperand(i));
6356 }
6357 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6358 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6359 Ops.data(), Ops.size(),
6360 MemInt->getMemoryVT(),
6361 MemInt->getMemOperand());
6362
6363 // Update the uses.
6364 std::vector<SDValue> NewResults;
6365 for (unsigned i = 0; i < NumResultVecs; ++i) {
6366 NewResults.push_back(SDValue(UpdN.getNode(), i));
6367 }
6368 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6369 DCI.CombineTo(N, NewResults);
6370 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6371
6372 break;
Owen Anderson76706012011-04-05 21:48:57 +00006373 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006374 return SDValue();
6375}
6376
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006377/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6378/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6379/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6380/// return true.
6381static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6382 SelectionDAG &DAG = DCI.DAG;
6383 EVT VT = N->getValueType(0);
6384 // vldN-dup instructions only support 64-bit vectors for N > 1.
6385 if (!VT.is64BitVector())
6386 return false;
6387
6388 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6389 SDNode *VLD = N->getOperand(0).getNode();
6390 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6391 return false;
6392 unsigned NumVecs = 0;
6393 unsigned NewOpc = 0;
6394 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6395 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6396 NumVecs = 2;
6397 NewOpc = ARMISD::VLD2DUP;
6398 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6399 NumVecs = 3;
6400 NewOpc = ARMISD::VLD3DUP;
6401 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6402 NumVecs = 4;
6403 NewOpc = ARMISD::VLD4DUP;
6404 } else {
6405 return false;
6406 }
6407
6408 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6409 // numbers match the load.
6410 unsigned VLDLaneNo =
6411 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6412 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6413 UI != UE; ++UI) {
6414 // Ignore uses of the chain result.
6415 if (UI.getUse().getResNo() == NumVecs)
6416 continue;
6417 SDNode *User = *UI;
6418 if (User->getOpcode() != ARMISD::VDUPLANE ||
6419 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6420 return false;
6421 }
6422
6423 // Create the vldN-dup node.
6424 EVT Tys[5];
6425 unsigned n;
6426 for (n = 0; n < NumVecs; ++n)
6427 Tys[n] = VT;
6428 Tys[n] = MVT::Other;
6429 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6430 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6431 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6432 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6433 Ops, 2, VLDMemInt->getMemoryVT(),
6434 VLDMemInt->getMemOperand());
6435
6436 // Update the uses.
6437 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6438 UI != UE; ++UI) {
6439 unsigned ResNo = UI.getUse().getResNo();
6440 // Ignore uses of the chain result.
6441 if (ResNo == NumVecs)
6442 continue;
6443 SDNode *User = *UI;
6444 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6445 }
6446
6447 // Now the vldN-lane intrinsic is dead except for its chain result.
6448 // Update uses of the chain.
6449 std::vector<SDValue> VLDDupResults;
6450 for (unsigned n = 0; n < NumVecs; ++n)
6451 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6452 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6453 DCI.CombineTo(VLD, VLDDupResults);
6454
6455 return true;
6456}
6457
Bob Wilson9e82bf12010-07-14 01:22:12 +00006458/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6459/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006460static SDValue PerformVDUPLANECombine(SDNode *N,
6461 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006462 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006463
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006464 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6465 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6466 if (CombineVLDDUP(N, DCI))
6467 return SDValue(N, 0);
6468
6469 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6470 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006471 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006472 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006473 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006474 return SDValue();
6475
6476 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6477 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6478 // The canonical VMOV for a zero vector uses a 32-bit element size.
6479 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6480 unsigned EltBits;
6481 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6482 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006483 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006484 if (EltSize > VT.getVectorElementType().getSizeInBits())
6485 return SDValue();
6486
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006487 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006488}
6489
Eric Christopherfa6f5912011-06-29 21:10:36 +00006490// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00006491// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
6492static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
6493{
Chad Rosier118c9a02011-06-28 17:26:57 +00006494 integerPart cN;
6495 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00006496 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
6497 I != E; I++) {
6498 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
6499 if (!C)
6500 return false;
6501
Eric Christopherfa6f5912011-06-29 21:10:36 +00006502 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00006503 APFloat APF = C->getValueAPF();
6504 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
6505 != APFloat::opOK || !isExact)
6506 return false;
6507
6508 c0 = (I == 0) ? cN : c0;
6509 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
6510 return false;
6511 }
6512 C = c0;
6513 return true;
6514}
6515
6516/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
6517/// can replace combinations of VMUL and VCVT (floating-point to integer)
6518/// when the VMUL has a constant operand that is a power of 2.
6519///
6520/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6521/// vmul.f32 d16, d17, d16
6522/// vcvt.s32.f32 d16, d16
6523/// becomes:
6524/// vcvt.s32.f32 d16, d16, #3
6525static SDValue PerformVCVTCombine(SDNode *N,
6526 TargetLowering::DAGCombinerInfo &DCI,
6527 const ARMSubtarget *Subtarget) {
6528 SelectionDAG &DAG = DCI.DAG;
6529 SDValue Op = N->getOperand(0);
6530
6531 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
6532 Op.getOpcode() != ISD::FMUL)
6533 return SDValue();
6534
6535 uint64_t C;
6536 SDValue N0 = Op->getOperand(0);
6537 SDValue ConstVec = Op->getOperand(1);
6538 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
6539
Eric Christopherfa6f5912011-06-29 21:10:36 +00006540 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00006541 !isConstVecPow2(ConstVec, isSigned, C))
6542 return SDValue();
6543
6544 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
6545 Intrinsic::arm_neon_vcvtfp2fxu;
6546 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6547 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006548 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00006549 DAG.getConstant(Log2_64(C), MVT::i32));
6550}
6551
6552/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
6553/// can replace combinations of VCVT (integer to floating-point) and VDIV
6554/// when the VDIV has a constant operand that is a power of 2.
6555///
6556/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6557/// vcvt.f32.s32 d16, d16
6558/// vdiv.f32 d16, d17, d16
6559/// becomes:
6560/// vcvt.f32.s32 d16, d16, #3
6561static SDValue PerformVDIVCombine(SDNode *N,
6562 TargetLowering::DAGCombinerInfo &DCI,
6563 const ARMSubtarget *Subtarget) {
6564 SelectionDAG &DAG = DCI.DAG;
6565 SDValue Op = N->getOperand(0);
6566 unsigned OpOpcode = Op.getNode()->getOpcode();
6567
6568 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
6569 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
6570 return SDValue();
6571
6572 uint64_t C;
6573 SDValue ConstVec = N->getOperand(1);
6574 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
6575
6576 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
6577 !isConstVecPow2(ConstVec, isSigned, C))
6578 return SDValue();
6579
Eric Christopherfa6f5912011-06-29 21:10:36 +00006580 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00006581 Intrinsic::arm_neon_vcvtfxu2fp;
6582 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6583 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006584 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00006585 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
6586}
6587
6588/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00006589/// operand of a vector shift operation, where all the elements of the
6590/// build_vector must have the same constant integer value.
6591static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6592 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006593 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006594 Op = Op.getOperand(0);
6595 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6596 APInt SplatBits, SplatUndef;
6597 unsigned SplatBitSize;
6598 bool HasAnyUndefs;
6599 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6600 HasAnyUndefs, ElementBits) ||
6601 SplatBitSize > ElementBits)
6602 return false;
6603 Cnt = SplatBits.getSExtValue();
6604 return true;
6605}
6606
6607/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6608/// operand of a vector shift left operation. That value must be in the range:
6609/// 0 <= Value < ElementBits for a left shift; or
6610/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006611static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006612 assert(VT.isVector() && "vector shift count is not a vector type");
6613 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6614 if (! getVShiftImm(Op, ElementBits, Cnt))
6615 return false;
6616 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6617}
6618
6619/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6620/// operand of a vector shift right operation. For a shift opcode, the value
6621/// is positive, but for an intrinsic the value count must be negative. The
6622/// absolute value must be in the range:
6623/// 1 <= |Value| <= ElementBits for a right shift; or
6624/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006625static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006626 int64_t &Cnt) {
6627 assert(VT.isVector() && "vector shift count is not a vector type");
6628 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6629 if (! getVShiftImm(Op, ElementBits, Cnt))
6630 return false;
6631 if (isIntrinsic)
6632 Cnt = -Cnt;
6633 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6634}
6635
6636/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6637static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6638 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6639 switch (IntNo) {
6640 default:
6641 // Don't do anything for most intrinsics.
6642 break;
6643
6644 // Vector shifts: check for immediate versions and lower them.
6645 // Note: This is done during DAG combining instead of DAG legalizing because
6646 // the build_vectors for 64-bit vector element shift counts are generally
6647 // not legal, and it is hard to see their values after they get legalized to
6648 // loads from a constant pool.
6649 case Intrinsic::arm_neon_vshifts:
6650 case Intrinsic::arm_neon_vshiftu:
6651 case Intrinsic::arm_neon_vshiftls:
6652 case Intrinsic::arm_neon_vshiftlu:
6653 case Intrinsic::arm_neon_vshiftn:
6654 case Intrinsic::arm_neon_vrshifts:
6655 case Intrinsic::arm_neon_vrshiftu:
6656 case Intrinsic::arm_neon_vrshiftn:
6657 case Intrinsic::arm_neon_vqshifts:
6658 case Intrinsic::arm_neon_vqshiftu:
6659 case Intrinsic::arm_neon_vqshiftsu:
6660 case Intrinsic::arm_neon_vqshiftns:
6661 case Intrinsic::arm_neon_vqshiftnu:
6662 case Intrinsic::arm_neon_vqshiftnsu:
6663 case Intrinsic::arm_neon_vqrshiftns:
6664 case Intrinsic::arm_neon_vqrshiftnu:
6665 case Intrinsic::arm_neon_vqrshiftnsu: {
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 switch (IntNo) {
6671 case Intrinsic::arm_neon_vshifts:
6672 case Intrinsic::arm_neon_vshiftu:
6673 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6674 VShiftOpc = ARMISD::VSHL;
6675 break;
6676 }
6677 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6678 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6679 ARMISD::VSHRs : ARMISD::VSHRu);
6680 break;
6681 }
6682 return SDValue();
6683
6684 case Intrinsic::arm_neon_vshiftls:
6685 case Intrinsic::arm_neon_vshiftlu:
6686 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6687 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006688 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006689
6690 case Intrinsic::arm_neon_vrshifts:
6691 case Intrinsic::arm_neon_vrshiftu:
6692 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6693 break;
6694 return SDValue();
6695
6696 case Intrinsic::arm_neon_vqshifts:
6697 case Intrinsic::arm_neon_vqshiftu:
6698 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6699 break;
6700 return SDValue();
6701
6702 case Intrinsic::arm_neon_vqshiftsu:
6703 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6704 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006705 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006706
6707 case Intrinsic::arm_neon_vshiftn:
6708 case Intrinsic::arm_neon_vrshiftn:
6709 case Intrinsic::arm_neon_vqshiftns:
6710 case Intrinsic::arm_neon_vqshiftnu:
6711 case Intrinsic::arm_neon_vqshiftnsu:
6712 case Intrinsic::arm_neon_vqrshiftns:
6713 case Intrinsic::arm_neon_vqrshiftnu:
6714 case Intrinsic::arm_neon_vqrshiftnsu:
6715 // Narrowing shifts require an immediate right shift.
6716 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6717 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006718 llvm_unreachable("invalid shift count for narrowing vector shift "
6719 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006720
6721 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006722 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006723 }
6724
6725 switch (IntNo) {
6726 case Intrinsic::arm_neon_vshifts:
6727 case Intrinsic::arm_neon_vshiftu:
6728 // Opcode already set above.
6729 break;
6730 case Intrinsic::arm_neon_vshiftls:
6731 case Intrinsic::arm_neon_vshiftlu:
6732 if (Cnt == VT.getVectorElementType().getSizeInBits())
6733 VShiftOpc = ARMISD::VSHLLi;
6734 else
6735 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6736 ARMISD::VSHLLs : ARMISD::VSHLLu);
6737 break;
6738 case Intrinsic::arm_neon_vshiftn:
6739 VShiftOpc = ARMISD::VSHRN; break;
6740 case Intrinsic::arm_neon_vrshifts:
6741 VShiftOpc = ARMISD::VRSHRs; break;
6742 case Intrinsic::arm_neon_vrshiftu:
6743 VShiftOpc = ARMISD::VRSHRu; break;
6744 case Intrinsic::arm_neon_vrshiftn:
6745 VShiftOpc = ARMISD::VRSHRN; break;
6746 case Intrinsic::arm_neon_vqshifts:
6747 VShiftOpc = ARMISD::VQSHLs; break;
6748 case Intrinsic::arm_neon_vqshiftu:
6749 VShiftOpc = ARMISD::VQSHLu; break;
6750 case Intrinsic::arm_neon_vqshiftsu:
6751 VShiftOpc = ARMISD::VQSHLsu; break;
6752 case Intrinsic::arm_neon_vqshiftns:
6753 VShiftOpc = ARMISD::VQSHRNs; break;
6754 case Intrinsic::arm_neon_vqshiftnu:
6755 VShiftOpc = ARMISD::VQSHRNu; break;
6756 case Intrinsic::arm_neon_vqshiftnsu:
6757 VShiftOpc = ARMISD::VQSHRNsu; break;
6758 case Intrinsic::arm_neon_vqrshiftns:
6759 VShiftOpc = ARMISD::VQRSHRNs; break;
6760 case Intrinsic::arm_neon_vqrshiftnu:
6761 VShiftOpc = ARMISD::VQRSHRNu; break;
6762 case Intrinsic::arm_neon_vqrshiftnsu:
6763 VShiftOpc = ARMISD::VQRSHRNsu; break;
6764 }
6765
6766 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006767 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006768 }
6769
6770 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006771 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006772 int64_t Cnt;
6773 unsigned VShiftOpc = 0;
6774
6775 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6776 VShiftOpc = ARMISD::VSLI;
6777 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6778 VShiftOpc = ARMISD::VSRI;
6779 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006780 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006781 }
6782
6783 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6784 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006785 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006786 }
6787
6788 case Intrinsic::arm_neon_vqrshifts:
6789 case Intrinsic::arm_neon_vqrshiftu:
6790 // No immediate versions of these to check for.
6791 break;
6792 }
6793
6794 return SDValue();
6795}
6796
6797/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6798/// lowers them. As with the vector shift intrinsics, this is done during DAG
6799/// combining instead of DAG legalizing because the build_vectors for 64-bit
6800/// vector element shift counts are generally not legal, and it is hard to see
6801/// their values after they get legalized to loads from a constant pool.
6802static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6803 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006804 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006805
6806 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006807 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6808 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006809 return SDValue();
6810
6811 assert(ST->hasNEON() && "unexpected vector shift");
6812 int64_t Cnt;
6813
6814 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006815 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006816
6817 case ISD::SHL:
6818 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6819 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006820 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006821 break;
6822
6823 case ISD::SRA:
6824 case ISD::SRL:
6825 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6826 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6827 ARMISD::VSHRs : ARMISD::VSHRu);
6828 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006829 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006830 }
6831 }
6832 return SDValue();
6833}
6834
6835/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6836/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6837static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6838 const ARMSubtarget *ST) {
6839 SDValue N0 = N->getOperand(0);
6840
6841 // Check for sign- and zero-extensions of vector extract operations of 8-
6842 // and 16-bit vector elements. NEON supports these directly. They are
6843 // handled during DAG combining because type legalization will promote them
6844 // to 32-bit types and it is messy to recognize the operations after that.
6845 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6846 SDValue Vec = N0.getOperand(0);
6847 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006848 EVT VT = N->getValueType(0);
6849 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006850 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6851
Owen Anderson825b72b2009-08-11 20:47:22 +00006852 if (VT == MVT::i32 &&
6853 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006854 TLI.isTypeLegal(Vec.getValueType()) &&
6855 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006856
6857 unsigned Opc = 0;
6858 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006859 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006860 case ISD::SIGN_EXTEND:
6861 Opc = ARMISD::VGETLANEs;
6862 break;
6863 case ISD::ZERO_EXTEND:
6864 case ISD::ANY_EXTEND:
6865 Opc = ARMISD::VGETLANEu;
6866 break;
6867 }
6868 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6869 }
6870 }
6871
6872 return SDValue();
6873}
6874
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006875/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6876/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6877static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6878 const ARMSubtarget *ST) {
6879 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006880 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006881 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6882 // a NaN; only do the transformation when it matches that behavior.
6883
6884 // For now only do this when using NEON for FP operations; if using VFP, it
6885 // is not obvious that the benefit outweighs the cost of switching to the
6886 // NEON pipeline.
6887 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6888 N->getValueType(0) != MVT::f32)
6889 return SDValue();
6890
6891 SDValue CondLHS = N->getOperand(0);
6892 SDValue CondRHS = N->getOperand(1);
6893 SDValue LHS = N->getOperand(2);
6894 SDValue RHS = N->getOperand(3);
6895 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6896
6897 unsigned Opcode = 0;
6898 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006899 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006900 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006901 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006902 IsReversed = true ; // x CC y ? y : x
6903 } else {
6904 return SDValue();
6905 }
6906
Bob Wilsone742bb52010-02-24 22:15:53 +00006907 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006908 switch (CC) {
6909 default: break;
6910 case ISD::SETOLT:
6911 case ISD::SETOLE:
6912 case ISD::SETLT:
6913 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006914 case ISD::SETULT:
6915 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006916 // If LHS is NaN, an ordered comparison will be false and the result will
6917 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6918 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6919 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6920 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6921 break;
6922 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6923 // will return -0, so vmin can only be used for unsafe math or if one of
6924 // the operands is known to be nonzero.
6925 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6926 !UnsafeFPMath &&
6927 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6928 break;
6929 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006930 break;
6931
6932 case ISD::SETOGT:
6933 case ISD::SETOGE:
6934 case ISD::SETGT:
6935 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006936 case ISD::SETUGT:
6937 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006938 // If LHS is NaN, an ordered comparison will be false and the result will
6939 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6940 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6941 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6942 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6943 break;
6944 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6945 // will return +0, so vmax can only be used for unsafe math or if one of
6946 // the operands is known to be nonzero.
6947 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6948 !UnsafeFPMath &&
6949 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6950 break;
6951 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006952 break;
6953 }
6954
6955 if (!Opcode)
6956 return SDValue();
6957 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6958}
6959
Dan Gohman475871a2008-07-27 21:46:04 +00006960SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006961 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006962 switch (N->getOpcode()) {
6963 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00006964 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006965 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006966 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006967 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006968 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006969 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006970 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006971 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006972 case ISD::STORE: return PerformSTORECombine(N, DCI);
6973 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6974 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006975 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006976 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00006977 case ISD::FP_TO_SINT:
6978 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
6979 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006980 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006981 case ISD::SHL:
6982 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006983 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006984 case ISD::SIGN_EXTEND:
6985 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006986 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6987 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006988 case ARMISD::VLD2DUP:
6989 case ARMISD::VLD3DUP:
6990 case ARMISD::VLD4DUP:
6991 return CombineBaseUpdate(N, DCI);
6992 case ISD::INTRINSIC_VOID:
6993 case ISD::INTRINSIC_W_CHAIN:
6994 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6995 case Intrinsic::arm_neon_vld1:
6996 case Intrinsic::arm_neon_vld2:
6997 case Intrinsic::arm_neon_vld3:
6998 case Intrinsic::arm_neon_vld4:
6999 case Intrinsic::arm_neon_vld2lane:
7000 case Intrinsic::arm_neon_vld3lane:
7001 case Intrinsic::arm_neon_vld4lane:
7002 case Intrinsic::arm_neon_vst1:
7003 case Intrinsic::arm_neon_vst2:
7004 case Intrinsic::arm_neon_vst3:
7005 case Intrinsic::arm_neon_vst4:
7006 case Intrinsic::arm_neon_vst2lane:
7007 case Intrinsic::arm_neon_vst3lane:
7008 case Intrinsic::arm_neon_vst4lane:
7009 return CombineBaseUpdate(N, DCI);
7010 default: break;
7011 }
7012 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007013 }
Dan Gohman475871a2008-07-27 21:46:04 +00007014 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007015}
7016
Evan Cheng31959b12011-02-02 01:06:55 +00007017bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
7018 EVT VT) const {
7019 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
7020}
7021
Bill Wendlingaf566342009-08-15 21:21:19 +00007022bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00007023 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00007024 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00007025
7026 switch (VT.getSimpleVT().SimpleTy) {
7027 default:
7028 return false;
7029 case MVT::i8:
7030 case MVT::i16:
7031 case MVT::i32:
7032 return true;
7033 // FIXME: VLD1 etc with standard alignment is legal.
7034 }
7035}
7036
Evan Chenge6c835f2009-08-14 20:09:37 +00007037static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
7038 if (V < 0)
7039 return false;
7040
7041 unsigned Scale = 1;
7042 switch (VT.getSimpleVT().SimpleTy) {
7043 default: return false;
7044 case MVT::i1:
7045 case MVT::i8:
7046 // Scale == 1;
7047 break;
7048 case MVT::i16:
7049 // Scale == 2;
7050 Scale = 2;
7051 break;
7052 case MVT::i32:
7053 // Scale == 4;
7054 Scale = 4;
7055 break;
7056 }
7057
7058 if ((V & (Scale - 1)) != 0)
7059 return false;
7060 V /= Scale;
7061 return V == (V & ((1LL << 5) - 1));
7062}
7063
7064static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
7065 const ARMSubtarget *Subtarget) {
7066 bool isNeg = false;
7067 if (V < 0) {
7068 isNeg = true;
7069 V = - V;
7070 }
7071
7072 switch (VT.getSimpleVT().SimpleTy) {
7073 default: return false;
7074 case MVT::i1:
7075 case MVT::i8:
7076 case MVT::i16:
7077 case MVT::i32:
7078 // + imm12 or - imm8
7079 if (isNeg)
7080 return V == (V & ((1LL << 8) - 1));
7081 return V == (V & ((1LL << 12) - 1));
7082 case MVT::f32:
7083 case MVT::f64:
7084 // Same as ARM mode. FIXME: NEON?
7085 if (!Subtarget->hasVFP2())
7086 return false;
7087 if ((V & 3) != 0)
7088 return false;
7089 V >>= 2;
7090 return V == (V & ((1LL << 8) - 1));
7091 }
7092}
7093
Evan Chengb01fad62007-03-12 23:30:29 +00007094/// isLegalAddressImmediate - Return true if the integer value can be used
7095/// as the offset of the target addressing mode for load / store of the
7096/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00007097static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007098 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00007099 if (V == 0)
7100 return true;
7101
Evan Cheng65011532009-03-09 19:15:00 +00007102 if (!VT.isSimple())
7103 return false;
7104
Evan Chenge6c835f2009-08-14 20:09:37 +00007105 if (Subtarget->isThumb1Only())
7106 return isLegalT1AddressImmediate(V, VT);
7107 else if (Subtarget->isThumb2())
7108 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007109
Evan Chenge6c835f2009-08-14 20:09:37 +00007110 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007111 if (V < 0)
7112 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007114 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007115 case MVT::i1:
7116 case MVT::i8:
7117 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007118 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007119 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007120 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007121 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007122 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007123 case MVT::f32:
7124 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007125 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007126 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007127 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007128 return false;
7129 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007130 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007131 }
Evan Chenga8e29892007-01-19 07:51:42 +00007132}
7133
Evan Chenge6c835f2009-08-14 20:09:37 +00007134bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7135 EVT VT) const {
7136 int Scale = AM.Scale;
7137 if (Scale < 0)
7138 return false;
7139
7140 switch (VT.getSimpleVT().SimpleTy) {
7141 default: return false;
7142 case MVT::i1:
7143 case MVT::i8:
7144 case MVT::i16:
7145 case MVT::i32:
7146 if (Scale == 1)
7147 return true;
7148 // r + r << imm
7149 Scale = Scale & ~1;
7150 return Scale == 2 || Scale == 4 || Scale == 8;
7151 case MVT::i64:
7152 // r + r
7153 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7154 return true;
7155 return false;
7156 case MVT::isVoid:
7157 // Note, we allow "void" uses (basically, uses that aren't loads or
7158 // stores), because arm allows folding a scale into many arithmetic
7159 // operations. This should be made more precise and revisited later.
7160
7161 // Allow r << imm, but the imm has to be a multiple of two.
7162 if (Scale & 1) return false;
7163 return isPowerOf2_32(Scale);
7164 }
7165}
7166
Chris Lattner37caf8c2007-04-09 23:33:39 +00007167/// isLegalAddressingMode - Return true if the addressing mode represented
7168/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007169bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007170 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007171 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007172 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007173 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007174
Chris Lattner37caf8c2007-04-09 23:33:39 +00007175 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007176 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007177 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007178
Chris Lattner37caf8c2007-04-09 23:33:39 +00007179 switch (AM.Scale) {
7180 case 0: // no scale reg, must be "r+i" or "r", or "i".
7181 break;
7182 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007183 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007184 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007185 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007186 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007187 // ARM doesn't support any R+R*scale+imm addr modes.
7188 if (AM.BaseOffs)
7189 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007190
Bob Wilson2c7dab12009-04-08 17:55:28 +00007191 if (!VT.isSimple())
7192 return false;
7193
Evan Chenge6c835f2009-08-14 20:09:37 +00007194 if (Subtarget->isThumb2())
7195 return isLegalT2ScaledAddressingMode(AM, VT);
7196
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007197 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007198 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007199 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007200 case MVT::i1:
7201 case MVT::i8:
7202 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007203 if (Scale < 0) Scale = -Scale;
7204 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007205 return true;
7206 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007207 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007208 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007209 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007210 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007211 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007212 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007213 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007214
Owen Anderson825b72b2009-08-11 20:47:22 +00007215 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007216 // Note, we allow "void" uses (basically, uses that aren't loads or
7217 // stores), because arm allows folding a scale into many arithmetic
7218 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007219
Chris Lattner37caf8c2007-04-09 23:33:39 +00007220 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007221 if (Scale & 1) return false;
7222 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007223 }
7224 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007225 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007226 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007227}
7228
Evan Cheng77e47512009-11-11 19:05:52 +00007229/// isLegalICmpImmediate - Return true if the specified immediate is legal
7230/// icmp immediate, that is the target has icmp instructions which can compare
7231/// a register against the immediate without having to materialize the
7232/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007233bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007234 if (!Subtarget->isThumb())
7235 return ARM_AM::getSOImmVal(Imm) != -1;
7236 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007237 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007238 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007239}
7240
Dan Gohmancca82142011-05-03 00:46:49 +00007241/// isLegalAddImmediate - Return true if the specified immediate is legal
7242/// add immediate, that is the target has add instructions which can add
7243/// a register with the immediate without having to materialize the
7244/// immediate into a register.
7245bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7246 return ARM_AM::getSOImmVal(Imm) != -1;
7247}
7248
Owen Andersone50ed302009-08-10 22:56:29 +00007249static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007250 bool isSEXTLoad, SDValue &Base,
7251 SDValue &Offset, bool &isInc,
7252 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007253 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7254 return false;
7255
Owen Anderson825b72b2009-08-11 20:47:22 +00007256 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007257 // AddressingMode 3
7258 Base = Ptr->getOperand(0);
7259 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007260 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007261 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007262 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007263 isInc = false;
7264 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7265 return true;
7266 }
7267 }
7268 isInc = (Ptr->getOpcode() == ISD::ADD);
7269 Offset = Ptr->getOperand(1);
7270 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007271 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007272 // AddressingMode 2
7273 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007274 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007275 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007276 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007277 isInc = false;
7278 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7279 Base = Ptr->getOperand(0);
7280 return true;
7281 }
7282 }
7283
7284 if (Ptr->getOpcode() == ISD::ADD) {
7285 isInc = true;
7286 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
7287 if (ShOpcVal != ARM_AM::no_shift) {
7288 Base = Ptr->getOperand(1);
7289 Offset = Ptr->getOperand(0);
7290 } else {
7291 Base = Ptr->getOperand(0);
7292 Offset = Ptr->getOperand(1);
7293 }
7294 return true;
7295 }
7296
7297 isInc = (Ptr->getOpcode() == ISD::ADD);
7298 Base = Ptr->getOperand(0);
7299 Offset = Ptr->getOperand(1);
7300 return true;
7301 }
7302
Jim Grosbache5165492009-11-09 00:11:35 +00007303 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007304 return false;
7305}
7306
Owen Andersone50ed302009-08-10 22:56:29 +00007307static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007308 bool isSEXTLoad, SDValue &Base,
7309 SDValue &Offset, bool &isInc,
7310 SelectionDAG &DAG) {
7311 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7312 return false;
7313
7314 Base = Ptr->getOperand(0);
7315 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7316 int RHSC = (int)RHS->getZExtValue();
7317 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7318 assert(Ptr->getOpcode() == ISD::ADD);
7319 isInc = false;
7320 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7321 return true;
7322 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7323 isInc = Ptr->getOpcode() == ISD::ADD;
7324 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7325 return true;
7326 }
7327 }
7328
7329 return false;
7330}
7331
Evan Chenga8e29892007-01-19 07:51:42 +00007332/// getPreIndexedAddressParts - returns true by value, base pointer and
7333/// offset pointer and addressing mode by reference if the node's address
7334/// can be legally represented as pre-indexed load / store address.
7335bool
Dan Gohman475871a2008-07-27 21:46:04 +00007336ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7337 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007338 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007339 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007340 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007341 return false;
7342
Owen Andersone50ed302009-08-10 22:56:29 +00007343 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007344 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007345 bool isSEXTLoad = false;
7346 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7347 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007348 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007349 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7350 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7351 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007352 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007353 } else
7354 return false;
7355
7356 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007357 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007358 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007359 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7360 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007361 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007362 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007363 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007364 if (!isLegal)
7365 return false;
7366
7367 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7368 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007369}
7370
7371/// getPostIndexedAddressParts - returns true by value, base pointer and
7372/// offset pointer and addressing mode by reference if this node can be
7373/// combined with a load / store to form a post-indexed load / store.
7374bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007375 SDValue &Base,
7376 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007377 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007378 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007379 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007380 return false;
7381
Owen Andersone50ed302009-08-10 22:56:29 +00007382 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007383 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007384 bool isSEXTLoad = false;
7385 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007386 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007387 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007388 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7389 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007390 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007391 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007392 } else
7393 return false;
7394
7395 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007396 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007397 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007398 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007399 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007400 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007401 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7402 isInc, DAG);
7403 if (!isLegal)
7404 return false;
7405
Evan Cheng28dad2a2010-05-18 21:31:17 +00007406 if (Ptr != Base) {
7407 // Swap base ptr and offset to catch more post-index load / store when
7408 // it's legal. In Thumb2 mode, offset must be an immediate.
7409 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7410 !Subtarget->isThumb2())
7411 std::swap(Base, Offset);
7412
7413 // Post-indexed load / store update the base pointer.
7414 if (Ptr != Base)
7415 return false;
7416 }
7417
Evan Chenge88d5ce2009-07-02 07:28:31 +00007418 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7419 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007420}
7421
Dan Gohman475871a2008-07-27 21:46:04 +00007422void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007423 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007424 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007425 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007426 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007427 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007428 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007429 switch (Op.getOpcode()) {
7430 default: break;
7431 case ARMISD::CMOV: {
7432 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007433 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007434 if (KnownZero == 0 && KnownOne == 0) return;
7435
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007436 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007437 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7438 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007439 KnownZero &= KnownZeroRHS;
7440 KnownOne &= KnownOneRHS;
7441 return;
7442 }
7443 }
7444}
7445
7446//===----------------------------------------------------------------------===//
7447// ARM Inline Assembly Support
7448//===----------------------------------------------------------------------===//
7449
Evan Cheng55d42002011-01-08 01:24:27 +00007450bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7451 // Looking for "rev" which is V6+.
7452 if (!Subtarget->hasV6Ops())
7453 return false;
7454
7455 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7456 std::string AsmStr = IA->getAsmString();
7457 SmallVector<StringRef, 4> AsmPieces;
7458 SplitString(AsmStr, AsmPieces, ";\n");
7459
7460 switch (AsmPieces.size()) {
7461 default: return false;
7462 case 1:
7463 AsmStr = AsmPieces[0];
7464 AsmPieces.clear();
7465 SplitString(AsmStr, AsmPieces, " \t,");
7466
7467 // rev $0, $1
7468 if (AsmPieces.size() == 3 &&
7469 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7470 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7471 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7472 if (Ty && Ty->getBitWidth() == 32)
7473 return IntrinsicLowering::LowerToByteSwap(CI);
7474 }
7475 break;
7476 }
7477
7478 return false;
7479}
7480
Evan Chenga8e29892007-01-19 07:51:42 +00007481/// getConstraintType - Given a constraint letter, return the type of
7482/// constraint it is for this target.
7483ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007484ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7485 if (Constraint.size() == 1) {
7486 switch (Constraint[0]) {
7487 default: break;
7488 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007489 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00007490 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007491 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007492 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00007493 case 'j': return C_Other; // Constant for movw.
Chris Lattner4234f572007-03-25 02:14:49 +00007494 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007495 } else if (Constraint.size() == 2) {
7496 switch (Constraint[0]) {
7497 default: break;
7498 // All 'U+' constraints are addresses.
7499 case 'U': return C_Memory;
7500 }
Evan Chenga8e29892007-01-19 07:51:42 +00007501 }
Chris Lattner4234f572007-03-25 02:14:49 +00007502 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007503}
7504
John Thompson44ab89e2010-10-29 17:29:13 +00007505/// Examine constraint type and operand type and determine a weight value.
7506/// This object must already have been set up with the operand type
7507/// and the current alternative constraint selected.
7508TargetLowering::ConstraintWeight
7509ARMTargetLowering::getSingleConstraintMatchWeight(
7510 AsmOperandInfo &info, const char *constraint) const {
7511 ConstraintWeight weight = CW_Invalid;
7512 Value *CallOperandVal = info.CallOperandVal;
7513 // If we don't have a value, we can't do a match,
7514 // but allow it at the lowest weight.
7515 if (CallOperandVal == NULL)
7516 return CW_Default;
7517 const Type *type = CallOperandVal->getType();
7518 // Look at the constraint type.
7519 switch (*constraint) {
7520 default:
7521 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7522 break;
7523 case 'l':
7524 if (type->isIntegerTy()) {
7525 if (Subtarget->isThumb())
7526 weight = CW_SpecificReg;
7527 else
7528 weight = CW_Register;
7529 }
7530 break;
7531 case 'w':
7532 if (type->isFloatingPointTy())
7533 weight = CW_Register;
7534 break;
7535 }
7536 return weight;
7537}
7538
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007539typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7540RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007541ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007542 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007543 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007544 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007545 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007546 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007547 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007548 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007549 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007550 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007551 case 'h': // High regs or no regs.
7552 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007553 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007554 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007555 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007556 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007557 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007558 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007559 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007560 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007561 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007562 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007563 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007564 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007565 case 'x':
7566 if (VT == MVT::f32)
7567 return RCPair(0U, ARM::SPR_8RegisterClass);
7568 if (VT.getSizeInBits() == 64)
7569 return RCPair(0U, ARM::DPR_8RegisterClass);
7570 if (VT.getSizeInBits() == 128)
7571 return RCPair(0U, ARM::QPR_8RegisterClass);
7572 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007573 case 't':
7574 if (VT == MVT::f32)
7575 return RCPair(0U, ARM::SPRRegisterClass);
7576 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007577 }
7578 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007579 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007580 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007581
Evan Chenga8e29892007-01-19 07:51:42 +00007582 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7583}
7584
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007585/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7586/// vector. If it is invalid, don't add anything to Ops.
7587void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007588 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007589 std::vector<SDValue>&Ops,
7590 SelectionDAG &DAG) const {
7591 SDValue Result(0, 0);
7592
Eric Christopher100c8332011-06-02 23:16:42 +00007593 // Currently only support length 1 constraints.
7594 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007595
Eric Christopher100c8332011-06-02 23:16:42 +00007596 char ConstraintLetter = Constraint[0];
7597 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007598 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007599 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007600 case 'I': case 'J': case 'K': case 'L':
7601 case 'M': case 'N': case 'O':
7602 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7603 if (!C)
7604 return;
7605
7606 int64_t CVal64 = C->getSExtValue();
7607 int CVal = (int) CVal64;
7608 // None of these constraints allow values larger than 32 bits. Check
7609 // that the value fits in an int.
7610 if (CVal != CVal64)
7611 return;
7612
Eric Christopher100c8332011-06-02 23:16:42 +00007613 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007614 case 'j':
7615 // Constant suitable for movw, must be between 0 and
7616 // 65535.
7617 if (Subtarget->hasV6T2Ops())
7618 if (CVal >= 0 && CVal <= 65535)
7619 break;
7620 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007621 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007622 if (Subtarget->isThumb1Only()) {
7623 // This must be a constant between 0 and 255, for ADD
7624 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007625 if (CVal >= 0 && CVal <= 255)
7626 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007627 } else if (Subtarget->isThumb2()) {
7628 // A constant that can be used as an immediate value in a
7629 // data-processing instruction.
7630 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7631 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007632 } else {
7633 // A constant that can be used as an immediate value in a
7634 // data-processing instruction.
7635 if (ARM_AM::getSOImmVal(CVal) != -1)
7636 break;
7637 }
7638 return;
7639
7640 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007641 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007642 // This must be a constant between -255 and -1, for negated ADD
7643 // immediates. This can be used in GCC with an "n" modifier that
7644 // prints the negated value, for use with SUB instructions. It is
7645 // not useful otherwise but is implemented for compatibility.
7646 if (CVal >= -255 && CVal <= -1)
7647 break;
7648 } else {
7649 // This must be a constant between -4095 and 4095. It is not clear
7650 // what this constraint is intended for. Implemented for
7651 // compatibility with GCC.
7652 if (CVal >= -4095 && CVal <= 4095)
7653 break;
7654 }
7655 return;
7656
7657 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007658 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007659 // A 32-bit value where only one byte has a nonzero value. Exclude
7660 // zero to match GCC. This constraint is used by GCC internally for
7661 // constants that can be loaded with a move/shift combination.
7662 // It is not useful otherwise but is implemented for compatibility.
7663 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7664 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007665 } else if (Subtarget->isThumb2()) {
7666 // A constant whose bitwise inverse can be used as an immediate
7667 // value in a data-processing instruction. This can be used in GCC
7668 // with a "B" modifier that prints the inverted value, for use with
7669 // BIC and MVN instructions. It is not useful otherwise but is
7670 // implemented for compatibility.
7671 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7672 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007673 } else {
7674 // A constant whose bitwise inverse can be used as an immediate
7675 // value in a data-processing instruction. This can be used in GCC
7676 // with a "B" modifier that prints the inverted value, for use with
7677 // BIC and MVN instructions. It is not useful otherwise but is
7678 // implemented for compatibility.
7679 if (ARM_AM::getSOImmVal(~CVal) != -1)
7680 break;
7681 }
7682 return;
7683
7684 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007685 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007686 // This must be a constant between -7 and 7,
7687 // for 3-operand ADD/SUB immediate instructions.
7688 if (CVal >= -7 && CVal < 7)
7689 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007690 } else if (Subtarget->isThumb2()) {
7691 // A constant whose negation can be used as an immediate value in a
7692 // data-processing instruction. This can be used in GCC with an "n"
7693 // modifier that prints the negated value, for use with SUB
7694 // instructions. It is not useful otherwise but is implemented for
7695 // compatibility.
7696 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7697 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007698 } else {
7699 // A constant whose negation can be used as an immediate value in a
7700 // data-processing instruction. This can be used in GCC with an "n"
7701 // modifier that prints the negated value, for use with SUB
7702 // instructions. It is not useful otherwise but is implemented for
7703 // compatibility.
7704 if (ARM_AM::getSOImmVal(-CVal) != -1)
7705 break;
7706 }
7707 return;
7708
7709 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007710 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007711 // This must be a multiple of 4 between 0 and 1020, for
7712 // ADD sp + immediate.
7713 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7714 break;
7715 } else {
7716 // A power of two or a constant between 0 and 32. This is used in
7717 // GCC for the shift amount on shifted register operands, but it is
7718 // useful in general for any shift amounts.
7719 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7720 break;
7721 }
7722 return;
7723
7724 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007725 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007726 // This must be a constant between 0 and 31, for shift amounts.
7727 if (CVal >= 0 && CVal <= 31)
7728 break;
7729 }
7730 return;
7731
7732 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007733 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007734 // This must be a multiple of 4 between -508 and 508, for
7735 // ADD/SUB sp = sp + immediate.
7736 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7737 break;
7738 }
7739 return;
7740 }
7741 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7742 break;
7743 }
7744
7745 if (Result.getNode()) {
7746 Ops.push_back(Result);
7747 return;
7748 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007749 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007750}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007751
7752bool
7753ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7754 // The ARM target isn't yet aware of offsets.
7755 return false;
7756}
Evan Cheng39382422009-10-28 01:44:26 +00007757
7758int ARM::getVFPf32Imm(const APFloat &FPImm) {
7759 APInt Imm = FPImm.bitcastToAPInt();
7760 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7761 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7762 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7763
7764 // We can handle 4 bits of mantissa.
7765 // mantissa = (16+UInt(e:f:g:h))/16.
7766 if (Mantissa & 0x7ffff)
7767 return -1;
7768 Mantissa >>= 19;
7769 if ((Mantissa & 0xf) != Mantissa)
7770 return -1;
7771
7772 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7773 if (Exp < -3 || Exp > 4)
7774 return -1;
7775 Exp = ((Exp+3) & 0x7) ^ 4;
7776
7777 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7778}
7779
7780int ARM::getVFPf64Imm(const APFloat &FPImm) {
7781 APInt Imm = FPImm.bitcastToAPInt();
7782 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7783 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7784 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7785
7786 // We can handle 4 bits of mantissa.
7787 // mantissa = (16+UInt(e:f:g:h))/16.
7788 if (Mantissa & 0xffffffffffffLL)
7789 return -1;
7790 Mantissa >>= 48;
7791 if ((Mantissa & 0xf) != Mantissa)
7792 return -1;
7793
7794 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7795 if (Exp < -3 || Exp > 4)
7796 return -1;
7797 Exp = ((Exp+3) & 0x7) ^ 4;
7798
7799 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7800}
7801
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007802bool ARM::isBitFieldInvertedMask(unsigned v) {
7803 if (v == 0xffffffff)
7804 return 0;
7805 // there can be 1's on either or both "outsides", all the "inside"
7806 // bits must be 0's
7807 unsigned int lsb = 0, msb = 31;
7808 while (v & (1 << msb)) --msb;
7809 while (v & (1 << lsb)) ++lsb;
7810 for (unsigned int i = lsb; i <= msb; ++i) {
7811 if (v & (1 << i))
7812 return 0;
7813 }
7814 return 1;
7815}
7816
Evan Cheng39382422009-10-28 01:44:26 +00007817/// isFPImmLegal - Returns true if the target can instruction select the
7818/// specified FP immediate natively. If false, the legalizer will
7819/// materialize the FP immediate as a load from a constant pool.
7820bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7821 if (!Subtarget->hasVFP3())
7822 return false;
7823 if (VT == MVT::f32)
7824 return ARM::getVFPf32Imm(Imm) != -1;
7825 if (VT == MVT::f64)
7826 return ARM::getVFPf64Imm(Imm) != -1;
7827 return false;
7828}
Bob Wilson65ffec42010-09-21 17:56:22 +00007829
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007830/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007831/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7832/// specified in the intrinsic calls.
7833bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7834 const CallInst &I,
7835 unsigned Intrinsic) const {
7836 switch (Intrinsic) {
7837 case Intrinsic::arm_neon_vld1:
7838 case Intrinsic::arm_neon_vld2:
7839 case Intrinsic::arm_neon_vld3:
7840 case Intrinsic::arm_neon_vld4:
7841 case Intrinsic::arm_neon_vld2lane:
7842 case Intrinsic::arm_neon_vld3lane:
7843 case Intrinsic::arm_neon_vld4lane: {
7844 Info.opc = ISD::INTRINSIC_W_CHAIN;
7845 // Conservatively set memVT to the entire set of vectors loaded.
7846 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7847 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7848 Info.ptrVal = I.getArgOperand(0);
7849 Info.offset = 0;
7850 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7851 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7852 Info.vol = false; // volatile loads with NEON intrinsics not supported
7853 Info.readMem = true;
7854 Info.writeMem = false;
7855 return true;
7856 }
7857 case Intrinsic::arm_neon_vst1:
7858 case Intrinsic::arm_neon_vst2:
7859 case Intrinsic::arm_neon_vst3:
7860 case Intrinsic::arm_neon_vst4:
7861 case Intrinsic::arm_neon_vst2lane:
7862 case Intrinsic::arm_neon_vst3lane:
7863 case Intrinsic::arm_neon_vst4lane: {
7864 Info.opc = ISD::INTRINSIC_VOID;
7865 // Conservatively set memVT to the entire set of vectors stored.
7866 unsigned NumElts = 0;
7867 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7868 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7869 if (!ArgTy->isVectorTy())
7870 break;
7871 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7872 }
7873 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7874 Info.ptrVal = I.getArgOperand(0);
7875 Info.offset = 0;
7876 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7877 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7878 Info.vol = false; // volatile stores with NEON intrinsics not supported
7879 Info.readMem = false;
7880 Info.writeMem = true;
7881 return true;
7882 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007883 case Intrinsic::arm_strexd: {
7884 Info.opc = ISD::INTRINSIC_W_CHAIN;
7885 Info.memVT = MVT::i64;
7886 Info.ptrVal = I.getArgOperand(2);
7887 Info.offset = 0;
7888 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007889 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007890 Info.readMem = false;
7891 Info.writeMem = true;
7892 return true;
7893 }
7894 case Intrinsic::arm_ldrexd: {
7895 Info.opc = ISD::INTRINSIC_W_CHAIN;
7896 Info.memVT = MVT::i64;
7897 Info.ptrVal = I.getArgOperand(0);
7898 Info.offset = 0;
7899 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007900 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007901 Info.readMem = true;
7902 Info.writeMem = false;
7903 return true;
7904 }
Bob Wilson65ffec42010-09-21 17:56:22 +00007905 default:
7906 break;
7907 }
7908
7909 return false;
7910}