blob: 8cd9aa27d77c9c18342684ad589a3c097cef96e7 [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::
Evan Cheng0110ac62010-06-19 01:01:32 +00001640 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001641 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1642 // LR. This means if we need to reload LR, it takes an extra instructions,
1643 // which outweighs the value of the tail call; but here we don't know yet
1644 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001645 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001646 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001647
1648 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1649 // but we need to make sure there are enough registers; the only valid
1650 // registers are the 4 used for parameters. We don't currently do this
1651 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001652 if (Subtarget->isThumb1Only())
1653 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001654
Dale Johannesen51e28e62010-06-03 21:09:53 +00001655 // If the calling conventions do not match, then we'd better make sure the
1656 // results are returned in the same way as what the caller expects.
1657 if (!CCMatch) {
1658 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001659 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1660 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001661 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1662
1663 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001664 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1665 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001666 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1667
1668 if (RVLocs1.size() != RVLocs2.size())
1669 return false;
1670 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1671 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1672 return false;
1673 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1674 return false;
1675 if (RVLocs1[i].isRegLoc()) {
1676 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1677 return false;
1678 } else {
1679 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1680 return false;
1681 }
1682 }
1683 }
1684
1685 // If the callee takes no arguments then go on to check the results of the
1686 // call.
1687 if (!Outs.empty()) {
1688 // Check if stack adjustment is needed. For now, do not do this if any
1689 // argument is passed on the stack.
1690 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001691 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1692 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001693 CCInfo.AnalyzeCallOperands(Outs,
1694 CCAssignFnForNode(CalleeCC, false, isVarArg));
1695 if (CCInfo.getNextStackOffset()) {
1696 MachineFunction &MF = DAG.getMachineFunction();
1697
1698 // Check if the arguments are already laid out in the right way as
1699 // the caller's fixed stack objects.
1700 MachineFrameInfo *MFI = MF.getFrameInfo();
1701 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1702 const ARMInstrInfo *TII =
1703 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001704 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1705 i != e;
1706 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001707 CCValAssign &VA = ArgLocs[i];
1708 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001709 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001710 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001711 if (VA.getLocInfo() == CCValAssign::Indirect)
1712 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001713 if (VA.needsCustom()) {
1714 // f64 and vector types are split into multiple registers or
1715 // register/stack-slot combinations. The types will not match
1716 // the registers; give up on memory f64 refs until we figure
1717 // out what to do about this.
1718 if (!VA.isRegLoc())
1719 return false;
1720 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001721 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001722 if (RegVT == MVT::v2f64) {
1723 if (!ArgLocs[++i].isRegLoc())
1724 return false;
1725 if (!ArgLocs[++i].isRegLoc())
1726 return false;
1727 }
1728 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001729 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1730 MFI, MRI, TII))
1731 return false;
1732 }
1733 }
1734 }
1735 }
1736
1737 return true;
1738}
1739
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740SDValue
1741ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001742 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001743 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001744 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001745 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001746
Bob Wilsondee46d72009-04-17 20:35:10 +00001747 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001748 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001749
Bob Wilsondee46d72009-04-17 20:35:10 +00001750 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001751 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1752 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001753
Dan Gohman98ca4f22009-08-05 01:29:28 +00001754 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001755 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1756 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757
1758 // If this is the first return lowered for this function, add
1759 // the regs to the liveout set for the function.
1760 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1761 for (unsigned i = 0; i != RVLocs.size(); ++i)
1762 if (RVLocs[i].isRegLoc())
1763 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001764 }
1765
Bob Wilson1f595bb2009-04-17 19:07:39 +00001766 SDValue Flag;
1767
1768 // Copy the result values into the output registers.
1769 for (unsigned i = 0, realRVLocIdx = 0;
1770 i != RVLocs.size();
1771 ++i, ++realRVLocIdx) {
1772 CCValAssign &VA = RVLocs[i];
1773 assert(VA.isRegLoc() && "Can only return in registers!");
1774
Dan Gohmanc9403652010-07-07 15:54:55 +00001775 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001776
1777 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001778 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001779 case CCValAssign::Full: break;
1780 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001781 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001782 break;
1783 }
1784
Bob Wilson1f595bb2009-04-17 19:07:39 +00001785 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001786 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001787 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001788 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1789 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001790 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001792
1793 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1794 Flag = Chain.getValue(1);
1795 VA = RVLocs[++i]; // skip ahead to next loc
1796 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1797 HalfGPRs.getValue(1), Flag);
1798 Flag = Chain.getValue(1);
1799 VA = RVLocs[++i]; // skip ahead to next loc
1800
1801 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001802 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1803 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001804 }
1805 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1806 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001807 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001808 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001809 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001810 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001811 VA = RVLocs[++i]; // skip ahead to next loc
1812 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1813 Flag);
1814 } else
1815 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1816
Bob Wilsondee46d72009-04-17 20:35:10 +00001817 // Guarantee that all emitted copies are
1818 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001819 Flag = Chain.getValue(1);
1820 }
1821
1822 SDValue result;
1823 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001825 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001826 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001827
1828 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001829}
1830
Evan Cheng3d2125c2010-11-30 23:55:39 +00001831bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1832 if (N->getNumValues() != 1)
1833 return false;
1834 if (!N->hasNUsesOfValue(1, 0))
1835 return false;
1836
1837 unsigned NumCopies = 0;
1838 SDNode* Copies[2];
1839 SDNode *Use = *N->use_begin();
1840 if (Use->getOpcode() == ISD::CopyToReg) {
1841 Copies[NumCopies++] = Use;
1842 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1843 // f64 returned in a pair of GPRs.
1844 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1845 UI != UE; ++UI) {
1846 if (UI->getOpcode() != ISD::CopyToReg)
1847 return false;
1848 Copies[UI.getUse().getResNo()] = *UI;
1849 ++NumCopies;
1850 }
1851 } else if (Use->getOpcode() == ISD::BITCAST) {
1852 // f32 returned in a single GPR.
1853 if (!Use->hasNUsesOfValue(1, 0))
1854 return false;
1855 Use = *Use->use_begin();
1856 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1857 return false;
1858 Copies[NumCopies++] = Use;
1859 } else {
1860 return false;
1861 }
1862
1863 if (NumCopies != 1 && NumCopies != 2)
1864 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001865
1866 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001867 for (unsigned i = 0; i < NumCopies; ++i) {
1868 SDNode *Copy = Copies[i];
1869 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1870 UI != UE; ++UI) {
1871 if (UI->getOpcode() == ISD::CopyToReg) {
1872 SDNode *Use = *UI;
1873 if (Use == Copies[0] || Use == Copies[1])
1874 continue;
1875 return false;
1876 }
1877 if (UI->getOpcode() != ARMISD::RET_FLAG)
1878 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001879 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001880 }
1881 }
1882
Evan Cheng1bf891a2010-12-01 22:59:46 +00001883 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001884}
1885
Evan Cheng485fafc2011-03-21 01:19:09 +00001886bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1887 if (!EnableARMTailCalls)
1888 return false;
1889
1890 if (!CI->isTailCall())
1891 return false;
1892
1893 return !Subtarget->isThumb1Only();
1894}
1895
Bob Wilsonb62d2572009-11-03 00:02:05 +00001896// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1897// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1898// one of the above mentioned nodes. It has to be wrapped because otherwise
1899// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1900// be used to form addressing mode. These wrapped nodes will be selected
1901// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001902static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001903 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001904 // FIXME there is no actual debug info here
1905 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001906 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001907 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001908 if (CP->isMachineConstantPoolEntry())
1909 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1910 CP->getAlignment());
1911 else
1912 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1913 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001914 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001915}
1916
Jim Grosbache1102ca2010-07-19 17:20:38 +00001917unsigned ARMTargetLowering::getJumpTableEncoding() const {
1918 return MachineJumpTableInfo::EK_Inline;
1919}
1920
Dan Gohmand858e902010-04-17 15:26:15 +00001921SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1922 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001923 MachineFunction &MF = DAG.getMachineFunction();
1924 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1925 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001926 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001927 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001928 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001929 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1930 SDValue CPAddr;
1931 if (RelocM == Reloc::Static) {
1932 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1933 } else {
1934 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001935 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001936 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1937 ARMCP::CPBlockAddress,
1938 PCAdj);
1939 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1940 }
1941 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1942 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001943 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001944 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001945 if (RelocM == Reloc::Static)
1946 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001947 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001948 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001949}
1950
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001951// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001952SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001953ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001954 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001955 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001956 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001957 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001958 MachineFunction &MF = DAG.getMachineFunction();
1959 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001960 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001961 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001962 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001963 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001964 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001965 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001966 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001967 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001968 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001969 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001970
Evan Chenge7e0d622009-11-06 22:24:13 +00001971 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001972 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001973
1974 // call __tls_get_addr.
1975 ArgListTy Args;
1976 ArgListEntry Entry;
1977 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001978 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001979 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001980 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001981 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001982 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1983 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001984 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001985 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001986 return CallResult.first;
1987}
1988
1989// Lower ISD::GlobalTLSAddress using the "initial exec" or
1990// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001991SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001992ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001993 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001994 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001995 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001996 SDValue Offset;
1997 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001998 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001999 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002000 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002001
Chris Lattner4fb63d02009-07-15 04:12:33 +00002002 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002003 MachineFunction &MF = DAG.getMachineFunction();
2004 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002005 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002006 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002007 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2008 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002009 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002010 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002011 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002012 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002013 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002014 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002015 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002016 Chain = Offset.getValue(1);
2017
Evan Chenge7e0d622009-11-06 22:24:13 +00002018 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002019 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002020
Evan Cheng9eda6892009-10-31 03:39:36 +00002021 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002022 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002023 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002024 } else {
2025 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002026 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002027 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002028 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002029 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002030 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002031 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002032 }
2033
2034 // The address of the thread local variable is the add of the thread
2035 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002036 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002037}
2038
Dan Gohman475871a2008-07-27 21:46:04 +00002039SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002040ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002041 // TODO: implement the "local dynamic" model
2042 assert(Subtarget->isTargetELF() &&
2043 "TLS not implemented for non-ELF targets");
2044 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2045 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2046 // otherwise use the "Local Exec" TLS Model
2047 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2048 return LowerToTLSGeneralDynamicModel(GA, DAG);
2049 else
2050 return LowerToTLSExecModels(GA, DAG);
2051}
2052
Dan Gohman475871a2008-07-27 21:46:04 +00002053SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002054 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002055 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002056 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002057 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002058 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2059 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002060 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002061 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002062 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002063 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002064 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002065 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002066 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002067 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002068 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002069 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002070 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002071 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002072 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002073 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002074 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002075 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002076 }
2077
2078 // If we have T2 ops, we can materialize the address directly via movt/movw
2079 // pair. This is always cheaper.
2080 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002081 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002082 // FIXME: Once remat is capable of dealing with instructions with register
2083 // operands, expand this into two nodes.
2084 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2085 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002086 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002087 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2088 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2089 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2090 MachinePointerInfo::getConstantPool(),
2091 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002092 }
2093}
2094
Dan Gohman475871a2008-07-27 21:46:04 +00002095SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002096 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002097 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002098 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002099 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002100 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002101 MachineFunction &MF = DAG.getMachineFunction();
2102 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2103
Evan Cheng4abce0c2011-05-27 20:11:27 +00002104 // FIXME: Enable this for static codegen when tool issues are fixed.
2105 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002106 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002107 // FIXME: Once remat is capable of dealing with instructions with register
2108 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002109 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002110 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2111 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2112
Evan Cheng53519f02011-01-21 18:55:51 +00002113 unsigned Wrapper = (RelocM == Reloc::PIC_)
2114 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2115 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002116 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002117 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2118 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2119 MachinePointerInfo::getGOT(), false, false, 0);
2120 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002121 }
2122
2123 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002124 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002125 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002126 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002127 } else {
2128 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002129 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2130 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002131 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002132 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002133 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002134 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002135
Evan Cheng9eda6892009-10-31 03:39:36 +00002136 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002137 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002138 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002139 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002140
2141 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002142 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002143 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002144 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002145
Evan Cheng63476a82009-09-03 07:04:02 +00002146 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002147 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002148 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002149
2150 return Result;
2151}
2152
Dan Gohman475871a2008-07-27 21:46:04 +00002153SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002154 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002155 assert(Subtarget->isTargetELF() &&
2156 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002157 MachineFunction &MF = DAG.getMachineFunction();
2158 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002159 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002160 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002161 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002162 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002163 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2164 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002165 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002166 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002167 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002168 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002169 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002170 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002171 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002172 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002173}
2174
Jim Grosbach0e0da732009-05-12 23:59:14 +00002175SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002176ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2177 const {
2178 DebugLoc dl = Op.getDebugLoc();
2179 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002180 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002181}
2182
2183SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002184ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2185 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002186 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002187 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2188 Op.getOperand(1), Val);
2189}
2190
2191SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002192ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2193 DebugLoc dl = Op.getDebugLoc();
2194 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2195 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2196}
2197
2198SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002199ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002200 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002201 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002202 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002203 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002204 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002205 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002206 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002207 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2208 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002209 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002210 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002211 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002212 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002213 EVT PtrVT = getPointerTy();
2214 DebugLoc dl = Op.getDebugLoc();
2215 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2216 SDValue CPAddr;
2217 unsigned PCAdj = (RelocM != Reloc::PIC_)
2218 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002219 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002220 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2221 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002222 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002223 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002224 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002225 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002226 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002227 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002228
2229 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002230 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002231 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2232 }
2233 return Result;
2234 }
Evan Cheng92e39162011-03-29 23:06:19 +00002235 case Intrinsic::arm_neon_vmulls:
2236 case Intrinsic::arm_neon_vmullu: {
2237 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2238 ? ARMISD::VMULLs : ARMISD::VMULLu;
2239 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2240 Op.getOperand(1), Op.getOperand(2));
2241 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002242 }
2243}
2244
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002245static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002246 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002247 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002248 if (!Subtarget->hasDataBarrier()) {
2249 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2250 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2251 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002252 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002253 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002254 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002255 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002256 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002257
2258 SDValue Op5 = Op.getOperand(5);
2259 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2260 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2261 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2262 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2263
2264 ARM_MB::MemBOpt DMBOpt;
2265 if (isDeviceBarrier)
2266 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2267 else
2268 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2269 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2270 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002271}
2272
Evan Chengdfed19f2010-11-03 06:34:55 +00002273static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2274 const ARMSubtarget *Subtarget) {
2275 // ARM pre v5TE and Thumb1 does not have preload instructions.
2276 if (!(Subtarget->isThumb2() ||
2277 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2278 // Just preserve the chain.
2279 return Op.getOperand(0);
2280
2281 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002282 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2283 if (!isRead &&
2284 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2285 // ARMv7 with MP extension has PLDW.
2286 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002287
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002288 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2289 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002290 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002291 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002292 isData = ~isData & 1;
2293 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002294
2295 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002296 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2297 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002298}
2299
Dan Gohman1e93df62010-04-17 14:41:14 +00002300static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2301 MachineFunction &MF = DAG.getMachineFunction();
2302 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2303
Evan Chenga8e29892007-01-19 07:51:42 +00002304 // vastart just stores the address of the VarArgsFrameIndex slot into the
2305 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002306 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002307 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002308 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002309 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002310 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2311 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002312}
2313
Dan Gohman475871a2008-07-27 21:46:04 +00002314SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002315ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2316 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002317 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002318 MachineFunction &MF = DAG.getMachineFunction();
2319 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2320
2321 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002322 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002323 RC = ARM::tGPRRegisterClass;
2324 else
2325 RC = ARM::GPRRegisterClass;
2326
2327 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002328 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002329 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002330
2331 SDValue ArgValue2;
2332 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002333 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002334 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002335
2336 // Create load node to retrieve arguments from the stack.
2337 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002338 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002339 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002340 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002341 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002342 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002343 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002344 }
2345
Jim Grosbache5165492009-11-09 00:11:35 +00002346 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002347}
2348
Stuart Hastingsc7315872011-04-20 16:47:52 +00002349void
2350ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2351 unsigned &VARegSize, unsigned &VARegSaveSize)
2352 const {
2353 unsigned NumGPRs;
2354 if (CCInfo.isFirstByValRegValid())
2355 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2356 else {
2357 unsigned int firstUnalloced;
2358 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2359 sizeof(GPRArgRegs) /
2360 sizeof(GPRArgRegs[0]));
2361 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2362 }
2363
2364 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2365 VARegSize = NumGPRs * 4;
2366 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2367}
2368
2369// The remaining GPRs hold either the beginning of variable-argument
2370// data, or the beginning of an aggregate passed by value (usuall
2371// byval). Either way, we allocate stack slots adjacent to the data
2372// provided by our caller, and store the unallocated registers there.
2373// If this is a variadic function, the va_list pointer will begin with
2374// these values; otherwise, this reassembles a (byval) structure that
2375// was split between registers and memory.
2376void
2377ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2378 DebugLoc dl, SDValue &Chain,
2379 unsigned ArgOffset) const {
2380 MachineFunction &MF = DAG.getMachineFunction();
2381 MachineFrameInfo *MFI = MF.getFrameInfo();
2382 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2383 unsigned firstRegToSaveIndex;
2384 if (CCInfo.isFirstByValRegValid())
2385 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2386 else {
2387 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2388 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2389 }
2390
2391 unsigned VARegSize, VARegSaveSize;
2392 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2393 if (VARegSaveSize) {
2394 // If this function is vararg, store any remaining integer argument regs
2395 // to their spots on the stack so that they may be loaded by deferencing
2396 // the result of va_next.
2397 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002398 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2399 ArgOffset + VARegSaveSize
2400 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002401 false));
2402 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2403 getPointerTy());
2404
2405 SmallVector<SDValue, 4> MemOps;
2406 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2407 TargetRegisterClass *RC;
2408 if (AFI->isThumb1OnlyFunction())
2409 RC = ARM::tGPRRegisterClass;
2410 else
2411 RC = ARM::GPRRegisterClass;
2412
2413 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2414 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2415 SDValue Store =
2416 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002417 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002418 false, false, 0);
2419 MemOps.push_back(Store);
2420 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2421 DAG.getConstant(4, getPointerTy()));
2422 }
2423 if (!MemOps.empty())
2424 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2425 &MemOps[0], MemOps.size());
2426 } else
2427 // This will point to the next argument passed via stack.
2428 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2429}
2430
Bob Wilson5bafff32009-06-22 23:27:02 +00002431SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002432ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002433 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002434 const SmallVectorImpl<ISD::InputArg>
2435 &Ins,
2436 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002437 SmallVectorImpl<SDValue> &InVals)
2438 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002439 MachineFunction &MF = DAG.getMachineFunction();
2440 MachineFrameInfo *MFI = MF.getFrameInfo();
2441
Bob Wilson1f595bb2009-04-17 19:07:39 +00002442 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2443
2444 // Assign locations to all of the incoming arguments.
2445 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002446 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2447 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002448 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002449 CCAssignFnForNode(CallConv, /* Return*/ false,
2450 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002451
2452 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002453 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002454
Stuart Hastingsf222e592011-02-28 17:17:53 +00002455 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002456 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2457 CCValAssign &VA = ArgLocs[i];
2458
Bob Wilsondee46d72009-04-17 20:35:10 +00002459 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002460 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002461 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002462
Bob Wilson1f595bb2009-04-17 19:07:39 +00002463 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002464 // f64 and vector types are split up into multiple registers or
2465 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002466 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002467 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002468 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002469 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002470 SDValue ArgValue2;
2471 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002472 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002473 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2474 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002475 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002476 false, false, 0);
2477 } else {
2478 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2479 Chain, DAG, dl);
2480 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002481 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2482 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002483 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002484 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002485 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2486 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002487 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002488
Bob Wilson5bafff32009-06-22 23:27:02 +00002489 } else {
2490 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002491
Owen Anderson825b72b2009-08-11 20:47:22 +00002492 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002493 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002494 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002495 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002496 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002497 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002498 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002499 RC = (AFI->isThumb1OnlyFunction() ?
2500 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002501 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002502 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002503
2504 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002505 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002506 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002507 }
2508
2509 // If this is an 8 or 16-bit value, it is really passed promoted
2510 // to 32 bits. Insert an assert[sz]ext to capture this, then
2511 // truncate to the right size.
2512 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002513 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002514 case CCValAssign::Full: break;
2515 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002516 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002517 break;
2518 case CCValAssign::SExt:
2519 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2520 DAG.getValueType(VA.getValVT()));
2521 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2522 break;
2523 case CCValAssign::ZExt:
2524 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2525 DAG.getValueType(VA.getValVT()));
2526 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2527 break;
2528 }
2529
Dan Gohman98ca4f22009-08-05 01:29:28 +00002530 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002531
2532 } else { // VA.isRegLoc()
2533
2534 // sanity check
2535 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002536 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002537
Stuart Hastingsf222e592011-02-28 17:17:53 +00002538 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002539
Stuart Hastingsf222e592011-02-28 17:17:53 +00002540 // Some Ins[] entries become multiple ArgLoc[] entries.
2541 // Process them only once.
2542 if (index != lastInsIndex)
2543 {
2544 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002545 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002546 // This can be changed with more analysis.
2547 // In case of tail call optimization mark all arguments mutable.
2548 // Since they could be overwritten by lowering of arguments in case of
2549 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002550 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002551 unsigned VARegSize, VARegSaveSize;
2552 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2553 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2554 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002555 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002556 int FI = MFI->CreateFixedObject(Bytes,
2557 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002558 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2559 } else {
2560 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2561 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002562
Stuart Hastingsf222e592011-02-28 17:17:53 +00002563 // Create load nodes to retrieve arguments from the stack.
2564 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2565 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2566 MachinePointerInfo::getFixedStack(FI),
2567 false, false, 0));
2568 }
2569 lastInsIndex = index;
2570 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002571 }
2572 }
2573
2574 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002575 if (isVarArg)
2576 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002577
Dan Gohman98ca4f22009-08-05 01:29:28 +00002578 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002579}
2580
2581/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002582static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002583 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002584 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002585 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002586 // Maybe this has already been legalized into the constant pool?
2587 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002588 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002589 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002590 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002591 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002592 }
2593 }
2594 return false;
2595}
2596
Evan Chenga8e29892007-01-19 07:51:42 +00002597/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2598/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002599SDValue
2600ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002601 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002602 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002603 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002604 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002605 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002606 // Constant does not fit, try adjusting it by one?
2607 switch (CC) {
2608 default: break;
2609 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002610 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002611 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002612 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002613 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002614 }
2615 break;
2616 case ISD::SETULT:
2617 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002618 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002619 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002620 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002621 }
2622 break;
2623 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002624 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002625 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002626 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002627 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002628 }
2629 break;
2630 case ISD::SETULE:
2631 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002632 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002633 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002634 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002635 }
2636 break;
2637 }
2638 }
2639 }
2640
2641 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002642 ARMISD::NodeType CompareType;
2643 switch (CondCode) {
2644 default:
2645 CompareType = ARMISD::CMP;
2646 break;
2647 case ARMCC::EQ:
2648 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002649 // Uses only Z Flag
2650 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002651 break;
2652 }
Evan Cheng218977b2010-07-13 19:27:42 +00002653 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002654 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002655}
2656
2657/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002658SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002659ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002660 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002661 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002662 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002663 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002664 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002665 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2666 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002667}
2668
Bob Wilson79f56c92011-03-08 01:17:20 +00002669/// duplicateCmp - Glue values can have only one use, so this function
2670/// duplicates a comparison node.
2671SDValue
2672ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2673 unsigned Opc = Cmp.getOpcode();
2674 DebugLoc DL = Cmp.getDebugLoc();
2675 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2676 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2677
2678 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2679 Cmp = Cmp.getOperand(0);
2680 Opc = Cmp.getOpcode();
2681 if (Opc == ARMISD::CMPFP)
2682 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2683 else {
2684 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2685 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2686 }
2687 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2688}
2689
Bill Wendlingde2b1512010-08-11 08:43:16 +00002690SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2691 SDValue Cond = Op.getOperand(0);
2692 SDValue SelectTrue = Op.getOperand(1);
2693 SDValue SelectFalse = Op.getOperand(2);
2694 DebugLoc dl = Op.getDebugLoc();
2695
2696 // Convert:
2697 //
2698 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2699 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2700 //
2701 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2702 const ConstantSDNode *CMOVTrue =
2703 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2704 const ConstantSDNode *CMOVFalse =
2705 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2706
2707 if (CMOVTrue && CMOVFalse) {
2708 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2709 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2710
2711 SDValue True;
2712 SDValue False;
2713 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2714 True = SelectTrue;
2715 False = SelectFalse;
2716 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2717 True = SelectFalse;
2718 False = SelectTrue;
2719 }
2720
2721 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002722 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002723 SDValue ARMcc = Cond.getOperand(2);
2724 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002725 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002726 assert(True.getValueType() == VT);
2727 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002728 }
2729 }
2730 }
2731
2732 return DAG.getSelectCC(dl, Cond,
2733 DAG.getConstant(0, Cond.getValueType()),
2734 SelectTrue, SelectFalse, ISD::SETNE);
2735}
2736
Dan Gohmand858e902010-04-17 15:26:15 +00002737SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002738 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002739 SDValue LHS = Op.getOperand(0);
2740 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002741 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002742 SDValue TrueVal = Op.getOperand(2);
2743 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002744 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002745
Owen Anderson825b72b2009-08-11 20:47:22 +00002746 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002747 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002748 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002749 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2750 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002751 }
2752
2753 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002754 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002755
Evan Cheng218977b2010-07-13 19:27:42 +00002756 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2757 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002758 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002759 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002760 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002761 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002762 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002763 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002764 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002765 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002766 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002767 }
2768 return Result;
2769}
2770
Evan Cheng218977b2010-07-13 19:27:42 +00002771/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2772/// to morph to an integer compare sequence.
2773static bool canChangeToInt(SDValue Op, bool &SeenZero,
2774 const ARMSubtarget *Subtarget) {
2775 SDNode *N = Op.getNode();
2776 if (!N->hasOneUse())
2777 // Otherwise it requires moving the value from fp to integer registers.
2778 return false;
2779 if (!N->getNumValues())
2780 return false;
2781 EVT VT = Op.getValueType();
2782 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2783 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2784 // vmrs are very slow, e.g. cortex-a8.
2785 return false;
2786
2787 if (isFloatingPointZero(Op)) {
2788 SeenZero = true;
2789 return true;
2790 }
2791 return ISD::isNormalLoad(N);
2792}
2793
2794static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2795 if (isFloatingPointZero(Op))
2796 return DAG.getConstant(0, MVT::i32);
2797
2798 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2799 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002800 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002801 Ld->isVolatile(), Ld->isNonTemporal(),
2802 Ld->getAlignment());
2803
2804 llvm_unreachable("Unknown VFP cmp argument!");
2805}
2806
2807static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2808 SDValue &RetVal1, SDValue &RetVal2) {
2809 if (isFloatingPointZero(Op)) {
2810 RetVal1 = DAG.getConstant(0, MVT::i32);
2811 RetVal2 = DAG.getConstant(0, MVT::i32);
2812 return;
2813 }
2814
2815 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2816 SDValue Ptr = Ld->getBasePtr();
2817 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2818 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002819 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002820 Ld->isVolatile(), Ld->isNonTemporal(),
2821 Ld->getAlignment());
2822
2823 EVT PtrType = Ptr.getValueType();
2824 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2825 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2826 PtrType, Ptr, DAG.getConstant(4, PtrType));
2827 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2828 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002829 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002830 Ld->isVolatile(), Ld->isNonTemporal(),
2831 NewAlign);
2832 return;
2833 }
2834
2835 llvm_unreachable("Unknown VFP cmp argument!");
2836}
2837
2838/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2839/// f32 and even f64 comparisons to integer ones.
2840SDValue
2841ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2842 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002843 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002844 SDValue LHS = Op.getOperand(2);
2845 SDValue RHS = Op.getOperand(3);
2846 SDValue Dest = Op.getOperand(4);
2847 DebugLoc dl = Op.getDebugLoc();
2848
2849 bool SeenZero = false;
2850 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2851 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002852 // If one of the operand is zero, it's safe to ignore the NaN case since
2853 // we only care about equality comparisons.
2854 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002855 // If unsafe fp math optimization is enabled and there are no other uses of
2856 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002857 // to an integer comparison.
2858 if (CC == ISD::SETOEQ)
2859 CC = ISD::SETEQ;
2860 else if (CC == ISD::SETUNE)
2861 CC = ISD::SETNE;
2862
2863 SDValue ARMcc;
2864 if (LHS.getValueType() == MVT::f32) {
2865 LHS = bitcastf32Toi32(LHS, DAG);
2866 RHS = bitcastf32Toi32(RHS, DAG);
2867 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2868 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2869 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2870 Chain, Dest, ARMcc, CCR, Cmp);
2871 }
2872
2873 SDValue LHS1, LHS2;
2874 SDValue RHS1, RHS2;
2875 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2876 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2877 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2878 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002879 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002880 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2881 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2882 }
2883
2884 return SDValue();
2885}
2886
2887SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2888 SDValue Chain = Op.getOperand(0);
2889 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2890 SDValue LHS = Op.getOperand(2);
2891 SDValue RHS = Op.getOperand(3);
2892 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002893 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002894
Owen Anderson825b72b2009-08-11 20:47:22 +00002895 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002896 SDValue ARMcc;
2897 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002899 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002900 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002901 }
2902
Owen Anderson825b72b2009-08-11 20:47:22 +00002903 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002904
2905 if (UnsafeFPMath &&
2906 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2907 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2908 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2909 if (Result.getNode())
2910 return Result;
2911 }
2912
Evan Chenga8e29892007-01-19 07:51:42 +00002913 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002914 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002915
Evan Cheng218977b2010-07-13 19:27:42 +00002916 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2917 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002918 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002919 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002920 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002921 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002922 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002923 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2924 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002925 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002926 }
2927 return Res;
2928}
2929
Dan Gohmand858e902010-04-17 15:26:15 +00002930SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002931 SDValue Chain = Op.getOperand(0);
2932 SDValue Table = Op.getOperand(1);
2933 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002934 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002935
Owen Andersone50ed302009-08-10 22:56:29 +00002936 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002937 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2938 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002939 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002940 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002941 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002942 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2943 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002944 if (Subtarget->isThumb2()) {
2945 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2946 // which does another jump to the destination. This also makes it easier
2947 // to translate it to TBB / TBH later.
2948 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002950 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002951 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002952 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002953 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002954 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002955 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002956 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002957 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002959 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002960 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002961 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002962 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002963 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002964 }
Evan Chenga8e29892007-01-19 07:51:42 +00002965}
2966
Bob Wilson76a312b2010-03-19 22:51:32 +00002967static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2968 DebugLoc dl = Op.getDebugLoc();
2969 unsigned Opc;
2970
2971 switch (Op.getOpcode()) {
2972 default:
2973 assert(0 && "Invalid opcode!");
2974 case ISD::FP_TO_SINT:
2975 Opc = ARMISD::FTOSI;
2976 break;
2977 case ISD::FP_TO_UINT:
2978 Opc = ARMISD::FTOUI;
2979 break;
2980 }
2981 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002982 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002983}
2984
Cameron Zwarich3007d332011-03-29 21:41:55 +00002985static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2986 EVT VT = Op.getValueType();
2987 DebugLoc dl = Op.getDebugLoc();
2988
2989 EVT OperandVT = Op.getOperand(0).getValueType();
2990 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2991 if (VT != MVT::v4f32)
2992 return DAG.UnrollVectorOp(Op.getNode());
2993
2994 unsigned CastOpc;
2995 unsigned Opc;
2996 switch (Op.getOpcode()) {
2997 default:
2998 assert(0 && "Invalid opcode!");
2999 case ISD::SINT_TO_FP:
3000 CastOpc = ISD::SIGN_EXTEND;
3001 Opc = ISD::SINT_TO_FP;
3002 break;
3003 case ISD::UINT_TO_FP:
3004 CastOpc = ISD::ZERO_EXTEND;
3005 Opc = ISD::UINT_TO_FP;
3006 break;
3007 }
3008
3009 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3010 return DAG.getNode(Opc, dl, VT, Op);
3011}
3012
Bob Wilson76a312b2010-03-19 22:51:32 +00003013static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3014 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003015 if (VT.isVector())
3016 return LowerVectorINT_TO_FP(Op, DAG);
3017
Bob Wilson76a312b2010-03-19 22:51:32 +00003018 DebugLoc dl = Op.getDebugLoc();
3019 unsigned Opc;
3020
3021 switch (Op.getOpcode()) {
3022 default:
3023 assert(0 && "Invalid opcode!");
3024 case ISD::SINT_TO_FP:
3025 Opc = ARMISD::SITOF;
3026 break;
3027 case ISD::UINT_TO_FP:
3028 Opc = ARMISD::UITOF;
3029 break;
3030 }
3031
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003032 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003033 return DAG.getNode(Opc, dl, VT, Op);
3034}
3035
Evan Cheng515fe3a2010-07-08 02:08:50 +00003036SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003037 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003038 SDValue Tmp0 = Op.getOperand(0);
3039 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003040 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003041 EVT VT = Op.getValueType();
3042 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003043 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3044 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3045 bool UseNEON = !InGPR && Subtarget->hasNEON();
3046
3047 if (UseNEON) {
3048 // Use VBSL to copy the sign bit.
3049 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3050 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3051 DAG.getTargetConstant(EncodedVal, MVT::i32));
3052 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3053 if (VT == MVT::f64)
3054 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3055 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3056 DAG.getConstant(32, MVT::i32));
3057 else /*if (VT == MVT::f32)*/
3058 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3059 if (SrcVT == MVT::f32) {
3060 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3061 if (VT == MVT::f64)
3062 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3063 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3064 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003065 } else if (VT == MVT::f32)
3066 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3067 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3068 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003069 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3070 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3071
3072 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3073 MVT::i32);
3074 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3075 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3076 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003077
Evan Chenge573fb32011-02-23 02:24:55 +00003078 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3079 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3080 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003081 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003082 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3083 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3084 DAG.getConstant(0, MVT::i32));
3085 } else {
3086 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3087 }
3088
3089 return Res;
3090 }
Evan Chengc143dd42011-02-11 02:28:55 +00003091
3092 // Bitcast operand 1 to i32.
3093 if (SrcVT == MVT::f64)
3094 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3095 &Tmp1, 1).getValue(1);
3096 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3097
Evan Chenge573fb32011-02-23 02:24:55 +00003098 // Or in the signbit with integer operations.
3099 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3100 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3101 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3102 if (VT == MVT::f32) {
3103 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3104 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3105 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3106 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003107 }
3108
Evan Chenge573fb32011-02-23 02:24:55 +00003109 // f64: Or the high part with signbit and then combine two parts.
3110 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3111 &Tmp0, 1);
3112 SDValue Lo = Tmp0.getValue(0);
3113 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3114 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3115 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003116}
3117
Evan Cheng2457f2c2010-05-22 01:47:14 +00003118SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3119 MachineFunction &MF = DAG.getMachineFunction();
3120 MachineFrameInfo *MFI = MF.getFrameInfo();
3121 MFI->setReturnAddressIsTaken(true);
3122
3123 EVT VT = Op.getValueType();
3124 DebugLoc dl = Op.getDebugLoc();
3125 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3126 if (Depth) {
3127 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3128 SDValue Offset = DAG.getConstant(4, MVT::i32);
3129 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3130 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003131 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003132 }
3133
3134 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003135 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003136 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3137}
3138
Dan Gohmand858e902010-04-17 15:26:15 +00003139SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003140 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3141 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003142
Owen Andersone50ed302009-08-10 22:56:29 +00003143 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003144 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3145 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003146 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003147 ? ARM::R7 : ARM::R11;
3148 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3149 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003150 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3151 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003152 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003153 return FrameAddr;
3154}
3155
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003156/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003157/// expand a bit convert where either the source or destination type is i64 to
3158/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3159/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3160/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003161static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003162 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3163 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003164 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003165
Bob Wilson9f3f0612010-04-17 05:30:19 +00003166 // This function is only supposed to be called for i64 types, either as the
3167 // source or destination of the bit convert.
3168 EVT SrcVT = Op.getValueType();
3169 EVT DstVT = N->getValueType(0);
3170 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003171 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003172
Bob Wilson9f3f0612010-04-17 05:30:19 +00003173 // Turn i64->f64 into VMOVDRR.
3174 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003175 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3176 DAG.getConstant(0, MVT::i32));
3177 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3178 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003179 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003180 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003181 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003182
Jim Grosbache5165492009-11-09 00:11:35 +00003183 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003184 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3185 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3186 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3187 // Merge the pieces into a single i64 value.
3188 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3189 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003190
Bob Wilson9f3f0612010-04-17 05:30:19 +00003191 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003192}
3193
Bob Wilson5bafff32009-06-22 23:27:02 +00003194/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003195/// Zero vectors are used to represent vector negation and in those cases
3196/// will be implemented with the NEON VNEG instruction. However, VNEG does
3197/// not support i64 elements, so sometimes the zero vectors will need to be
3198/// explicitly constructed. Regardless, use a canonical VMOV to create the
3199/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003200static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003201 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003202 // The canonical modified immediate encoding of a zero vector is....0!
3203 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3204 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3205 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003206 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003207}
3208
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003209/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3210/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003211SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3212 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003213 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3214 EVT VT = Op.getValueType();
3215 unsigned VTBits = VT.getSizeInBits();
3216 DebugLoc dl = Op.getDebugLoc();
3217 SDValue ShOpLo = Op.getOperand(0);
3218 SDValue ShOpHi = Op.getOperand(1);
3219 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003220 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003221 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003222
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003223 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3224
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003225 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3226 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3227 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3228 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3229 DAG.getConstant(VTBits, MVT::i32));
3230 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3231 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003232 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003233
3234 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3235 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003236 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003237 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003238 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003239 CCR, Cmp);
3240
3241 SDValue Ops[2] = { Lo, Hi };
3242 return DAG.getMergeValues(Ops, 2, dl);
3243}
3244
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003245/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3246/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003247SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3248 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003249 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3250 EVT VT = Op.getValueType();
3251 unsigned VTBits = VT.getSizeInBits();
3252 DebugLoc dl = Op.getDebugLoc();
3253 SDValue ShOpLo = Op.getOperand(0);
3254 SDValue ShOpHi = Op.getOperand(1);
3255 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003256 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003257
3258 assert(Op.getOpcode() == ISD::SHL_PARTS);
3259 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3260 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3261 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3262 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3263 DAG.getConstant(VTBits, MVT::i32));
3264 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3265 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3266
3267 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3268 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3269 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003270 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003271 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003272 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003273 CCR, Cmp);
3274
3275 SDValue Ops[2] = { Lo, Hi };
3276 return DAG.getMergeValues(Ops, 2, dl);
3277}
3278
Jim Grosbach4725ca72010-09-08 03:54:02 +00003279SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003280 SelectionDAG &DAG) const {
3281 // The rounding mode is in bits 23:22 of the FPSCR.
3282 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3283 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3284 // so that the shift + and get folded into a bitfield extract.
3285 DebugLoc dl = Op.getDebugLoc();
3286 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3287 DAG.getConstant(Intrinsic::arm_get_fpscr,
3288 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003289 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003290 DAG.getConstant(1U << 22, MVT::i32));
3291 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3292 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003293 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003294 DAG.getConstant(3, MVT::i32));
3295}
3296
Jim Grosbach3482c802010-01-18 19:58:49 +00003297static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3298 const ARMSubtarget *ST) {
3299 EVT VT = N->getValueType(0);
3300 DebugLoc dl = N->getDebugLoc();
3301
3302 if (!ST->hasV6T2Ops())
3303 return SDValue();
3304
3305 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3306 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3307}
3308
Bob Wilson5bafff32009-06-22 23:27:02 +00003309static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3310 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003311 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003312 DebugLoc dl = N->getDebugLoc();
3313
Bob Wilsond5448bb2010-11-18 21:16:28 +00003314 if (!VT.isVector())
3315 return SDValue();
3316
Bob Wilson5bafff32009-06-22 23:27:02 +00003317 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003318 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003319
Bob Wilsond5448bb2010-11-18 21:16:28 +00003320 // Left shifts translate directly to the vshiftu intrinsic.
3321 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003322 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003323 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3324 N->getOperand(0), N->getOperand(1));
3325
3326 assert((N->getOpcode() == ISD::SRA ||
3327 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3328
3329 // NEON uses the same intrinsics for both left and right shifts. For
3330 // right shifts, the shift amounts are negative, so negate the vector of
3331 // shift amounts.
3332 EVT ShiftVT = N->getOperand(1).getValueType();
3333 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3334 getZeroVector(ShiftVT, DAG, dl),
3335 N->getOperand(1));
3336 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3337 Intrinsic::arm_neon_vshifts :
3338 Intrinsic::arm_neon_vshiftu);
3339 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3340 DAG.getConstant(vshiftInt, MVT::i32),
3341 N->getOperand(0), NegatedCount);
3342}
3343
3344static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3345 const ARMSubtarget *ST) {
3346 EVT VT = N->getValueType(0);
3347 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003348
Eli Friedmance392eb2009-08-22 03:13:10 +00003349 // We can get here for a node like i32 = ISD::SHL i32, i64
3350 if (VT != MVT::i64)
3351 return SDValue();
3352
3353 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003354 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003355
Chris Lattner27a6c732007-11-24 07:07:01 +00003356 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3357 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003358 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003359 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003360
Chris Lattner27a6c732007-11-24 07:07:01 +00003361 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003362 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003363
Chris Lattner27a6c732007-11-24 07:07:01 +00003364 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003366 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003368 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003369
Chris Lattner27a6c732007-11-24 07:07:01 +00003370 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3371 // captures the result into a carry flag.
3372 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003373 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003374
Chris Lattner27a6c732007-11-24 07:07:01 +00003375 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003376 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003377
Chris Lattner27a6c732007-11-24 07:07:01 +00003378 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003379 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003380}
3381
Bob Wilson5bafff32009-06-22 23:27:02 +00003382static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3383 SDValue TmpOp0, TmpOp1;
3384 bool Invert = false;
3385 bool Swap = false;
3386 unsigned Opc = 0;
3387
3388 SDValue Op0 = Op.getOperand(0);
3389 SDValue Op1 = Op.getOperand(1);
3390 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003391 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003392 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3393 DebugLoc dl = Op.getDebugLoc();
3394
3395 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3396 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003397 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003398 case ISD::SETUNE:
3399 case ISD::SETNE: Invert = true; // Fallthrough
3400 case ISD::SETOEQ:
3401 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3402 case ISD::SETOLT:
3403 case ISD::SETLT: Swap = true; // Fallthrough
3404 case ISD::SETOGT:
3405 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3406 case ISD::SETOLE:
3407 case ISD::SETLE: Swap = true; // Fallthrough
3408 case ISD::SETOGE:
3409 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3410 case ISD::SETUGE: Swap = true; // Fallthrough
3411 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3412 case ISD::SETUGT: Swap = true; // Fallthrough
3413 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3414 case ISD::SETUEQ: Invert = true; // Fallthrough
3415 case ISD::SETONE:
3416 // Expand this to (OLT | OGT).
3417 TmpOp0 = Op0;
3418 TmpOp1 = Op1;
3419 Opc = ISD::OR;
3420 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3421 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3422 break;
3423 case ISD::SETUO: Invert = true; // Fallthrough
3424 case ISD::SETO:
3425 // Expand this to (OLT | OGE).
3426 TmpOp0 = Op0;
3427 TmpOp1 = Op1;
3428 Opc = ISD::OR;
3429 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3430 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3431 break;
3432 }
3433 } else {
3434 // Integer comparisons.
3435 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003436 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003437 case ISD::SETNE: Invert = true;
3438 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3439 case ISD::SETLT: Swap = true;
3440 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3441 case ISD::SETLE: Swap = true;
3442 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3443 case ISD::SETULT: Swap = true;
3444 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3445 case ISD::SETULE: Swap = true;
3446 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3447 }
3448
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003449 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003450 if (Opc == ARMISD::VCEQ) {
3451
3452 SDValue AndOp;
3453 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3454 AndOp = Op0;
3455 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3456 AndOp = Op1;
3457
3458 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003459 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003460 AndOp = AndOp.getOperand(0);
3461
3462 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3463 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003464 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3465 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003466 Invert = !Invert;
3467 }
3468 }
3469 }
3470
3471 if (Swap)
3472 std::swap(Op0, Op1);
3473
Owen Andersonc24cb352010-11-08 23:21:22 +00003474 // If one of the operands is a constant vector zero, attempt to fold the
3475 // comparison to a specialized compare-against-zero form.
3476 SDValue SingleOp;
3477 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3478 SingleOp = Op0;
3479 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3480 if (Opc == ARMISD::VCGE)
3481 Opc = ARMISD::VCLEZ;
3482 else if (Opc == ARMISD::VCGT)
3483 Opc = ARMISD::VCLTZ;
3484 SingleOp = Op1;
3485 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003486
Owen Andersonc24cb352010-11-08 23:21:22 +00003487 SDValue Result;
3488 if (SingleOp.getNode()) {
3489 switch (Opc) {
3490 case ARMISD::VCEQ:
3491 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3492 case ARMISD::VCGE:
3493 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3494 case ARMISD::VCLEZ:
3495 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3496 case ARMISD::VCGT:
3497 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3498 case ARMISD::VCLTZ:
3499 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3500 default:
3501 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3502 }
3503 } else {
3504 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3505 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003506
3507 if (Invert)
3508 Result = DAG.getNOT(dl, Result, VT);
3509
3510 return Result;
3511}
3512
Bob Wilsond3c42842010-06-14 22:19:57 +00003513/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3514/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003515/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003516static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3517 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003518 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003519 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003520
Bob Wilson827b2102010-06-15 19:05:35 +00003521 // SplatBitSize is set to the smallest size that splats the vector, so a
3522 // zero vector will always have SplatBitSize == 8. However, NEON modified
3523 // immediate instructions others than VMOV do not support the 8-bit encoding
3524 // of a zero vector, and the default encoding of zero is supposed to be the
3525 // 32-bit version.
3526 if (SplatBits == 0)
3527 SplatBitSize = 32;
3528
Bob Wilson5bafff32009-06-22 23:27:02 +00003529 switch (SplatBitSize) {
3530 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003531 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003532 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003533 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003534 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003535 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003536 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003537 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003538 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003539
3540 case 16:
3541 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003542 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003543 if ((SplatBits & ~0xff) == 0) {
3544 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003545 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003546 Imm = SplatBits;
3547 break;
3548 }
3549 if ((SplatBits & ~0xff00) == 0) {
3550 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003551 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003552 Imm = SplatBits >> 8;
3553 break;
3554 }
3555 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003556
3557 case 32:
3558 // NEON's 32-bit VMOV supports splat values where:
3559 // * only one byte is nonzero, or
3560 // * the least significant byte is 0xff and the second byte is nonzero, or
3561 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003562 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003563 if ((SplatBits & ~0xff) == 0) {
3564 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003565 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003566 Imm = SplatBits;
3567 break;
3568 }
3569 if ((SplatBits & ~0xff00) == 0) {
3570 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003571 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003572 Imm = SplatBits >> 8;
3573 break;
3574 }
3575 if ((SplatBits & ~0xff0000) == 0) {
3576 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003577 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003578 Imm = SplatBits >> 16;
3579 break;
3580 }
3581 if ((SplatBits & ~0xff000000) == 0) {
3582 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003583 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003584 Imm = SplatBits >> 24;
3585 break;
3586 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003587
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003588 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3589 if (type == OtherModImm) return SDValue();
3590
Bob Wilson5bafff32009-06-22 23:27:02 +00003591 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003592 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3593 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003594 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003595 Imm = SplatBits >> 8;
3596 SplatBits |= 0xff;
3597 break;
3598 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003599
3600 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003601 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3602 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003603 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003604 Imm = SplatBits >> 16;
3605 SplatBits |= 0xffff;
3606 break;
3607 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003608
3609 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3610 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3611 // VMOV.I32. A (very) minor optimization would be to replicate the value
3612 // and fall through here to test for a valid 64-bit splat. But, then the
3613 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003614 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003615
3616 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003617 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003618 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003619 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003620 uint64_t BitMask = 0xff;
3621 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003622 unsigned ImmMask = 1;
3623 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003624 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003625 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003626 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003627 Imm |= ImmMask;
3628 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003629 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003630 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003631 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003632 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003633 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003634 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003635 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003636 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003637 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003638 break;
3639 }
3640
Bob Wilson1a913ed2010-06-11 21:34:50 +00003641 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003642 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003643 return SDValue();
3644 }
3645
Bob Wilsoncba270d2010-07-13 21:16:48 +00003646 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3647 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003648}
3649
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003650static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3651 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003652 unsigned NumElts = VT.getVectorNumElements();
3653 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003654
3655 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3656 if (M[0] < 0)
3657 return false;
3658
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003659 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003660
3661 // If this is a VEXT shuffle, the immediate value is the index of the first
3662 // element. The other shuffle indices must be the successive elements after
3663 // the first one.
3664 unsigned ExpectedElt = Imm;
3665 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003666 // Increment the expected index. If it wraps around, it may still be
3667 // a VEXT but the source vectors must be swapped.
3668 ExpectedElt += 1;
3669 if (ExpectedElt == NumElts * 2) {
3670 ExpectedElt = 0;
3671 ReverseVEXT = true;
3672 }
3673
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003674 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003675 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003676 return false;
3677 }
3678
3679 // Adjust the index value if the source operands will be swapped.
3680 if (ReverseVEXT)
3681 Imm -= NumElts;
3682
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003683 return true;
3684}
3685
Bob Wilson8bb9e482009-07-26 00:39:34 +00003686/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3687/// instruction with the specified blocksize. (The order of the elements
3688/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003689static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3690 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003691 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3692 "Only possible block sizes for VREV are: 16, 32, 64");
3693
Bob Wilson8bb9e482009-07-26 00:39:34 +00003694 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003695 if (EltSz == 64)
3696 return false;
3697
3698 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003699 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003700 // If the first shuffle index is UNDEF, be optimistic.
3701 if (M[0] < 0)
3702 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003703
3704 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3705 return false;
3706
3707 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003708 if (M[i] < 0) continue; // ignore UNDEF indices
3709 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003710 return false;
3711 }
3712
3713 return true;
3714}
3715
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003716static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3717 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3718 // range, then 0 is placed into the resulting vector. So pretty much any mask
3719 // of 8 elements can work here.
3720 return VT == MVT::v8i8 && M.size() == 8;
3721}
3722
Bob Wilsonc692cb72009-08-21 20:54:19 +00003723static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3724 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003725 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3726 if (EltSz == 64)
3727 return false;
3728
Bob Wilsonc692cb72009-08-21 20:54:19 +00003729 unsigned NumElts = VT.getVectorNumElements();
3730 WhichResult = (M[0] == 0 ? 0 : 1);
3731 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003732 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3733 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003734 return false;
3735 }
3736 return true;
3737}
3738
Bob Wilson324f4f12009-12-03 06:40:55 +00003739/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3740/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3741/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3742static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3743 unsigned &WhichResult) {
3744 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3745 if (EltSz == 64)
3746 return false;
3747
3748 unsigned NumElts = VT.getVectorNumElements();
3749 WhichResult = (M[0] == 0 ? 0 : 1);
3750 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003751 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3752 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003753 return false;
3754 }
3755 return true;
3756}
3757
Bob Wilsonc692cb72009-08-21 20:54:19 +00003758static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3759 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003760 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3761 if (EltSz == 64)
3762 return false;
3763
Bob Wilsonc692cb72009-08-21 20:54:19 +00003764 unsigned NumElts = VT.getVectorNumElements();
3765 WhichResult = (M[0] == 0 ? 0 : 1);
3766 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003767 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003768 if ((unsigned) M[i] != 2 * i + WhichResult)
3769 return false;
3770 }
3771
3772 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003773 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003774 return false;
3775
3776 return true;
3777}
3778
Bob Wilson324f4f12009-12-03 06:40:55 +00003779/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3780/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3781/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3782static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3783 unsigned &WhichResult) {
3784 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3785 if (EltSz == 64)
3786 return false;
3787
3788 unsigned Half = VT.getVectorNumElements() / 2;
3789 WhichResult = (M[0] == 0 ? 0 : 1);
3790 for (unsigned j = 0; j != 2; ++j) {
3791 unsigned Idx = WhichResult;
3792 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003793 int MIdx = M[i + j * Half];
3794 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003795 return false;
3796 Idx += 2;
3797 }
3798 }
3799
3800 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3801 if (VT.is64BitVector() && EltSz == 32)
3802 return false;
3803
3804 return true;
3805}
3806
Bob Wilsonc692cb72009-08-21 20:54:19 +00003807static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3808 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003809 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3810 if (EltSz == 64)
3811 return false;
3812
Bob Wilsonc692cb72009-08-21 20:54:19 +00003813 unsigned NumElts = VT.getVectorNumElements();
3814 WhichResult = (M[0] == 0 ? 0 : 1);
3815 unsigned Idx = WhichResult * NumElts / 2;
3816 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003817 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3818 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003819 return false;
3820 Idx += 1;
3821 }
3822
3823 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003824 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003825 return false;
3826
3827 return true;
3828}
3829
Bob Wilson324f4f12009-12-03 06:40:55 +00003830/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3831/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3832/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3833static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3834 unsigned &WhichResult) {
3835 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3836 if (EltSz == 64)
3837 return false;
3838
3839 unsigned NumElts = VT.getVectorNumElements();
3840 WhichResult = (M[0] == 0 ? 0 : 1);
3841 unsigned Idx = WhichResult * NumElts / 2;
3842 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003843 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3844 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003845 return false;
3846 Idx += 1;
3847 }
3848
3849 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3850 if (VT.is64BitVector() && EltSz == 32)
3851 return false;
3852
3853 return true;
3854}
3855
Dale Johannesenf630c712010-07-29 20:10:08 +00003856// If N is an integer constant that can be moved into a register in one
3857// instruction, return an SDValue of such a constant (will become a MOV
3858// instruction). Otherwise return null.
3859static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3860 const ARMSubtarget *ST, DebugLoc dl) {
3861 uint64_t Val;
3862 if (!isa<ConstantSDNode>(N))
3863 return SDValue();
3864 Val = cast<ConstantSDNode>(N)->getZExtValue();
3865
3866 if (ST->isThumb1Only()) {
3867 if (Val <= 255 || ~Val <= 255)
3868 return DAG.getConstant(Val, MVT::i32);
3869 } else {
3870 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3871 return DAG.getConstant(Val, MVT::i32);
3872 }
3873 return SDValue();
3874}
3875
Bob Wilson5bafff32009-06-22 23:27:02 +00003876// If this is a case we can't handle, return null and let the default
3877// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003878SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3879 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003880 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003881 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003882 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003883
3884 APInt SplatBits, SplatUndef;
3885 unsigned SplatBitSize;
3886 bool HasAnyUndefs;
3887 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003888 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003889 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003890 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003891 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003892 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003893 DAG, VmovVT, VT.is128BitVector(),
3894 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003895 if (Val.getNode()) {
3896 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003897 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003898 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003899
3900 // Try an immediate VMVN.
3901 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3902 ((1LL << SplatBitSize) - 1));
3903 Val = isNEONModifiedImm(NegatedImm,
3904 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003905 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003906 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003907 if (Val.getNode()) {
3908 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003909 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003910 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003911 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003912 }
3913
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003914 // Scan through the operands to see if only one value is used.
3915 unsigned NumElts = VT.getVectorNumElements();
3916 bool isOnlyLowElement = true;
3917 bool usesOnlyOneValue = true;
3918 bool isConstant = true;
3919 SDValue Value;
3920 for (unsigned i = 0; i < NumElts; ++i) {
3921 SDValue V = Op.getOperand(i);
3922 if (V.getOpcode() == ISD::UNDEF)
3923 continue;
3924 if (i > 0)
3925 isOnlyLowElement = false;
3926 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3927 isConstant = false;
3928
3929 if (!Value.getNode())
3930 Value = V;
3931 else if (V != Value)
3932 usesOnlyOneValue = false;
3933 }
3934
3935 if (!Value.getNode())
3936 return DAG.getUNDEF(VT);
3937
3938 if (isOnlyLowElement)
3939 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3940
Dale Johannesenf630c712010-07-29 20:10:08 +00003941 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3942
Dale Johannesen575cd142010-10-19 20:00:17 +00003943 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3944 // i32 and try again.
3945 if (usesOnlyOneValue && EltSize <= 32) {
3946 if (!isConstant)
3947 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3948 if (VT.getVectorElementType().isFloatingPoint()) {
3949 SmallVector<SDValue, 8> Ops;
3950 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003951 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003952 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003953 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3954 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003955 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3956 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003957 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003958 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003959 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3960 if (Val.getNode())
3961 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003962 }
3963
3964 // If all elements are constants and the case above didn't get hit, fall back
3965 // to the default expansion, which will generate a load from the constant
3966 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003967 if (isConstant)
3968 return SDValue();
3969
Bob Wilson11a1dff2011-01-07 21:37:30 +00003970 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3971 if (NumElts >= 4) {
3972 SDValue shuffle = ReconstructShuffle(Op, DAG);
3973 if (shuffle != SDValue())
3974 return shuffle;
3975 }
3976
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003977 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003978 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3979 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003980 if (EltSize >= 32) {
3981 // Do the expansion with floating-point types, since that is what the VFP
3982 // registers are defined to use, and since i64 is not legal.
3983 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3984 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003985 SmallVector<SDValue, 8> Ops;
3986 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003987 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003988 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003989 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003990 }
3991
3992 return SDValue();
3993}
3994
Bob Wilson11a1dff2011-01-07 21:37:30 +00003995// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003996// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003997SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3998 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003999 DebugLoc dl = Op.getDebugLoc();
4000 EVT VT = Op.getValueType();
4001 unsigned NumElts = VT.getVectorNumElements();
4002
4003 SmallVector<SDValue, 2> SourceVecs;
4004 SmallVector<unsigned, 2> MinElts;
4005 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004006
Bob Wilson11a1dff2011-01-07 21:37:30 +00004007 for (unsigned i = 0; i < NumElts; ++i) {
4008 SDValue V = Op.getOperand(i);
4009 if (V.getOpcode() == ISD::UNDEF)
4010 continue;
4011 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4012 // A shuffle can only come from building a vector from various
4013 // elements of other vectors.
4014 return SDValue();
4015 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004016
Bob Wilson11a1dff2011-01-07 21:37:30 +00004017 // Record this extraction against the appropriate vector if possible...
4018 SDValue SourceVec = V.getOperand(0);
4019 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4020 bool FoundSource = false;
4021 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4022 if (SourceVecs[j] == SourceVec) {
4023 if (MinElts[j] > EltNo)
4024 MinElts[j] = EltNo;
4025 if (MaxElts[j] < EltNo)
4026 MaxElts[j] = EltNo;
4027 FoundSource = true;
4028 break;
4029 }
4030 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004031
Bob Wilson11a1dff2011-01-07 21:37:30 +00004032 // Or record a new source if not...
4033 if (!FoundSource) {
4034 SourceVecs.push_back(SourceVec);
4035 MinElts.push_back(EltNo);
4036 MaxElts.push_back(EltNo);
4037 }
4038 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004039
Bob Wilson11a1dff2011-01-07 21:37:30 +00004040 // Currently only do something sane when at most two source vectors
4041 // involved.
4042 if (SourceVecs.size() > 2)
4043 return SDValue();
4044
4045 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4046 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004047
Bob Wilson11a1dff2011-01-07 21:37:30 +00004048 // This loop extracts the usage patterns of the source vectors
4049 // and prepares appropriate SDValues for a shuffle if possible.
4050 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4051 if (SourceVecs[i].getValueType() == VT) {
4052 // No VEXT necessary
4053 ShuffleSrcs[i] = SourceVecs[i];
4054 VEXTOffsets[i] = 0;
4055 continue;
4056 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4057 // It probably isn't worth padding out a smaller vector just to
4058 // break it down again in a shuffle.
4059 return SDValue();
4060 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004061
Bob Wilson11a1dff2011-01-07 21:37:30 +00004062 // Since only 64-bit and 128-bit vectors are legal on ARM and
4063 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004064 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4065 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004066
Bob Wilson11a1dff2011-01-07 21:37:30 +00004067 if (MaxElts[i] - MinElts[i] >= NumElts) {
4068 // Span too large for a VEXT to cope
4069 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004070 }
4071
Bob Wilson11a1dff2011-01-07 21:37:30 +00004072 if (MinElts[i] >= NumElts) {
4073 // The extraction can just take the second half
4074 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004075 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4076 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004077 DAG.getIntPtrConstant(NumElts));
4078 } else if (MaxElts[i] < NumElts) {
4079 // The extraction can just take the first half
4080 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004081 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4082 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004083 DAG.getIntPtrConstant(0));
4084 } else {
4085 // An actual VEXT is needed
4086 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004087 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4088 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004089 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004090 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4091 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004092 DAG.getIntPtrConstant(NumElts));
4093 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4094 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4095 }
4096 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004097
Bob Wilson11a1dff2011-01-07 21:37:30 +00004098 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004099
Bob Wilson11a1dff2011-01-07 21:37:30 +00004100 for (unsigned i = 0; i < NumElts; ++i) {
4101 SDValue Entry = Op.getOperand(i);
4102 if (Entry.getOpcode() == ISD::UNDEF) {
4103 Mask.push_back(-1);
4104 continue;
4105 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004106
Bob Wilson11a1dff2011-01-07 21:37:30 +00004107 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004108 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4109 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004110 if (ExtractVec == SourceVecs[0]) {
4111 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4112 } else {
4113 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4114 }
4115 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004116
Bob Wilson11a1dff2011-01-07 21:37:30 +00004117 // Final check before we try to produce nonsense...
4118 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004119 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4120 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004121
Bob Wilson11a1dff2011-01-07 21:37:30 +00004122 return SDValue();
4123}
4124
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004125/// isShuffleMaskLegal - Targets can use this to indicate that they only
4126/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4127/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4128/// are assumed to be legal.
4129bool
4130ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4131 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004132 if (VT.getVectorNumElements() == 4 &&
4133 (VT.is128BitVector() || VT.is64BitVector())) {
4134 unsigned PFIndexes[4];
4135 for (unsigned i = 0; i != 4; ++i) {
4136 if (M[i] < 0)
4137 PFIndexes[i] = 8;
4138 else
4139 PFIndexes[i] = M[i];
4140 }
4141
4142 // Compute the index in the perfect shuffle table.
4143 unsigned PFTableIndex =
4144 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4145 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4146 unsigned Cost = (PFEntry >> 30);
4147
4148 if (Cost <= 4)
4149 return true;
4150 }
4151
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004152 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004153 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004154
Bob Wilson53dd2452010-06-07 23:53:38 +00004155 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4156 return (EltSize >= 32 ||
4157 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004158 isVREVMask(M, VT, 64) ||
4159 isVREVMask(M, VT, 32) ||
4160 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004161 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004162 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004163 isVTRNMask(M, VT, WhichResult) ||
4164 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004165 isVZIPMask(M, VT, WhichResult) ||
4166 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4167 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4168 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004169}
4170
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004171/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4172/// the specified operations to build the shuffle.
4173static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4174 SDValue RHS, SelectionDAG &DAG,
4175 DebugLoc dl) {
4176 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4177 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4178 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4179
4180 enum {
4181 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4182 OP_VREV,
4183 OP_VDUP0,
4184 OP_VDUP1,
4185 OP_VDUP2,
4186 OP_VDUP3,
4187 OP_VEXT1,
4188 OP_VEXT2,
4189 OP_VEXT3,
4190 OP_VUZPL, // VUZP, left result
4191 OP_VUZPR, // VUZP, right result
4192 OP_VZIPL, // VZIP, left result
4193 OP_VZIPR, // VZIP, right result
4194 OP_VTRNL, // VTRN, left result
4195 OP_VTRNR // VTRN, right result
4196 };
4197
4198 if (OpNum == OP_COPY) {
4199 if (LHSID == (1*9+2)*9+3) return LHS;
4200 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4201 return RHS;
4202 }
4203
4204 SDValue OpLHS, OpRHS;
4205 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4206 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4207 EVT VT = OpLHS.getValueType();
4208
4209 switch (OpNum) {
4210 default: llvm_unreachable("Unknown shuffle opcode!");
4211 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004212 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004213 if (VT.getVectorElementType() == MVT::i32 ||
4214 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004215 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4216 // vrev <4 x i16> -> VREV32
4217 if (VT.getVectorElementType() == MVT::i16)
4218 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4219 // vrev <4 x i8> -> VREV16
4220 assert(VT.getVectorElementType() == MVT::i8);
4221 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004222 case OP_VDUP0:
4223 case OP_VDUP1:
4224 case OP_VDUP2:
4225 case OP_VDUP3:
4226 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004227 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004228 case OP_VEXT1:
4229 case OP_VEXT2:
4230 case OP_VEXT3:
4231 return DAG.getNode(ARMISD::VEXT, dl, VT,
4232 OpLHS, OpRHS,
4233 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4234 case OP_VUZPL:
4235 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004236 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004237 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4238 case OP_VZIPL:
4239 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004240 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004241 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4242 case OP_VTRNL:
4243 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004244 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4245 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004246 }
4247}
4248
Bill Wendling69a05a72011-03-14 23:02:38 +00004249static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4250 SmallVectorImpl<int> &ShuffleMask,
4251 SelectionDAG &DAG) {
4252 // Check to see if we can use the VTBL instruction.
4253 SDValue V1 = Op.getOperand(0);
4254 SDValue V2 = Op.getOperand(1);
4255 DebugLoc DL = Op.getDebugLoc();
4256
4257 SmallVector<SDValue, 8> VTBLMask;
4258 for (SmallVectorImpl<int>::iterator
4259 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4260 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4261
4262 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4263 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4264 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4265 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004266
Owen Anderson76706012011-04-05 21:48:57 +00004267 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004268 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4269 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004270}
4271
Bob Wilson5bafff32009-06-22 23:27:02 +00004272static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004273 SDValue V1 = Op.getOperand(0);
4274 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004275 DebugLoc dl = Op.getDebugLoc();
4276 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004277 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004278 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004279
Bob Wilson28865062009-08-13 02:13:04 +00004280 // Convert shuffles that are directly supported on NEON to target-specific
4281 // DAG nodes, instead of keeping them as shuffles and matching them again
4282 // during code selection. This is more efficient and avoids the possibility
4283 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004284 // FIXME: floating-point vectors should be canonicalized to integer vectors
4285 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004286 SVN->getMask(ShuffleMask);
4287
Bob Wilson53dd2452010-06-07 23:53:38 +00004288 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4289 if (EltSize <= 32) {
4290 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4291 int Lane = SVN->getSplatIndex();
4292 // If this is undef splat, generate it via "just" vdup, if possible.
4293 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004294
Bob Wilson53dd2452010-06-07 23:53:38 +00004295 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4296 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4297 }
4298 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4299 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004300 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004301
4302 bool ReverseVEXT;
4303 unsigned Imm;
4304 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4305 if (ReverseVEXT)
4306 std::swap(V1, V2);
4307 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4308 DAG.getConstant(Imm, MVT::i32));
4309 }
4310
4311 if (isVREVMask(ShuffleMask, VT, 64))
4312 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4313 if (isVREVMask(ShuffleMask, VT, 32))
4314 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4315 if (isVREVMask(ShuffleMask, VT, 16))
4316 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4317
4318 // Check for Neon shuffles that modify both input vectors in place.
4319 // If both results are used, i.e., if there are two shuffles with the same
4320 // source operands and with masks corresponding to both results of one of
4321 // these operations, DAG memoization will ensure that a single node is
4322 // used for both shuffles.
4323 unsigned WhichResult;
4324 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4325 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4326 V1, V2).getValue(WhichResult);
4327 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4328 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4329 V1, V2).getValue(WhichResult);
4330 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4331 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4332 V1, V2).getValue(WhichResult);
4333
4334 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4335 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4336 V1, V1).getValue(WhichResult);
4337 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4338 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4339 V1, V1).getValue(WhichResult);
4340 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4341 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4342 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004343 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004344
Bob Wilsonc692cb72009-08-21 20:54:19 +00004345 // If the shuffle is not directly supported and it has 4 elements, use
4346 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004347 unsigned NumElts = VT.getVectorNumElements();
4348 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004349 unsigned PFIndexes[4];
4350 for (unsigned i = 0; i != 4; ++i) {
4351 if (ShuffleMask[i] < 0)
4352 PFIndexes[i] = 8;
4353 else
4354 PFIndexes[i] = ShuffleMask[i];
4355 }
4356
4357 // Compute the index in the perfect shuffle table.
4358 unsigned PFTableIndex =
4359 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004360 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4361 unsigned Cost = (PFEntry >> 30);
4362
4363 if (Cost <= 4)
4364 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4365 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004366
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004367 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004368 if (EltSize >= 32) {
4369 // Do the expansion with floating-point types, since that is what the VFP
4370 // registers are defined to use, and since i64 is not legal.
4371 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4372 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004373 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4374 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004375 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004376 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004377 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004378 Ops.push_back(DAG.getUNDEF(EltVT));
4379 else
4380 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4381 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4382 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4383 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004384 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004385 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004386 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004387 }
4388
Bill Wendling69a05a72011-03-14 23:02:38 +00004389 if (VT == MVT::v8i8) {
4390 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4391 if (NewOp.getNode())
4392 return NewOp;
4393 }
4394
Bob Wilson22cac0d2009-08-14 05:16:33 +00004395 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004396}
4397
Bob Wilson5bafff32009-06-22 23:27:02 +00004398static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004399 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004400 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004401 if (!isa<ConstantSDNode>(Lane))
4402 return SDValue();
4403
4404 SDValue Vec = Op.getOperand(0);
4405 if (Op.getValueType() == MVT::i32 &&
4406 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4407 DebugLoc dl = Op.getDebugLoc();
4408 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4409 }
4410
4411 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004412}
4413
Bob Wilsona6d65862009-08-03 20:36:38 +00004414static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4415 // The only time a CONCAT_VECTORS operation can have legal types is when
4416 // two 64-bit vectors are concatenated to a 128-bit vector.
4417 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4418 "unexpected CONCAT_VECTORS");
4419 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004420 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004421 SDValue Op0 = Op.getOperand(0);
4422 SDValue Op1 = Op.getOperand(1);
4423 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004425 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004426 DAG.getIntPtrConstant(0));
4427 if (Op1.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, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004430 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004431 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004432}
4433
Bob Wilson626613d2010-11-23 19:38:38 +00004434/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4435/// element has been zero/sign-extended, depending on the isSigned parameter,
4436/// from an integer type half its size.
4437static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4438 bool isSigned) {
4439 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4440 EVT VT = N->getValueType(0);
4441 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4442 SDNode *BVN = N->getOperand(0).getNode();
4443 if (BVN->getValueType(0) != MVT::v4i32 ||
4444 BVN->getOpcode() != ISD::BUILD_VECTOR)
4445 return false;
4446 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4447 unsigned HiElt = 1 - LoElt;
4448 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4449 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4450 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4451 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4452 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4453 return false;
4454 if (isSigned) {
4455 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4456 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4457 return true;
4458 } else {
4459 if (Hi0->isNullValue() && Hi1->isNullValue())
4460 return true;
4461 }
4462 return false;
4463 }
4464
4465 if (N->getOpcode() != ISD::BUILD_VECTOR)
4466 return false;
4467
4468 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4469 SDNode *Elt = N->getOperand(i).getNode();
4470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4471 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4472 unsigned HalfSize = EltSize / 2;
4473 if (isSigned) {
4474 int64_t SExtVal = C->getSExtValue();
4475 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4476 return false;
4477 } else {
4478 if ((C->getZExtValue() >> HalfSize) != 0)
4479 return false;
4480 }
4481 continue;
4482 }
4483 return false;
4484 }
4485
4486 return true;
4487}
4488
4489/// isSignExtended - Check if a node is a vector value that is sign-extended
4490/// or a constant BUILD_VECTOR with sign-extended elements.
4491static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4492 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4493 return true;
4494 if (isExtendedBUILD_VECTOR(N, DAG, true))
4495 return true;
4496 return false;
4497}
4498
4499/// isZeroExtended - Check if a node is a vector value that is zero-extended
4500/// or a constant BUILD_VECTOR with zero-extended elements.
4501static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4502 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4503 return true;
4504 if (isExtendedBUILD_VECTOR(N, DAG, false))
4505 return true;
4506 return false;
4507}
4508
4509/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4510/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004511static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4512 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4513 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004514 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4515 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4516 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4517 LD->isNonTemporal(), LD->getAlignment());
4518 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4519 // have been legalized as a BITCAST from v4i32.
4520 if (N->getOpcode() == ISD::BITCAST) {
4521 SDNode *BVN = N->getOperand(0).getNode();
4522 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4523 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4524 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4525 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4526 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4527 }
4528 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4529 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4530 EVT VT = N->getValueType(0);
4531 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4532 unsigned NumElts = VT.getVectorNumElements();
4533 MVT TruncVT = MVT::getIntegerVT(EltSize);
4534 SmallVector<SDValue, 8> Ops;
4535 for (unsigned i = 0; i != NumElts; ++i) {
4536 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4537 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004538 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004539 }
4540 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4541 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004542}
4543
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004544static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4545 unsigned Opcode = N->getOpcode();
4546 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4547 SDNode *N0 = N->getOperand(0).getNode();
4548 SDNode *N1 = N->getOperand(1).getNode();
4549 return N0->hasOneUse() && N1->hasOneUse() &&
4550 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4551 }
4552 return false;
4553}
4554
4555static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4556 unsigned Opcode = N->getOpcode();
4557 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4558 SDNode *N0 = N->getOperand(0).getNode();
4559 SDNode *N1 = N->getOperand(1).getNode();
4560 return N0->hasOneUse() && N1->hasOneUse() &&
4561 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4562 }
4563 return false;
4564}
4565
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004566static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4567 // Multiplications are only custom-lowered for 128-bit vectors so that
4568 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4569 EVT VT = Op.getValueType();
4570 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4571 SDNode *N0 = Op.getOperand(0).getNode();
4572 SDNode *N1 = Op.getOperand(1).getNode();
4573 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004574 bool isMLA = false;
4575 bool isN0SExt = isSignExtended(N0, DAG);
4576 bool isN1SExt = isSignExtended(N1, DAG);
4577 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004578 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004579 else {
4580 bool isN0ZExt = isZeroExtended(N0, DAG);
4581 bool isN1ZExt = isZeroExtended(N1, DAG);
4582 if (isN0ZExt && isN1ZExt)
4583 NewOpc = ARMISD::VMULLu;
4584 else if (isN1SExt || isN1ZExt) {
4585 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4586 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4587 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4588 NewOpc = ARMISD::VMULLs;
4589 isMLA = true;
4590 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4591 NewOpc = ARMISD::VMULLu;
4592 isMLA = true;
4593 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4594 std::swap(N0, N1);
4595 NewOpc = ARMISD::VMULLu;
4596 isMLA = true;
4597 }
4598 }
4599
4600 if (!NewOpc) {
4601 if (VT == MVT::v2i64)
4602 // Fall through to expand this. It is not legal.
4603 return SDValue();
4604 else
4605 // Other vector multiplications are legal.
4606 return Op;
4607 }
4608 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004609
4610 // Legalize to a VMULL instruction.
4611 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004612 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004613 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004614 if (!isMLA) {
4615 Op0 = SkipExtension(N0, DAG);
4616 assert(Op0.getValueType().is64BitVector() &&
4617 Op1.getValueType().is64BitVector() &&
4618 "unexpected types for extended operands to VMULL");
4619 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4620 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004621
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004622 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4623 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4624 // vmull q0, d4, d6
4625 // vmlal q0, d5, d6
4626 // is faster than
4627 // vaddl q0, d4, d5
4628 // vmovl q1, d6
4629 // vmul q0, q0, q1
4630 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4631 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4632 EVT Op1VT = Op1.getValueType();
4633 return DAG.getNode(N0->getOpcode(), DL, VT,
4634 DAG.getNode(NewOpc, DL, VT,
4635 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4636 DAG.getNode(NewOpc, DL, VT,
4637 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004638}
4639
Owen Anderson76706012011-04-05 21:48:57 +00004640static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004641LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4642 // Convert to float
4643 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4644 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4645 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4646 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4647 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4648 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4649 // Get reciprocal estimate.
4650 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004651 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004652 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4653 // Because char has a smaller range than uchar, we can actually get away
4654 // without any newton steps. This requires that we use a weird bias
4655 // of 0xb000, however (again, this has been exhaustively tested).
4656 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4657 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4658 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4659 Y = DAG.getConstant(0xb000, MVT::i32);
4660 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4661 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4662 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4663 // Convert back to short.
4664 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4665 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4666 return X;
4667}
4668
Owen Anderson76706012011-04-05 21:48:57 +00004669static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004670LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4671 SDValue N2;
4672 // Convert to float.
4673 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4674 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4675 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4676 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4677 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4678 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004679
Nate Begeman7973f352011-02-11 20:53:29 +00004680 // Use reciprocal estimate and one refinement step.
4681 // float4 recip = vrecpeq_f32(yf);
4682 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004683 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004684 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004685 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004686 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4687 N1, N2);
4688 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4689 // Because short has a smaller range than ushort, we can actually get away
4690 // with only a single newton step. This requires that we use a weird bias
4691 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004692 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004693 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4694 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004695 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004696 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4697 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4698 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4699 // Convert back to integer and return.
4700 // return vmovn_s32(vcvt_s32_f32(result));
4701 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4702 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4703 return N0;
4704}
4705
4706static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4707 EVT VT = Op.getValueType();
4708 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4709 "unexpected type for custom-lowering ISD::SDIV");
4710
4711 DebugLoc dl = Op.getDebugLoc();
4712 SDValue N0 = Op.getOperand(0);
4713 SDValue N1 = Op.getOperand(1);
4714 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004715
Nate Begeman7973f352011-02-11 20:53:29 +00004716 if (VT == MVT::v8i8) {
4717 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4718 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004719
Nate Begeman7973f352011-02-11 20:53:29 +00004720 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4721 DAG.getIntPtrConstant(4));
4722 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004723 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004724 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4725 DAG.getIntPtrConstant(0));
4726 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4727 DAG.getIntPtrConstant(0));
4728
4729 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4730 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4731
4732 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4733 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004734
Nate Begeman7973f352011-02-11 20:53:29 +00004735 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4736 return N0;
4737 }
4738 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4739}
4740
4741static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4742 EVT VT = Op.getValueType();
4743 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4744 "unexpected type for custom-lowering ISD::UDIV");
4745
4746 DebugLoc dl = Op.getDebugLoc();
4747 SDValue N0 = Op.getOperand(0);
4748 SDValue N1 = Op.getOperand(1);
4749 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004750
Nate Begeman7973f352011-02-11 20:53:29 +00004751 if (VT == MVT::v8i8) {
4752 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4753 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004754
Nate Begeman7973f352011-02-11 20:53:29 +00004755 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4756 DAG.getIntPtrConstant(4));
4757 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004758 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004759 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4760 DAG.getIntPtrConstant(0));
4761 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4762 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004763
Nate Begeman7973f352011-02-11 20:53:29 +00004764 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4765 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004766
Nate Begeman7973f352011-02-11 20:53:29 +00004767 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4768 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004769
4770 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004771 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4772 N0);
4773 return N0;
4774 }
Owen Anderson76706012011-04-05 21:48:57 +00004775
Nate Begeman7973f352011-02-11 20:53:29 +00004776 // v4i16 sdiv ... Convert to float.
4777 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4778 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4779 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4780 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4781 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004782 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004783
4784 // Use reciprocal estimate and two refinement steps.
4785 // float4 recip = vrecpeq_f32(yf);
4786 // recip *= vrecpsq_f32(yf, recip);
4787 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004788 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004789 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004790 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004791 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004792 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004793 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
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);
4798 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4799 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4800 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004801 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004802 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4803 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4804 N1 = DAG.getConstant(2, MVT::i32);
4805 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4806 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4807 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4808 // Convert back to integer and return.
4809 // return vmovn_u32(vcvt_s32_f32(result));
4810 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4811 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4812 return N0;
4813}
4814
Dan Gohmand858e902010-04-17 15:26:15 +00004815SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004816 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004817 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004818 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004819 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004820 case ISD::GlobalAddress:
4821 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4822 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004823 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004824 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004825 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4826 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004827 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004828 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004829 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004830 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004831 case ISD::SINT_TO_FP:
4832 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4833 case ISD::FP_TO_SINT:
4834 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004835 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004836 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004837 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004838 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004839 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004840 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004841 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004842 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4843 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004844 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004845 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004846 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004847 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004848 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004849 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004850 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004851 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004852 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004853 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004854 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004855 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004856 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004857 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004858 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004859 case ISD::SDIV: return LowerSDIV(Op, DAG);
4860 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004861 }
Dan Gohman475871a2008-07-27 21:46:04 +00004862 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004863}
4864
Duncan Sands1607f052008-12-01 11:39:25 +00004865/// ReplaceNodeResults - Replace the results of node with an illegal result
4866/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004867void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4868 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004869 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004870 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004871 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004872 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004873 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004874 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004875 case ISD::BITCAST:
4876 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004877 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004878 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004879 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004880 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004881 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004882 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004883 if (Res.getNode())
4884 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004885}
Chris Lattner27a6c732007-11-24 07:07:01 +00004886
Evan Chenga8e29892007-01-19 07:51:42 +00004887//===----------------------------------------------------------------------===//
4888// ARM Scheduler Hooks
4889//===----------------------------------------------------------------------===//
4890
4891MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004892ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4893 MachineBasicBlock *BB,
4894 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004895 unsigned dest = MI->getOperand(0).getReg();
4896 unsigned ptr = MI->getOperand(1).getReg();
4897 unsigned oldval = MI->getOperand(2).getReg();
4898 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004899 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4900 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004901 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004902
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004903 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4904 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004905 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004906 : ARM::GPRRegisterClass);
4907
4908 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004909 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4910 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4911 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004912 }
4913
Jim Grosbach5278eb82009-12-11 01:42:04 +00004914 unsigned ldrOpc, strOpc;
4915 switch (Size) {
4916 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004917 case 1:
4918 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004919 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004920 break;
4921 case 2:
4922 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4923 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4924 break;
4925 case 4:
4926 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4927 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4928 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004929 }
4930
4931 MachineFunction *MF = BB->getParent();
4932 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4933 MachineFunction::iterator It = BB;
4934 ++It; // insert the new blocks after the current block
4935
4936 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4937 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4938 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4939 MF->insert(It, loop1MBB);
4940 MF->insert(It, loop2MBB);
4941 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004942
4943 // Transfer the remainder of BB and its successor edges to exitMBB.
4944 exitMBB->splice(exitMBB->begin(), BB,
4945 llvm::next(MachineBasicBlock::iterator(MI)),
4946 BB->end());
4947 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004948
4949 // thisMBB:
4950 // ...
4951 // fallthrough --> loop1MBB
4952 BB->addSuccessor(loop1MBB);
4953
4954 // loop1MBB:
4955 // ldrex dest, [ptr]
4956 // cmp dest, oldval
4957 // bne exitMBB
4958 BB = loop1MBB;
4959 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004960 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004961 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004962 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4963 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004964 BB->addSuccessor(loop2MBB);
4965 BB->addSuccessor(exitMBB);
4966
4967 // loop2MBB:
4968 // strex scratch, newval, [ptr]
4969 // cmp scratch, #0
4970 // bne loop1MBB
4971 BB = loop2MBB;
4972 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4973 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004974 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004975 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004976 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4977 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004978 BB->addSuccessor(loop1MBB);
4979 BB->addSuccessor(exitMBB);
4980
4981 // exitMBB:
4982 // ...
4983 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004984
Dan Gohman14152b42010-07-06 20:24:04 +00004985 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004986
Jim Grosbach5278eb82009-12-11 01:42:04 +00004987 return BB;
4988}
4989
4990MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004991ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4992 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004993 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4994 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4995
4996 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004997 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004998 MachineFunction::iterator It = BB;
4999 ++It;
5000
5001 unsigned dest = MI->getOperand(0).getReg();
5002 unsigned ptr = MI->getOperand(1).getReg();
5003 unsigned incr = MI->getOperand(2).getReg();
5004 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005005 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005006
5007 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5008 if (isThumb2) {
5009 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5010 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5011 }
5012
Jim Grosbachc3c23542009-12-14 04:22:04 +00005013 unsigned ldrOpc, strOpc;
5014 switch (Size) {
5015 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005016 case 1:
5017 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005018 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005019 break;
5020 case 2:
5021 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5022 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5023 break;
5024 case 4:
5025 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5026 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5027 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005028 }
5029
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005030 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5031 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5032 MF->insert(It, loopMBB);
5033 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005034
5035 // Transfer the remainder of BB and its successor edges to exitMBB.
5036 exitMBB->splice(exitMBB->begin(), BB,
5037 llvm::next(MachineBasicBlock::iterator(MI)),
5038 BB->end());
5039 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005040
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005041 TargetRegisterClass *TRC =
5042 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5043 unsigned scratch = MRI.createVirtualRegister(TRC);
5044 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005045
5046 // thisMBB:
5047 // ...
5048 // fallthrough --> loopMBB
5049 BB->addSuccessor(loopMBB);
5050
5051 // loopMBB:
5052 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005053 // <binop> scratch2, dest, incr
5054 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005055 // cmp scratch, #0
5056 // bne- loopMBB
5057 // fallthrough --> exitMBB
5058 BB = loopMBB;
5059 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005060 if (BinOpcode) {
5061 // operand order needs to go the other way for NAND
5062 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5063 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5064 addReg(incr).addReg(dest)).addReg(0);
5065 else
5066 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5067 addReg(dest).addReg(incr)).addReg(0);
5068 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005069
5070 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5071 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005072 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005073 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005074 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5075 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005076
5077 BB->addSuccessor(loopMBB);
5078 BB->addSuccessor(exitMBB);
5079
5080 // exitMBB:
5081 // ...
5082 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005083
Dan Gohman14152b42010-07-06 20:24:04 +00005084 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005085
Jim Grosbachc3c23542009-12-14 04:22:04 +00005086 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005087}
5088
Jim Grosbachf7da8822011-04-26 19:44:18 +00005089MachineBasicBlock *
5090ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5091 MachineBasicBlock *BB,
5092 unsigned Size,
5093 bool signExtend,
5094 ARMCC::CondCodes Cond) const {
5095 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5096
5097 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5098 MachineFunction *MF = BB->getParent();
5099 MachineFunction::iterator It = BB;
5100 ++It;
5101
5102 unsigned dest = MI->getOperand(0).getReg();
5103 unsigned ptr = MI->getOperand(1).getReg();
5104 unsigned incr = MI->getOperand(2).getReg();
5105 unsigned oldval = dest;
5106 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005107 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005108
5109 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5110 if (isThumb2) {
5111 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5112 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5113 }
5114
Jim Grosbachf7da8822011-04-26 19:44:18 +00005115 unsigned ldrOpc, strOpc, extendOpc;
5116 switch (Size) {
5117 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5118 case 1:
5119 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5120 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
5121 extendOpc = isThumb2 ? ARM::t2SXTBr : ARM::SXTBr;
5122 break;
5123 case 2:
5124 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5125 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5126 extendOpc = isThumb2 ? ARM::t2SXTHr : ARM::SXTHr;
5127 break;
5128 case 4:
5129 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5130 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5131 extendOpc = 0;
5132 break;
5133 }
5134
5135 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5136 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5137 MF->insert(It, loopMBB);
5138 MF->insert(It, exitMBB);
5139
5140 // Transfer the remainder of BB and its successor edges to exitMBB.
5141 exitMBB->splice(exitMBB->begin(), BB,
5142 llvm::next(MachineBasicBlock::iterator(MI)),
5143 BB->end());
5144 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5145
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005146 TargetRegisterClass *TRC =
5147 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5148 unsigned scratch = MRI.createVirtualRegister(TRC);
5149 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005150
5151 // thisMBB:
5152 // ...
5153 // fallthrough --> loopMBB
5154 BB->addSuccessor(loopMBB);
5155
5156 // loopMBB:
5157 // ldrex dest, ptr
5158 // (sign extend dest, if required)
5159 // cmp dest, incr
5160 // cmov.cond scratch2, dest, incr
5161 // strex scratch, scratch2, ptr
5162 // cmp scratch, #0
5163 // bne- loopMBB
5164 // fallthrough --> exitMBB
5165 BB = loopMBB;
5166 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5167
5168 // Sign extend the value, if necessary.
5169 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005170 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005171 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval).addReg(dest));
5172 }
5173
5174 // Build compare and cmov instructions.
5175 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5176 .addReg(oldval).addReg(incr));
5177 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5178 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5179
5180 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5181 .addReg(ptr));
5182 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5183 .addReg(scratch).addImm(0));
5184 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5185 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5186
5187 BB->addSuccessor(loopMBB);
5188 BB->addSuccessor(exitMBB);
5189
5190 // exitMBB:
5191 // ...
5192 BB = exitMBB;
5193
5194 MI->eraseFromParent(); // The instruction is gone now.
5195
5196 return BB;
5197}
5198
Evan Cheng218977b2010-07-13 19:27:42 +00005199static
5200MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5201 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5202 E = MBB->succ_end(); I != E; ++I)
5203 if (*I != Succ)
5204 return *I;
5205 llvm_unreachable("Expecting a BB with two successors!");
5206}
5207
Andrew Trick1c3af772011-04-23 03:55:32 +00005208// FIXME: This opcode table should obviously be expressed in the target
5209// description. We probably just need a "machine opcode" value in the pseudo
5210// instruction. But the ideal solution maybe to simply remove the "S" version
5211// of the opcode altogether.
5212struct AddSubFlagsOpcodePair {
5213 unsigned PseudoOpc;
5214 unsigned MachineOpc;
5215};
5216
5217static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5218 {ARM::ADCSri, ARM::ADCri},
5219 {ARM::ADCSrr, ARM::ADCrr},
5220 {ARM::ADCSrs, ARM::ADCrs},
5221 {ARM::SBCSri, ARM::SBCri},
5222 {ARM::SBCSrr, ARM::SBCrr},
5223 {ARM::SBCSrs, ARM::SBCrs},
5224 {ARM::RSBSri, ARM::RSBri},
5225 {ARM::RSBSrr, ARM::RSBrr},
5226 {ARM::RSBSrs, ARM::RSBrs},
5227 {ARM::RSCSri, ARM::RSCri},
5228 {ARM::RSCSrs, ARM::RSCrs},
5229 {ARM::t2ADCSri, ARM::t2ADCri},
5230 {ARM::t2ADCSrr, ARM::t2ADCrr},
5231 {ARM::t2ADCSrs, ARM::t2ADCrs},
5232 {ARM::t2SBCSri, ARM::t2SBCri},
5233 {ARM::t2SBCSrr, ARM::t2SBCrr},
5234 {ARM::t2SBCSrs, ARM::t2SBCrs},
5235 {ARM::t2RSBSri, ARM::t2RSBri},
5236 {ARM::t2RSBSrs, ARM::t2RSBrs},
5237};
5238
5239// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5240// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5241//
5242// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5243// position to be recognized by the target descrition as the 'S' bit.
5244bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5245 MachineBasicBlock *BB) const {
5246 unsigned OldOpc = MI->getOpcode();
5247 unsigned NewOpc = 0;
5248
5249 // This is only called for instructions that need remapping, so iterating over
5250 // the tiny opcode table is not costly.
5251 static const int NPairs =
5252 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5253 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5254 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5255 if (OldOpc == Pair->PseudoOpc) {
5256 NewOpc = Pair->MachineOpc;
5257 break;
5258 }
5259 }
5260 if (!NewOpc)
5261 return false;
5262
5263 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5264 DebugLoc dl = MI->getDebugLoc();
5265 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5266 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5267 MIB.addOperand(MI->getOperand(i));
5268 AddDefaultPred(MIB);
5269 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5270 MI->eraseFromParent();
5271 return true;
5272}
5273
Jim Grosbache801dc42009-12-12 01:40:06 +00005274MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005275ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005276 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005277 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005278 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005279 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005280 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005281 default: {
5282 if (RemapAddSubWithFlags(MI, BB))
5283 return BB;
5284
Jim Grosbach5278eb82009-12-11 01:42:04 +00005285 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005286 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005287 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005288 case ARM::ATOMIC_LOAD_ADD_I8:
5289 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5290 case ARM::ATOMIC_LOAD_ADD_I16:
5291 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5292 case ARM::ATOMIC_LOAD_ADD_I32:
5293 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005294
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005295 case ARM::ATOMIC_LOAD_AND_I8:
5296 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5297 case ARM::ATOMIC_LOAD_AND_I16:
5298 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5299 case ARM::ATOMIC_LOAD_AND_I32:
5300 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005301
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005302 case ARM::ATOMIC_LOAD_OR_I8:
5303 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5304 case ARM::ATOMIC_LOAD_OR_I16:
5305 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5306 case ARM::ATOMIC_LOAD_OR_I32:
5307 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005308
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005309 case ARM::ATOMIC_LOAD_XOR_I8:
5310 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5311 case ARM::ATOMIC_LOAD_XOR_I16:
5312 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5313 case ARM::ATOMIC_LOAD_XOR_I32:
5314 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005315
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005316 case ARM::ATOMIC_LOAD_NAND_I8:
5317 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5318 case ARM::ATOMIC_LOAD_NAND_I16:
5319 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5320 case ARM::ATOMIC_LOAD_NAND_I32:
5321 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005322
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005323 case ARM::ATOMIC_LOAD_SUB_I8:
5324 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5325 case ARM::ATOMIC_LOAD_SUB_I16:
5326 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5327 case ARM::ATOMIC_LOAD_SUB_I32:
5328 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005329
Jim Grosbachf7da8822011-04-26 19:44:18 +00005330 case ARM::ATOMIC_LOAD_MIN_I8:
5331 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5332 case ARM::ATOMIC_LOAD_MIN_I16:
5333 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5334 case ARM::ATOMIC_LOAD_MIN_I32:
5335 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5336
5337 case ARM::ATOMIC_LOAD_MAX_I8:
5338 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5339 case ARM::ATOMIC_LOAD_MAX_I16:
5340 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5341 case ARM::ATOMIC_LOAD_MAX_I32:
5342 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5343
5344 case ARM::ATOMIC_LOAD_UMIN_I8:
5345 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5346 case ARM::ATOMIC_LOAD_UMIN_I16:
5347 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5348 case ARM::ATOMIC_LOAD_UMIN_I32:
5349 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5350
5351 case ARM::ATOMIC_LOAD_UMAX_I8:
5352 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5353 case ARM::ATOMIC_LOAD_UMAX_I16:
5354 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5355 case ARM::ATOMIC_LOAD_UMAX_I32:
5356 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5357
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005358 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5359 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5360 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005361
5362 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5363 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5364 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005365
Evan Cheng007ea272009-08-12 05:17:19 +00005366 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005367 // To "insert" a SELECT_CC instruction, we actually have to insert the
5368 // diamond control-flow pattern. The incoming instruction knows the
5369 // destination vreg to set, the condition code register to branch on, the
5370 // true/false values to select between, and a branch opcode to use.
5371 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005372 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005373 ++It;
5374
5375 // thisMBB:
5376 // ...
5377 // TrueVal = ...
5378 // cmpTY ccX, r1, r2
5379 // bCC copy1MBB
5380 // fallthrough --> copy0MBB
5381 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005382 MachineFunction *F = BB->getParent();
5383 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5384 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005385 F->insert(It, copy0MBB);
5386 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005387
5388 // Transfer the remainder of BB and its successor edges to sinkMBB.
5389 sinkMBB->splice(sinkMBB->begin(), BB,
5390 llvm::next(MachineBasicBlock::iterator(MI)),
5391 BB->end());
5392 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5393
Dan Gohman258c58c2010-07-06 15:49:48 +00005394 BB->addSuccessor(copy0MBB);
5395 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005396
Dan Gohman14152b42010-07-06 20:24:04 +00005397 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5398 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5399
Evan Chenga8e29892007-01-19 07:51:42 +00005400 // copy0MBB:
5401 // %FalseValue = ...
5402 // # fallthrough to sinkMBB
5403 BB = copy0MBB;
5404
5405 // Update machine-CFG edges
5406 BB->addSuccessor(sinkMBB);
5407
5408 // sinkMBB:
5409 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5410 // ...
5411 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005412 BuildMI(*BB, BB->begin(), dl,
5413 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005414 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5415 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5416
Dan Gohman14152b42010-07-06 20:24:04 +00005417 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005418 return BB;
5419 }
Evan Cheng86198642009-08-07 00:34:42 +00005420
Evan Cheng218977b2010-07-13 19:27:42 +00005421 case ARM::BCCi64:
5422 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005423 // If there is an unconditional branch to the other successor, remove it.
5424 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005425
Evan Cheng218977b2010-07-13 19:27:42 +00005426 // Compare both parts that make up the double comparison separately for
5427 // equality.
5428 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5429
5430 unsigned LHS1 = MI->getOperand(1).getReg();
5431 unsigned LHS2 = MI->getOperand(2).getReg();
5432 if (RHSisZero) {
5433 AddDefaultPred(BuildMI(BB, dl,
5434 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5435 .addReg(LHS1).addImm(0));
5436 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5437 .addReg(LHS2).addImm(0)
5438 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5439 } else {
5440 unsigned RHS1 = MI->getOperand(3).getReg();
5441 unsigned RHS2 = MI->getOperand(4).getReg();
5442 AddDefaultPred(BuildMI(BB, dl,
5443 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5444 .addReg(LHS1).addReg(RHS1));
5445 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5446 .addReg(LHS2).addReg(RHS2)
5447 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5448 }
5449
5450 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5451 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5452 if (MI->getOperand(0).getImm() == ARMCC::NE)
5453 std::swap(destMBB, exitMBB);
5454
5455 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5456 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5457 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5458 .addMBB(exitMBB);
5459
5460 MI->eraseFromParent(); // The pseudo instruction is gone now.
5461 return BB;
5462 }
Evan Chenga8e29892007-01-19 07:51:42 +00005463 }
5464}
5465
5466//===----------------------------------------------------------------------===//
5467// ARM Optimization Hooks
5468//===----------------------------------------------------------------------===//
5469
Chris Lattnerd1980a52009-03-12 06:52:53 +00005470static
5471SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5472 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005473 SelectionDAG &DAG = DCI.DAG;
5474 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005475 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005476 unsigned Opc = N->getOpcode();
5477 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5478 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5479 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5480 ISD::CondCode CC = ISD::SETCC_INVALID;
5481
5482 if (isSlctCC) {
5483 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5484 } else {
5485 SDValue CCOp = Slct.getOperand(0);
5486 if (CCOp.getOpcode() == ISD::SETCC)
5487 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5488 }
5489
5490 bool DoXform = false;
5491 bool InvCC = false;
5492 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5493 "Bad input!");
5494
5495 if (LHS.getOpcode() == ISD::Constant &&
5496 cast<ConstantSDNode>(LHS)->isNullValue()) {
5497 DoXform = true;
5498 } else if (CC != ISD::SETCC_INVALID &&
5499 RHS.getOpcode() == ISD::Constant &&
5500 cast<ConstantSDNode>(RHS)->isNullValue()) {
5501 std::swap(LHS, RHS);
5502 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005503 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005504 Op0.getOperand(0).getValueType();
5505 bool isInt = OpVT.isInteger();
5506 CC = ISD::getSetCCInverse(CC, isInt);
5507
5508 if (!TLI.isCondCodeLegal(CC, OpVT))
5509 return SDValue(); // Inverse operator isn't legal.
5510
5511 DoXform = true;
5512 InvCC = true;
5513 }
5514
5515 if (DoXform) {
5516 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5517 if (isSlctCC)
5518 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5519 Slct.getOperand(0), Slct.getOperand(1), CC);
5520 SDValue CCOp = Slct.getOperand(0);
5521 if (InvCC)
5522 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5523 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5524 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5525 CCOp, OtherOp, Result);
5526 }
5527 return SDValue();
5528}
5529
Eric Christopherfa6f5912011-06-29 21:10:36 +00005530// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00005531// (only after legalization).
5532static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5533 TargetLowering::DAGCombinerInfo &DCI,
5534 const ARMSubtarget *Subtarget) {
5535
5536 // Only perform optimization if after legalize, and if NEON is available. We
5537 // also expected both operands to be BUILD_VECTORs.
5538 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5539 || N0.getOpcode() != ISD::BUILD_VECTOR
5540 || N1.getOpcode() != ISD::BUILD_VECTOR)
5541 return SDValue();
5542
5543 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5544 EVT VT = N->getValueType(0);
5545 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5546 return SDValue();
5547
5548 // Check that the vector operands are of the right form.
5549 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5550 // operands, where N is the size of the formed vector.
5551 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5552 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005553
5554 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005555 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005556 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005557 SDValue Vec = N0->getOperand(0)->getOperand(0);
5558 SDNode *V = Vec.getNode();
5559 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005560
Eric Christopherfa6f5912011-06-29 21:10:36 +00005561 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00005562 // check to see if each of their operands are an EXTRACT_VECTOR with
5563 // the same vector and appropriate index.
5564 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5565 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5566 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00005567
Tanya Lattner189531f2011-06-14 23:48:48 +00005568 SDValue ExtVec0 = N0->getOperand(i);
5569 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005570
Tanya Lattner189531f2011-06-14 23:48:48 +00005571 // First operand is the vector, verify its the same.
5572 if (V != ExtVec0->getOperand(0).getNode() ||
5573 V != ExtVec1->getOperand(0).getNode())
5574 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00005575
Tanya Lattner189531f2011-06-14 23:48:48 +00005576 // Second is the constant, verify its correct.
5577 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5578 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00005579
Tanya Lattner189531f2011-06-14 23:48:48 +00005580 // For the constant, we want to see all the even or all the odd.
5581 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5582 || C1->getZExtValue() != nextIndex+1)
5583 return SDValue();
5584
5585 // Increment index.
5586 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005587 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00005588 return SDValue();
5589 }
5590
5591 // Create VPADDL node.
5592 SelectionDAG &DAG = DCI.DAG;
5593 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00005594
5595 // Build operand list.
5596 SmallVector<SDValue, 8> Ops;
5597 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5598 TLI.getPointerTy()));
5599
5600 // Input is the vector.
5601 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005602
Tanya Lattner189531f2011-06-14 23:48:48 +00005603 // Get widened type and narrowed type.
5604 MVT widenType;
5605 unsigned numElem = VT.getVectorNumElements();
5606 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5607 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5608 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5609 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5610 default:
5611 assert(0 && "Invalid vector element type for padd optimization.");
5612 }
5613
5614 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5615 widenType, &Ops[0], Ops.size());
5616 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5617}
5618
Bob Wilson3d5792a2010-07-29 20:34:14 +00005619/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5620/// operands N0 and N1. This is a helper for PerformADDCombine that is
5621/// called with the default operands, and if that fails, with commuted
5622/// operands.
5623static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005624 TargetLowering::DAGCombinerInfo &DCI,
5625 const ARMSubtarget *Subtarget){
5626
5627 // Attempt to create vpaddl for this add.
5628 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5629 if (Result.getNode())
5630 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005631
Chris Lattnerd1980a52009-03-12 06:52:53 +00005632 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5633 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5634 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5635 if (Result.getNode()) return Result;
5636 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005637 return SDValue();
5638}
5639
Bob Wilson3d5792a2010-07-29 20:34:14 +00005640/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5641///
5642static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005643 TargetLowering::DAGCombinerInfo &DCI,
5644 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005645 SDValue N0 = N->getOperand(0);
5646 SDValue N1 = N->getOperand(1);
5647
5648 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005649 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005650 if (Result.getNode())
5651 return Result;
5652
5653 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005654 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005655}
5656
Chris Lattnerd1980a52009-03-12 06:52:53 +00005657/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005658///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005659static SDValue PerformSUBCombine(SDNode *N,
5660 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005661 SDValue N0 = N->getOperand(0);
5662 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005663
Chris Lattnerd1980a52009-03-12 06:52:53 +00005664 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5665 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5666 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5667 if (Result.getNode()) return Result;
5668 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005669
Chris Lattnerd1980a52009-03-12 06:52:53 +00005670 return SDValue();
5671}
5672
Evan Cheng463d3582011-03-31 19:38:48 +00005673/// PerformVMULCombine
5674/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5675/// special multiplier accumulator forwarding.
5676/// vmul d3, d0, d2
5677/// vmla d3, d1, d2
5678/// is faster than
5679/// vadd d3, d0, d1
5680/// vmul d3, d3, d2
5681static SDValue PerformVMULCombine(SDNode *N,
5682 TargetLowering::DAGCombinerInfo &DCI,
5683 const ARMSubtarget *Subtarget) {
5684 if (!Subtarget->hasVMLxForwarding())
5685 return SDValue();
5686
5687 SelectionDAG &DAG = DCI.DAG;
5688 SDValue N0 = N->getOperand(0);
5689 SDValue N1 = N->getOperand(1);
5690 unsigned Opcode = N0.getOpcode();
5691 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5692 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00005693 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00005694 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5695 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5696 return SDValue();
5697 std::swap(N0, N1);
5698 }
5699
5700 EVT VT = N->getValueType(0);
5701 DebugLoc DL = N->getDebugLoc();
5702 SDValue N00 = N0->getOperand(0);
5703 SDValue N01 = N0->getOperand(1);
5704 return DAG.getNode(Opcode, DL, VT,
5705 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5706 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5707}
5708
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005709static SDValue PerformMULCombine(SDNode *N,
5710 TargetLowering::DAGCombinerInfo &DCI,
5711 const ARMSubtarget *Subtarget) {
5712 SelectionDAG &DAG = DCI.DAG;
5713
5714 if (Subtarget->isThumb1Only())
5715 return SDValue();
5716
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005717 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5718 return SDValue();
5719
5720 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005721 if (VT.is64BitVector() || VT.is128BitVector())
5722 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005723 if (VT != MVT::i32)
5724 return SDValue();
5725
5726 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5727 if (!C)
5728 return SDValue();
5729
5730 uint64_t MulAmt = C->getZExtValue();
5731 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5732 ShiftAmt = ShiftAmt & (32 - 1);
5733 SDValue V = N->getOperand(0);
5734 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005735
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005736 SDValue Res;
5737 MulAmt >>= ShiftAmt;
5738 if (isPowerOf2_32(MulAmt - 1)) {
5739 // (mul x, 2^N + 1) => (add (shl x, N), x)
5740 Res = DAG.getNode(ISD::ADD, DL, VT,
5741 V, DAG.getNode(ISD::SHL, DL, VT,
5742 V, DAG.getConstant(Log2_32(MulAmt-1),
5743 MVT::i32)));
5744 } else if (isPowerOf2_32(MulAmt + 1)) {
5745 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5746 Res = DAG.getNode(ISD::SUB, DL, VT,
5747 DAG.getNode(ISD::SHL, DL, VT,
5748 V, DAG.getConstant(Log2_32(MulAmt+1),
5749 MVT::i32)),
5750 V);
5751 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005752 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005753
5754 if (ShiftAmt != 0)
5755 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5756 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005757
5758 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005759 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005760 return SDValue();
5761}
5762
Owen Anderson080c0922010-11-05 19:27:46 +00005763static SDValue PerformANDCombine(SDNode *N,
5764 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005765
Owen Anderson080c0922010-11-05 19:27:46 +00005766 // Attempt to use immediate-form VBIC
5767 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5768 DebugLoc dl = N->getDebugLoc();
5769 EVT VT = N->getValueType(0);
5770 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005771
Tanya Lattner0433b212011-04-07 15:24:20 +00005772 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5773 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005774
Owen Anderson080c0922010-11-05 19:27:46 +00005775 APInt SplatBits, SplatUndef;
5776 unsigned SplatBitSize;
5777 bool HasAnyUndefs;
5778 if (BVN &&
5779 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5780 if (SplatBitSize <= 64) {
5781 EVT VbicVT;
5782 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5783 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005784 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005785 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005786 if (Val.getNode()) {
5787 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005788 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005789 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005790 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005791 }
5792 }
5793 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005794
Owen Anderson080c0922010-11-05 19:27:46 +00005795 return SDValue();
5796}
5797
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005798/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5799static SDValue PerformORCombine(SDNode *N,
5800 TargetLowering::DAGCombinerInfo &DCI,
5801 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005802 // Attempt to use immediate-form VORR
5803 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5804 DebugLoc dl = N->getDebugLoc();
5805 EVT VT = N->getValueType(0);
5806 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005807
Tanya Lattner0433b212011-04-07 15:24:20 +00005808 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5809 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005810
Owen Anderson60f48702010-11-03 23:15:26 +00005811 APInt SplatBits, SplatUndef;
5812 unsigned SplatBitSize;
5813 bool HasAnyUndefs;
5814 if (BVN && Subtarget->hasNEON() &&
5815 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5816 if (SplatBitSize <= 64) {
5817 EVT VorrVT;
5818 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5819 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005820 DAG, VorrVT, VT.is128BitVector(),
5821 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005822 if (Val.getNode()) {
5823 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005824 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005825 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005826 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005827 }
5828 }
5829 }
5830
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005831 SDValue N0 = N->getOperand(0);
5832 if (N0.getOpcode() != ISD::AND)
5833 return SDValue();
5834 SDValue N1 = N->getOperand(1);
5835
5836 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5837 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5838 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5839 APInt SplatUndef;
5840 unsigned SplatBitSize;
5841 bool HasAnyUndefs;
5842
5843 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5844 APInt SplatBits0;
5845 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5846 HasAnyUndefs) && !HasAnyUndefs) {
5847 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5848 APInt SplatBits1;
5849 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5850 HasAnyUndefs) && !HasAnyUndefs &&
5851 SplatBits0 == ~SplatBits1) {
5852 // Canonicalize the vector type to make instruction selection simpler.
5853 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5854 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5855 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005856 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005857 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5858 }
5859 }
5860 }
5861
Jim Grosbach54238562010-07-17 03:30:54 +00005862 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5863 // reasonable.
5864
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005865 // BFI is only available on V6T2+
5866 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5867 return SDValue();
5868
Jim Grosbach54238562010-07-17 03:30:54 +00005869 DebugLoc DL = N->getDebugLoc();
5870 // 1) or (and A, mask), val => ARMbfi A, val, mask
5871 // iff (val & mask) == val
5872 //
5873 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5874 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005875 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005876 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005877 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005878 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005879
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005880 if (VT != MVT::i32)
5881 return SDValue();
5882
Evan Cheng30fb13f2010-12-13 20:32:54 +00005883 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005884
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005885 // The value and the mask need to be constants so we can verify this is
5886 // actually a bitfield set. If the mask is 0xffff, we can do better
5887 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005888 SDValue MaskOp = N0.getOperand(1);
5889 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5890 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005891 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005892 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005893 if (Mask == 0xffff)
5894 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005895 SDValue Res;
5896 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005897 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5898 if (N1C) {
5899 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005900 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005901 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005902
Evan Chenga9688c42010-12-11 04:11:38 +00005903 if (ARM::isBitFieldInvertedMask(Mask)) {
5904 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005905
Evan Cheng30fb13f2010-12-13 20:32:54 +00005906 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005907 DAG.getConstant(Val, MVT::i32),
5908 DAG.getConstant(Mask, MVT::i32));
5909
5910 // Do not add new nodes to DAG combiner worklist.
5911 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005912 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005913 }
Jim Grosbach54238562010-07-17 03:30:54 +00005914 } else if (N1.getOpcode() == ISD::AND) {
5915 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005916 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5917 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005918 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005919 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005920
Eric Christopher29aeed12011-03-26 01:21:03 +00005921 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5922 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005923 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005924 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005925 // The pack halfword instruction works better for masks that fit it,
5926 // so use that when it's available.
5927 if (Subtarget->hasT2ExtractPack() &&
5928 (Mask == 0xffff || Mask == 0xffff0000))
5929 return SDValue();
5930 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005931 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005932 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005933 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005934 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005935 DAG.getConstant(Mask, MVT::i32));
5936 // Do not add new nodes to DAG combiner worklist.
5937 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005938 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005939 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005940 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005941 // The pack halfword instruction works better for masks that fit it,
5942 // so use that when it's available.
5943 if (Subtarget->hasT2ExtractPack() &&
5944 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5945 return SDValue();
5946 // 2b
5947 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005948 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005949 DAG.getConstant(lsb, MVT::i32));
5950 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005951 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005952 // Do not add new nodes to DAG combiner worklist.
5953 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005954 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005955 }
5956 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005957
Evan Cheng30fb13f2010-12-13 20:32:54 +00005958 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5959 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5960 ARM::isBitFieldInvertedMask(~Mask)) {
5961 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5962 // where lsb(mask) == #shamt and masked bits of B are known zero.
5963 SDValue ShAmt = N00.getOperand(1);
5964 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5965 unsigned LSB = CountTrailingZeros_32(Mask);
5966 if (ShAmtC != LSB)
5967 return SDValue();
5968
5969 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5970 DAG.getConstant(~Mask, MVT::i32));
5971
5972 // Do not add new nodes to DAG combiner worklist.
5973 DCI.CombineTo(N, Res, false);
5974 }
5975
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005976 return SDValue();
5977}
5978
Evan Chengbf188ae2011-06-15 01:12:31 +00005979/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
5980/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00005981static SDValue PerformBFICombine(SDNode *N,
5982 TargetLowering::DAGCombinerInfo &DCI) {
5983 SDValue N1 = N->getOperand(1);
5984 if (N1.getOpcode() == ISD::AND) {
5985 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5986 if (!N11C)
5987 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00005988 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5989 unsigned LSB = CountTrailingZeros_32(~InvMask);
5990 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
5991 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00005992 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00005993 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00005994 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5995 N->getOperand(0), N1.getOperand(0),
5996 N->getOperand(2));
5997 }
5998 return SDValue();
5999}
6000
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006001/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
6002/// ARMISD::VMOVRRD.
6003static SDValue PerformVMOVRRDCombine(SDNode *N,
6004 TargetLowering::DAGCombinerInfo &DCI) {
6005 // vmovrrd(vmovdrr x, y) -> x,y
6006 SDValue InDouble = N->getOperand(0);
6007 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6008 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006009
6010 // vmovrrd(load f64) -> (load i32), (load i32)
6011 SDNode *InNode = InDouble.getNode();
6012 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6013 InNode->getValueType(0) == MVT::f64 &&
6014 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6015 !cast<LoadSDNode>(InNode)->isVolatile()) {
6016 // TODO: Should this be done for non-FrameIndex operands?
6017 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6018
6019 SelectionDAG &DAG = DCI.DAG;
6020 DebugLoc DL = LD->getDebugLoc();
6021 SDValue BasePtr = LD->getBasePtr();
6022 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6023 LD->getPointerInfo(), LD->isVolatile(),
6024 LD->isNonTemporal(), LD->getAlignment());
6025
6026 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6027 DAG.getConstant(4, MVT::i32));
6028 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6029 LD->getPointerInfo(), LD->isVolatile(),
6030 LD->isNonTemporal(),
6031 std::min(4U, LD->getAlignment() / 2));
6032
6033 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6034 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6035 DCI.RemoveFromWorklist(LD);
6036 DAG.DeleteNode(LD);
6037 return Result;
6038 }
6039
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006040 return SDValue();
6041}
6042
6043/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6044/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6045static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6046 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6047 SDValue Op0 = N->getOperand(0);
6048 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006049 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006050 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006051 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006052 Op1 = Op1.getOperand(0);
6053 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6054 Op0.getNode() == Op1.getNode() &&
6055 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006056 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006057 N->getValueType(0), Op0.getOperand(0));
6058 return SDValue();
6059}
6060
Bob Wilson31600902010-12-21 06:43:19 +00006061/// PerformSTORECombine - Target-specific dag combine xforms for
6062/// ISD::STORE.
6063static SDValue PerformSTORECombine(SDNode *N,
6064 TargetLowering::DAGCombinerInfo &DCI) {
6065 // Bitcast an i64 store extracted from a vector to f64.
6066 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6067 StoreSDNode *St = cast<StoreSDNode>(N);
6068 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006069 if (!ISD::isNormalStore(St) || St->isVolatile())
6070 return SDValue();
6071
6072 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6073 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6074 SelectionDAG &DAG = DCI.DAG;
6075 DebugLoc DL = St->getDebugLoc();
6076 SDValue BasePtr = St->getBasePtr();
6077 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6078 StVal.getNode()->getOperand(0), BasePtr,
6079 St->getPointerInfo(), St->isVolatile(),
6080 St->isNonTemporal(), St->getAlignment());
6081
6082 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6083 DAG.getConstant(4, MVT::i32));
6084 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6085 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6086 St->isNonTemporal(),
6087 std::min(4U, St->getAlignment() / 2));
6088 }
6089
6090 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006091 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6092 return SDValue();
6093
6094 SelectionDAG &DAG = DCI.DAG;
6095 DebugLoc dl = StVal.getDebugLoc();
6096 SDValue IntVec = StVal.getOperand(0);
6097 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6098 IntVec.getValueType().getVectorNumElements());
6099 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6100 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6101 Vec, StVal.getOperand(1));
6102 dl = N->getDebugLoc();
6103 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6104 // Make the DAGCombiner fold the bitcasts.
6105 DCI.AddToWorklist(Vec.getNode());
6106 DCI.AddToWorklist(ExtElt.getNode());
6107 DCI.AddToWorklist(V.getNode());
6108 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6109 St->getPointerInfo(), St->isVolatile(),
6110 St->isNonTemporal(), St->getAlignment(),
6111 St->getTBAAInfo());
6112}
6113
6114/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6115/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6116/// i64 vector to have f64 elements, since the value can then be loaded
6117/// directly into a VFP register.
6118static bool hasNormalLoadOperand(SDNode *N) {
6119 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6120 for (unsigned i = 0; i < NumElts; ++i) {
6121 SDNode *Elt = N->getOperand(i).getNode();
6122 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6123 return true;
6124 }
6125 return false;
6126}
6127
Bob Wilson75f02882010-09-17 22:59:05 +00006128/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6129/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006130static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6131 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006132 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6133 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6134 // into a pair of GPRs, which is fine when the value is used as a scalar,
6135 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006136 SelectionDAG &DAG = DCI.DAG;
6137 if (N->getNumOperands() == 2) {
6138 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6139 if (RV.getNode())
6140 return RV;
6141 }
Bob Wilson75f02882010-09-17 22:59:05 +00006142
Bob Wilson31600902010-12-21 06:43:19 +00006143 // Load i64 elements as f64 values so that type legalization does not split
6144 // them up into i32 values.
6145 EVT VT = N->getValueType(0);
6146 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6147 return SDValue();
6148 DebugLoc dl = N->getDebugLoc();
6149 SmallVector<SDValue, 8> Ops;
6150 unsigned NumElts = VT.getVectorNumElements();
6151 for (unsigned i = 0; i < NumElts; ++i) {
6152 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6153 Ops.push_back(V);
6154 // Make the DAGCombiner fold the bitcast.
6155 DCI.AddToWorklist(V.getNode());
6156 }
6157 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6158 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6159 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6160}
6161
6162/// PerformInsertEltCombine - Target-specific dag combine xforms for
6163/// ISD::INSERT_VECTOR_ELT.
6164static SDValue PerformInsertEltCombine(SDNode *N,
6165 TargetLowering::DAGCombinerInfo &DCI) {
6166 // Bitcast an i64 load inserted into a vector to f64.
6167 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6168 EVT VT = N->getValueType(0);
6169 SDNode *Elt = N->getOperand(1).getNode();
6170 if (VT.getVectorElementType() != MVT::i64 ||
6171 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6172 return SDValue();
6173
6174 SelectionDAG &DAG = DCI.DAG;
6175 DebugLoc dl = N->getDebugLoc();
6176 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6177 VT.getVectorNumElements());
6178 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6179 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6180 // Make the DAGCombiner fold the bitcasts.
6181 DCI.AddToWorklist(Vec.getNode());
6182 DCI.AddToWorklist(V.getNode());
6183 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6184 Vec, V, N->getOperand(2));
6185 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006186}
6187
Bob Wilsonf20700c2010-10-27 20:38:28 +00006188/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6189/// ISD::VECTOR_SHUFFLE.
6190static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6191 // The LLVM shufflevector instruction does not require the shuffle mask
6192 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6193 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6194 // operands do not match the mask length, they are extended by concatenating
6195 // them with undef vectors. That is probably the right thing for other
6196 // targets, but for NEON it is better to concatenate two double-register
6197 // size vector operands into a single quad-register size vector. Do that
6198 // transformation here:
6199 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6200 // shuffle(concat(v1, v2), undef)
6201 SDValue Op0 = N->getOperand(0);
6202 SDValue Op1 = N->getOperand(1);
6203 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6204 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6205 Op0.getNumOperands() != 2 ||
6206 Op1.getNumOperands() != 2)
6207 return SDValue();
6208 SDValue Concat0Op1 = Op0.getOperand(1);
6209 SDValue Concat1Op1 = Op1.getOperand(1);
6210 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6211 Concat1Op1.getOpcode() != ISD::UNDEF)
6212 return SDValue();
6213 // Skip the transformation if any of the types are illegal.
6214 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6215 EVT VT = N->getValueType(0);
6216 if (!TLI.isTypeLegal(VT) ||
6217 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6218 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6219 return SDValue();
6220
6221 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6222 Op0.getOperand(0), Op1.getOperand(0));
6223 // Translate the shuffle mask.
6224 SmallVector<int, 16> NewMask;
6225 unsigned NumElts = VT.getVectorNumElements();
6226 unsigned HalfElts = NumElts/2;
6227 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6228 for (unsigned n = 0; n < NumElts; ++n) {
6229 int MaskElt = SVN->getMaskElt(n);
6230 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006231 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006232 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006233 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006234 NewElt = HalfElts + MaskElt - NumElts;
6235 NewMask.push_back(NewElt);
6236 }
6237 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6238 DAG.getUNDEF(VT), NewMask.data());
6239}
6240
Bob Wilson1c3ef902011-02-07 17:43:21 +00006241/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6242/// NEON load/store intrinsics to merge base address updates.
6243static SDValue CombineBaseUpdate(SDNode *N,
6244 TargetLowering::DAGCombinerInfo &DCI) {
6245 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6246 return SDValue();
6247
6248 SelectionDAG &DAG = DCI.DAG;
6249 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6250 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6251 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6252 SDValue Addr = N->getOperand(AddrOpIdx);
6253
6254 // Search for a use of the address operand that is an increment.
6255 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6256 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6257 SDNode *User = *UI;
6258 if (User->getOpcode() != ISD::ADD ||
6259 UI.getUse().getResNo() != Addr.getResNo())
6260 continue;
6261
6262 // Check that the add is independent of the load/store. Otherwise, folding
6263 // it would create a cycle.
6264 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6265 continue;
6266
6267 // Find the new opcode for the updating load/store.
6268 bool isLoad = true;
6269 bool isLaneOp = false;
6270 unsigned NewOpc = 0;
6271 unsigned NumVecs = 0;
6272 if (isIntrinsic) {
6273 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6274 switch (IntNo) {
6275 default: assert(0 && "unexpected intrinsic for Neon base update");
6276 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6277 NumVecs = 1; break;
6278 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6279 NumVecs = 2; break;
6280 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6281 NumVecs = 3; break;
6282 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6283 NumVecs = 4; break;
6284 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6285 NumVecs = 2; isLaneOp = true; break;
6286 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6287 NumVecs = 3; isLaneOp = true; break;
6288 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6289 NumVecs = 4; isLaneOp = true; break;
6290 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6291 NumVecs = 1; isLoad = false; break;
6292 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6293 NumVecs = 2; isLoad = false; break;
6294 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6295 NumVecs = 3; isLoad = false; break;
6296 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6297 NumVecs = 4; isLoad = false; break;
6298 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6299 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6300 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6301 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6302 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6303 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6304 }
6305 } else {
6306 isLaneOp = true;
6307 switch (N->getOpcode()) {
6308 default: assert(0 && "unexpected opcode for Neon base update");
6309 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6310 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6311 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6312 }
6313 }
6314
6315 // Find the size of memory referenced by the load/store.
6316 EVT VecTy;
6317 if (isLoad)
6318 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006319 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006320 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6321 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6322 if (isLaneOp)
6323 NumBytes /= VecTy.getVectorNumElements();
6324
6325 // If the increment is a constant, it must match the memory ref size.
6326 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6327 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6328 uint64_t IncVal = CInc->getZExtValue();
6329 if (IncVal != NumBytes)
6330 continue;
6331 } else if (NumBytes >= 3 * 16) {
6332 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6333 // separate instructions that make it harder to use a non-constant update.
6334 continue;
6335 }
6336
6337 // Create the new updating load/store node.
6338 EVT Tys[6];
6339 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6340 unsigned n;
6341 for (n = 0; n < NumResultVecs; ++n)
6342 Tys[n] = VecTy;
6343 Tys[n++] = MVT::i32;
6344 Tys[n] = MVT::Other;
6345 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6346 SmallVector<SDValue, 8> Ops;
6347 Ops.push_back(N->getOperand(0)); // incoming chain
6348 Ops.push_back(N->getOperand(AddrOpIdx));
6349 Ops.push_back(Inc);
6350 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6351 Ops.push_back(N->getOperand(i));
6352 }
6353 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6354 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6355 Ops.data(), Ops.size(),
6356 MemInt->getMemoryVT(),
6357 MemInt->getMemOperand());
6358
6359 // Update the uses.
6360 std::vector<SDValue> NewResults;
6361 for (unsigned i = 0; i < NumResultVecs; ++i) {
6362 NewResults.push_back(SDValue(UpdN.getNode(), i));
6363 }
6364 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6365 DCI.CombineTo(N, NewResults);
6366 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6367
6368 break;
Owen Anderson76706012011-04-05 21:48:57 +00006369 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006370 return SDValue();
6371}
6372
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006373/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6374/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6375/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6376/// return true.
6377static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6378 SelectionDAG &DAG = DCI.DAG;
6379 EVT VT = N->getValueType(0);
6380 // vldN-dup instructions only support 64-bit vectors for N > 1.
6381 if (!VT.is64BitVector())
6382 return false;
6383
6384 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6385 SDNode *VLD = N->getOperand(0).getNode();
6386 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6387 return false;
6388 unsigned NumVecs = 0;
6389 unsigned NewOpc = 0;
6390 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6391 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6392 NumVecs = 2;
6393 NewOpc = ARMISD::VLD2DUP;
6394 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6395 NumVecs = 3;
6396 NewOpc = ARMISD::VLD3DUP;
6397 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6398 NumVecs = 4;
6399 NewOpc = ARMISD::VLD4DUP;
6400 } else {
6401 return false;
6402 }
6403
6404 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6405 // numbers match the load.
6406 unsigned VLDLaneNo =
6407 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6408 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6409 UI != UE; ++UI) {
6410 // Ignore uses of the chain result.
6411 if (UI.getUse().getResNo() == NumVecs)
6412 continue;
6413 SDNode *User = *UI;
6414 if (User->getOpcode() != ARMISD::VDUPLANE ||
6415 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6416 return false;
6417 }
6418
6419 // Create the vldN-dup node.
6420 EVT Tys[5];
6421 unsigned n;
6422 for (n = 0; n < NumVecs; ++n)
6423 Tys[n] = VT;
6424 Tys[n] = MVT::Other;
6425 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6426 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6427 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6428 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6429 Ops, 2, VLDMemInt->getMemoryVT(),
6430 VLDMemInt->getMemOperand());
6431
6432 // Update the uses.
6433 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6434 UI != UE; ++UI) {
6435 unsigned ResNo = UI.getUse().getResNo();
6436 // Ignore uses of the chain result.
6437 if (ResNo == NumVecs)
6438 continue;
6439 SDNode *User = *UI;
6440 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6441 }
6442
6443 // Now the vldN-lane intrinsic is dead except for its chain result.
6444 // Update uses of the chain.
6445 std::vector<SDValue> VLDDupResults;
6446 for (unsigned n = 0; n < NumVecs; ++n)
6447 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6448 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6449 DCI.CombineTo(VLD, VLDDupResults);
6450
6451 return true;
6452}
6453
Bob Wilson9e82bf12010-07-14 01:22:12 +00006454/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6455/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006456static SDValue PerformVDUPLANECombine(SDNode *N,
6457 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006458 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006459
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006460 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6461 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6462 if (CombineVLDDUP(N, DCI))
6463 return SDValue(N, 0);
6464
6465 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6466 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006467 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006468 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006469 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006470 return SDValue();
6471
6472 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6473 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6474 // The canonical VMOV for a zero vector uses a 32-bit element size.
6475 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6476 unsigned EltBits;
6477 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6478 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006479 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006480 if (EltSize > VT.getVectorElementType().getSizeInBits())
6481 return SDValue();
6482
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006483 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006484}
6485
Eric Christopherfa6f5912011-06-29 21:10:36 +00006486// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00006487// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
6488static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
6489{
Chad Rosier118c9a02011-06-28 17:26:57 +00006490 integerPart cN;
6491 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00006492 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
6493 I != E; I++) {
6494 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
6495 if (!C)
6496 return false;
6497
Eric Christopherfa6f5912011-06-29 21:10:36 +00006498 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00006499 APFloat APF = C->getValueAPF();
6500 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
6501 != APFloat::opOK || !isExact)
6502 return false;
6503
6504 c0 = (I == 0) ? cN : c0;
6505 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
6506 return false;
6507 }
6508 C = c0;
6509 return true;
6510}
6511
6512/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
6513/// can replace combinations of VMUL and VCVT (floating-point to integer)
6514/// when the VMUL has a constant operand that is a power of 2.
6515///
6516/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6517/// vmul.f32 d16, d17, d16
6518/// vcvt.s32.f32 d16, d16
6519/// becomes:
6520/// vcvt.s32.f32 d16, d16, #3
6521static SDValue PerformVCVTCombine(SDNode *N,
6522 TargetLowering::DAGCombinerInfo &DCI,
6523 const ARMSubtarget *Subtarget) {
6524 SelectionDAG &DAG = DCI.DAG;
6525 SDValue Op = N->getOperand(0);
6526
6527 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
6528 Op.getOpcode() != ISD::FMUL)
6529 return SDValue();
6530
6531 uint64_t C;
6532 SDValue N0 = Op->getOperand(0);
6533 SDValue ConstVec = Op->getOperand(1);
6534 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
6535
Eric Christopherfa6f5912011-06-29 21:10:36 +00006536 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00006537 !isConstVecPow2(ConstVec, isSigned, C))
6538 return SDValue();
6539
6540 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
6541 Intrinsic::arm_neon_vcvtfp2fxu;
6542 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6543 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006544 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00006545 DAG.getConstant(Log2_64(C), MVT::i32));
6546}
6547
6548/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
6549/// can replace combinations of VCVT (integer to floating-point) and VDIV
6550/// when the VDIV has a constant operand that is a power of 2.
6551///
6552/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6553/// vcvt.f32.s32 d16, d16
6554/// vdiv.f32 d16, d17, d16
6555/// becomes:
6556/// vcvt.f32.s32 d16, d16, #3
6557static SDValue PerformVDIVCombine(SDNode *N,
6558 TargetLowering::DAGCombinerInfo &DCI,
6559 const ARMSubtarget *Subtarget) {
6560 SelectionDAG &DAG = DCI.DAG;
6561 SDValue Op = N->getOperand(0);
6562 unsigned OpOpcode = Op.getNode()->getOpcode();
6563
6564 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
6565 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
6566 return SDValue();
6567
6568 uint64_t C;
6569 SDValue ConstVec = N->getOperand(1);
6570 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
6571
6572 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
6573 !isConstVecPow2(ConstVec, isSigned, C))
6574 return SDValue();
6575
Eric Christopherfa6f5912011-06-29 21:10:36 +00006576 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00006577 Intrinsic::arm_neon_vcvtfxu2fp;
6578 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6579 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006580 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00006581 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
6582}
6583
6584/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00006585/// operand of a vector shift operation, where all the elements of the
6586/// build_vector must have the same constant integer value.
6587static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6588 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006589 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006590 Op = Op.getOperand(0);
6591 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6592 APInt SplatBits, SplatUndef;
6593 unsigned SplatBitSize;
6594 bool HasAnyUndefs;
6595 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6596 HasAnyUndefs, ElementBits) ||
6597 SplatBitSize > ElementBits)
6598 return false;
6599 Cnt = SplatBits.getSExtValue();
6600 return true;
6601}
6602
6603/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6604/// operand of a vector shift left operation. That value must be in the range:
6605/// 0 <= Value < ElementBits for a left shift; or
6606/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006607static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006608 assert(VT.isVector() && "vector shift count is not a vector type");
6609 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6610 if (! getVShiftImm(Op, ElementBits, Cnt))
6611 return false;
6612 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6613}
6614
6615/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6616/// operand of a vector shift right operation. For a shift opcode, the value
6617/// is positive, but for an intrinsic the value count must be negative. The
6618/// absolute value must be in the range:
6619/// 1 <= |Value| <= ElementBits for a right shift; or
6620/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006621static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006622 int64_t &Cnt) {
6623 assert(VT.isVector() && "vector shift count is not a vector type");
6624 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6625 if (! getVShiftImm(Op, ElementBits, Cnt))
6626 return false;
6627 if (isIntrinsic)
6628 Cnt = -Cnt;
6629 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6630}
6631
6632/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6633static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6634 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6635 switch (IntNo) {
6636 default:
6637 // Don't do anything for most intrinsics.
6638 break;
6639
6640 // Vector shifts: check for immediate versions and lower them.
6641 // Note: This is done during DAG combining instead of DAG legalizing because
6642 // the build_vectors for 64-bit vector element shift counts are generally
6643 // not legal, and it is hard to see their values after they get legalized to
6644 // loads from a constant pool.
6645 case Intrinsic::arm_neon_vshifts:
6646 case Intrinsic::arm_neon_vshiftu:
6647 case Intrinsic::arm_neon_vshiftls:
6648 case Intrinsic::arm_neon_vshiftlu:
6649 case Intrinsic::arm_neon_vshiftn:
6650 case Intrinsic::arm_neon_vrshifts:
6651 case Intrinsic::arm_neon_vrshiftu:
6652 case Intrinsic::arm_neon_vrshiftn:
6653 case Intrinsic::arm_neon_vqshifts:
6654 case Intrinsic::arm_neon_vqshiftu:
6655 case Intrinsic::arm_neon_vqshiftsu:
6656 case Intrinsic::arm_neon_vqshiftns:
6657 case Intrinsic::arm_neon_vqshiftnu:
6658 case Intrinsic::arm_neon_vqshiftnsu:
6659 case Intrinsic::arm_neon_vqrshiftns:
6660 case Intrinsic::arm_neon_vqrshiftnu:
6661 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006662 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006663 int64_t Cnt;
6664 unsigned VShiftOpc = 0;
6665
6666 switch (IntNo) {
6667 case Intrinsic::arm_neon_vshifts:
6668 case Intrinsic::arm_neon_vshiftu:
6669 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6670 VShiftOpc = ARMISD::VSHL;
6671 break;
6672 }
6673 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6674 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6675 ARMISD::VSHRs : ARMISD::VSHRu);
6676 break;
6677 }
6678 return SDValue();
6679
6680 case Intrinsic::arm_neon_vshiftls:
6681 case Intrinsic::arm_neon_vshiftlu:
6682 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6683 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006684 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006685
6686 case Intrinsic::arm_neon_vrshifts:
6687 case Intrinsic::arm_neon_vrshiftu:
6688 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6689 break;
6690 return SDValue();
6691
6692 case Intrinsic::arm_neon_vqshifts:
6693 case Intrinsic::arm_neon_vqshiftu:
6694 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6695 break;
6696 return SDValue();
6697
6698 case Intrinsic::arm_neon_vqshiftsu:
6699 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6700 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006701 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006702
6703 case Intrinsic::arm_neon_vshiftn:
6704 case Intrinsic::arm_neon_vrshiftn:
6705 case Intrinsic::arm_neon_vqshiftns:
6706 case Intrinsic::arm_neon_vqshiftnu:
6707 case Intrinsic::arm_neon_vqshiftnsu:
6708 case Intrinsic::arm_neon_vqrshiftns:
6709 case Intrinsic::arm_neon_vqrshiftnu:
6710 case Intrinsic::arm_neon_vqrshiftnsu:
6711 // Narrowing shifts require an immediate right shift.
6712 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6713 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006714 llvm_unreachable("invalid shift count for narrowing vector shift "
6715 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006716
6717 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006718 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006719 }
6720
6721 switch (IntNo) {
6722 case Intrinsic::arm_neon_vshifts:
6723 case Intrinsic::arm_neon_vshiftu:
6724 // Opcode already set above.
6725 break;
6726 case Intrinsic::arm_neon_vshiftls:
6727 case Intrinsic::arm_neon_vshiftlu:
6728 if (Cnt == VT.getVectorElementType().getSizeInBits())
6729 VShiftOpc = ARMISD::VSHLLi;
6730 else
6731 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6732 ARMISD::VSHLLs : ARMISD::VSHLLu);
6733 break;
6734 case Intrinsic::arm_neon_vshiftn:
6735 VShiftOpc = ARMISD::VSHRN; break;
6736 case Intrinsic::arm_neon_vrshifts:
6737 VShiftOpc = ARMISD::VRSHRs; break;
6738 case Intrinsic::arm_neon_vrshiftu:
6739 VShiftOpc = ARMISD::VRSHRu; break;
6740 case Intrinsic::arm_neon_vrshiftn:
6741 VShiftOpc = ARMISD::VRSHRN; break;
6742 case Intrinsic::arm_neon_vqshifts:
6743 VShiftOpc = ARMISD::VQSHLs; break;
6744 case Intrinsic::arm_neon_vqshiftu:
6745 VShiftOpc = ARMISD::VQSHLu; break;
6746 case Intrinsic::arm_neon_vqshiftsu:
6747 VShiftOpc = ARMISD::VQSHLsu; break;
6748 case Intrinsic::arm_neon_vqshiftns:
6749 VShiftOpc = ARMISD::VQSHRNs; break;
6750 case Intrinsic::arm_neon_vqshiftnu:
6751 VShiftOpc = ARMISD::VQSHRNu; break;
6752 case Intrinsic::arm_neon_vqshiftnsu:
6753 VShiftOpc = ARMISD::VQSHRNsu; break;
6754 case Intrinsic::arm_neon_vqrshiftns:
6755 VShiftOpc = ARMISD::VQRSHRNs; break;
6756 case Intrinsic::arm_neon_vqrshiftnu:
6757 VShiftOpc = ARMISD::VQRSHRNu; break;
6758 case Intrinsic::arm_neon_vqrshiftnsu:
6759 VShiftOpc = ARMISD::VQRSHRNsu; break;
6760 }
6761
6762 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006763 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006764 }
6765
6766 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006767 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006768 int64_t Cnt;
6769 unsigned VShiftOpc = 0;
6770
6771 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6772 VShiftOpc = ARMISD::VSLI;
6773 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6774 VShiftOpc = ARMISD::VSRI;
6775 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006776 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006777 }
6778
6779 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6780 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006781 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006782 }
6783
6784 case Intrinsic::arm_neon_vqrshifts:
6785 case Intrinsic::arm_neon_vqrshiftu:
6786 // No immediate versions of these to check for.
6787 break;
6788 }
6789
6790 return SDValue();
6791}
6792
6793/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6794/// lowers them. As with the vector shift intrinsics, this is done during DAG
6795/// combining instead of DAG legalizing because the build_vectors for 64-bit
6796/// vector element shift counts are generally not legal, and it is hard to see
6797/// their values after they get legalized to loads from a constant pool.
6798static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6799 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006800 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006801
6802 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006803 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6804 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006805 return SDValue();
6806
6807 assert(ST->hasNEON() && "unexpected vector shift");
6808 int64_t Cnt;
6809
6810 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006811 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006812
6813 case ISD::SHL:
6814 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6815 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006816 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006817 break;
6818
6819 case ISD::SRA:
6820 case ISD::SRL:
6821 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6822 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6823 ARMISD::VSHRs : ARMISD::VSHRu);
6824 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006825 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006826 }
6827 }
6828 return SDValue();
6829}
6830
6831/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6832/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6833static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6834 const ARMSubtarget *ST) {
6835 SDValue N0 = N->getOperand(0);
6836
6837 // Check for sign- and zero-extensions of vector extract operations of 8-
6838 // and 16-bit vector elements. NEON supports these directly. They are
6839 // handled during DAG combining because type legalization will promote them
6840 // to 32-bit types and it is messy to recognize the operations after that.
6841 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6842 SDValue Vec = N0.getOperand(0);
6843 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006844 EVT VT = N->getValueType(0);
6845 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006846 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6847
Owen Anderson825b72b2009-08-11 20:47:22 +00006848 if (VT == MVT::i32 &&
6849 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006850 TLI.isTypeLegal(Vec.getValueType()) &&
6851 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006852
6853 unsigned Opc = 0;
6854 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006855 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006856 case ISD::SIGN_EXTEND:
6857 Opc = ARMISD::VGETLANEs;
6858 break;
6859 case ISD::ZERO_EXTEND:
6860 case ISD::ANY_EXTEND:
6861 Opc = ARMISD::VGETLANEu;
6862 break;
6863 }
6864 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6865 }
6866 }
6867
6868 return SDValue();
6869}
6870
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006871/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6872/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6873static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6874 const ARMSubtarget *ST) {
6875 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006876 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006877 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6878 // a NaN; only do the transformation when it matches that behavior.
6879
6880 // For now only do this when using NEON for FP operations; if using VFP, it
6881 // is not obvious that the benefit outweighs the cost of switching to the
6882 // NEON pipeline.
6883 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6884 N->getValueType(0) != MVT::f32)
6885 return SDValue();
6886
6887 SDValue CondLHS = N->getOperand(0);
6888 SDValue CondRHS = N->getOperand(1);
6889 SDValue LHS = N->getOperand(2);
6890 SDValue RHS = N->getOperand(3);
6891 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6892
6893 unsigned Opcode = 0;
6894 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006895 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006896 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006897 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006898 IsReversed = true ; // x CC y ? y : x
6899 } else {
6900 return SDValue();
6901 }
6902
Bob Wilsone742bb52010-02-24 22:15:53 +00006903 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006904 switch (CC) {
6905 default: break;
6906 case ISD::SETOLT:
6907 case ISD::SETOLE:
6908 case ISD::SETLT:
6909 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006910 case ISD::SETULT:
6911 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006912 // If LHS is NaN, an ordered comparison will be false and the result will
6913 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6914 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6915 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6916 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6917 break;
6918 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6919 // will return -0, so vmin can only be used for unsafe math or if one of
6920 // the operands is known to be nonzero.
6921 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6922 !UnsafeFPMath &&
6923 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6924 break;
6925 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006926 break;
6927
6928 case ISD::SETOGT:
6929 case ISD::SETOGE:
6930 case ISD::SETGT:
6931 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006932 case ISD::SETUGT:
6933 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006934 // If LHS is NaN, an ordered comparison will be false and the result will
6935 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6936 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6937 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6938 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6939 break;
6940 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6941 // will return +0, so vmax can only be used for unsafe math or if one of
6942 // the operands is known to be nonzero.
6943 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6944 !UnsafeFPMath &&
6945 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6946 break;
6947 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006948 break;
6949 }
6950
6951 if (!Opcode)
6952 return SDValue();
6953 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6954}
6955
Dan Gohman475871a2008-07-27 21:46:04 +00006956SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006957 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006958 switch (N->getOpcode()) {
6959 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00006960 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006961 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006962 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006963 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006964 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006965 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006966 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006967 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006968 case ISD::STORE: return PerformSTORECombine(N, DCI);
6969 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6970 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006971 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006972 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00006973 case ISD::FP_TO_SINT:
6974 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
6975 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006976 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006977 case ISD::SHL:
6978 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006979 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006980 case ISD::SIGN_EXTEND:
6981 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006982 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6983 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006984 case ARMISD::VLD2DUP:
6985 case ARMISD::VLD3DUP:
6986 case ARMISD::VLD4DUP:
6987 return CombineBaseUpdate(N, DCI);
6988 case ISD::INTRINSIC_VOID:
6989 case ISD::INTRINSIC_W_CHAIN:
6990 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6991 case Intrinsic::arm_neon_vld1:
6992 case Intrinsic::arm_neon_vld2:
6993 case Intrinsic::arm_neon_vld3:
6994 case Intrinsic::arm_neon_vld4:
6995 case Intrinsic::arm_neon_vld2lane:
6996 case Intrinsic::arm_neon_vld3lane:
6997 case Intrinsic::arm_neon_vld4lane:
6998 case Intrinsic::arm_neon_vst1:
6999 case Intrinsic::arm_neon_vst2:
7000 case Intrinsic::arm_neon_vst3:
7001 case Intrinsic::arm_neon_vst4:
7002 case Intrinsic::arm_neon_vst2lane:
7003 case Intrinsic::arm_neon_vst3lane:
7004 case Intrinsic::arm_neon_vst4lane:
7005 return CombineBaseUpdate(N, DCI);
7006 default: break;
7007 }
7008 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007009 }
Dan Gohman475871a2008-07-27 21:46:04 +00007010 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007011}
7012
Evan Cheng31959b12011-02-02 01:06:55 +00007013bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
7014 EVT VT) const {
7015 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
7016}
7017
Bill Wendlingaf566342009-08-15 21:21:19 +00007018bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00007019 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00007020 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00007021
7022 switch (VT.getSimpleVT().SimpleTy) {
7023 default:
7024 return false;
7025 case MVT::i8:
7026 case MVT::i16:
7027 case MVT::i32:
7028 return true;
7029 // FIXME: VLD1 etc with standard alignment is legal.
7030 }
7031}
7032
Evan Chenge6c835f2009-08-14 20:09:37 +00007033static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
7034 if (V < 0)
7035 return false;
7036
7037 unsigned Scale = 1;
7038 switch (VT.getSimpleVT().SimpleTy) {
7039 default: return false;
7040 case MVT::i1:
7041 case MVT::i8:
7042 // Scale == 1;
7043 break;
7044 case MVT::i16:
7045 // Scale == 2;
7046 Scale = 2;
7047 break;
7048 case MVT::i32:
7049 // Scale == 4;
7050 Scale = 4;
7051 break;
7052 }
7053
7054 if ((V & (Scale - 1)) != 0)
7055 return false;
7056 V /= Scale;
7057 return V == (V & ((1LL << 5) - 1));
7058}
7059
7060static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
7061 const ARMSubtarget *Subtarget) {
7062 bool isNeg = false;
7063 if (V < 0) {
7064 isNeg = true;
7065 V = - V;
7066 }
7067
7068 switch (VT.getSimpleVT().SimpleTy) {
7069 default: return false;
7070 case MVT::i1:
7071 case MVT::i8:
7072 case MVT::i16:
7073 case MVT::i32:
7074 // + imm12 or - imm8
7075 if (isNeg)
7076 return V == (V & ((1LL << 8) - 1));
7077 return V == (V & ((1LL << 12) - 1));
7078 case MVT::f32:
7079 case MVT::f64:
7080 // Same as ARM mode. FIXME: NEON?
7081 if (!Subtarget->hasVFP2())
7082 return false;
7083 if ((V & 3) != 0)
7084 return false;
7085 V >>= 2;
7086 return V == (V & ((1LL << 8) - 1));
7087 }
7088}
7089
Evan Chengb01fad62007-03-12 23:30:29 +00007090/// isLegalAddressImmediate - Return true if the integer value can be used
7091/// as the offset of the target addressing mode for load / store of the
7092/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00007093static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007094 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00007095 if (V == 0)
7096 return true;
7097
Evan Cheng65011532009-03-09 19:15:00 +00007098 if (!VT.isSimple())
7099 return false;
7100
Evan Chenge6c835f2009-08-14 20:09:37 +00007101 if (Subtarget->isThumb1Only())
7102 return isLegalT1AddressImmediate(V, VT);
7103 else if (Subtarget->isThumb2())
7104 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007105
Evan Chenge6c835f2009-08-14 20:09:37 +00007106 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007107 if (V < 0)
7108 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007109 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007110 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007111 case MVT::i1:
7112 case MVT::i8:
7113 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007114 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007115 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007116 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007117 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007118 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007119 case MVT::f32:
7120 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007121 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007122 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007123 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007124 return false;
7125 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007126 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007127 }
Evan Chenga8e29892007-01-19 07:51:42 +00007128}
7129
Evan Chenge6c835f2009-08-14 20:09:37 +00007130bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7131 EVT VT) const {
7132 int Scale = AM.Scale;
7133 if (Scale < 0)
7134 return false;
7135
7136 switch (VT.getSimpleVT().SimpleTy) {
7137 default: return false;
7138 case MVT::i1:
7139 case MVT::i8:
7140 case MVT::i16:
7141 case MVT::i32:
7142 if (Scale == 1)
7143 return true;
7144 // r + r << imm
7145 Scale = Scale & ~1;
7146 return Scale == 2 || Scale == 4 || Scale == 8;
7147 case MVT::i64:
7148 // r + r
7149 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7150 return true;
7151 return false;
7152 case MVT::isVoid:
7153 // Note, we allow "void" uses (basically, uses that aren't loads or
7154 // stores), because arm allows folding a scale into many arithmetic
7155 // operations. This should be made more precise and revisited later.
7156
7157 // Allow r << imm, but the imm has to be a multiple of two.
7158 if (Scale & 1) return false;
7159 return isPowerOf2_32(Scale);
7160 }
7161}
7162
Chris Lattner37caf8c2007-04-09 23:33:39 +00007163/// isLegalAddressingMode - Return true if the addressing mode represented
7164/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007165bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007166 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007167 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007168 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007169 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007170
Chris Lattner37caf8c2007-04-09 23:33:39 +00007171 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007172 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007173 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007174
Chris Lattner37caf8c2007-04-09 23:33:39 +00007175 switch (AM.Scale) {
7176 case 0: // no scale reg, must be "r+i" or "r", or "i".
7177 break;
7178 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007179 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007180 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007181 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007182 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007183 // ARM doesn't support any R+R*scale+imm addr modes.
7184 if (AM.BaseOffs)
7185 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007186
Bob Wilson2c7dab12009-04-08 17:55:28 +00007187 if (!VT.isSimple())
7188 return false;
7189
Evan Chenge6c835f2009-08-14 20:09:37 +00007190 if (Subtarget->isThumb2())
7191 return isLegalT2ScaledAddressingMode(AM, VT);
7192
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007193 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007194 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007195 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007196 case MVT::i1:
7197 case MVT::i8:
7198 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007199 if (Scale < 0) Scale = -Scale;
7200 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007201 return true;
7202 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007203 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007204 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007205 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007206 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007207 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007208 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007209 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007210
Owen Anderson825b72b2009-08-11 20:47:22 +00007211 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007212 // Note, we allow "void" uses (basically, uses that aren't loads or
7213 // stores), because arm allows folding a scale into many arithmetic
7214 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007215
Chris Lattner37caf8c2007-04-09 23:33:39 +00007216 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007217 if (Scale & 1) return false;
7218 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007219 }
7220 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007221 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007222 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007223}
7224
Evan Cheng77e47512009-11-11 19:05:52 +00007225/// isLegalICmpImmediate - Return true if the specified immediate is legal
7226/// icmp immediate, that is the target has icmp instructions which can compare
7227/// a register against the immediate without having to materialize the
7228/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007229bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007230 if (!Subtarget->isThumb())
7231 return ARM_AM::getSOImmVal(Imm) != -1;
7232 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007233 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007234 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007235}
7236
Dan Gohmancca82142011-05-03 00:46:49 +00007237/// isLegalAddImmediate - Return true if the specified immediate is legal
7238/// add immediate, that is the target has add instructions which can add
7239/// a register with the immediate without having to materialize the
7240/// immediate into a register.
7241bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7242 return ARM_AM::getSOImmVal(Imm) != -1;
7243}
7244
Owen Andersone50ed302009-08-10 22:56:29 +00007245static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007246 bool isSEXTLoad, SDValue &Base,
7247 SDValue &Offset, bool &isInc,
7248 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007249 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7250 return false;
7251
Owen Anderson825b72b2009-08-11 20:47:22 +00007252 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007253 // AddressingMode 3
7254 Base = Ptr->getOperand(0);
7255 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007256 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007257 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007258 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007259 isInc = false;
7260 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7261 return true;
7262 }
7263 }
7264 isInc = (Ptr->getOpcode() == ISD::ADD);
7265 Offset = Ptr->getOperand(1);
7266 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007267 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007268 // AddressingMode 2
7269 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007270 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007271 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007272 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007273 isInc = false;
7274 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7275 Base = Ptr->getOperand(0);
7276 return true;
7277 }
7278 }
7279
7280 if (Ptr->getOpcode() == ISD::ADD) {
7281 isInc = true;
7282 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
7283 if (ShOpcVal != ARM_AM::no_shift) {
7284 Base = Ptr->getOperand(1);
7285 Offset = Ptr->getOperand(0);
7286 } else {
7287 Base = Ptr->getOperand(0);
7288 Offset = Ptr->getOperand(1);
7289 }
7290 return true;
7291 }
7292
7293 isInc = (Ptr->getOpcode() == ISD::ADD);
7294 Base = Ptr->getOperand(0);
7295 Offset = Ptr->getOperand(1);
7296 return true;
7297 }
7298
Jim Grosbache5165492009-11-09 00:11:35 +00007299 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007300 return false;
7301}
7302
Owen Andersone50ed302009-08-10 22:56:29 +00007303static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007304 bool isSEXTLoad, SDValue &Base,
7305 SDValue &Offset, bool &isInc,
7306 SelectionDAG &DAG) {
7307 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7308 return false;
7309
7310 Base = Ptr->getOperand(0);
7311 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7312 int RHSC = (int)RHS->getZExtValue();
7313 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7314 assert(Ptr->getOpcode() == ISD::ADD);
7315 isInc = false;
7316 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7317 return true;
7318 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7319 isInc = Ptr->getOpcode() == ISD::ADD;
7320 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7321 return true;
7322 }
7323 }
7324
7325 return false;
7326}
7327
Evan Chenga8e29892007-01-19 07:51:42 +00007328/// getPreIndexedAddressParts - returns true by value, base pointer and
7329/// offset pointer and addressing mode by reference if the node's address
7330/// can be legally represented as pre-indexed load / store address.
7331bool
Dan Gohman475871a2008-07-27 21:46:04 +00007332ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7333 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007334 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007335 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007336 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007337 return false;
7338
Owen Andersone50ed302009-08-10 22:56:29 +00007339 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007340 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007341 bool isSEXTLoad = false;
7342 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7343 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007344 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007345 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7346 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7347 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007348 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007349 } else
7350 return false;
7351
7352 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007353 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007354 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007355 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7356 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007357 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007358 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007359 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007360 if (!isLegal)
7361 return false;
7362
7363 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7364 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007365}
7366
7367/// getPostIndexedAddressParts - returns true by value, base pointer and
7368/// offset pointer and addressing mode by reference if this node can be
7369/// combined with a load / store to form a post-indexed load / store.
7370bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007371 SDValue &Base,
7372 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007373 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007374 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007375 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007376 return false;
7377
Owen Andersone50ed302009-08-10 22:56:29 +00007378 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007379 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007380 bool isSEXTLoad = false;
7381 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007382 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007383 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007384 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7385 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007386 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007387 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007388 } else
7389 return false;
7390
7391 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007392 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007393 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007394 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007395 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007396 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007397 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7398 isInc, DAG);
7399 if (!isLegal)
7400 return false;
7401
Evan Cheng28dad2a2010-05-18 21:31:17 +00007402 if (Ptr != Base) {
7403 // Swap base ptr and offset to catch more post-index load / store when
7404 // it's legal. In Thumb2 mode, offset must be an immediate.
7405 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7406 !Subtarget->isThumb2())
7407 std::swap(Base, Offset);
7408
7409 // Post-indexed load / store update the base pointer.
7410 if (Ptr != Base)
7411 return false;
7412 }
7413
Evan Chenge88d5ce2009-07-02 07:28:31 +00007414 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7415 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007416}
7417
Dan Gohman475871a2008-07-27 21:46:04 +00007418void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007419 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007420 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007421 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007422 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007423 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007424 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007425 switch (Op.getOpcode()) {
7426 default: break;
7427 case ARMISD::CMOV: {
7428 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007429 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007430 if (KnownZero == 0 && KnownOne == 0) return;
7431
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007432 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007433 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7434 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007435 KnownZero &= KnownZeroRHS;
7436 KnownOne &= KnownOneRHS;
7437 return;
7438 }
7439 }
7440}
7441
7442//===----------------------------------------------------------------------===//
7443// ARM Inline Assembly Support
7444//===----------------------------------------------------------------------===//
7445
Evan Cheng55d42002011-01-08 01:24:27 +00007446bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7447 // Looking for "rev" which is V6+.
7448 if (!Subtarget->hasV6Ops())
7449 return false;
7450
7451 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7452 std::string AsmStr = IA->getAsmString();
7453 SmallVector<StringRef, 4> AsmPieces;
7454 SplitString(AsmStr, AsmPieces, ";\n");
7455
7456 switch (AsmPieces.size()) {
7457 default: return false;
7458 case 1:
7459 AsmStr = AsmPieces[0];
7460 AsmPieces.clear();
7461 SplitString(AsmStr, AsmPieces, " \t,");
7462
7463 // rev $0, $1
7464 if (AsmPieces.size() == 3 &&
7465 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7466 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7467 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7468 if (Ty && Ty->getBitWidth() == 32)
7469 return IntrinsicLowering::LowerToByteSwap(CI);
7470 }
7471 break;
7472 }
7473
7474 return false;
7475}
7476
Evan Chenga8e29892007-01-19 07:51:42 +00007477/// getConstraintType - Given a constraint letter, return the type of
7478/// constraint it is for this target.
7479ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007480ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7481 if (Constraint.size() == 1) {
7482 switch (Constraint[0]) {
7483 default: break;
7484 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007485 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00007486 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007487 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007488 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00007489 case 'j': return C_Other; // Constant for movw.
Chris Lattner4234f572007-03-25 02:14:49 +00007490 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007491 } else if (Constraint.size() == 2) {
7492 switch (Constraint[0]) {
7493 default: break;
7494 // All 'U+' constraints are addresses.
7495 case 'U': return C_Memory;
7496 }
Evan Chenga8e29892007-01-19 07:51:42 +00007497 }
Chris Lattner4234f572007-03-25 02:14:49 +00007498 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007499}
7500
John Thompson44ab89e2010-10-29 17:29:13 +00007501/// Examine constraint type and operand type and determine a weight value.
7502/// This object must already have been set up with the operand type
7503/// and the current alternative constraint selected.
7504TargetLowering::ConstraintWeight
7505ARMTargetLowering::getSingleConstraintMatchWeight(
7506 AsmOperandInfo &info, const char *constraint) const {
7507 ConstraintWeight weight = CW_Invalid;
7508 Value *CallOperandVal = info.CallOperandVal;
7509 // If we don't have a value, we can't do a match,
7510 // but allow it at the lowest weight.
7511 if (CallOperandVal == NULL)
7512 return CW_Default;
7513 const Type *type = CallOperandVal->getType();
7514 // Look at the constraint type.
7515 switch (*constraint) {
7516 default:
7517 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7518 break;
7519 case 'l':
7520 if (type->isIntegerTy()) {
7521 if (Subtarget->isThumb())
7522 weight = CW_SpecificReg;
7523 else
7524 weight = CW_Register;
7525 }
7526 break;
7527 case 'w':
7528 if (type->isFloatingPointTy())
7529 weight = CW_Register;
7530 break;
7531 }
7532 return weight;
7533}
7534
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007535typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7536RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007537ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007538 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007539 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007540 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007541 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007542 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007543 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007544 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007545 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007546 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007547 case 'h': // High regs or no regs.
7548 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007549 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007550 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007551 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007552 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007553 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007554 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007555 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007556 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007557 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007558 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007559 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007560 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007561 case 'x':
7562 if (VT == MVT::f32)
7563 return RCPair(0U, ARM::SPR_8RegisterClass);
7564 if (VT.getSizeInBits() == 64)
7565 return RCPair(0U, ARM::DPR_8RegisterClass);
7566 if (VT.getSizeInBits() == 128)
7567 return RCPair(0U, ARM::QPR_8RegisterClass);
7568 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007569 case 't':
7570 if (VT == MVT::f32)
7571 return RCPair(0U, ARM::SPRRegisterClass);
7572 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007573 }
7574 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007575 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007576 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007577
Evan Chenga8e29892007-01-19 07:51:42 +00007578 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7579}
7580
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007581/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7582/// vector. If it is invalid, don't add anything to Ops.
7583void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007584 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007585 std::vector<SDValue>&Ops,
7586 SelectionDAG &DAG) const {
7587 SDValue Result(0, 0);
7588
Eric Christopher100c8332011-06-02 23:16:42 +00007589 // Currently only support length 1 constraints.
7590 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007591
Eric Christopher100c8332011-06-02 23:16:42 +00007592 char ConstraintLetter = Constraint[0];
7593 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007594 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007595 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007596 case 'I': case 'J': case 'K': case 'L':
7597 case 'M': case 'N': case 'O':
7598 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7599 if (!C)
7600 return;
7601
7602 int64_t CVal64 = C->getSExtValue();
7603 int CVal = (int) CVal64;
7604 // None of these constraints allow values larger than 32 bits. Check
7605 // that the value fits in an int.
7606 if (CVal != CVal64)
7607 return;
7608
Eric Christopher100c8332011-06-02 23:16:42 +00007609 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007610 case 'j':
7611 // Constant suitable for movw, must be between 0 and
7612 // 65535.
7613 if (Subtarget->hasV6T2Ops())
7614 if (CVal >= 0 && CVal <= 65535)
7615 break;
7616 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007617 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007618 if (Subtarget->isThumb1Only()) {
7619 // This must be a constant between 0 and 255, for ADD
7620 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007621 if (CVal >= 0 && CVal <= 255)
7622 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007623 } else if (Subtarget->isThumb2()) {
7624 // A constant that can be used as an immediate value in a
7625 // data-processing instruction.
7626 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7627 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007628 } else {
7629 // A constant that can be used as an immediate value in a
7630 // data-processing instruction.
7631 if (ARM_AM::getSOImmVal(CVal) != -1)
7632 break;
7633 }
7634 return;
7635
7636 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007637 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007638 // This must be a constant between -255 and -1, for negated ADD
7639 // immediates. This can be used in GCC with an "n" modifier that
7640 // prints the negated value, for use with SUB instructions. It is
7641 // not useful otherwise but is implemented for compatibility.
7642 if (CVal >= -255 && CVal <= -1)
7643 break;
7644 } else {
7645 // This must be a constant between -4095 and 4095. It is not clear
7646 // what this constraint is intended for. Implemented for
7647 // compatibility with GCC.
7648 if (CVal >= -4095 && CVal <= 4095)
7649 break;
7650 }
7651 return;
7652
7653 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007654 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007655 // A 32-bit value where only one byte has a nonzero value. Exclude
7656 // zero to match GCC. This constraint is used by GCC internally for
7657 // constants that can be loaded with a move/shift combination.
7658 // It is not useful otherwise but is implemented for compatibility.
7659 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7660 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007661 } else if (Subtarget->isThumb2()) {
7662 // A constant whose bitwise inverse can be used as an immediate
7663 // value in a data-processing instruction. This can be used in GCC
7664 // with a "B" modifier that prints the inverted value, for use with
7665 // BIC and MVN instructions. It is not useful otherwise but is
7666 // implemented for compatibility.
7667 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7668 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007669 } else {
7670 // A constant whose bitwise inverse can be used as an immediate
7671 // value in a data-processing instruction. This can be used in GCC
7672 // with a "B" modifier that prints the inverted value, for use with
7673 // BIC and MVN instructions. It is not useful otherwise but is
7674 // implemented for compatibility.
7675 if (ARM_AM::getSOImmVal(~CVal) != -1)
7676 break;
7677 }
7678 return;
7679
7680 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007681 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007682 // This must be a constant between -7 and 7,
7683 // for 3-operand ADD/SUB immediate instructions.
7684 if (CVal >= -7 && CVal < 7)
7685 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007686 } else if (Subtarget->isThumb2()) {
7687 // A constant whose negation can be used as an immediate value in a
7688 // data-processing instruction. This can be used in GCC with an "n"
7689 // modifier that prints the negated value, for use with SUB
7690 // instructions. It is not useful otherwise but is implemented for
7691 // compatibility.
7692 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7693 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007694 } else {
7695 // A constant whose negation can be used as an immediate value in a
7696 // data-processing instruction. This can be used in GCC with an "n"
7697 // modifier that prints the negated value, for use with SUB
7698 // instructions. It is not useful otherwise but is implemented for
7699 // compatibility.
7700 if (ARM_AM::getSOImmVal(-CVal) != -1)
7701 break;
7702 }
7703 return;
7704
7705 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007706 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007707 // This must be a multiple of 4 between 0 and 1020, for
7708 // ADD sp + immediate.
7709 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7710 break;
7711 } else {
7712 // A power of two or a constant between 0 and 32. This is used in
7713 // GCC for the shift amount on shifted register operands, but it is
7714 // useful in general for any shift amounts.
7715 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7716 break;
7717 }
7718 return;
7719
7720 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007721 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007722 // This must be a constant between 0 and 31, for shift amounts.
7723 if (CVal >= 0 && CVal <= 31)
7724 break;
7725 }
7726 return;
7727
7728 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007729 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007730 // This must be a multiple of 4 between -508 and 508, for
7731 // ADD/SUB sp = sp + immediate.
7732 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7733 break;
7734 }
7735 return;
7736 }
7737 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7738 break;
7739 }
7740
7741 if (Result.getNode()) {
7742 Ops.push_back(Result);
7743 return;
7744 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007745 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007746}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007747
7748bool
7749ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7750 // The ARM target isn't yet aware of offsets.
7751 return false;
7752}
Evan Cheng39382422009-10-28 01:44:26 +00007753
7754int ARM::getVFPf32Imm(const APFloat &FPImm) {
7755 APInt Imm = FPImm.bitcastToAPInt();
7756 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7757 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7758 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7759
7760 // We can handle 4 bits of mantissa.
7761 // mantissa = (16+UInt(e:f:g:h))/16.
7762 if (Mantissa & 0x7ffff)
7763 return -1;
7764 Mantissa >>= 19;
7765 if ((Mantissa & 0xf) != Mantissa)
7766 return -1;
7767
7768 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7769 if (Exp < -3 || Exp > 4)
7770 return -1;
7771 Exp = ((Exp+3) & 0x7) ^ 4;
7772
7773 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7774}
7775
7776int ARM::getVFPf64Imm(const APFloat &FPImm) {
7777 APInt Imm = FPImm.bitcastToAPInt();
7778 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7779 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7780 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7781
7782 // We can handle 4 bits of mantissa.
7783 // mantissa = (16+UInt(e:f:g:h))/16.
7784 if (Mantissa & 0xffffffffffffLL)
7785 return -1;
7786 Mantissa >>= 48;
7787 if ((Mantissa & 0xf) != Mantissa)
7788 return -1;
7789
7790 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7791 if (Exp < -3 || Exp > 4)
7792 return -1;
7793 Exp = ((Exp+3) & 0x7) ^ 4;
7794
7795 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7796}
7797
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007798bool ARM::isBitFieldInvertedMask(unsigned v) {
7799 if (v == 0xffffffff)
7800 return 0;
7801 // there can be 1's on either or both "outsides", all the "inside"
7802 // bits must be 0's
7803 unsigned int lsb = 0, msb = 31;
7804 while (v & (1 << msb)) --msb;
7805 while (v & (1 << lsb)) ++lsb;
7806 for (unsigned int i = lsb; i <= msb; ++i) {
7807 if (v & (1 << i))
7808 return 0;
7809 }
7810 return 1;
7811}
7812
Evan Cheng39382422009-10-28 01:44:26 +00007813/// isFPImmLegal - Returns true if the target can instruction select the
7814/// specified FP immediate natively. If false, the legalizer will
7815/// materialize the FP immediate as a load from a constant pool.
7816bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7817 if (!Subtarget->hasVFP3())
7818 return false;
7819 if (VT == MVT::f32)
7820 return ARM::getVFPf32Imm(Imm) != -1;
7821 if (VT == MVT::f64)
7822 return ARM::getVFPf64Imm(Imm) != -1;
7823 return false;
7824}
Bob Wilson65ffec42010-09-21 17:56:22 +00007825
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007826/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007827/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7828/// specified in the intrinsic calls.
7829bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7830 const CallInst &I,
7831 unsigned Intrinsic) const {
7832 switch (Intrinsic) {
7833 case Intrinsic::arm_neon_vld1:
7834 case Intrinsic::arm_neon_vld2:
7835 case Intrinsic::arm_neon_vld3:
7836 case Intrinsic::arm_neon_vld4:
7837 case Intrinsic::arm_neon_vld2lane:
7838 case Intrinsic::arm_neon_vld3lane:
7839 case Intrinsic::arm_neon_vld4lane: {
7840 Info.opc = ISD::INTRINSIC_W_CHAIN;
7841 // Conservatively set memVT to the entire set of vectors loaded.
7842 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7843 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7844 Info.ptrVal = I.getArgOperand(0);
7845 Info.offset = 0;
7846 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7847 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7848 Info.vol = false; // volatile loads with NEON intrinsics not supported
7849 Info.readMem = true;
7850 Info.writeMem = false;
7851 return true;
7852 }
7853 case Intrinsic::arm_neon_vst1:
7854 case Intrinsic::arm_neon_vst2:
7855 case Intrinsic::arm_neon_vst3:
7856 case Intrinsic::arm_neon_vst4:
7857 case Intrinsic::arm_neon_vst2lane:
7858 case Intrinsic::arm_neon_vst3lane:
7859 case Intrinsic::arm_neon_vst4lane: {
7860 Info.opc = ISD::INTRINSIC_VOID;
7861 // Conservatively set memVT to the entire set of vectors stored.
7862 unsigned NumElts = 0;
7863 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7864 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7865 if (!ArgTy->isVectorTy())
7866 break;
7867 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7868 }
7869 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7870 Info.ptrVal = I.getArgOperand(0);
7871 Info.offset = 0;
7872 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7873 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7874 Info.vol = false; // volatile stores with NEON intrinsics not supported
7875 Info.readMem = false;
7876 Info.writeMem = true;
7877 return true;
7878 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007879 case Intrinsic::arm_strexd: {
7880 Info.opc = ISD::INTRINSIC_W_CHAIN;
7881 Info.memVT = MVT::i64;
7882 Info.ptrVal = I.getArgOperand(2);
7883 Info.offset = 0;
7884 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007885 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007886 Info.readMem = false;
7887 Info.writeMem = true;
7888 return true;
7889 }
7890 case Intrinsic::arm_ldrexd: {
7891 Info.opc = ISD::INTRINSIC_W_CHAIN;
7892 Info.memVT = MVT::i64;
7893 Info.ptrVal = I.getArgOperand(0);
7894 Info.offset = 0;
7895 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007896 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007897 Info.readMem = true;
7898 Info.writeMem = false;
7899 return true;
7900 }
Bob Wilson65ffec42010-09-21 17:56:22 +00007901 default:
7902 break;
7903 }
7904
7905 return false;
7906}