blob: a4ee03251383cc5c32176db040f61b48138c537e [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000017#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000026#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Cameron Zwaricha86686e2011-06-10 20:59:24 +000075namespace llvm {
76 class ARMCCState : public CCState {
77 public:
78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
80 LLVMContext &C, ParmContext PC)
81 : CCState(CC, isVarArg, MF, TM, locs, C) {
82 assert(((PC == Call) || (PC == Prologue)) &&
83 "ARMCCState users must specify whether their context is call"
84 "or prologue generation.");
85 CallOrPrologue = PC;
86 }
87 };
88}
89
Stuart Hastingsc7315872011-04-20 16:47:52 +000090// The APCS parameter registers.
91static const unsigned GPRArgRegs[] = {
92 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93};
94
Owen Andersone50ed302009-08-10 22:56:29 +000095void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
96 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000097 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
100 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 }
106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000109 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000110 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000111 if (ElemTy != MVT::i32) {
112 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
116 }
Owen Anderson70671842009-08-10 20:18:46 +0000117 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
118 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000119 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000120 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000121 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
122 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
125 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
126 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000127 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
128 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000129 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
130 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
131 setTruncStoreAction(VT.getSimpleVT(),
132 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000134 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135
136 // Promote all bit-wise operations.
137 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000139 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000146 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000147 }
Bob Wilson16330762009-09-16 00:17:28 +0000148
149 // Neon does not support vector divide/remainder operations.
150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Owen Andersone50ed302009-08-10 22:56:29 +0000158void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000159 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000161}
162
Owen Andersone50ed302009-08-10 22:56:29 +0000163void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000164 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000166}
167
Chris Lattnerf0144122009-07-28 03:13:23 +0000168static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000170 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000171
Chris Lattner80ec2792009-08-02 00:34:36 +0000172 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173}
174
Evan Chenga8e29892007-01-19 07:51:42 +0000175ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000176 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000178 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000179 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 // Uses VFP for Thumb libfuncs if available.
183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
184 // Single-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 // Double-precision floating-point arithmetic.
191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 // Single-precision comparisons.
197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 // Double-precision comparisons.
216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000224
Evan Chengb1df8f22007-04-27 08:15:43 +0000225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000233
Evan Chengb1df8f22007-04-27 08:15:43 +0000234 // Floating-point to integer conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000241
Evan Chengb1df8f22007-04-27 08:15:43 +0000242 // Conversions between floating types.
243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
245
246 // Integer to floating-point conversions.
247 // i64 conversions are done via library routines even when generating VFP
248 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000249 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
250 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256 }
257
Bob Wilson2f954612009-05-22 17:38:41 +0000258 // These libcalls are not available in 32-bit.
259 setLibcallName(RTLIB::SHL_I128, 0);
260 setLibcallName(RTLIB::SRL_I128, 0);
261 setLibcallName(RTLIB::SRA_I128, 0);
262
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000263 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000265 // RTABI chapter 4.1.2, Table 2
266 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
267 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
268 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
269 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
270 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
274
275 // Double-precision floating-point comparison helper functions
276 // RTABI chapter 4.1.2, Table 3
277 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
278 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
279 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
281 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
282 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
284 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
286 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
288 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
289 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
290 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
291 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
293 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
301
302 // Single-precision floating-point arithmetic helper functions
303 // RTABI chapter 4.1.2, Table 4
304 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
305 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
306 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
307 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
308 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
312
313 // Single-precision floating-point comparison helper functions
314 // RTABI chapter 4.1.2, Table 5
315 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
316 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
317 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
319 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
320 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
322 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
324 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
326 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
327 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
328 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
329 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
331 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
339
340 // Floating-point to integer conversions.
341 // RTABI chapter 4.1.2, Table 6
342 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
343 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
346 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
347 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
350 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
358
359 // Conversions between floating types.
360 // RTABI chapter 4.1.2, Table 7
361 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
362 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
363 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000365
366 // Integer to floating-point conversions.
367 // RTABI chapter 4.1.2, Table 8
368 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
369 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
370 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
371 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
372 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
373 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
374 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
375 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384
385 // Long long helper functions
386 // RTABI chapter 4.2, Table 9
387 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
388 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
389 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
390 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
391 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
392 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
393 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
399
400 // Integer division functions
401 // RTABI chapter 4.3.1
402 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
403 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
404 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
405 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
407 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
408 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000413 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000414
415 // Memory operations
416 // RTABI chapter 4.3.4
417 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
418 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
419 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000420 }
421
David Goodwinf1daf7d2009-07-08 23:10:31 +0000422 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000424 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000426 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000428 if (!Subtarget->isFPOnlySP())
429 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000430
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000432 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000433
434 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 addDRTypeForNEON(MVT::v2f32);
436 addDRTypeForNEON(MVT::v8i8);
437 addDRTypeForNEON(MVT::v4i16);
438 addDRTypeForNEON(MVT::v2i32);
439 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addQRTypeForNEON(MVT::v4f32);
442 addQRTypeForNEON(MVT::v2f64);
443 addQRTypeForNEON(MVT::v16i8);
444 addQRTypeForNEON(MVT::v8i16);
445 addQRTypeForNEON(MVT::v4i32);
446 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000447
Bob Wilson74dc72e2009-09-15 23:55:57 +0000448 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
449 // neither Neon nor VFP support any arithmetic operations on it.
450 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
451 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
452 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
453 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
454 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
455 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
456 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
457 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
458 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
459 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
460 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
461 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
462 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
463 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
464 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
465 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
467 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
468 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
469 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
470 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
471 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
472 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
473 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
474
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000475 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
476
Bob Wilson642b3292009-09-16 00:32:15 +0000477 // Neon does not support some operations on v1i64 and v2i64 types.
478 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000479 // Custom handling for some quad-vector types to detect VMULL.
480 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
481 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
482 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000483 // Custom handling for some vector types to avoid expensive expansions
484 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
485 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
486 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
487 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000488 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
489 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000490 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
491 // a destination type that is wider than the source.
492 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
493 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000494
Bob Wilson1c3ef902011-02-07 17:43:21 +0000495 setTargetDAGCombine(ISD::INTRINSIC_VOID);
496 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000497 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
498 setTargetDAGCombine(ISD::SHL);
499 setTargetDAGCombine(ISD::SRL);
500 setTargetDAGCombine(ISD::SRA);
501 setTargetDAGCombine(ISD::SIGN_EXTEND);
502 setTargetDAGCombine(ISD::ZERO_EXTEND);
503 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000504 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000505 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000506 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000507 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
508 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000509 setTargetDAGCombine(ISD::FP_TO_SINT);
510 setTargetDAGCombine(ISD::FP_TO_UINT);
511 setTargetDAGCombine(ISD::FDIV);
Bob Wilson5bafff32009-06-22 23:27:02 +0000512 }
513
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000514 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000515
516 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000518
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000519 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000521
Evan Chenga8e29892007-01-19 07:51:42 +0000522 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000523 if (!Subtarget->isThumb1Only()) {
524 for (unsigned im = (unsigned)ISD::PRE_INC;
525 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setIndexedLoadAction(im, MVT::i1, Legal);
527 setIndexedLoadAction(im, MVT::i8, Legal);
528 setIndexedLoadAction(im, MVT::i16, Legal);
529 setIndexedLoadAction(im, MVT::i32, Legal);
530 setIndexedStoreAction(im, MVT::i1, Legal);
531 setIndexedStoreAction(im, MVT::i8, Legal);
532 setIndexedStoreAction(im, MVT::i16, Legal);
533 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000534 }
Evan Chenga8e29892007-01-19 07:51:42 +0000535 }
536
537 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000538 setOperationAction(ISD::MUL, MVT::i64, Expand);
539 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000540 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
542 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000543 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000544 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
545 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000546 setOperationAction(ISD::MULHS, MVT::i32, Expand);
547
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000548 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000549 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000550 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::SRL, MVT::i64, Custom);
552 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000553
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);
Eli Friedman14648462011-07-27 22:21:52 +0000604 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000605 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
606 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000607 } else {
608 // Set them all for expansion, which will force libcalls.
609 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000610 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000611 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000612 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000613 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000614 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000615 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000616 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000617 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000618 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000619 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000620 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000621 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000622 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman069e2ed2011-08-26 02:59:24 +0000623 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
624 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000625 // Since the libcalls include locking, fold in the fences
626 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000627 }
Evan Chenga8e29892007-01-19 07:51:42 +0000628
Evan Cheng416941d2010-11-04 05:19:35 +0000629 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000630
Eli Friedmana2c6f452010-06-26 04:36:50 +0000631 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
632 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
634 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000635 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000637
Nate Begemand1fb5832010-08-03 21:31:55 +0000638 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000639 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
640 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000641 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000642 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
643 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000644
645 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000647 if (Subtarget->isTargetDarwin()) {
648 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
649 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000650 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000651 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000652 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000653
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::SETCC, MVT::i32, Expand);
655 setOperationAction(ISD::SETCC, MVT::f32, Expand);
656 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000657 setOperationAction(ISD::SELECT, MVT::i32, Custom);
658 setOperationAction(ISD::SELECT, MVT::f32, Custom);
659 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
661 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
662 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000663
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
665 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
666 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
667 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
668 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000669
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000670 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::FSIN, MVT::f64, Expand);
672 setOperationAction(ISD::FSIN, MVT::f32, Expand);
673 setOperationAction(ISD::FCOS, MVT::f32, Expand);
674 setOperationAction(ISD::FCOS, MVT::f64, Expand);
675 setOperationAction(ISD::FREM, MVT::f64, Expand);
676 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000677 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
679 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000680 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::FPOW, MVT::f64, Expand);
682 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000683
Cameron Zwarich33390842011-07-08 21:39:21 +0000684 setOperationAction(ISD::FMA, MVT::f64, Expand);
685 setOperationAction(ISD::FMA, MVT::f32, Expand);
686
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000687 // Various VFP goodness
688 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000689 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
690 if (Subtarget->hasVFP2()) {
691 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
692 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
693 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
694 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
695 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000696 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000697 if (!Subtarget->hasFP16()) {
698 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
699 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000700 }
Evan Cheng110cf482008-04-01 01:50:16 +0000701 }
Evan Chenga8e29892007-01-19 07:51:42 +0000702
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000703 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000704 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000705 setTargetDAGCombine(ISD::ADD);
706 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000707 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000708
Owen Anderson080c0922010-11-05 19:27:46 +0000709 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000710 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000711 if (Subtarget->hasNEON())
712 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000713
Evan Chenga8e29892007-01-19 07:51:42 +0000714 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000715
Evan Chengf7d87ee2010-05-21 00:43:17 +0000716 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
717 setSchedulingPreference(Sched::RegPressure);
718 else
719 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000720
Evan Cheng05219282011-01-06 06:52:41 +0000721 //// temporary - rewrite interface to use type
722 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000723
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000724 // On ARM arguments smaller than 4 bytes are extended, so all arguments
725 // are at least 4 bytes aligned.
726 setMinStackArgumentAlignment(4);
727
Evan Chengfff606d2010-09-24 19:07:23 +0000728 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000729
730 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000731}
732
Andrew Trick32cec0a2011-01-19 02:35:27 +0000733// FIXME: It might make sense to define the representative register class as the
734// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
735// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
736// SPR's representative would be DPR_VFP2. This should work well if register
737// pressure tracking were modified such that a register use would increment the
738// pressure of the register class's representative and all of it's super
739// classes' representatives transitively. We have not implemented this because
740// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000741// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000742// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000743std::pair<const TargetRegisterClass*, uint8_t>
744ARMTargetLowering::findRepresentativeClass(EVT VT) const{
745 const TargetRegisterClass *RRC = 0;
746 uint8_t Cost = 1;
747 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000748 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000749 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000750 // Use DPR as representative register class for all floating point
751 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
752 // the cost is 1 for both f32 and f64.
753 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000754 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000755 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000756 // When NEON is used for SP, only half of the register file is available
757 // because operations that define both SP and DP results will be constrained
758 // to the VFP2 class (D0-D15). We currently model this constraint prior to
759 // coalescing by double-counting the SP regs. See the FIXME above.
760 if (Subtarget->useNEONForSinglePrecisionFP())
761 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000762 break;
763 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
764 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000765 RRC = ARM::DPRRegisterClass;
766 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000767 break;
768 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000769 RRC = ARM::DPRRegisterClass;
770 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000771 break;
772 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000773 RRC = ARM::DPRRegisterClass;
774 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000775 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000776 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000777 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000778}
779
Evan Chenga8e29892007-01-19 07:51:42 +0000780const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
781 switch (Opcode) {
782 default: return 0;
783 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000784 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000785 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000786 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
787 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000788 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000789 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
790 case ARMISD::tCALL: return "ARMISD::tCALL";
791 case ARMISD::BRCOND: return "ARMISD::BRCOND";
792 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000793 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000794 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
795 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
796 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000797 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000798 case ARMISD::CMPFP: return "ARMISD::CMPFP";
799 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000800 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000801 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
802 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000803
Jim Grosbach3482c802010-01-18 19:58:49 +0000804 case ARMISD::RBIT: return "ARMISD::RBIT";
805
Bob Wilson76a312b2010-03-19 22:51:32 +0000806 case ARMISD::FTOSI: return "ARMISD::FTOSI";
807 case ARMISD::FTOUI: return "ARMISD::FTOUI";
808 case ARMISD::SITOF: return "ARMISD::SITOF";
809 case ARMISD::UITOF: return "ARMISD::UITOF";
810
Evan Chenga8e29892007-01-19 07:51:42 +0000811 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
812 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
813 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000814
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000815 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
816 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000817
Evan Chengc5942082009-10-28 06:55:03 +0000818 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
819 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000820 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000821
Dale Johannesen51e28e62010-06-03 21:09:53 +0000822 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000823
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000824 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000825
Evan Cheng86198642009-08-07 00:34:42 +0000826 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
827
Jim Grosbach3728e962009-12-10 00:11:09 +0000828 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000829 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000830
Evan Chengdfed19f2010-11-03 06:34:55 +0000831 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
832
Bob Wilson5bafff32009-06-22 23:27:02 +0000833 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000834 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000835 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000836 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
837 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000838 case ARMISD::VCGEU: return "ARMISD::VCGEU";
839 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000840 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
841 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000842 case ARMISD::VCGTU: return "ARMISD::VCGTU";
843 case ARMISD::VTST: return "ARMISD::VTST";
844
845 case ARMISD::VSHL: return "ARMISD::VSHL";
846 case ARMISD::VSHRs: return "ARMISD::VSHRs";
847 case ARMISD::VSHRu: return "ARMISD::VSHRu";
848 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
849 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
850 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
851 case ARMISD::VSHRN: return "ARMISD::VSHRN";
852 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
853 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
854 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
855 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
856 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
857 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
858 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
859 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
860 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
861 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
862 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
863 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
864 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
865 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000866 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000867 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000868 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000869 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000870 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000871 case ARMISD::VREV64: return "ARMISD::VREV64";
872 case ARMISD::VREV32: return "ARMISD::VREV32";
873 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000874 case ARMISD::VZIP: return "ARMISD::VZIP";
875 case ARMISD::VUZP: return "ARMISD::VUZP";
876 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000877 case ARMISD::VTBL1: return "ARMISD::VTBL1";
878 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000879 case ARMISD::VMULLs: return "ARMISD::VMULLs";
880 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000881 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000882 case ARMISD::FMAX: return "ARMISD::FMAX";
883 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000884 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000885 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
886 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000887 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000888 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
889 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
890 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000891 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
892 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
893 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
894 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
895 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
896 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
897 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
898 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
899 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
900 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
901 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
902 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
903 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
904 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
905 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
906 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
907 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000908 }
909}
910
Evan Cheng06b666c2010-05-15 02:18:07 +0000911/// getRegClassFor - Return the register class that should be used for the
912/// specified value type.
913TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
914 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
915 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
916 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000917 if (Subtarget->hasNEON()) {
918 if (VT == MVT::v4i64)
919 return ARM::QQPRRegisterClass;
920 else if (VT == MVT::v8i64)
921 return ARM::QQQQPRRegisterClass;
922 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000923 return TargetLowering::getRegClassFor(VT);
924}
925
Eric Christopherab695882010-07-21 22:26:11 +0000926// Create a fast isel object.
927FastISel *
928ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
929 return ARM::createFastISel(funcInfo);
930}
931
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000932/// getMaximalGlobalOffset - Returns the maximal possible offset which can
933/// be used for loads / stores from the global.
934unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
935 return (Subtarget->isThumb1Only() ? 127 : 4095);
936}
937
Evan Cheng1cc39842010-05-20 23:26:43 +0000938Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000939 unsigned NumVals = N->getNumValues();
940 if (!NumVals)
941 return Sched::RegPressure;
942
943 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000944 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000945 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000946 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000947 if (VT.isFloatingPoint() || VT.isVector())
948 return Sched::Latency;
949 }
Evan Chengc10f5432010-05-28 23:25:23 +0000950
951 if (!N->isMachineOpcode())
952 return Sched::RegPressure;
953
954 // Load are scheduled for latency even if there instruction itinerary
955 // is not available.
956 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000957 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000958
Evan Chenge837dea2011-06-28 19:10:37 +0000959 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +0000960 return Sched::RegPressure;
961 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +0000962 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000963 return Sched::Latency;
964
Evan Cheng1cc39842010-05-20 23:26:43 +0000965 return Sched::RegPressure;
966}
967
Evan Chenga8e29892007-01-19 07:51:42 +0000968//===----------------------------------------------------------------------===//
969// Lowering Code
970//===----------------------------------------------------------------------===//
971
Evan Chenga8e29892007-01-19 07:51:42 +0000972/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
973static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
974 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000975 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000976 case ISD::SETNE: return ARMCC::NE;
977 case ISD::SETEQ: return ARMCC::EQ;
978 case ISD::SETGT: return ARMCC::GT;
979 case ISD::SETGE: return ARMCC::GE;
980 case ISD::SETLT: return ARMCC::LT;
981 case ISD::SETLE: return ARMCC::LE;
982 case ISD::SETUGT: return ARMCC::HI;
983 case ISD::SETUGE: return ARMCC::HS;
984 case ISD::SETULT: return ARMCC::LO;
985 case ISD::SETULE: return ARMCC::LS;
986 }
987}
988
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000989/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
990static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000991 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000992 CondCode2 = ARMCC::AL;
993 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000994 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000995 case ISD::SETEQ:
996 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
997 case ISD::SETGT:
998 case ISD::SETOGT: CondCode = ARMCC::GT; break;
999 case ISD::SETGE:
1000 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1001 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001002 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001003 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1004 case ISD::SETO: CondCode = ARMCC::VC; break;
1005 case ISD::SETUO: CondCode = ARMCC::VS; break;
1006 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1007 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1008 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1009 case ISD::SETLT:
1010 case ISD::SETULT: CondCode = ARMCC::LT; break;
1011 case ISD::SETLE:
1012 case ISD::SETULE: CondCode = ARMCC::LE; break;
1013 case ISD::SETNE:
1014 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1015 }
Evan Chenga8e29892007-01-19 07:51:42 +00001016}
1017
Bob Wilson1f595bb2009-04-17 19:07:39 +00001018//===----------------------------------------------------------------------===//
1019// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001020//===----------------------------------------------------------------------===//
1021
1022#include "ARMGenCallingConv.inc"
1023
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001024/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1025/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001026CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001027 bool Return,
1028 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001029 switch (CC) {
1030 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001031 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001032 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001033 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001034 if (!Subtarget->isAAPCS_ABI())
1035 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1036 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1037 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1038 }
1039 // Fallthrough
1040 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001041 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001042 if (!Subtarget->isAAPCS_ABI())
1043 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1044 else if (Subtarget->hasVFP2() &&
1045 FloatABIType == FloatABI::Hard && !isVarArg)
1046 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1047 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1048 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001049 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001050 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001051 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001052 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001053 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001054 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001055 }
1056}
1057
Dan Gohman98ca4f22009-08-05 01:29:28 +00001058/// LowerCallResult - Lower the result values of a call into the
1059/// appropriate copies out of appropriate physical registers.
1060SDValue
1061ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001062 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001063 const SmallVectorImpl<ISD::InputArg> &Ins,
1064 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001065 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001066
Bob Wilson1f595bb2009-04-17 19:07:39 +00001067 // Assign locations to each value returned by this call.
1068 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001069 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1070 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001071 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001072 CCAssignFnForNode(CallConv, /* Return*/ true,
1073 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001074
1075 // Copy all of the result registers out of their specified physreg.
1076 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1077 CCValAssign VA = RVLocs[i];
1078
Bob Wilson80915242009-04-25 00:33:20 +00001079 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001080 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001081 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001083 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001084 Chain = Lo.getValue(1);
1085 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001087 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001088 InFlag);
1089 Chain = Hi.getValue(1);
1090 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001091 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001092
Owen Anderson825b72b2009-08-11 20:47:22 +00001093 if (VA.getLocVT() == MVT::v2f64) {
1094 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1095 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1096 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001097
1098 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001099 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001100 Chain = Lo.getValue(1);
1101 InFlag = Lo.getValue(2);
1102 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001103 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001104 Chain = Hi.getValue(1);
1105 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001106 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1108 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001109 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001110 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001111 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1112 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001113 Chain = Val.getValue(1);
1114 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115 }
Bob Wilson80915242009-04-25 00:33:20 +00001116
1117 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001118 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001119 case CCValAssign::Full: break;
1120 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001121 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001122 break;
1123 }
1124
Dan Gohman98ca4f22009-08-05 01:29:28 +00001125 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001126 }
1127
Dan Gohman98ca4f22009-08-05 01:29:28 +00001128 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129}
1130
Bob Wilsondee46d72009-04-17 20:35:10 +00001131/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001133ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1134 SDValue StackPtr, SDValue Arg,
1135 DebugLoc dl, SelectionDAG &DAG,
1136 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001137 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001138 unsigned LocMemOffset = VA.getLocMemOffset();
1139 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1140 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001142 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001143 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001144}
1145
Dan Gohman98ca4f22009-08-05 01:29:28 +00001146void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001147 SDValue Chain, SDValue &Arg,
1148 RegsToPassVector &RegsToPass,
1149 CCValAssign &VA, CCValAssign &NextVA,
1150 SDValue &StackPtr,
1151 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001152 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001153
Jim Grosbache5165492009-11-09 00:11:35 +00001154 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001155 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001156 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1157
1158 if (NextVA.isRegLoc())
1159 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1160 else {
1161 assert(NextVA.isMemLoc());
1162 if (StackPtr.getNode() == 0)
1163 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1164
Dan Gohman98ca4f22009-08-05 01:29:28 +00001165 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1166 dl, DAG, NextVA,
1167 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001168 }
1169}
1170
Dan Gohman98ca4f22009-08-05 01:29:28 +00001171/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001172/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1173/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001174SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001175ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001176 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001177 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001178 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001179 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 const SmallVectorImpl<ISD::InputArg> &Ins,
1181 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001182 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001183 MachineFunction &MF = DAG.getMachineFunction();
1184 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1185 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001186 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001187 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001188 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001189 if (isTailCall) {
1190 // Check if it's really possible to do a tail call.
1191 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1192 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001193 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001194 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1195 // detected sibcalls.
1196 if (isTailCall) {
1197 ++NumTailCalls;
1198 IsSibCall = true;
1199 }
1200 }
Evan Chenga8e29892007-01-19 07:51:42 +00001201
Bob Wilson1f595bb2009-04-17 19:07:39 +00001202 // Analyze operands of the call, assigning locations to each operand.
1203 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001204 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1205 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001207 CCAssignFnForNode(CallConv, /* Return*/ false,
1208 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001209
Bob Wilson1f595bb2009-04-17 19:07:39 +00001210 // Get a count of how many bytes are to be pushed on the stack.
1211 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001212
Dale Johannesen51e28e62010-06-03 21:09:53 +00001213 // For tail calls, memory operands are available in our caller's stack.
1214 if (IsSibCall)
1215 NumBytes = 0;
1216
Evan Chenga8e29892007-01-19 07:51:42 +00001217 // Adjust the stack pointer for the new arguments...
1218 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001219 if (!IsSibCall)
1220 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001221
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001222 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001223
Bob Wilson5bafff32009-06-22 23:27:02 +00001224 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001225 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001226
Bob Wilson1f595bb2009-04-17 19:07:39 +00001227 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001228 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001229 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1230 i != e;
1231 ++i, ++realArgIdx) {
1232 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001233 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001234 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001235 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001236
Bob Wilson1f595bb2009-04-17 19:07:39 +00001237 // Promote the value if needed.
1238 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001239 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001240 case CCValAssign::Full: break;
1241 case CCValAssign::SExt:
1242 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1243 break;
1244 case CCValAssign::ZExt:
1245 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1246 break;
1247 case CCValAssign::AExt:
1248 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1249 break;
1250 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001251 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001253 }
1254
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001255 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 if (VA.getLocVT() == MVT::v2f64) {
1258 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1259 DAG.getConstant(0, MVT::i32));
1260 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1261 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262
Dan Gohman98ca4f22009-08-05 01:29:28 +00001263 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001264 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1265
1266 VA = ArgLocs[++i]; // skip ahead to next loc
1267 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001268 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001269 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1270 } else {
1271 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001272
Dan Gohman98ca4f22009-08-05 01:29:28 +00001273 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1274 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001275 }
1276 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001277 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001278 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279 }
1280 } else if (VA.isRegLoc()) {
1281 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001282 } else if (isByVal) {
1283 assert(VA.isMemLoc());
1284 unsigned offset = 0;
1285
1286 // True if this byval aggregate will be split between registers
1287 // and memory.
1288 if (CCInfo.isFirstByValRegValid()) {
1289 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1290 unsigned int i, j;
1291 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1292 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1293 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1294 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1295 MachinePointerInfo(),
1296 false, false, 0);
1297 MemOpChains.push_back(Load.getValue(1));
1298 RegsToPass.push_back(std::make_pair(j, Load));
1299 }
1300 offset = ARM::R4 - CCInfo.getFirstByValReg();
1301 CCInfo.clearFirstByValReg();
1302 }
1303
1304 unsigned LocMemOffset = VA.getLocMemOffset();
1305 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1306 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1307 StkPtrOff);
1308 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1309 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1310 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1311 MVT::i32);
1312 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1313 Flags.getByValAlign(),
1314 /*isVolatile=*/false,
1315 /*AlwaysInline=*/false,
1316 MachinePointerInfo(0),
1317 MachinePointerInfo(0)));
1318
1319 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001320 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001321
Dan Gohman98ca4f22009-08-05 01:29:28 +00001322 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1323 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001324 }
Evan Chenga8e29892007-01-19 07:51:42 +00001325 }
1326
1327 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001328 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001329 &MemOpChains[0], MemOpChains.size());
1330
1331 // Build a sequence of copy-to-reg nodes chained together with token chain
1332 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001333 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001334 // Tail call byval lowering might overwrite argument registers so in case of
1335 // tail call optimization the copies to registers are lowered later.
1336 if (!isTailCall)
1337 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1338 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1339 RegsToPass[i].second, InFlag);
1340 InFlag = Chain.getValue(1);
1341 }
Evan Chenga8e29892007-01-19 07:51:42 +00001342
Dale Johannesen51e28e62010-06-03 21:09:53 +00001343 // For tail calls lower the arguments to the 'real' stack slot.
1344 if (isTailCall) {
1345 // Force all the incoming stack arguments to be loaded from the stack
1346 // before any new outgoing arguments are stored to the stack, because the
1347 // outgoing stack slots may alias the incoming argument stack slots, and
1348 // the alias isn't otherwise explicit. This is slightly more conservative
1349 // than necessary, because it means that each store effectively depends
1350 // on every argument instead of just those arguments it would clobber.
1351
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001352 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001353 InFlag = SDValue();
1354 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1355 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1356 RegsToPass[i].second, InFlag);
1357 InFlag = Chain.getValue(1);
1358 }
1359 InFlag =SDValue();
1360 }
1361
Bill Wendling056292f2008-09-16 21:48:12 +00001362 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1363 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1364 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001365 bool isDirect = false;
1366 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001367 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001368 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001369
1370 if (EnableARMLongCalls) {
1371 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1372 && "long-calls with non-static relocation model!");
1373 // Handle a global address or an external symbol. If it's not one of
1374 // those, the target's already in a register, so we don't need to do
1375 // anything extra.
1376 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001377 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001378 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001379 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001380 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1381 ARMPCLabelIndex,
1382 ARMCP::CPValue, 0);
1383 // Get the address of the callee into a register
1384 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1385 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1386 Callee = DAG.getLoad(getPointerTy(), dl,
1387 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001388 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001389 false, false, 0);
1390 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1391 const char *Sym = S->getSymbol();
1392
1393 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001394 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001395 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1396 Sym, ARMPCLabelIndex, 0);
1397 // Get the address of the callee into a register
1398 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1399 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1400 Callee = DAG.getLoad(getPointerTy(), dl,
1401 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001402 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001403 false, false, 0);
1404 }
1405 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001406 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001407 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001408 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001409 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001410 getTargetMachine().getRelocationModel() != Reloc::Static;
1411 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001412 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001413 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001414 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001415 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001416 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001417 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001418 ARMPCLabelIndex,
1419 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001420 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001421 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001422 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001423 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001424 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001425 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001426 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001427 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001428 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001429 } else {
1430 // On ELF targets for PIC code, direct calls should go through the PLT
1431 unsigned OpFlags = 0;
1432 if (Subtarget->isTargetELF() &&
1433 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1434 OpFlags = ARMII::MO_PLT;
1435 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1436 }
Bill Wendling056292f2008-09-16 21:48:12 +00001437 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001438 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001439 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001440 getTargetMachine().getRelocationModel() != Reloc::Static;
1441 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001442 // tBX takes a register source operand.
1443 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001444 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001445 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001446 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001447 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001448 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001449 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001450 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001451 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001452 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001453 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001454 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001455 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001456 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001457 } else {
1458 unsigned OpFlags = 0;
1459 // On ELF targets for PIC code, direct calls should go through the PLT
1460 if (Subtarget->isTargetELF() &&
1461 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1462 OpFlags = ARMII::MO_PLT;
1463 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1464 }
Evan Chenga8e29892007-01-19 07:51:42 +00001465 }
1466
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001467 // FIXME: handle tail calls differently.
1468 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001469 if (Subtarget->isThumb()) {
1470 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001471 CallOpc = ARMISD::CALL_NOLINK;
1472 else
1473 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1474 } else {
1475 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001476 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1477 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001478 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001479
Dan Gohman475871a2008-07-27 21:46:04 +00001480 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001481 Ops.push_back(Chain);
1482 Ops.push_back(Callee);
1483
1484 // Add argument registers to the end of the list so that they are known live
1485 // into the call.
1486 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1487 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1488 RegsToPass[i].second.getValueType()));
1489
Gabor Greifba36cb52008-08-28 21:40:38 +00001490 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001491 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001492
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001493 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001494 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001495 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001496
Duncan Sands4bdcb612008-07-02 17:40:58 +00001497 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001498 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001499 InFlag = Chain.getValue(1);
1500
Chris Lattnere563bbc2008-10-11 22:08:30 +00001501 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1502 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001503 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001504 InFlag = Chain.getValue(1);
1505
Bob Wilson1f595bb2009-04-17 19:07:39 +00001506 // Handle result values, copying them out of physregs into vregs that we
1507 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1509 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001510}
1511
Stuart Hastingsf222e592011-02-28 17:17:53 +00001512/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001513/// on the stack. Remember the next parameter register to allocate,
1514/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001515/// this.
1516void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001517llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1518 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1519 assert((State->getCallOrPrologue() == Prologue ||
1520 State->getCallOrPrologue() == Call) &&
1521 "unhandled ParmContext");
1522 if ((!State->isFirstByValRegValid()) &&
1523 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1524 State->setFirstByValReg(reg);
1525 // At a call site, a byval parameter that is split between
1526 // registers and memory needs its size truncated here. In a
1527 // function prologue, such byval parameters are reassembled in
1528 // memory, and are not truncated.
1529 if (State->getCallOrPrologue() == Call) {
1530 unsigned excess = 4 * (ARM::R4 - reg);
1531 assert(size >= excess && "expected larger existing stack allocation");
1532 size -= excess;
1533 }
1534 }
1535 // Confiscate any remaining parameter registers to preclude their
1536 // assignment to subsequent parameters.
1537 while (State->AllocateReg(GPRArgRegs, 4))
1538 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001539}
1540
Dale Johannesen51e28e62010-06-03 21:09:53 +00001541/// MatchingStackOffset - Return true if the given stack call argument is
1542/// already available in the same position (relatively) of the caller's
1543/// incoming argument stack.
1544static
1545bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1546 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1547 const ARMInstrInfo *TII) {
1548 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1549 int FI = INT_MAX;
1550 if (Arg.getOpcode() == ISD::CopyFromReg) {
1551 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001552 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001553 return false;
1554 MachineInstr *Def = MRI->getVRegDef(VR);
1555 if (!Def)
1556 return false;
1557 if (!Flags.isByVal()) {
1558 if (!TII->isLoadFromStackSlot(Def, FI))
1559 return false;
1560 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001561 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001562 }
1563 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1564 if (Flags.isByVal())
1565 // ByVal argument is passed in as a pointer but it's now being
1566 // dereferenced. e.g.
1567 // define @foo(%struct.X* %A) {
1568 // tail call @bar(%struct.X* byval %A)
1569 // }
1570 return false;
1571 SDValue Ptr = Ld->getBasePtr();
1572 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1573 if (!FINode)
1574 return false;
1575 FI = FINode->getIndex();
1576 } else
1577 return false;
1578
1579 assert(FI != INT_MAX);
1580 if (!MFI->isFixedObjectIndex(FI))
1581 return false;
1582 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1583}
1584
1585/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1586/// for tail call optimization. Targets which want to do tail call
1587/// optimization should implement this function.
1588bool
1589ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1590 CallingConv::ID CalleeCC,
1591 bool isVarArg,
1592 bool isCalleeStructRet,
1593 bool isCallerStructRet,
1594 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001595 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001596 const SmallVectorImpl<ISD::InputArg> &Ins,
1597 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001598 const Function *CallerF = DAG.getMachineFunction().getFunction();
1599 CallingConv::ID CallerCC = CallerF->getCallingConv();
1600 bool CCMatch = CallerCC == CalleeCC;
1601
1602 // Look for obvious safe cases to perform tail call optimization that do not
1603 // require ABI changes. This is what gcc calls sibcall.
1604
Jim Grosbach7616b642010-06-16 23:45:49 +00001605 // Do not sibcall optimize vararg calls unless the call site is not passing
1606 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001607 if (isVarArg && !Outs.empty())
1608 return false;
1609
1610 // Also avoid sibcall optimization if either caller or callee uses struct
1611 // return semantics.
1612 if (isCalleeStructRet || isCallerStructRet)
1613 return false;
1614
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001615 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001616 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1617 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1618 // support in the assembler and linker to be used. This would need to be
1619 // fixed to fully support tail calls in Thumb1.
1620 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001621 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1622 // LR. This means if we need to reload LR, it takes an extra instructions,
1623 // which outweighs the value of the tail call; but here we don't know yet
1624 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001625 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001626 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001627
1628 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1629 // but we need to make sure there are enough registers; the only valid
1630 // registers are the 4 used for parameters. We don't currently do this
1631 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001632 if (Subtarget->isThumb1Only())
1633 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001634
Dale Johannesen51e28e62010-06-03 21:09:53 +00001635 // If the calling conventions do not match, then we'd better make sure the
1636 // results are returned in the same way as what the caller expects.
1637 if (!CCMatch) {
1638 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001639 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1640 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001641 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1642
1643 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001644 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1645 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001646 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1647
1648 if (RVLocs1.size() != RVLocs2.size())
1649 return false;
1650 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1651 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1652 return false;
1653 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1654 return false;
1655 if (RVLocs1[i].isRegLoc()) {
1656 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1657 return false;
1658 } else {
1659 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1660 return false;
1661 }
1662 }
1663 }
1664
1665 // If the callee takes no arguments then go on to check the results of the
1666 // call.
1667 if (!Outs.empty()) {
1668 // Check if stack adjustment is needed. For now, do not do this if any
1669 // argument is passed on the stack.
1670 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001671 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1672 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001673 CCInfo.AnalyzeCallOperands(Outs,
1674 CCAssignFnForNode(CalleeCC, false, isVarArg));
1675 if (CCInfo.getNextStackOffset()) {
1676 MachineFunction &MF = DAG.getMachineFunction();
1677
1678 // Check if the arguments are already laid out in the right way as
1679 // the caller's fixed stack objects.
1680 MachineFrameInfo *MFI = MF.getFrameInfo();
1681 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1682 const ARMInstrInfo *TII =
1683 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001684 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1685 i != e;
1686 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001687 CCValAssign &VA = ArgLocs[i];
1688 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001689 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001690 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001691 if (VA.getLocInfo() == CCValAssign::Indirect)
1692 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001693 if (VA.needsCustom()) {
1694 // f64 and vector types are split into multiple registers or
1695 // register/stack-slot combinations. The types will not match
1696 // the registers; give up on memory f64 refs until we figure
1697 // out what to do about this.
1698 if (!VA.isRegLoc())
1699 return false;
1700 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001701 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001702 if (RegVT == MVT::v2f64) {
1703 if (!ArgLocs[++i].isRegLoc())
1704 return false;
1705 if (!ArgLocs[++i].isRegLoc())
1706 return false;
1707 }
1708 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001709 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1710 MFI, MRI, TII))
1711 return false;
1712 }
1713 }
1714 }
1715 }
1716
1717 return true;
1718}
1719
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720SDValue
1721ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001722 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001723 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001724 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001725 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001726
Bob Wilsondee46d72009-04-17 20:35:10 +00001727 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001728 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001729
Bob Wilsondee46d72009-04-17 20:35:10 +00001730 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001731 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1732 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001733
Dan Gohman98ca4f22009-08-05 01:29:28 +00001734 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001735 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1736 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001737
1738 // If this is the first return lowered for this function, add
1739 // the regs to the liveout set for the function.
1740 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1741 for (unsigned i = 0; i != RVLocs.size(); ++i)
1742 if (RVLocs[i].isRegLoc())
1743 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001744 }
1745
Bob Wilson1f595bb2009-04-17 19:07:39 +00001746 SDValue Flag;
1747
1748 // Copy the result values into the output registers.
1749 for (unsigned i = 0, realRVLocIdx = 0;
1750 i != RVLocs.size();
1751 ++i, ++realRVLocIdx) {
1752 CCValAssign &VA = RVLocs[i];
1753 assert(VA.isRegLoc() && "Can only return in registers!");
1754
Dan Gohmanc9403652010-07-07 15:54:55 +00001755 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001756
1757 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001758 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001759 case CCValAssign::Full: break;
1760 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001761 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001762 break;
1763 }
1764
Bob Wilson1f595bb2009-04-17 19:07:39 +00001765 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001767 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1769 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001770 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001772
1773 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1774 Flag = Chain.getValue(1);
1775 VA = RVLocs[++i]; // skip ahead to next loc
1776 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1777 HalfGPRs.getValue(1), Flag);
1778 Flag = Chain.getValue(1);
1779 VA = RVLocs[++i]; // skip ahead to next loc
1780
1781 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1783 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001784 }
1785 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1786 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001787 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001788 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001789 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001790 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001791 VA = RVLocs[++i]; // skip ahead to next loc
1792 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1793 Flag);
1794 } else
1795 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1796
Bob Wilsondee46d72009-04-17 20:35:10 +00001797 // Guarantee that all emitted copies are
1798 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001799 Flag = Chain.getValue(1);
1800 }
1801
1802 SDValue result;
1803 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001804 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001805 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001807
1808 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001809}
1810
Evan Cheng3d2125c2010-11-30 23:55:39 +00001811bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1812 if (N->getNumValues() != 1)
1813 return false;
1814 if (!N->hasNUsesOfValue(1, 0))
1815 return false;
1816
1817 unsigned NumCopies = 0;
1818 SDNode* Copies[2];
1819 SDNode *Use = *N->use_begin();
1820 if (Use->getOpcode() == ISD::CopyToReg) {
1821 Copies[NumCopies++] = Use;
1822 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1823 // f64 returned in a pair of GPRs.
1824 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1825 UI != UE; ++UI) {
1826 if (UI->getOpcode() != ISD::CopyToReg)
1827 return false;
1828 Copies[UI.getUse().getResNo()] = *UI;
1829 ++NumCopies;
1830 }
1831 } else if (Use->getOpcode() == ISD::BITCAST) {
1832 // f32 returned in a single GPR.
1833 if (!Use->hasNUsesOfValue(1, 0))
1834 return false;
1835 Use = *Use->use_begin();
1836 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1837 return false;
1838 Copies[NumCopies++] = Use;
1839 } else {
1840 return false;
1841 }
1842
1843 if (NumCopies != 1 && NumCopies != 2)
1844 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001845
1846 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001847 for (unsigned i = 0; i < NumCopies; ++i) {
1848 SDNode *Copy = Copies[i];
1849 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1850 UI != UE; ++UI) {
1851 if (UI->getOpcode() == ISD::CopyToReg) {
1852 SDNode *Use = *UI;
1853 if (Use == Copies[0] || Use == Copies[1])
1854 continue;
1855 return false;
1856 }
1857 if (UI->getOpcode() != ARMISD::RET_FLAG)
1858 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001859 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001860 }
1861 }
1862
Evan Cheng1bf891a2010-12-01 22:59:46 +00001863 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001864}
1865
Evan Cheng485fafc2011-03-21 01:19:09 +00001866bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1867 if (!EnableARMTailCalls)
1868 return false;
1869
1870 if (!CI->isTailCall())
1871 return false;
1872
1873 return !Subtarget->isThumb1Only();
1874}
1875
Bob Wilsonb62d2572009-11-03 00:02:05 +00001876// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1877// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1878// one of the above mentioned nodes. It has to be wrapped because otherwise
1879// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1880// be used to form addressing mode. These wrapped nodes will be selected
1881// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001882static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001883 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001884 // FIXME there is no actual debug info here
1885 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001886 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001887 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001888 if (CP->isMachineConstantPoolEntry())
1889 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1890 CP->getAlignment());
1891 else
1892 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1893 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001895}
1896
Jim Grosbache1102ca2010-07-19 17:20:38 +00001897unsigned ARMTargetLowering::getJumpTableEncoding() const {
1898 return MachineJumpTableInfo::EK_Inline;
1899}
1900
Dan Gohmand858e902010-04-17 15:26:15 +00001901SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1902 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001903 MachineFunction &MF = DAG.getMachineFunction();
1904 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1905 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001906 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001907 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001908 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001909 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1910 SDValue CPAddr;
1911 if (RelocM == Reloc::Static) {
1912 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1913 } else {
1914 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001915 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001916 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1917 ARMCP::CPBlockAddress,
1918 PCAdj);
1919 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1920 }
1921 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1922 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001923 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001924 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001925 if (RelocM == Reloc::Static)
1926 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001927 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001928 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001929}
1930
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001931// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001932SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001933ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001934 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001935 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001936 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001937 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001938 MachineFunction &MF = DAG.getMachineFunction();
1939 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001940 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001941 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001942 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001943 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001944 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001946 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001947 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001948 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001949 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001950
Evan Chenge7e0d622009-11-06 22:24:13 +00001951 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001952 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001953
1954 // call __tls_get_addr.
1955 ArgListTy Args;
1956 ArgListEntry Entry;
1957 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001958 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001959 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001960 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001961 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001962 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00001963 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001965 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001966 return CallResult.first;
1967}
1968
1969// Lower ISD::GlobalTLSAddress using the "initial exec" or
1970// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001971SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001972ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001973 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001974 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001975 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue Offset;
1977 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001978 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001979 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001980 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001981
Chris Lattner4fb63d02009-07-15 04:12:33 +00001982 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001983 MachineFunction &MF = DAG.getMachineFunction();
1984 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001985 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001986 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001987 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1988 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001989 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001990 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001991 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001992 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001993 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001994 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001995 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001996 Chain = Offset.getValue(1);
1997
Evan Chenge7e0d622009-11-06 22:24:13 +00001998 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001999 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002000
Evan Cheng9eda6892009-10-31 03:39:36 +00002001 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002002 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002003 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002004 } else {
2005 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002006 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002007 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002008 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002009 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002010 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002011 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002012 }
2013
2014 // The address of the thread local variable is the add of the thread
2015 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002016 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002017}
2018
Dan Gohman475871a2008-07-27 21:46:04 +00002019SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002020ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002021 // TODO: implement the "local dynamic" model
2022 assert(Subtarget->isTargetELF() &&
2023 "TLS not implemented for non-ELF targets");
2024 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2025 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2026 // otherwise use the "Local Exec" TLS Model
2027 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2028 return LowerToTLSGeneralDynamicModel(GA, DAG);
2029 else
2030 return LowerToTLSExecModels(GA, DAG);
2031}
2032
Dan Gohman475871a2008-07-27 21:46:04 +00002033SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002034 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002035 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002036 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002037 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002038 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2039 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002040 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002041 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002042 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002043 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002045 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002046 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002047 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002048 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002050 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002051 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002052 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002053 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002054 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002055 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002056 }
2057
2058 // If we have T2 ops, we can materialize the address directly via movt/movw
2059 // pair. This is always cheaper.
2060 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002061 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002062 // FIXME: Once remat is capable of dealing with instructions with register
2063 // operands, expand this into two nodes.
2064 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2065 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002066 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002067 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2068 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2069 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2070 MachinePointerInfo::getConstantPool(),
2071 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002072 }
2073}
2074
Dan Gohman475871a2008-07-27 21:46:04 +00002075SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002076 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002077 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002078 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002079 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002080 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002081 MachineFunction &MF = DAG.getMachineFunction();
2082 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2083
Evan Cheng4abce0c2011-05-27 20:11:27 +00002084 // FIXME: Enable this for static codegen when tool issues are fixed.
2085 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002086 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002087 // FIXME: Once remat is capable of dealing with instructions with register
2088 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002089 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002090 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2091 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2092
Evan Cheng53519f02011-01-21 18:55:51 +00002093 unsigned Wrapper = (RelocM == Reloc::PIC_)
2094 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2095 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002096 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002097 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2098 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2099 MachinePointerInfo::getGOT(), false, false, 0);
2100 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002101 }
2102
2103 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002104 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002105 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002106 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002107 } else {
2108 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002109 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2110 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002111 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002112 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002113 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002114 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002115
Evan Cheng9eda6892009-10-31 03:39:36 +00002116 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002117 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002118 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002119 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002120
2121 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002122 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002123 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002124 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002125
Evan Cheng63476a82009-09-03 07:04:02 +00002126 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002127 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002128 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002129
2130 return Result;
2131}
2132
Dan Gohman475871a2008-07-27 21:46:04 +00002133SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002134 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002135 assert(Subtarget->isTargetELF() &&
2136 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002137 MachineFunction &MF = DAG.getMachineFunction();
2138 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002139 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002140 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002141 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002142 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002143 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2144 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002145 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002146 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002147 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002148 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002149 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002150 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002151 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002152 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002153}
2154
Jim Grosbach0e0da732009-05-12 23:59:14 +00002155SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002156ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2157 const {
2158 DebugLoc dl = Op.getDebugLoc();
2159 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002160 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002161}
2162
2163SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002164ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2165 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002166 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002167 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2168 Op.getOperand(1), Val);
2169}
2170
2171SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002172ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2173 DebugLoc dl = Op.getDebugLoc();
2174 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2175 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2176}
2177
2178SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002179ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002180 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002181 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002182 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002183 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002184 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002185 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002186 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002187 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2188 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002189 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002190 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002191 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002192 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002193 EVT PtrVT = getPointerTy();
2194 DebugLoc dl = Op.getDebugLoc();
2195 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2196 SDValue CPAddr;
2197 unsigned PCAdj = (RelocM != Reloc::PIC_)
2198 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002199 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002200 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2201 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002202 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002203 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002204 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002205 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002206 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002207 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002208
2209 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002210 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002211 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2212 }
2213 return Result;
2214 }
Evan Cheng92e39162011-03-29 23:06:19 +00002215 case Intrinsic::arm_neon_vmulls:
2216 case Intrinsic::arm_neon_vmullu: {
2217 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2218 ? ARMISD::VMULLs : ARMISD::VMULLu;
2219 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2220 Op.getOperand(1), Op.getOperand(2));
2221 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002222 }
2223}
2224
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002225static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002226 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002227 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002228 if (!Subtarget->hasDataBarrier()) {
2229 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2230 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2231 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002232 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002233 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002234 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002235 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002236 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002237
2238 SDValue Op5 = Op.getOperand(5);
2239 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2240 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2241 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2242 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2243
2244 ARM_MB::MemBOpt DMBOpt;
2245 if (isDeviceBarrier)
2246 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2247 else
2248 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2249 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2250 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002251}
2252
Eli Friedman26689ac2011-08-03 21:06:02 +00002253
2254static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2255 const ARMSubtarget *Subtarget) {
2256 // FIXME: handle "fence singlethread" more efficiently.
2257 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002258 if (!Subtarget->hasDataBarrier()) {
2259 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2260 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2261 // here.
2262 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2263 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002264 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002265 DAG.getConstant(0, MVT::i32));
2266 }
2267
Eli Friedman26689ac2011-08-03 21:06:02 +00002268 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002269 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002270}
2271
Evan Chengdfed19f2010-11-03 06:34:55 +00002272static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2273 const ARMSubtarget *Subtarget) {
2274 // ARM pre v5TE and Thumb1 does not have preload instructions.
2275 if (!(Subtarget->isThumb2() ||
2276 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2277 // Just preserve the chain.
2278 return Op.getOperand(0);
2279
2280 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002281 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2282 if (!isRead &&
2283 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2284 // ARMv7 with MP extension has PLDW.
2285 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002286
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002287 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2288 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002289 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002290 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002291 isData = ~isData & 1;
2292 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002293
2294 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002295 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2296 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002297}
2298
Dan Gohman1e93df62010-04-17 14:41:14 +00002299static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2300 MachineFunction &MF = DAG.getMachineFunction();
2301 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2302
Evan Chenga8e29892007-01-19 07:51:42 +00002303 // vastart just stores the address of the VarArgsFrameIndex slot into the
2304 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002305 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002306 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002307 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002308 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002309 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2310 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002311}
2312
Dan Gohman475871a2008-07-27 21:46:04 +00002313SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002314ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2315 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002316 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002317 MachineFunction &MF = DAG.getMachineFunction();
2318 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2319
2320 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002321 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002322 RC = ARM::tGPRRegisterClass;
2323 else
2324 RC = ARM::GPRRegisterClass;
2325
2326 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002327 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002328 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002329
2330 SDValue ArgValue2;
2331 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002332 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002333 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002334
2335 // Create load node to retrieve arguments from the stack.
2336 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002337 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002338 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002339 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002340 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002341 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002343 }
2344
Jim Grosbache5165492009-11-09 00:11:35 +00002345 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002346}
2347
Stuart Hastingsc7315872011-04-20 16:47:52 +00002348void
2349ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2350 unsigned &VARegSize, unsigned &VARegSaveSize)
2351 const {
2352 unsigned NumGPRs;
2353 if (CCInfo.isFirstByValRegValid())
2354 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2355 else {
2356 unsigned int firstUnalloced;
2357 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2358 sizeof(GPRArgRegs) /
2359 sizeof(GPRArgRegs[0]));
2360 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2361 }
2362
2363 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2364 VARegSize = NumGPRs * 4;
2365 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2366}
2367
2368// The remaining GPRs hold either the beginning of variable-argument
2369// data, or the beginning of an aggregate passed by value (usuall
2370// byval). Either way, we allocate stack slots adjacent to the data
2371// provided by our caller, and store the unallocated registers there.
2372// If this is a variadic function, the va_list pointer will begin with
2373// these values; otherwise, this reassembles a (byval) structure that
2374// was split between registers and memory.
2375void
2376ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2377 DebugLoc dl, SDValue &Chain,
2378 unsigned ArgOffset) const {
2379 MachineFunction &MF = DAG.getMachineFunction();
2380 MachineFrameInfo *MFI = MF.getFrameInfo();
2381 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2382 unsigned firstRegToSaveIndex;
2383 if (CCInfo.isFirstByValRegValid())
2384 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2385 else {
2386 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2387 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2388 }
2389
2390 unsigned VARegSize, VARegSaveSize;
2391 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2392 if (VARegSaveSize) {
2393 // If this function is vararg, store any remaining integer argument regs
2394 // to their spots on the stack so that they may be loaded by deferencing
2395 // the result of va_next.
2396 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002397 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2398 ArgOffset + VARegSaveSize
2399 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002400 false));
2401 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2402 getPointerTy());
2403
2404 SmallVector<SDValue, 4> MemOps;
2405 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2406 TargetRegisterClass *RC;
2407 if (AFI->isThumb1OnlyFunction())
2408 RC = ARM::tGPRRegisterClass;
2409 else
2410 RC = ARM::GPRRegisterClass;
2411
2412 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2413 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2414 SDValue Store =
2415 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002416 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002417 false, false, 0);
2418 MemOps.push_back(Store);
2419 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2420 DAG.getConstant(4, getPointerTy()));
2421 }
2422 if (!MemOps.empty())
2423 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2424 &MemOps[0], MemOps.size());
2425 } else
2426 // This will point to the next argument passed via stack.
2427 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2428}
2429
Bob Wilson5bafff32009-06-22 23:27:02 +00002430SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002431ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002432 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002433 const SmallVectorImpl<ISD::InputArg>
2434 &Ins,
2435 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002436 SmallVectorImpl<SDValue> &InVals)
2437 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002438 MachineFunction &MF = DAG.getMachineFunction();
2439 MachineFrameInfo *MFI = MF.getFrameInfo();
2440
Bob Wilson1f595bb2009-04-17 19:07:39 +00002441 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2442
2443 // Assign locations to all of the incoming arguments.
2444 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002445 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2446 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002447 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002448 CCAssignFnForNode(CallConv, /* Return*/ false,
2449 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002450
2451 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002452 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002453
Stuart Hastingsf222e592011-02-28 17:17:53 +00002454 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002455 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2456 CCValAssign &VA = ArgLocs[i];
2457
Bob Wilsondee46d72009-04-17 20:35:10 +00002458 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002459 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002460 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002461
Bob Wilson1f595bb2009-04-17 19:07:39 +00002462 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002463 // f64 and vector types are split up into multiple registers or
2464 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002465 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002466 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002467 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002468 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002469 SDValue ArgValue2;
2470 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002471 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002472 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2473 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002474 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002475 false, false, 0);
2476 } else {
2477 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2478 Chain, DAG, dl);
2479 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002480 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2481 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002482 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002483 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002484 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2485 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002486 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002487
Bob Wilson5bafff32009-06-22 23:27:02 +00002488 } else {
2489 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002490
Owen Anderson825b72b2009-08-11 20:47:22 +00002491 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002492 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002493 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002494 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002495 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002496 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002497 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002498 RC = (AFI->isThumb1OnlyFunction() ?
2499 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002500 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002501 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002502
2503 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002504 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002505 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002506 }
2507
2508 // If this is an 8 or 16-bit value, it is really passed promoted
2509 // to 32 bits. Insert an assert[sz]ext to capture this, then
2510 // truncate to the right size.
2511 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002512 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002513 case CCValAssign::Full: break;
2514 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002515 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002516 break;
2517 case CCValAssign::SExt:
2518 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2519 DAG.getValueType(VA.getValVT()));
2520 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2521 break;
2522 case CCValAssign::ZExt:
2523 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2524 DAG.getValueType(VA.getValVT()));
2525 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2526 break;
2527 }
2528
Dan Gohman98ca4f22009-08-05 01:29:28 +00002529 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002530
2531 } else { // VA.isRegLoc()
2532
2533 // sanity check
2534 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002535 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002536
Stuart Hastingsf222e592011-02-28 17:17:53 +00002537 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002538
Stuart Hastingsf222e592011-02-28 17:17:53 +00002539 // Some Ins[] entries become multiple ArgLoc[] entries.
2540 // Process them only once.
2541 if (index != lastInsIndex)
2542 {
2543 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002544 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002545 // This can be changed with more analysis.
2546 // In case of tail call optimization mark all arguments mutable.
2547 // Since they could be overwritten by lowering of arguments in case of
2548 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002549 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002550 unsigned VARegSize, VARegSaveSize;
2551 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2552 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2553 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002554 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002555 int FI = MFI->CreateFixedObject(Bytes,
2556 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002557 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2558 } else {
2559 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2560 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002561
Stuart Hastingsf222e592011-02-28 17:17:53 +00002562 // Create load nodes to retrieve arguments from the stack.
2563 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2564 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2565 MachinePointerInfo::getFixedStack(FI),
2566 false, false, 0));
2567 }
2568 lastInsIndex = index;
2569 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002570 }
2571 }
2572
2573 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002574 if (isVarArg)
2575 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002576
Dan Gohman98ca4f22009-08-05 01:29:28 +00002577 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002578}
2579
2580/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002581static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002582 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002583 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002584 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002585 // Maybe this has already been legalized into the constant pool?
2586 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002587 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002588 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002589 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002590 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002591 }
2592 }
2593 return false;
2594}
2595
Evan Chenga8e29892007-01-19 07:51:42 +00002596/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2597/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002598SDValue
2599ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002600 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002601 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002602 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002603 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002604 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002605 // Constant does not fit, try adjusting it by one?
2606 switch (CC) {
2607 default: break;
2608 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002609 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002610 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002611 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002612 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002613 }
2614 break;
2615 case ISD::SETULT:
2616 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002617 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002618 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002619 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002620 }
2621 break;
2622 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002623 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002624 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002625 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002626 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002627 }
2628 break;
2629 case ISD::SETULE:
2630 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002631 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002632 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002633 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002634 }
2635 break;
2636 }
2637 }
2638 }
2639
2640 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002641 ARMISD::NodeType CompareType;
2642 switch (CondCode) {
2643 default:
2644 CompareType = ARMISD::CMP;
2645 break;
2646 case ARMCC::EQ:
2647 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002648 // Uses only Z Flag
2649 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002650 break;
2651 }
Evan Cheng218977b2010-07-13 19:27:42 +00002652 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002653 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002654}
2655
2656/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002657SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002658ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002659 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002660 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002661 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002662 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002663 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002664 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2665 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002666}
2667
Bob Wilson79f56c92011-03-08 01:17:20 +00002668/// duplicateCmp - Glue values can have only one use, so this function
2669/// duplicates a comparison node.
2670SDValue
2671ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2672 unsigned Opc = Cmp.getOpcode();
2673 DebugLoc DL = Cmp.getDebugLoc();
2674 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2675 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2676
2677 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2678 Cmp = Cmp.getOperand(0);
2679 Opc = Cmp.getOpcode();
2680 if (Opc == ARMISD::CMPFP)
2681 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2682 else {
2683 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2684 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2685 }
2686 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2687}
2688
Bill Wendlingde2b1512010-08-11 08:43:16 +00002689SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2690 SDValue Cond = Op.getOperand(0);
2691 SDValue SelectTrue = Op.getOperand(1);
2692 SDValue SelectFalse = Op.getOperand(2);
2693 DebugLoc dl = Op.getDebugLoc();
2694
2695 // Convert:
2696 //
2697 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2698 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2699 //
2700 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2701 const ConstantSDNode *CMOVTrue =
2702 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2703 const ConstantSDNode *CMOVFalse =
2704 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2705
2706 if (CMOVTrue && CMOVFalse) {
2707 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2708 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2709
2710 SDValue True;
2711 SDValue False;
2712 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2713 True = SelectTrue;
2714 False = SelectFalse;
2715 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2716 True = SelectFalse;
2717 False = SelectTrue;
2718 }
2719
2720 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002721 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002722 SDValue ARMcc = Cond.getOperand(2);
2723 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002724 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002725 assert(True.getValueType() == VT);
2726 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002727 }
2728 }
2729 }
2730
2731 return DAG.getSelectCC(dl, Cond,
2732 DAG.getConstant(0, Cond.getValueType()),
2733 SelectTrue, SelectFalse, ISD::SETNE);
2734}
2735
Dan Gohmand858e902010-04-17 15:26:15 +00002736SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002737 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002738 SDValue LHS = Op.getOperand(0);
2739 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002740 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002741 SDValue TrueVal = Op.getOperand(2);
2742 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002743 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002744
Owen Anderson825b72b2009-08-11 20:47:22 +00002745 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002746 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002747 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002748 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Evan Chenge721f5c2011-07-13 00:42:17 +00002749 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002750 }
2751
2752 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002753 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002754
Evan Cheng218977b2010-07-13 19:27:42 +00002755 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2756 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002757 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002758 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002759 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002760 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002761 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002762 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002763 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002764 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002765 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002766 }
2767 return Result;
2768}
2769
Evan Cheng218977b2010-07-13 19:27:42 +00002770/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2771/// to morph to an integer compare sequence.
2772static bool canChangeToInt(SDValue Op, bool &SeenZero,
2773 const ARMSubtarget *Subtarget) {
2774 SDNode *N = Op.getNode();
2775 if (!N->hasOneUse())
2776 // Otherwise it requires moving the value from fp to integer registers.
2777 return false;
2778 if (!N->getNumValues())
2779 return false;
2780 EVT VT = Op.getValueType();
2781 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2782 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2783 // vmrs are very slow, e.g. cortex-a8.
2784 return false;
2785
2786 if (isFloatingPointZero(Op)) {
2787 SeenZero = true;
2788 return true;
2789 }
2790 return ISD::isNormalLoad(N);
2791}
2792
2793static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2794 if (isFloatingPointZero(Op))
2795 return DAG.getConstant(0, MVT::i32);
2796
2797 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2798 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002799 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002800 Ld->isVolatile(), Ld->isNonTemporal(),
2801 Ld->getAlignment());
2802
2803 llvm_unreachable("Unknown VFP cmp argument!");
2804}
2805
2806static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2807 SDValue &RetVal1, SDValue &RetVal2) {
2808 if (isFloatingPointZero(Op)) {
2809 RetVal1 = DAG.getConstant(0, MVT::i32);
2810 RetVal2 = DAG.getConstant(0, MVT::i32);
2811 return;
2812 }
2813
2814 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2815 SDValue Ptr = Ld->getBasePtr();
2816 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2817 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002818 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002819 Ld->isVolatile(), Ld->isNonTemporal(),
2820 Ld->getAlignment());
2821
2822 EVT PtrType = Ptr.getValueType();
2823 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2824 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2825 PtrType, Ptr, DAG.getConstant(4, PtrType));
2826 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2827 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002828 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002829 Ld->isVolatile(), Ld->isNonTemporal(),
2830 NewAlign);
2831 return;
2832 }
2833
2834 llvm_unreachable("Unknown VFP cmp argument!");
2835}
2836
2837/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2838/// f32 and even f64 comparisons to integer ones.
2839SDValue
2840ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2841 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002842 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002843 SDValue LHS = Op.getOperand(2);
2844 SDValue RHS = Op.getOperand(3);
2845 SDValue Dest = Op.getOperand(4);
2846 DebugLoc dl = Op.getDebugLoc();
2847
2848 bool SeenZero = false;
2849 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2850 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002851 // If one of the operand is zero, it's safe to ignore the NaN case since
2852 // we only care about equality comparisons.
2853 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002854 // If unsafe fp math optimization is enabled and there are no other uses of
2855 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002856 // to an integer comparison.
2857 if (CC == ISD::SETOEQ)
2858 CC = ISD::SETEQ;
2859 else if (CC == ISD::SETUNE)
2860 CC = ISD::SETNE;
2861
2862 SDValue ARMcc;
2863 if (LHS.getValueType() == MVT::f32) {
2864 LHS = bitcastf32Toi32(LHS, DAG);
2865 RHS = bitcastf32Toi32(RHS, DAG);
2866 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2867 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2868 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2869 Chain, Dest, ARMcc, CCR, Cmp);
2870 }
2871
2872 SDValue LHS1, LHS2;
2873 SDValue RHS1, RHS2;
2874 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2875 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2876 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2877 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002878 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002879 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2880 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2881 }
2882
2883 return SDValue();
2884}
2885
2886SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2887 SDValue Chain = Op.getOperand(0);
2888 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2889 SDValue LHS = Op.getOperand(2);
2890 SDValue RHS = Op.getOperand(3);
2891 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002892 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002893
Owen Anderson825b72b2009-08-11 20:47:22 +00002894 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002895 SDValue ARMcc;
2896 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002897 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002899 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002900 }
2901
Owen Anderson825b72b2009-08-11 20:47:22 +00002902 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002903
2904 if (UnsafeFPMath &&
2905 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2906 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2907 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2908 if (Result.getNode())
2909 return Result;
2910 }
2911
Evan Chenga8e29892007-01-19 07:51:42 +00002912 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002913 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002914
Evan Cheng218977b2010-07-13 19:27:42 +00002915 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2916 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002917 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002918 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002919 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002920 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002921 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002922 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2923 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002924 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002925 }
2926 return Res;
2927}
2928
Dan Gohmand858e902010-04-17 15:26:15 +00002929SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002930 SDValue Chain = Op.getOperand(0);
2931 SDValue Table = Op.getOperand(1);
2932 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002933 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002934
Owen Andersone50ed302009-08-10 22:56:29 +00002935 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002936 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2937 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002938 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002939 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002940 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002941 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2942 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002943 if (Subtarget->isThumb2()) {
2944 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2945 // which does another jump to the destination. This also makes it easier
2946 // to translate it to TBB / TBH later.
2947 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002948 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002949 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002950 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002951 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002952 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002953 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002954 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002955 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002956 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002958 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002959 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002960 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002961 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002962 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002963 }
Evan Chenga8e29892007-01-19 07:51:42 +00002964}
2965
Bob Wilson76a312b2010-03-19 22:51:32 +00002966static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2967 DebugLoc dl = Op.getDebugLoc();
2968 unsigned Opc;
2969
2970 switch (Op.getOpcode()) {
2971 default:
2972 assert(0 && "Invalid opcode!");
2973 case ISD::FP_TO_SINT:
2974 Opc = ARMISD::FTOSI;
2975 break;
2976 case ISD::FP_TO_UINT:
2977 Opc = ARMISD::FTOUI;
2978 break;
2979 }
2980 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002981 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002982}
2983
Cameron Zwarich3007d332011-03-29 21:41:55 +00002984static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2985 EVT VT = Op.getValueType();
2986 DebugLoc dl = Op.getDebugLoc();
2987
Duncan Sands1f6a3292011-08-12 14:54:45 +00002988 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
2989 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00002990 if (VT != MVT::v4f32)
2991 return DAG.UnrollVectorOp(Op.getNode());
2992
2993 unsigned CastOpc;
2994 unsigned Opc;
2995 switch (Op.getOpcode()) {
2996 default:
2997 assert(0 && "Invalid opcode!");
2998 case ISD::SINT_TO_FP:
2999 CastOpc = ISD::SIGN_EXTEND;
3000 Opc = ISD::SINT_TO_FP;
3001 break;
3002 case ISD::UINT_TO_FP:
3003 CastOpc = ISD::ZERO_EXTEND;
3004 Opc = ISD::UINT_TO_FP;
3005 break;
3006 }
3007
3008 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3009 return DAG.getNode(Opc, dl, VT, Op);
3010}
3011
Bob Wilson76a312b2010-03-19 22:51:32 +00003012static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3013 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003014 if (VT.isVector())
3015 return LowerVectorINT_TO_FP(Op, DAG);
3016
Bob Wilson76a312b2010-03-19 22:51:32 +00003017 DebugLoc dl = Op.getDebugLoc();
3018 unsigned Opc;
3019
3020 switch (Op.getOpcode()) {
3021 default:
3022 assert(0 && "Invalid opcode!");
3023 case ISD::SINT_TO_FP:
3024 Opc = ARMISD::SITOF;
3025 break;
3026 case ISD::UINT_TO_FP:
3027 Opc = ARMISD::UITOF;
3028 break;
3029 }
3030
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003031 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003032 return DAG.getNode(Opc, dl, VT, Op);
3033}
3034
Evan Cheng515fe3a2010-07-08 02:08:50 +00003035SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003036 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003037 SDValue Tmp0 = Op.getOperand(0);
3038 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003039 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003040 EVT VT = Op.getValueType();
3041 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003042 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3043 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3044 bool UseNEON = !InGPR && Subtarget->hasNEON();
3045
3046 if (UseNEON) {
3047 // Use VBSL to copy the sign bit.
3048 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3049 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3050 DAG.getTargetConstant(EncodedVal, MVT::i32));
3051 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3052 if (VT == MVT::f64)
3053 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3054 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3055 DAG.getConstant(32, MVT::i32));
3056 else /*if (VT == MVT::f32)*/
3057 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3058 if (SrcVT == MVT::f32) {
3059 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3060 if (VT == MVT::f64)
3061 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3062 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3063 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003064 } else if (VT == MVT::f32)
3065 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3066 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3067 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003068 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3069 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3070
3071 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3072 MVT::i32);
3073 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3074 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3075 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003076
Evan Chenge573fb32011-02-23 02:24:55 +00003077 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3078 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3079 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003080 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003081 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3082 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3083 DAG.getConstant(0, MVT::i32));
3084 } else {
3085 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3086 }
3087
3088 return Res;
3089 }
Evan Chengc143dd42011-02-11 02:28:55 +00003090
3091 // Bitcast operand 1 to i32.
3092 if (SrcVT == MVT::f64)
3093 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3094 &Tmp1, 1).getValue(1);
3095 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3096
Evan Chenge573fb32011-02-23 02:24:55 +00003097 // Or in the signbit with integer operations.
3098 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3099 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3100 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3101 if (VT == MVT::f32) {
3102 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3103 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3104 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3105 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003106 }
3107
Evan Chenge573fb32011-02-23 02:24:55 +00003108 // f64: Or the high part with signbit and then combine two parts.
3109 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3110 &Tmp0, 1);
3111 SDValue Lo = Tmp0.getValue(0);
3112 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3113 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3114 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003115}
3116
Evan Cheng2457f2c2010-05-22 01:47:14 +00003117SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3118 MachineFunction &MF = DAG.getMachineFunction();
3119 MachineFrameInfo *MFI = MF.getFrameInfo();
3120 MFI->setReturnAddressIsTaken(true);
3121
3122 EVT VT = Op.getValueType();
3123 DebugLoc dl = Op.getDebugLoc();
3124 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3125 if (Depth) {
3126 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3127 SDValue Offset = DAG.getConstant(4, MVT::i32);
3128 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3129 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003130 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003131 }
3132
3133 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003134 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003135 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3136}
3137
Dan Gohmand858e902010-04-17 15:26:15 +00003138SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003139 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3140 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003141
Owen Andersone50ed302009-08-10 22:56:29 +00003142 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003143 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3144 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003145 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003146 ? ARM::R7 : ARM::R11;
3147 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3148 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003149 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3150 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003151 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003152 return FrameAddr;
3153}
3154
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003155/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003156/// expand a bit convert where either the source or destination type is i64 to
3157/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3158/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3159/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003160static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003161 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3162 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003163 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003164
Bob Wilson9f3f0612010-04-17 05:30:19 +00003165 // This function is only supposed to be called for i64 types, either as the
3166 // source or destination of the bit convert.
3167 EVT SrcVT = Op.getValueType();
3168 EVT DstVT = N->getValueType(0);
3169 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003170 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003171
Bob Wilson9f3f0612010-04-17 05:30:19 +00003172 // Turn i64->f64 into VMOVDRR.
3173 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003174 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3175 DAG.getConstant(0, MVT::i32));
3176 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3177 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003178 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003179 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003180 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003181
Jim Grosbache5165492009-11-09 00:11:35 +00003182 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003183 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3184 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3185 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3186 // Merge the pieces into a single i64 value.
3187 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3188 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003189
Bob Wilson9f3f0612010-04-17 05:30:19 +00003190 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003191}
3192
Bob Wilson5bafff32009-06-22 23:27:02 +00003193/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003194/// Zero vectors are used to represent vector negation and in those cases
3195/// will be implemented with the NEON VNEG instruction. However, VNEG does
3196/// not support i64 elements, so sometimes the zero vectors will need to be
3197/// explicitly constructed. Regardless, use a canonical VMOV to create the
3198/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003199static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003200 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003201 // The canonical modified immediate encoding of a zero vector is....0!
3202 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3203 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3204 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003205 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003206}
3207
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003208/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3209/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003210SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3211 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003212 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3213 EVT VT = Op.getValueType();
3214 unsigned VTBits = VT.getSizeInBits();
3215 DebugLoc dl = Op.getDebugLoc();
3216 SDValue ShOpLo = Op.getOperand(0);
3217 SDValue ShOpHi = Op.getOperand(1);
3218 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003219 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003220 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003221
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003222 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3223
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003224 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3225 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3226 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3227 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3228 DAG.getConstant(VTBits, MVT::i32));
3229 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3230 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003231 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003232
3233 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3234 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003235 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003236 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003237 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003238 CCR, Cmp);
3239
3240 SDValue Ops[2] = { Lo, Hi };
3241 return DAG.getMergeValues(Ops, 2, dl);
3242}
3243
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003244/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3245/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003246SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3247 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003248 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3249 EVT VT = Op.getValueType();
3250 unsigned VTBits = VT.getSizeInBits();
3251 DebugLoc dl = Op.getDebugLoc();
3252 SDValue ShOpLo = Op.getOperand(0);
3253 SDValue ShOpHi = Op.getOperand(1);
3254 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003255 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003256
3257 assert(Op.getOpcode() == ISD::SHL_PARTS);
3258 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3259 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3260 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3261 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3262 DAG.getConstant(VTBits, MVT::i32));
3263 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3264 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3265
3266 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3267 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3268 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003269 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003270 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003271 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003272 CCR, Cmp);
3273
3274 SDValue Ops[2] = { Lo, Hi };
3275 return DAG.getMergeValues(Ops, 2, dl);
3276}
3277
Jim Grosbach4725ca72010-09-08 03:54:02 +00003278SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003279 SelectionDAG &DAG) const {
3280 // The rounding mode is in bits 23:22 of the FPSCR.
3281 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3282 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3283 // so that the shift + and get folded into a bitfield extract.
3284 DebugLoc dl = Op.getDebugLoc();
3285 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3286 DAG.getConstant(Intrinsic::arm_get_fpscr,
3287 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003288 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003289 DAG.getConstant(1U << 22, MVT::i32));
3290 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3291 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003292 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003293 DAG.getConstant(3, MVT::i32));
3294}
3295
Jim Grosbach3482c802010-01-18 19:58:49 +00003296static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3297 const ARMSubtarget *ST) {
3298 EVT VT = N->getValueType(0);
3299 DebugLoc dl = N->getDebugLoc();
3300
3301 if (!ST->hasV6T2Ops())
3302 return SDValue();
3303
3304 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3305 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3306}
3307
Bob Wilson5bafff32009-06-22 23:27:02 +00003308static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3309 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003310 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003311 DebugLoc dl = N->getDebugLoc();
3312
Bob Wilsond5448bb2010-11-18 21:16:28 +00003313 if (!VT.isVector())
3314 return SDValue();
3315
Bob Wilson5bafff32009-06-22 23:27:02 +00003316 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003317 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003318
Bob Wilsond5448bb2010-11-18 21:16:28 +00003319 // Left shifts translate directly to the vshiftu intrinsic.
3320 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003321 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003322 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3323 N->getOperand(0), N->getOperand(1));
3324
3325 assert((N->getOpcode() == ISD::SRA ||
3326 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3327
3328 // NEON uses the same intrinsics for both left and right shifts. For
3329 // right shifts, the shift amounts are negative, so negate the vector of
3330 // shift amounts.
3331 EVT ShiftVT = N->getOperand(1).getValueType();
3332 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3333 getZeroVector(ShiftVT, DAG, dl),
3334 N->getOperand(1));
3335 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3336 Intrinsic::arm_neon_vshifts :
3337 Intrinsic::arm_neon_vshiftu);
3338 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3339 DAG.getConstant(vshiftInt, MVT::i32),
3340 N->getOperand(0), NegatedCount);
3341}
3342
3343static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3344 const ARMSubtarget *ST) {
3345 EVT VT = N->getValueType(0);
3346 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003347
Eli Friedmance392eb2009-08-22 03:13:10 +00003348 // We can get here for a node like i32 = ISD::SHL i32, i64
3349 if (VT != MVT::i64)
3350 return SDValue();
3351
3352 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003353 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003354
Chris Lattner27a6c732007-11-24 07:07:01 +00003355 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3356 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003357 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003358 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003359
Chris Lattner27a6c732007-11-24 07:07:01 +00003360 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003361 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003362
Chris Lattner27a6c732007-11-24 07:07:01 +00003363 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003364 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003365 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003366 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003367 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003368
Chris Lattner27a6c732007-11-24 07:07:01 +00003369 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3370 // captures the result into a carry flag.
3371 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003372 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003373
Chris Lattner27a6c732007-11-24 07:07:01 +00003374 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003375 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003376
Chris Lattner27a6c732007-11-24 07:07:01 +00003377 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003378 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003379}
3380
Bob Wilson5bafff32009-06-22 23:27:02 +00003381static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3382 SDValue TmpOp0, TmpOp1;
3383 bool Invert = false;
3384 bool Swap = false;
3385 unsigned Opc = 0;
3386
3387 SDValue Op0 = Op.getOperand(0);
3388 SDValue Op1 = Op.getOperand(1);
3389 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003390 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003391 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3392 DebugLoc dl = Op.getDebugLoc();
3393
3394 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3395 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003396 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003397 case ISD::SETUNE:
3398 case ISD::SETNE: Invert = true; // Fallthrough
3399 case ISD::SETOEQ:
3400 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3401 case ISD::SETOLT:
3402 case ISD::SETLT: Swap = true; // Fallthrough
3403 case ISD::SETOGT:
3404 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3405 case ISD::SETOLE:
3406 case ISD::SETLE: Swap = true; // Fallthrough
3407 case ISD::SETOGE:
3408 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3409 case ISD::SETUGE: Swap = true; // Fallthrough
3410 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3411 case ISD::SETUGT: Swap = true; // Fallthrough
3412 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3413 case ISD::SETUEQ: Invert = true; // Fallthrough
3414 case ISD::SETONE:
3415 // Expand this to (OLT | OGT).
3416 TmpOp0 = Op0;
3417 TmpOp1 = Op1;
3418 Opc = ISD::OR;
3419 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3420 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3421 break;
3422 case ISD::SETUO: Invert = true; // Fallthrough
3423 case ISD::SETO:
3424 // Expand this to (OLT | OGE).
3425 TmpOp0 = Op0;
3426 TmpOp1 = Op1;
3427 Opc = ISD::OR;
3428 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3429 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3430 break;
3431 }
3432 } else {
3433 // Integer comparisons.
3434 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003435 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003436 case ISD::SETNE: Invert = true;
3437 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3438 case ISD::SETLT: Swap = true;
3439 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3440 case ISD::SETLE: Swap = true;
3441 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3442 case ISD::SETULT: Swap = true;
3443 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3444 case ISD::SETULE: Swap = true;
3445 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3446 }
3447
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003448 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003449 if (Opc == ARMISD::VCEQ) {
3450
3451 SDValue AndOp;
3452 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3453 AndOp = Op0;
3454 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3455 AndOp = Op1;
3456
3457 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003458 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003459 AndOp = AndOp.getOperand(0);
3460
3461 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3462 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003463 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3464 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003465 Invert = !Invert;
3466 }
3467 }
3468 }
3469
3470 if (Swap)
3471 std::swap(Op0, Op1);
3472
Owen Andersonc24cb352010-11-08 23:21:22 +00003473 // If one of the operands is a constant vector zero, attempt to fold the
3474 // comparison to a specialized compare-against-zero form.
3475 SDValue SingleOp;
3476 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3477 SingleOp = Op0;
3478 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3479 if (Opc == ARMISD::VCGE)
3480 Opc = ARMISD::VCLEZ;
3481 else if (Opc == ARMISD::VCGT)
3482 Opc = ARMISD::VCLTZ;
3483 SingleOp = Op1;
3484 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003485
Owen Andersonc24cb352010-11-08 23:21:22 +00003486 SDValue Result;
3487 if (SingleOp.getNode()) {
3488 switch (Opc) {
3489 case ARMISD::VCEQ:
3490 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3491 case ARMISD::VCGE:
3492 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3493 case ARMISD::VCLEZ:
3494 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3495 case ARMISD::VCGT:
3496 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3497 case ARMISD::VCLTZ:
3498 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3499 default:
3500 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3501 }
3502 } else {
3503 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3504 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003505
3506 if (Invert)
3507 Result = DAG.getNOT(dl, Result, VT);
3508
3509 return Result;
3510}
3511
Bob Wilsond3c42842010-06-14 22:19:57 +00003512/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3513/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003514/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003515static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3516 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003517 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003518 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003519
Bob Wilson827b2102010-06-15 19:05:35 +00003520 // SplatBitSize is set to the smallest size that splats the vector, so a
3521 // zero vector will always have SplatBitSize == 8. However, NEON modified
3522 // immediate instructions others than VMOV do not support the 8-bit encoding
3523 // of a zero vector, and the default encoding of zero is supposed to be the
3524 // 32-bit version.
3525 if (SplatBits == 0)
3526 SplatBitSize = 32;
3527
Bob Wilson5bafff32009-06-22 23:27:02 +00003528 switch (SplatBitSize) {
3529 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003530 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003531 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003532 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003533 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003534 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003535 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003536 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003537 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003538
3539 case 16:
3540 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003541 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003542 if ((SplatBits & ~0xff) == 0) {
3543 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003544 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003545 Imm = SplatBits;
3546 break;
3547 }
3548 if ((SplatBits & ~0xff00) == 0) {
3549 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003550 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003551 Imm = SplatBits >> 8;
3552 break;
3553 }
3554 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003555
3556 case 32:
3557 // NEON's 32-bit VMOV supports splat values where:
3558 // * only one byte is nonzero, or
3559 // * the least significant byte is 0xff and the second byte is nonzero, or
3560 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003561 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003562 if ((SplatBits & ~0xff) == 0) {
3563 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003564 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003565 Imm = SplatBits;
3566 break;
3567 }
3568 if ((SplatBits & ~0xff00) == 0) {
3569 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003570 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003571 Imm = SplatBits >> 8;
3572 break;
3573 }
3574 if ((SplatBits & ~0xff0000) == 0) {
3575 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003576 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003577 Imm = SplatBits >> 16;
3578 break;
3579 }
3580 if ((SplatBits & ~0xff000000) == 0) {
3581 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003582 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003583 Imm = SplatBits >> 24;
3584 break;
3585 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003586
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003587 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3588 if (type == OtherModImm) return SDValue();
3589
Bob Wilson5bafff32009-06-22 23:27:02 +00003590 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003591 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3592 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003593 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003594 Imm = SplatBits >> 8;
3595 SplatBits |= 0xff;
3596 break;
3597 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003598
3599 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003600 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3601 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003602 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003603 Imm = SplatBits >> 16;
3604 SplatBits |= 0xffff;
3605 break;
3606 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003607
3608 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3609 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3610 // VMOV.I32. A (very) minor optimization would be to replicate the value
3611 // and fall through here to test for a valid 64-bit splat. But, then the
3612 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003613 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003614
3615 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003616 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003617 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003618 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003619 uint64_t BitMask = 0xff;
3620 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003621 unsigned ImmMask = 1;
3622 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003623 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003624 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003625 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003626 Imm |= ImmMask;
3627 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003628 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003629 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003630 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003631 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003632 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003633 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003634 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003635 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003636 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003637 break;
3638 }
3639
Bob Wilson1a913ed2010-06-11 21:34:50 +00003640 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003641 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003642 return SDValue();
3643 }
3644
Bob Wilsoncba270d2010-07-13 21:16:48 +00003645 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3646 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003647}
3648
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003649static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3650 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003651 unsigned NumElts = VT.getVectorNumElements();
3652 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003653
3654 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3655 if (M[0] < 0)
3656 return false;
3657
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003658 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003659
3660 // If this is a VEXT shuffle, the immediate value is the index of the first
3661 // element. The other shuffle indices must be the successive elements after
3662 // the first one.
3663 unsigned ExpectedElt = Imm;
3664 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003665 // Increment the expected index. If it wraps around, it may still be
3666 // a VEXT but the source vectors must be swapped.
3667 ExpectedElt += 1;
3668 if (ExpectedElt == NumElts * 2) {
3669 ExpectedElt = 0;
3670 ReverseVEXT = true;
3671 }
3672
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003673 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003674 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003675 return false;
3676 }
3677
3678 // Adjust the index value if the source operands will be swapped.
3679 if (ReverseVEXT)
3680 Imm -= NumElts;
3681
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003682 return true;
3683}
3684
Bob Wilson8bb9e482009-07-26 00:39:34 +00003685/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3686/// instruction with the specified blocksize. (The order of the elements
3687/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003688static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3689 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003690 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3691 "Only possible block sizes for VREV are: 16, 32, 64");
3692
Bob Wilson8bb9e482009-07-26 00:39:34 +00003693 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003694 if (EltSz == 64)
3695 return false;
3696
3697 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003698 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003699 // If the first shuffle index is UNDEF, be optimistic.
3700 if (M[0] < 0)
3701 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003702
3703 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3704 return false;
3705
3706 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003707 if (M[i] < 0) continue; // ignore UNDEF indices
3708 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003709 return false;
3710 }
3711
3712 return true;
3713}
3714
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003715static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3716 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3717 // range, then 0 is placed into the resulting vector. So pretty much any mask
3718 // of 8 elements can work here.
3719 return VT == MVT::v8i8 && M.size() == 8;
3720}
3721
Bob Wilsonc692cb72009-08-21 20:54:19 +00003722static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3723 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003724 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3725 if (EltSz == 64)
3726 return false;
3727
Bob Wilsonc692cb72009-08-21 20:54:19 +00003728 unsigned NumElts = VT.getVectorNumElements();
3729 WhichResult = (M[0] == 0 ? 0 : 1);
3730 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003731 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3732 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003733 return false;
3734 }
3735 return true;
3736}
3737
Bob Wilson324f4f12009-12-03 06:40:55 +00003738/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3739/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3740/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3741static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3742 unsigned &WhichResult) {
3743 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3744 if (EltSz == 64)
3745 return false;
3746
3747 unsigned NumElts = VT.getVectorNumElements();
3748 WhichResult = (M[0] == 0 ? 0 : 1);
3749 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003750 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3751 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003752 return false;
3753 }
3754 return true;
3755}
3756
Bob Wilsonc692cb72009-08-21 20:54:19 +00003757static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3758 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003759 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3760 if (EltSz == 64)
3761 return false;
3762
Bob Wilsonc692cb72009-08-21 20:54:19 +00003763 unsigned NumElts = VT.getVectorNumElements();
3764 WhichResult = (M[0] == 0 ? 0 : 1);
3765 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003766 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003767 if ((unsigned) M[i] != 2 * i + WhichResult)
3768 return false;
3769 }
3770
3771 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003772 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003773 return false;
3774
3775 return true;
3776}
3777
Bob Wilson324f4f12009-12-03 06:40:55 +00003778/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3779/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3780/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3781static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3782 unsigned &WhichResult) {
3783 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3784 if (EltSz == 64)
3785 return false;
3786
3787 unsigned Half = VT.getVectorNumElements() / 2;
3788 WhichResult = (M[0] == 0 ? 0 : 1);
3789 for (unsigned j = 0; j != 2; ++j) {
3790 unsigned Idx = WhichResult;
3791 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003792 int MIdx = M[i + j * Half];
3793 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003794 return false;
3795 Idx += 2;
3796 }
3797 }
3798
3799 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3800 if (VT.is64BitVector() && EltSz == 32)
3801 return false;
3802
3803 return true;
3804}
3805
Bob Wilsonc692cb72009-08-21 20:54:19 +00003806static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3807 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003808 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3809 if (EltSz == 64)
3810 return false;
3811
Bob Wilsonc692cb72009-08-21 20:54:19 +00003812 unsigned NumElts = VT.getVectorNumElements();
3813 WhichResult = (M[0] == 0 ? 0 : 1);
3814 unsigned Idx = WhichResult * NumElts / 2;
3815 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003816 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3817 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003818 return false;
3819 Idx += 1;
3820 }
3821
3822 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003823 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003824 return false;
3825
3826 return true;
3827}
3828
Bob Wilson324f4f12009-12-03 06:40:55 +00003829/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3830/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3831/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3832static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3833 unsigned &WhichResult) {
3834 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3835 if (EltSz == 64)
3836 return false;
3837
3838 unsigned NumElts = VT.getVectorNumElements();
3839 WhichResult = (M[0] == 0 ? 0 : 1);
3840 unsigned Idx = WhichResult * NumElts / 2;
3841 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003842 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3843 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003844 return false;
3845 Idx += 1;
3846 }
3847
3848 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3849 if (VT.is64BitVector() && EltSz == 32)
3850 return false;
3851
3852 return true;
3853}
3854
Dale Johannesenf630c712010-07-29 20:10:08 +00003855// If N is an integer constant that can be moved into a register in one
3856// instruction, return an SDValue of such a constant (will become a MOV
3857// instruction). Otherwise return null.
3858static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3859 const ARMSubtarget *ST, DebugLoc dl) {
3860 uint64_t Val;
3861 if (!isa<ConstantSDNode>(N))
3862 return SDValue();
3863 Val = cast<ConstantSDNode>(N)->getZExtValue();
3864
3865 if (ST->isThumb1Only()) {
3866 if (Val <= 255 || ~Val <= 255)
3867 return DAG.getConstant(Val, MVT::i32);
3868 } else {
3869 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3870 return DAG.getConstant(Val, MVT::i32);
3871 }
3872 return SDValue();
3873}
3874
Bob Wilson5bafff32009-06-22 23:27:02 +00003875// If this is a case we can't handle, return null and let the default
3876// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003877SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3878 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003879 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003880 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003881 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003882
3883 APInt SplatBits, SplatUndef;
3884 unsigned SplatBitSize;
3885 bool HasAnyUndefs;
3886 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003887 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003888 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003889 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003890 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003891 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003892 DAG, VmovVT, VT.is128BitVector(),
3893 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003894 if (Val.getNode()) {
3895 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003896 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003897 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003898
3899 // Try an immediate VMVN.
3900 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3901 ((1LL << SplatBitSize) - 1));
3902 Val = isNEONModifiedImm(NegatedImm,
3903 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003904 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003905 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003906 if (Val.getNode()) {
3907 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003908 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003909 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003910 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003911 }
3912
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003913 // Scan through the operands to see if only one value is used.
3914 unsigned NumElts = VT.getVectorNumElements();
3915 bool isOnlyLowElement = true;
3916 bool usesOnlyOneValue = true;
3917 bool isConstant = true;
3918 SDValue Value;
3919 for (unsigned i = 0; i < NumElts; ++i) {
3920 SDValue V = Op.getOperand(i);
3921 if (V.getOpcode() == ISD::UNDEF)
3922 continue;
3923 if (i > 0)
3924 isOnlyLowElement = false;
3925 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3926 isConstant = false;
3927
3928 if (!Value.getNode())
3929 Value = V;
3930 else if (V != Value)
3931 usesOnlyOneValue = false;
3932 }
3933
3934 if (!Value.getNode())
3935 return DAG.getUNDEF(VT);
3936
3937 if (isOnlyLowElement)
3938 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3939
Dale Johannesenf630c712010-07-29 20:10:08 +00003940 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3941
Dale Johannesen575cd142010-10-19 20:00:17 +00003942 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3943 // i32 and try again.
3944 if (usesOnlyOneValue && EltSize <= 32) {
3945 if (!isConstant)
3946 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3947 if (VT.getVectorElementType().isFloatingPoint()) {
3948 SmallVector<SDValue, 8> Ops;
3949 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003950 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003951 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003952 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3953 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003954 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3955 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003956 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003957 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003958 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3959 if (Val.getNode())
3960 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003961 }
3962
3963 // If all elements are constants and the case above didn't get hit, fall back
3964 // to the default expansion, which will generate a load from the constant
3965 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003966 if (isConstant)
3967 return SDValue();
3968
Bob Wilson11a1dff2011-01-07 21:37:30 +00003969 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3970 if (NumElts >= 4) {
3971 SDValue shuffle = ReconstructShuffle(Op, DAG);
3972 if (shuffle != SDValue())
3973 return shuffle;
3974 }
3975
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003976 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003977 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3978 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003979 if (EltSize >= 32) {
3980 // Do the expansion with floating-point types, since that is what the VFP
3981 // registers are defined to use, and since i64 is not legal.
3982 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3983 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003984 SmallVector<SDValue, 8> Ops;
3985 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003986 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003987 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003988 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003989 }
3990
3991 return SDValue();
3992}
3993
Bob Wilson11a1dff2011-01-07 21:37:30 +00003994// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003995// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003996SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3997 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003998 DebugLoc dl = Op.getDebugLoc();
3999 EVT VT = Op.getValueType();
4000 unsigned NumElts = VT.getVectorNumElements();
4001
4002 SmallVector<SDValue, 2> SourceVecs;
4003 SmallVector<unsigned, 2> MinElts;
4004 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004005
Bob Wilson11a1dff2011-01-07 21:37:30 +00004006 for (unsigned i = 0; i < NumElts; ++i) {
4007 SDValue V = Op.getOperand(i);
4008 if (V.getOpcode() == ISD::UNDEF)
4009 continue;
4010 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4011 // A shuffle can only come from building a vector from various
4012 // elements of other vectors.
4013 return SDValue();
4014 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004015
Bob Wilson11a1dff2011-01-07 21:37:30 +00004016 // Record this extraction against the appropriate vector if possible...
4017 SDValue SourceVec = V.getOperand(0);
4018 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4019 bool FoundSource = false;
4020 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4021 if (SourceVecs[j] == SourceVec) {
4022 if (MinElts[j] > EltNo)
4023 MinElts[j] = EltNo;
4024 if (MaxElts[j] < EltNo)
4025 MaxElts[j] = EltNo;
4026 FoundSource = true;
4027 break;
4028 }
4029 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004030
Bob Wilson11a1dff2011-01-07 21:37:30 +00004031 // Or record a new source if not...
4032 if (!FoundSource) {
4033 SourceVecs.push_back(SourceVec);
4034 MinElts.push_back(EltNo);
4035 MaxElts.push_back(EltNo);
4036 }
4037 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004038
Bob Wilson11a1dff2011-01-07 21:37:30 +00004039 // Currently only do something sane when at most two source vectors
4040 // involved.
4041 if (SourceVecs.size() > 2)
4042 return SDValue();
4043
4044 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4045 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004046
Bob Wilson11a1dff2011-01-07 21:37:30 +00004047 // This loop extracts the usage patterns of the source vectors
4048 // and prepares appropriate SDValues for a shuffle if possible.
4049 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4050 if (SourceVecs[i].getValueType() == VT) {
4051 // No VEXT necessary
4052 ShuffleSrcs[i] = SourceVecs[i];
4053 VEXTOffsets[i] = 0;
4054 continue;
4055 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4056 // It probably isn't worth padding out a smaller vector just to
4057 // break it down again in a shuffle.
4058 return SDValue();
4059 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004060
Bob Wilson11a1dff2011-01-07 21:37:30 +00004061 // Since only 64-bit and 128-bit vectors are legal on ARM and
4062 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004063 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4064 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004065
Bob Wilson11a1dff2011-01-07 21:37:30 +00004066 if (MaxElts[i] - MinElts[i] >= NumElts) {
4067 // Span too large for a VEXT to cope
4068 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004069 }
4070
Bob Wilson11a1dff2011-01-07 21:37:30 +00004071 if (MinElts[i] >= NumElts) {
4072 // The extraction can just take the second half
4073 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004074 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4075 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004076 DAG.getIntPtrConstant(NumElts));
4077 } else if (MaxElts[i] < NumElts) {
4078 // The extraction can just take the first half
4079 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004080 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4081 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004082 DAG.getIntPtrConstant(0));
4083 } else {
4084 // An actual VEXT is needed
4085 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004086 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4087 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004088 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004089 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4090 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004091 DAG.getIntPtrConstant(NumElts));
4092 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4093 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4094 }
4095 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004096
Bob Wilson11a1dff2011-01-07 21:37:30 +00004097 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004098
Bob Wilson11a1dff2011-01-07 21:37:30 +00004099 for (unsigned i = 0; i < NumElts; ++i) {
4100 SDValue Entry = Op.getOperand(i);
4101 if (Entry.getOpcode() == ISD::UNDEF) {
4102 Mask.push_back(-1);
4103 continue;
4104 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004105
Bob Wilson11a1dff2011-01-07 21:37:30 +00004106 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004107 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4108 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004109 if (ExtractVec == SourceVecs[0]) {
4110 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4111 } else {
4112 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4113 }
4114 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004115
Bob Wilson11a1dff2011-01-07 21:37:30 +00004116 // Final check before we try to produce nonsense...
4117 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004118 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4119 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004120
Bob Wilson11a1dff2011-01-07 21:37:30 +00004121 return SDValue();
4122}
4123
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004124/// isShuffleMaskLegal - Targets can use this to indicate that they only
4125/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4126/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4127/// are assumed to be legal.
4128bool
4129ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4130 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004131 if (VT.getVectorNumElements() == 4 &&
4132 (VT.is128BitVector() || VT.is64BitVector())) {
4133 unsigned PFIndexes[4];
4134 for (unsigned i = 0; i != 4; ++i) {
4135 if (M[i] < 0)
4136 PFIndexes[i] = 8;
4137 else
4138 PFIndexes[i] = M[i];
4139 }
4140
4141 // Compute the index in the perfect shuffle table.
4142 unsigned PFTableIndex =
4143 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4144 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4145 unsigned Cost = (PFEntry >> 30);
4146
4147 if (Cost <= 4)
4148 return true;
4149 }
4150
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004151 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004152 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004153
Bob Wilson53dd2452010-06-07 23:53:38 +00004154 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4155 return (EltSize >= 32 ||
4156 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004157 isVREVMask(M, VT, 64) ||
4158 isVREVMask(M, VT, 32) ||
4159 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004160 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004161 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004162 isVTRNMask(M, VT, WhichResult) ||
4163 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004164 isVZIPMask(M, VT, WhichResult) ||
4165 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4166 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4167 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004168}
4169
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004170/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4171/// the specified operations to build the shuffle.
4172static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4173 SDValue RHS, SelectionDAG &DAG,
4174 DebugLoc dl) {
4175 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4176 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4177 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4178
4179 enum {
4180 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4181 OP_VREV,
4182 OP_VDUP0,
4183 OP_VDUP1,
4184 OP_VDUP2,
4185 OP_VDUP3,
4186 OP_VEXT1,
4187 OP_VEXT2,
4188 OP_VEXT3,
4189 OP_VUZPL, // VUZP, left result
4190 OP_VUZPR, // VUZP, right result
4191 OP_VZIPL, // VZIP, left result
4192 OP_VZIPR, // VZIP, right result
4193 OP_VTRNL, // VTRN, left result
4194 OP_VTRNR // VTRN, right result
4195 };
4196
4197 if (OpNum == OP_COPY) {
4198 if (LHSID == (1*9+2)*9+3) return LHS;
4199 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4200 return RHS;
4201 }
4202
4203 SDValue OpLHS, OpRHS;
4204 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4205 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4206 EVT VT = OpLHS.getValueType();
4207
4208 switch (OpNum) {
4209 default: llvm_unreachable("Unknown shuffle opcode!");
4210 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004211 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004212 if (VT.getVectorElementType() == MVT::i32 ||
4213 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004214 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4215 // vrev <4 x i16> -> VREV32
4216 if (VT.getVectorElementType() == MVT::i16)
4217 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4218 // vrev <4 x i8> -> VREV16
4219 assert(VT.getVectorElementType() == MVT::i8);
4220 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004221 case OP_VDUP0:
4222 case OP_VDUP1:
4223 case OP_VDUP2:
4224 case OP_VDUP3:
4225 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004226 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004227 case OP_VEXT1:
4228 case OP_VEXT2:
4229 case OP_VEXT3:
4230 return DAG.getNode(ARMISD::VEXT, dl, VT,
4231 OpLHS, OpRHS,
4232 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4233 case OP_VUZPL:
4234 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004235 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004236 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4237 case OP_VZIPL:
4238 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004239 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004240 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4241 case OP_VTRNL:
4242 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004243 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4244 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004245 }
4246}
4247
Bill Wendling69a05a72011-03-14 23:02:38 +00004248static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4249 SmallVectorImpl<int> &ShuffleMask,
4250 SelectionDAG &DAG) {
4251 // Check to see if we can use the VTBL instruction.
4252 SDValue V1 = Op.getOperand(0);
4253 SDValue V2 = Op.getOperand(1);
4254 DebugLoc DL = Op.getDebugLoc();
4255
4256 SmallVector<SDValue, 8> VTBLMask;
4257 for (SmallVectorImpl<int>::iterator
4258 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4259 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4260
4261 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4262 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4263 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4264 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004265
Owen Anderson76706012011-04-05 21:48:57 +00004266 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004267 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4268 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004269}
4270
Bob Wilson5bafff32009-06-22 23:27:02 +00004271static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004272 SDValue V1 = Op.getOperand(0);
4273 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004274 DebugLoc dl = Op.getDebugLoc();
4275 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004276 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004277 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004278
Bob Wilson28865062009-08-13 02:13:04 +00004279 // Convert shuffles that are directly supported on NEON to target-specific
4280 // DAG nodes, instead of keeping them as shuffles and matching them again
4281 // during code selection. This is more efficient and avoids the possibility
4282 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004283 // FIXME: floating-point vectors should be canonicalized to integer vectors
4284 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004285 SVN->getMask(ShuffleMask);
4286
Bob Wilson53dd2452010-06-07 23:53:38 +00004287 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4288 if (EltSize <= 32) {
4289 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4290 int Lane = SVN->getSplatIndex();
4291 // If this is undef splat, generate it via "just" vdup, if possible.
4292 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004293
Bob Wilson53dd2452010-06-07 23:53:38 +00004294 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4295 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4296 }
4297 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4298 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004299 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004300
4301 bool ReverseVEXT;
4302 unsigned Imm;
4303 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4304 if (ReverseVEXT)
4305 std::swap(V1, V2);
4306 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4307 DAG.getConstant(Imm, MVT::i32));
4308 }
4309
4310 if (isVREVMask(ShuffleMask, VT, 64))
4311 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4312 if (isVREVMask(ShuffleMask, VT, 32))
4313 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4314 if (isVREVMask(ShuffleMask, VT, 16))
4315 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4316
4317 // Check for Neon shuffles that modify both input vectors in place.
4318 // If both results are used, i.e., if there are two shuffles with the same
4319 // source operands and with masks corresponding to both results of one of
4320 // these operations, DAG memoization will ensure that a single node is
4321 // used for both shuffles.
4322 unsigned WhichResult;
4323 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4324 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4325 V1, V2).getValue(WhichResult);
4326 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4327 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4328 V1, V2).getValue(WhichResult);
4329 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4330 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4331 V1, V2).getValue(WhichResult);
4332
4333 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4334 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4335 V1, V1).getValue(WhichResult);
4336 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4337 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4338 V1, V1).getValue(WhichResult);
4339 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4340 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4341 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004342 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004343
Bob Wilsonc692cb72009-08-21 20:54:19 +00004344 // If the shuffle is not directly supported and it has 4 elements, use
4345 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004346 unsigned NumElts = VT.getVectorNumElements();
4347 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004348 unsigned PFIndexes[4];
4349 for (unsigned i = 0; i != 4; ++i) {
4350 if (ShuffleMask[i] < 0)
4351 PFIndexes[i] = 8;
4352 else
4353 PFIndexes[i] = ShuffleMask[i];
4354 }
4355
4356 // Compute the index in the perfect shuffle table.
4357 unsigned PFTableIndex =
4358 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004359 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4360 unsigned Cost = (PFEntry >> 30);
4361
4362 if (Cost <= 4)
4363 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4364 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004365
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004366 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004367 if (EltSize >= 32) {
4368 // Do the expansion with floating-point types, since that is what the VFP
4369 // registers are defined to use, and since i64 is not legal.
4370 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4371 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004372 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4373 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004374 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004375 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004376 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004377 Ops.push_back(DAG.getUNDEF(EltVT));
4378 else
4379 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4380 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4381 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4382 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004383 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004384 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004385 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004386 }
4387
Bill Wendling69a05a72011-03-14 23:02:38 +00004388 if (VT == MVT::v8i8) {
4389 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4390 if (NewOp.getNode())
4391 return NewOp;
4392 }
4393
Bob Wilson22cac0d2009-08-14 05:16:33 +00004394 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004395}
4396
Bob Wilson5bafff32009-06-22 23:27:02 +00004397static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004398 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004399 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004400 if (!isa<ConstantSDNode>(Lane))
4401 return SDValue();
4402
4403 SDValue Vec = Op.getOperand(0);
4404 if (Op.getValueType() == MVT::i32 &&
4405 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4406 DebugLoc dl = Op.getDebugLoc();
4407 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4408 }
4409
4410 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004411}
4412
Bob Wilsona6d65862009-08-03 20:36:38 +00004413static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4414 // The only time a CONCAT_VECTORS operation can have legal types is when
4415 // two 64-bit vectors are concatenated to a 128-bit vector.
4416 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4417 "unexpected CONCAT_VECTORS");
4418 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004419 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004420 SDValue Op0 = Op.getOperand(0);
4421 SDValue Op1 = Op.getOperand(1);
4422 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004423 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004424 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004425 DAG.getIntPtrConstant(0));
4426 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004427 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004428 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004429 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004430 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004431}
4432
Bob Wilson626613d2010-11-23 19:38:38 +00004433/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4434/// element has been zero/sign-extended, depending on the isSigned parameter,
4435/// from an integer type half its size.
4436static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4437 bool isSigned) {
4438 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4439 EVT VT = N->getValueType(0);
4440 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4441 SDNode *BVN = N->getOperand(0).getNode();
4442 if (BVN->getValueType(0) != MVT::v4i32 ||
4443 BVN->getOpcode() != ISD::BUILD_VECTOR)
4444 return false;
4445 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4446 unsigned HiElt = 1 - LoElt;
4447 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4448 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4449 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4450 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4451 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4452 return false;
4453 if (isSigned) {
4454 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4455 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4456 return true;
4457 } else {
4458 if (Hi0->isNullValue() && Hi1->isNullValue())
4459 return true;
4460 }
4461 return false;
4462 }
4463
4464 if (N->getOpcode() != ISD::BUILD_VECTOR)
4465 return false;
4466
4467 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4468 SDNode *Elt = N->getOperand(i).getNode();
4469 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4470 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4471 unsigned HalfSize = EltSize / 2;
4472 if (isSigned) {
4473 int64_t SExtVal = C->getSExtValue();
4474 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4475 return false;
4476 } else {
4477 if ((C->getZExtValue() >> HalfSize) != 0)
4478 return false;
4479 }
4480 continue;
4481 }
4482 return false;
4483 }
4484
4485 return true;
4486}
4487
4488/// isSignExtended - Check if a node is a vector value that is sign-extended
4489/// or a constant BUILD_VECTOR with sign-extended elements.
4490static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4491 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4492 return true;
4493 if (isExtendedBUILD_VECTOR(N, DAG, true))
4494 return true;
4495 return false;
4496}
4497
4498/// isZeroExtended - Check if a node is a vector value that is zero-extended
4499/// or a constant BUILD_VECTOR with zero-extended elements.
4500static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4501 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4502 return true;
4503 if (isExtendedBUILD_VECTOR(N, DAG, false))
4504 return true;
4505 return false;
4506}
4507
4508/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4509/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004510static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4511 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4512 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004513 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4514 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4515 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4516 LD->isNonTemporal(), LD->getAlignment());
4517 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4518 // have been legalized as a BITCAST from v4i32.
4519 if (N->getOpcode() == ISD::BITCAST) {
4520 SDNode *BVN = N->getOperand(0).getNode();
4521 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4522 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4523 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4524 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4525 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4526 }
4527 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4528 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4529 EVT VT = N->getValueType(0);
4530 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4531 unsigned NumElts = VT.getVectorNumElements();
4532 MVT TruncVT = MVT::getIntegerVT(EltSize);
4533 SmallVector<SDValue, 8> Ops;
4534 for (unsigned i = 0; i != NumElts; ++i) {
4535 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4536 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004537 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004538 }
4539 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4540 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004541}
4542
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004543static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4544 unsigned Opcode = N->getOpcode();
4545 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4546 SDNode *N0 = N->getOperand(0).getNode();
4547 SDNode *N1 = N->getOperand(1).getNode();
4548 return N0->hasOneUse() && N1->hasOneUse() &&
4549 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4550 }
4551 return false;
4552}
4553
4554static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4555 unsigned Opcode = N->getOpcode();
4556 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4557 SDNode *N0 = N->getOperand(0).getNode();
4558 SDNode *N1 = N->getOperand(1).getNode();
4559 return N0->hasOneUse() && N1->hasOneUse() &&
4560 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4561 }
4562 return false;
4563}
4564
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004565static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4566 // Multiplications are only custom-lowered for 128-bit vectors so that
4567 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4568 EVT VT = Op.getValueType();
4569 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4570 SDNode *N0 = Op.getOperand(0).getNode();
4571 SDNode *N1 = Op.getOperand(1).getNode();
4572 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004573 bool isMLA = false;
4574 bool isN0SExt = isSignExtended(N0, DAG);
4575 bool isN1SExt = isSignExtended(N1, DAG);
4576 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004577 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004578 else {
4579 bool isN0ZExt = isZeroExtended(N0, DAG);
4580 bool isN1ZExt = isZeroExtended(N1, DAG);
4581 if (isN0ZExt && isN1ZExt)
4582 NewOpc = ARMISD::VMULLu;
4583 else if (isN1SExt || isN1ZExt) {
4584 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4585 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4586 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4587 NewOpc = ARMISD::VMULLs;
4588 isMLA = true;
4589 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4590 NewOpc = ARMISD::VMULLu;
4591 isMLA = true;
4592 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4593 std::swap(N0, N1);
4594 NewOpc = ARMISD::VMULLu;
4595 isMLA = true;
4596 }
4597 }
4598
4599 if (!NewOpc) {
4600 if (VT == MVT::v2i64)
4601 // Fall through to expand this. It is not legal.
4602 return SDValue();
4603 else
4604 // Other vector multiplications are legal.
4605 return Op;
4606 }
4607 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004608
4609 // Legalize to a VMULL instruction.
4610 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004611 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004612 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004613 if (!isMLA) {
4614 Op0 = SkipExtension(N0, DAG);
4615 assert(Op0.getValueType().is64BitVector() &&
4616 Op1.getValueType().is64BitVector() &&
4617 "unexpected types for extended operands to VMULL");
4618 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4619 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004620
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004621 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4622 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4623 // vmull q0, d4, d6
4624 // vmlal q0, d5, d6
4625 // is faster than
4626 // vaddl q0, d4, d5
4627 // vmovl q1, d6
4628 // vmul q0, q0, q1
4629 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4630 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4631 EVT Op1VT = Op1.getValueType();
4632 return DAG.getNode(N0->getOpcode(), DL, VT,
4633 DAG.getNode(NewOpc, DL, VT,
4634 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4635 DAG.getNode(NewOpc, DL, VT,
4636 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004637}
4638
Owen Anderson76706012011-04-05 21:48:57 +00004639static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004640LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4641 // Convert to float
4642 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4643 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4644 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4645 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4646 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4647 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4648 // Get reciprocal estimate.
4649 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004650 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004651 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4652 // Because char has a smaller range than uchar, we can actually get away
4653 // without any newton steps. This requires that we use a weird bias
4654 // of 0xb000, however (again, this has been exhaustively tested).
4655 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4656 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4657 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4658 Y = DAG.getConstant(0xb000, MVT::i32);
4659 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4660 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4661 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4662 // Convert back to short.
4663 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4664 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4665 return X;
4666}
4667
Owen Anderson76706012011-04-05 21:48:57 +00004668static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004669LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4670 SDValue N2;
4671 // Convert to float.
4672 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4673 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4674 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4675 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4676 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4677 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004678
Nate Begeman7973f352011-02-11 20:53:29 +00004679 // Use reciprocal estimate and one refinement step.
4680 // float4 recip = vrecpeq_f32(yf);
4681 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004682 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004683 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004684 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004685 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4686 N1, N2);
4687 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4688 // Because short has a smaller range than ushort, we can actually get away
4689 // with only a single newton step. This requires that we use a weird bias
4690 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004691 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004692 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4693 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004694 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004695 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4696 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4697 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4698 // Convert back to integer and return.
4699 // return vmovn_s32(vcvt_s32_f32(result));
4700 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4701 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4702 return N0;
4703}
4704
4705static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4706 EVT VT = Op.getValueType();
4707 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4708 "unexpected type for custom-lowering ISD::SDIV");
4709
4710 DebugLoc dl = Op.getDebugLoc();
4711 SDValue N0 = Op.getOperand(0);
4712 SDValue N1 = Op.getOperand(1);
4713 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004714
Nate Begeman7973f352011-02-11 20:53:29 +00004715 if (VT == MVT::v8i8) {
4716 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4717 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004718
Nate Begeman7973f352011-02-11 20:53:29 +00004719 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4720 DAG.getIntPtrConstant(4));
4721 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004722 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004723 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4724 DAG.getIntPtrConstant(0));
4725 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4726 DAG.getIntPtrConstant(0));
4727
4728 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4729 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4730
4731 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4732 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004733
Nate Begeman7973f352011-02-11 20:53:29 +00004734 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4735 return N0;
4736 }
4737 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4738}
4739
4740static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4741 EVT VT = Op.getValueType();
4742 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4743 "unexpected type for custom-lowering ISD::UDIV");
4744
4745 DebugLoc dl = Op.getDebugLoc();
4746 SDValue N0 = Op.getOperand(0);
4747 SDValue N1 = Op.getOperand(1);
4748 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004749
Nate Begeman7973f352011-02-11 20:53:29 +00004750 if (VT == MVT::v8i8) {
4751 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4752 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004753
Nate Begeman7973f352011-02-11 20:53:29 +00004754 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4755 DAG.getIntPtrConstant(4));
4756 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004757 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004758 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4759 DAG.getIntPtrConstant(0));
4760 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4761 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004762
Nate Begeman7973f352011-02-11 20:53:29 +00004763 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4764 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004765
Nate Begeman7973f352011-02-11 20:53:29 +00004766 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4767 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004768
4769 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004770 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4771 N0);
4772 return N0;
4773 }
Owen Anderson76706012011-04-05 21:48:57 +00004774
Nate Begeman7973f352011-02-11 20:53:29 +00004775 // v4i16 sdiv ... Convert to float.
4776 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4777 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4778 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4779 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4780 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004781 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004782
4783 // Use reciprocal estimate and two refinement steps.
4784 // float4 recip = vrecpeq_f32(yf);
4785 // recip *= vrecpsq_f32(yf, recip);
4786 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004787 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004788 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004789 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004790 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004791 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004792 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004793 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004794 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004795 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004796 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4797 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4798 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4799 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004800 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004801 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4802 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4803 N1 = DAG.getConstant(2, MVT::i32);
4804 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4805 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4806 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4807 // Convert back to integer and return.
4808 // return vmovn_u32(vcvt_s32_f32(result));
4809 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4810 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4811 return N0;
4812}
4813
Dan Gohmand858e902010-04-17 15:26:15 +00004814SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004815 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004816 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004817 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004818 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004819 case ISD::GlobalAddress:
4820 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4821 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004822 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004823 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004824 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4825 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004826 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004827 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004828 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00004829 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(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;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005121 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005122 break;
5123 case 2:
5124 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5125 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005126 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005127 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 Grosbachc5a8c862011-07-27 16:47:19 +00005171 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5172 .addReg(dest)
5173 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005174 }
5175
5176 // Build compare and cmov instructions.
5177 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5178 .addReg(oldval).addReg(incr));
5179 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5180 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5181
5182 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5183 .addReg(ptr));
5184 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5185 .addReg(scratch).addImm(0));
5186 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5187 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5188
5189 BB->addSuccessor(loopMBB);
5190 BB->addSuccessor(exitMBB);
5191
5192 // exitMBB:
5193 // ...
5194 BB = exitMBB;
5195
5196 MI->eraseFromParent(); // The instruction is gone now.
5197
5198 return BB;
5199}
5200
Evan Cheng218977b2010-07-13 19:27:42 +00005201static
5202MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5203 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5204 E = MBB->succ_end(); I != E; ++I)
5205 if (*I != Succ)
5206 return *I;
5207 llvm_unreachable("Expecting a BB with two successors!");
5208}
5209
Andrew Trick1c3af772011-04-23 03:55:32 +00005210// FIXME: This opcode table should obviously be expressed in the target
5211// description. We probably just need a "machine opcode" value in the pseudo
5212// instruction. But the ideal solution maybe to simply remove the "S" version
5213// of the opcode altogether.
5214struct AddSubFlagsOpcodePair {
5215 unsigned PseudoOpc;
5216 unsigned MachineOpc;
5217};
5218
5219static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5220 {ARM::ADCSri, ARM::ADCri},
5221 {ARM::ADCSrr, ARM::ADCrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005222 {ARM::ADCSrsi, ARM::ADCrsi},
5223 {ARM::ADCSrsr, ARM::ADCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005224 {ARM::SBCSri, ARM::SBCri},
5225 {ARM::SBCSrr, ARM::SBCrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005226 {ARM::SBCSrsi, ARM::SBCrsi},
5227 {ARM::SBCSrsr, ARM::SBCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005228 {ARM::RSBSri, ARM::RSBri},
5229 {ARM::RSBSrr, ARM::RSBrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005230 {ARM::RSBSrsi, ARM::RSBrsi},
5231 {ARM::RSBSrsr, ARM::RSBrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005232 {ARM::RSCSri, ARM::RSCri},
Owen Anderson92a20222011-07-21 18:54:16 +00005233 {ARM::RSCSrsi, ARM::RSCrsi},
5234 {ARM::RSCSrsr, ARM::RSCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005235 {ARM::t2ADCSri, ARM::t2ADCri},
5236 {ARM::t2ADCSrr, ARM::t2ADCrr},
5237 {ARM::t2ADCSrs, ARM::t2ADCrs},
5238 {ARM::t2SBCSri, ARM::t2SBCri},
5239 {ARM::t2SBCSrr, ARM::t2SBCrr},
5240 {ARM::t2SBCSrs, ARM::t2SBCrs},
5241 {ARM::t2RSBSri, ARM::t2RSBri},
5242 {ARM::t2RSBSrs, ARM::t2RSBrs},
5243};
5244
5245// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5246// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5247//
5248// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5249// position to be recognized by the target descrition as the 'S' bit.
5250bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5251 MachineBasicBlock *BB) const {
5252 unsigned OldOpc = MI->getOpcode();
5253 unsigned NewOpc = 0;
5254
5255 // This is only called for instructions that need remapping, so iterating over
5256 // the tiny opcode table is not costly.
5257 static const int NPairs =
5258 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5259 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5260 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5261 if (OldOpc == Pair->PseudoOpc) {
5262 NewOpc = Pair->MachineOpc;
5263 break;
5264 }
5265 }
5266 if (!NewOpc)
5267 return false;
5268
5269 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5270 DebugLoc dl = MI->getDebugLoc();
5271 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5272 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5273 MIB.addOperand(MI->getOperand(i));
5274 AddDefaultPred(MIB);
5275 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5276 MI->eraseFromParent();
5277 return true;
5278}
5279
Jim Grosbache801dc42009-12-12 01:40:06 +00005280MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005281ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005282 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005283 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005284 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005285 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005286 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005287 default: {
5288 if (RemapAddSubWithFlags(MI, BB))
5289 return BB;
5290
Jim Grosbach5278eb82009-12-11 01:42:04 +00005291 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005292 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005293 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005294 case ARM::STRi_preidx:
5295 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00005296 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00005297 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
5298 // Decode the offset.
5299 unsigned Offset = MI->getOperand(4).getImm();
5300 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
5301 Offset = ARM_AM::getAM2Offset(Offset);
5302 if (isSub)
5303 Offset = -Offset;
5304
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005305 MachineMemOperand *MMO = *MI->memoperands_begin();
Jim Grosbach19dec202011-08-05 20:35:44 +00005306 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc))
5307 .addOperand(MI->getOperand(0)) // Rn_wb
5308 .addOperand(MI->getOperand(1)) // Rt
5309 .addOperand(MI->getOperand(2)) // Rn
5310 .addImm(Offset) // offset (skip GPR==zero_reg)
5311 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005312 .addOperand(MI->getOperand(6))
5313 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00005314 MI->eraseFromParent();
5315 return BB;
5316 }
5317 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00005318 case ARM::STRBr_preidx:
5319 case ARM::STRH_preidx: {
5320 unsigned NewOpc;
5321 switch (MI->getOpcode()) {
5322 default: llvm_unreachable("unexpected opcode!");
5323 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
5324 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
5325 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
5326 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005327 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5328 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5329 MIB.addOperand(MI->getOperand(i));
5330 MI->eraseFromParent();
5331 return BB;
5332 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005333 case ARM::ATOMIC_LOAD_ADD_I8:
5334 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5335 case ARM::ATOMIC_LOAD_ADD_I16:
5336 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5337 case ARM::ATOMIC_LOAD_ADD_I32:
5338 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005339
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005340 case ARM::ATOMIC_LOAD_AND_I8:
5341 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5342 case ARM::ATOMIC_LOAD_AND_I16:
5343 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5344 case ARM::ATOMIC_LOAD_AND_I32:
5345 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005346
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005347 case ARM::ATOMIC_LOAD_OR_I8:
5348 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5349 case ARM::ATOMIC_LOAD_OR_I16:
5350 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5351 case ARM::ATOMIC_LOAD_OR_I32:
5352 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005353
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005354 case ARM::ATOMIC_LOAD_XOR_I8:
5355 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5356 case ARM::ATOMIC_LOAD_XOR_I16:
5357 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5358 case ARM::ATOMIC_LOAD_XOR_I32:
5359 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005360
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005361 case ARM::ATOMIC_LOAD_NAND_I8:
5362 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5363 case ARM::ATOMIC_LOAD_NAND_I16:
5364 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5365 case ARM::ATOMIC_LOAD_NAND_I32:
5366 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005367
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005368 case ARM::ATOMIC_LOAD_SUB_I8:
5369 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5370 case ARM::ATOMIC_LOAD_SUB_I16:
5371 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5372 case ARM::ATOMIC_LOAD_SUB_I32:
5373 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005374
Jim Grosbachf7da8822011-04-26 19:44:18 +00005375 case ARM::ATOMIC_LOAD_MIN_I8:
5376 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5377 case ARM::ATOMIC_LOAD_MIN_I16:
5378 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5379 case ARM::ATOMIC_LOAD_MIN_I32:
5380 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5381
5382 case ARM::ATOMIC_LOAD_MAX_I8:
5383 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5384 case ARM::ATOMIC_LOAD_MAX_I16:
5385 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5386 case ARM::ATOMIC_LOAD_MAX_I32:
5387 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5388
5389 case ARM::ATOMIC_LOAD_UMIN_I8:
5390 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5391 case ARM::ATOMIC_LOAD_UMIN_I16:
5392 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5393 case ARM::ATOMIC_LOAD_UMIN_I32:
5394 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5395
5396 case ARM::ATOMIC_LOAD_UMAX_I8:
5397 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5398 case ARM::ATOMIC_LOAD_UMAX_I16:
5399 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5400 case ARM::ATOMIC_LOAD_UMAX_I32:
5401 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5402
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005403 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5404 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5405 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005406
5407 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5408 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5409 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005410
Evan Cheng007ea272009-08-12 05:17:19 +00005411 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005412 // To "insert" a SELECT_CC instruction, we actually have to insert the
5413 // diamond control-flow pattern. The incoming instruction knows the
5414 // destination vreg to set, the condition code register to branch on, the
5415 // true/false values to select between, and a branch opcode to use.
5416 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005417 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005418 ++It;
5419
5420 // thisMBB:
5421 // ...
5422 // TrueVal = ...
5423 // cmpTY ccX, r1, r2
5424 // bCC copy1MBB
5425 // fallthrough --> copy0MBB
5426 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005427 MachineFunction *F = BB->getParent();
5428 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5429 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005430 F->insert(It, copy0MBB);
5431 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005432
5433 // Transfer the remainder of BB and its successor edges to sinkMBB.
5434 sinkMBB->splice(sinkMBB->begin(), BB,
5435 llvm::next(MachineBasicBlock::iterator(MI)),
5436 BB->end());
5437 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5438
Dan Gohman258c58c2010-07-06 15:49:48 +00005439 BB->addSuccessor(copy0MBB);
5440 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005441
Dan Gohman14152b42010-07-06 20:24:04 +00005442 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5443 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5444
Evan Chenga8e29892007-01-19 07:51:42 +00005445 // copy0MBB:
5446 // %FalseValue = ...
5447 // # fallthrough to sinkMBB
5448 BB = copy0MBB;
5449
5450 // Update machine-CFG edges
5451 BB->addSuccessor(sinkMBB);
5452
5453 // sinkMBB:
5454 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5455 // ...
5456 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005457 BuildMI(*BB, BB->begin(), dl,
5458 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005459 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5460 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5461
Dan Gohman14152b42010-07-06 20:24:04 +00005462 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005463 return BB;
5464 }
Evan Cheng86198642009-08-07 00:34:42 +00005465
Evan Cheng218977b2010-07-13 19:27:42 +00005466 case ARM::BCCi64:
5467 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005468 // If there is an unconditional branch to the other successor, remove it.
5469 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005470
Evan Cheng218977b2010-07-13 19:27:42 +00005471 // Compare both parts that make up the double comparison separately for
5472 // equality.
5473 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5474
5475 unsigned LHS1 = MI->getOperand(1).getReg();
5476 unsigned LHS2 = MI->getOperand(2).getReg();
5477 if (RHSisZero) {
5478 AddDefaultPred(BuildMI(BB, dl,
5479 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5480 .addReg(LHS1).addImm(0));
5481 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5482 .addReg(LHS2).addImm(0)
5483 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5484 } else {
5485 unsigned RHS1 = MI->getOperand(3).getReg();
5486 unsigned RHS2 = MI->getOperand(4).getReg();
5487 AddDefaultPred(BuildMI(BB, dl,
5488 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5489 .addReg(LHS1).addReg(RHS1));
5490 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5491 .addReg(LHS2).addReg(RHS2)
5492 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5493 }
5494
5495 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5496 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5497 if (MI->getOperand(0).getImm() == ARMCC::NE)
5498 std::swap(destMBB, exitMBB);
5499
5500 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5501 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5502 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5503 .addMBB(exitMBB);
5504
5505 MI->eraseFromParent(); // The pseudo instruction is gone now.
5506 return BB;
5507 }
Evan Chenga8e29892007-01-19 07:51:42 +00005508 }
5509}
5510
5511//===----------------------------------------------------------------------===//
5512// ARM Optimization Hooks
5513//===----------------------------------------------------------------------===//
5514
Chris Lattnerd1980a52009-03-12 06:52:53 +00005515static
5516SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5517 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005518 SelectionDAG &DAG = DCI.DAG;
5519 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005520 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005521 unsigned Opc = N->getOpcode();
5522 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5523 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5524 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5525 ISD::CondCode CC = ISD::SETCC_INVALID;
5526
5527 if (isSlctCC) {
5528 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5529 } else {
5530 SDValue CCOp = Slct.getOperand(0);
5531 if (CCOp.getOpcode() == ISD::SETCC)
5532 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5533 }
5534
5535 bool DoXform = false;
5536 bool InvCC = false;
5537 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5538 "Bad input!");
5539
5540 if (LHS.getOpcode() == ISD::Constant &&
5541 cast<ConstantSDNode>(LHS)->isNullValue()) {
5542 DoXform = true;
5543 } else if (CC != ISD::SETCC_INVALID &&
5544 RHS.getOpcode() == ISD::Constant &&
5545 cast<ConstantSDNode>(RHS)->isNullValue()) {
5546 std::swap(LHS, RHS);
5547 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005548 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005549 Op0.getOperand(0).getValueType();
5550 bool isInt = OpVT.isInteger();
5551 CC = ISD::getSetCCInverse(CC, isInt);
5552
5553 if (!TLI.isCondCodeLegal(CC, OpVT))
5554 return SDValue(); // Inverse operator isn't legal.
5555
5556 DoXform = true;
5557 InvCC = true;
5558 }
5559
5560 if (DoXform) {
5561 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5562 if (isSlctCC)
5563 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5564 Slct.getOperand(0), Slct.getOperand(1), CC);
5565 SDValue CCOp = Slct.getOperand(0);
5566 if (InvCC)
5567 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5568 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5569 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5570 CCOp, OtherOp, Result);
5571 }
5572 return SDValue();
5573}
5574
Eric Christopherfa6f5912011-06-29 21:10:36 +00005575// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00005576// (only after legalization).
5577static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5578 TargetLowering::DAGCombinerInfo &DCI,
5579 const ARMSubtarget *Subtarget) {
5580
5581 // Only perform optimization if after legalize, and if NEON is available. We
5582 // also expected both operands to be BUILD_VECTORs.
5583 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5584 || N0.getOpcode() != ISD::BUILD_VECTOR
5585 || N1.getOpcode() != ISD::BUILD_VECTOR)
5586 return SDValue();
5587
5588 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5589 EVT VT = N->getValueType(0);
5590 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5591 return SDValue();
5592
5593 // Check that the vector operands are of the right form.
5594 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5595 // operands, where N is the size of the formed vector.
5596 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5597 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005598
5599 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005600 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005601 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005602 SDValue Vec = N0->getOperand(0)->getOperand(0);
5603 SDNode *V = Vec.getNode();
5604 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005605
Eric Christopherfa6f5912011-06-29 21:10:36 +00005606 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00005607 // check to see if each of their operands are an EXTRACT_VECTOR with
5608 // the same vector and appropriate index.
5609 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5610 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5611 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00005612
Tanya Lattner189531f2011-06-14 23:48:48 +00005613 SDValue ExtVec0 = N0->getOperand(i);
5614 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005615
Tanya Lattner189531f2011-06-14 23:48:48 +00005616 // First operand is the vector, verify its the same.
5617 if (V != ExtVec0->getOperand(0).getNode() ||
5618 V != ExtVec1->getOperand(0).getNode())
5619 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00005620
Tanya Lattner189531f2011-06-14 23:48:48 +00005621 // Second is the constant, verify its correct.
5622 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5623 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00005624
Tanya Lattner189531f2011-06-14 23:48:48 +00005625 // For the constant, we want to see all the even or all the odd.
5626 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5627 || C1->getZExtValue() != nextIndex+1)
5628 return SDValue();
5629
5630 // Increment index.
5631 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005632 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00005633 return SDValue();
5634 }
5635
5636 // Create VPADDL node.
5637 SelectionDAG &DAG = DCI.DAG;
5638 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00005639
5640 // Build operand list.
5641 SmallVector<SDValue, 8> Ops;
5642 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5643 TLI.getPointerTy()));
5644
5645 // Input is the vector.
5646 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005647
Tanya Lattner189531f2011-06-14 23:48:48 +00005648 // Get widened type and narrowed type.
5649 MVT widenType;
5650 unsigned numElem = VT.getVectorNumElements();
5651 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5652 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5653 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5654 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5655 default:
5656 assert(0 && "Invalid vector element type for padd optimization.");
5657 }
5658
5659 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5660 widenType, &Ops[0], Ops.size());
5661 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5662}
5663
Bob Wilson3d5792a2010-07-29 20:34:14 +00005664/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5665/// operands N0 and N1. This is a helper for PerformADDCombine that is
5666/// called with the default operands, and if that fails, with commuted
5667/// operands.
5668static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005669 TargetLowering::DAGCombinerInfo &DCI,
5670 const ARMSubtarget *Subtarget){
5671
5672 // Attempt to create vpaddl for this add.
5673 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5674 if (Result.getNode())
5675 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005676
Chris Lattnerd1980a52009-03-12 06:52:53 +00005677 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5678 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5679 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5680 if (Result.getNode()) return Result;
5681 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005682 return SDValue();
5683}
5684
Bob Wilson3d5792a2010-07-29 20:34:14 +00005685/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5686///
5687static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005688 TargetLowering::DAGCombinerInfo &DCI,
5689 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005690 SDValue N0 = N->getOperand(0);
5691 SDValue N1 = N->getOperand(1);
5692
5693 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005694 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005695 if (Result.getNode())
5696 return Result;
5697
5698 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005699 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005700}
5701
Chris Lattnerd1980a52009-03-12 06:52:53 +00005702/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005703///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005704static SDValue PerformSUBCombine(SDNode *N,
5705 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005706 SDValue N0 = N->getOperand(0);
5707 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005708
Chris Lattnerd1980a52009-03-12 06:52:53 +00005709 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5710 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5711 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5712 if (Result.getNode()) return Result;
5713 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005714
Chris Lattnerd1980a52009-03-12 06:52:53 +00005715 return SDValue();
5716}
5717
Evan Cheng463d3582011-03-31 19:38:48 +00005718/// PerformVMULCombine
5719/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5720/// special multiplier accumulator forwarding.
5721/// vmul d3, d0, d2
5722/// vmla d3, d1, d2
5723/// is faster than
5724/// vadd d3, d0, d1
5725/// vmul d3, d3, d2
5726static SDValue PerformVMULCombine(SDNode *N,
5727 TargetLowering::DAGCombinerInfo &DCI,
5728 const ARMSubtarget *Subtarget) {
5729 if (!Subtarget->hasVMLxForwarding())
5730 return SDValue();
5731
5732 SelectionDAG &DAG = DCI.DAG;
5733 SDValue N0 = N->getOperand(0);
5734 SDValue N1 = N->getOperand(1);
5735 unsigned Opcode = N0.getOpcode();
5736 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5737 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00005738 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00005739 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5740 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5741 return SDValue();
5742 std::swap(N0, N1);
5743 }
5744
5745 EVT VT = N->getValueType(0);
5746 DebugLoc DL = N->getDebugLoc();
5747 SDValue N00 = N0->getOperand(0);
5748 SDValue N01 = N0->getOperand(1);
5749 return DAG.getNode(Opcode, DL, VT,
5750 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5751 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5752}
5753
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005754static SDValue PerformMULCombine(SDNode *N,
5755 TargetLowering::DAGCombinerInfo &DCI,
5756 const ARMSubtarget *Subtarget) {
5757 SelectionDAG &DAG = DCI.DAG;
5758
5759 if (Subtarget->isThumb1Only())
5760 return SDValue();
5761
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005762 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5763 return SDValue();
5764
5765 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005766 if (VT.is64BitVector() || VT.is128BitVector())
5767 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005768 if (VT != MVT::i32)
5769 return SDValue();
5770
5771 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5772 if (!C)
5773 return SDValue();
5774
5775 uint64_t MulAmt = C->getZExtValue();
5776 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5777 ShiftAmt = ShiftAmt & (32 - 1);
5778 SDValue V = N->getOperand(0);
5779 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005780
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005781 SDValue Res;
5782 MulAmt >>= ShiftAmt;
5783 if (isPowerOf2_32(MulAmt - 1)) {
5784 // (mul x, 2^N + 1) => (add (shl x, N), x)
5785 Res = DAG.getNode(ISD::ADD, DL, VT,
5786 V, DAG.getNode(ISD::SHL, DL, VT,
5787 V, DAG.getConstant(Log2_32(MulAmt-1),
5788 MVT::i32)));
5789 } else if (isPowerOf2_32(MulAmt + 1)) {
5790 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5791 Res = DAG.getNode(ISD::SUB, DL, VT,
5792 DAG.getNode(ISD::SHL, DL, VT,
5793 V, DAG.getConstant(Log2_32(MulAmt+1),
5794 MVT::i32)),
5795 V);
5796 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005797 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005798
5799 if (ShiftAmt != 0)
5800 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5801 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005802
5803 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005804 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005805 return SDValue();
5806}
5807
Owen Anderson080c0922010-11-05 19:27:46 +00005808static SDValue PerformANDCombine(SDNode *N,
5809 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005810
Owen Anderson080c0922010-11-05 19:27:46 +00005811 // Attempt to use immediate-form VBIC
5812 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5813 DebugLoc dl = N->getDebugLoc();
5814 EVT VT = N->getValueType(0);
5815 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005816
Tanya Lattner0433b212011-04-07 15:24:20 +00005817 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5818 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005819
Owen Anderson080c0922010-11-05 19:27:46 +00005820 APInt SplatBits, SplatUndef;
5821 unsigned SplatBitSize;
5822 bool HasAnyUndefs;
5823 if (BVN &&
5824 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5825 if (SplatBitSize <= 64) {
5826 EVT VbicVT;
5827 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5828 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005829 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005830 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005831 if (Val.getNode()) {
5832 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005833 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005834 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005835 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005836 }
5837 }
5838 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005839
Owen Anderson080c0922010-11-05 19:27:46 +00005840 return SDValue();
5841}
5842
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005843/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5844static SDValue PerformORCombine(SDNode *N,
5845 TargetLowering::DAGCombinerInfo &DCI,
5846 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005847 // Attempt to use immediate-form VORR
5848 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5849 DebugLoc dl = N->getDebugLoc();
5850 EVT VT = N->getValueType(0);
5851 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005852
Tanya Lattner0433b212011-04-07 15:24:20 +00005853 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5854 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005855
Owen Anderson60f48702010-11-03 23:15:26 +00005856 APInt SplatBits, SplatUndef;
5857 unsigned SplatBitSize;
5858 bool HasAnyUndefs;
5859 if (BVN && Subtarget->hasNEON() &&
5860 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5861 if (SplatBitSize <= 64) {
5862 EVT VorrVT;
5863 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5864 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005865 DAG, VorrVT, VT.is128BitVector(),
5866 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005867 if (Val.getNode()) {
5868 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005869 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005870 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005871 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005872 }
5873 }
5874 }
5875
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005876 SDValue N0 = N->getOperand(0);
5877 if (N0.getOpcode() != ISD::AND)
5878 return SDValue();
5879 SDValue N1 = N->getOperand(1);
5880
5881 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5882 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5883 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5884 APInt SplatUndef;
5885 unsigned SplatBitSize;
5886 bool HasAnyUndefs;
5887
5888 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5889 APInt SplatBits0;
5890 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5891 HasAnyUndefs) && !HasAnyUndefs) {
5892 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5893 APInt SplatBits1;
5894 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5895 HasAnyUndefs) && !HasAnyUndefs &&
5896 SplatBits0 == ~SplatBits1) {
5897 // Canonicalize the vector type to make instruction selection simpler.
5898 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5899 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5900 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005901 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005902 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5903 }
5904 }
5905 }
5906
Jim Grosbach54238562010-07-17 03:30:54 +00005907 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5908 // reasonable.
5909
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005910 // BFI is only available on V6T2+
5911 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5912 return SDValue();
5913
Jim Grosbach54238562010-07-17 03:30:54 +00005914 DebugLoc DL = N->getDebugLoc();
5915 // 1) or (and A, mask), val => ARMbfi A, val, mask
5916 // iff (val & mask) == val
5917 //
5918 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5919 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005920 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005921 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005922 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005923 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005924
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005925 if (VT != MVT::i32)
5926 return SDValue();
5927
Evan Cheng30fb13f2010-12-13 20:32:54 +00005928 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005929
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005930 // The value and the mask need to be constants so we can verify this is
5931 // actually a bitfield set. If the mask is 0xffff, we can do better
5932 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005933 SDValue MaskOp = N0.getOperand(1);
5934 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5935 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005936 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005937 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005938 if (Mask == 0xffff)
5939 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005940 SDValue Res;
5941 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005942 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5943 if (N1C) {
5944 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005945 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005946 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005947
Evan Chenga9688c42010-12-11 04:11:38 +00005948 if (ARM::isBitFieldInvertedMask(Mask)) {
5949 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005950
Evan Cheng30fb13f2010-12-13 20:32:54 +00005951 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005952 DAG.getConstant(Val, MVT::i32),
5953 DAG.getConstant(Mask, MVT::i32));
5954
5955 // Do not add new nodes to DAG combiner worklist.
5956 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005957 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005958 }
Jim Grosbach54238562010-07-17 03:30:54 +00005959 } else if (N1.getOpcode() == ISD::AND) {
5960 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005961 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5962 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005963 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005964 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005965
Eric Christopher29aeed12011-03-26 01:21:03 +00005966 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5967 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005968 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005969 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005970 // The pack halfword instruction works better for masks that fit it,
5971 // so use that when it's available.
5972 if (Subtarget->hasT2ExtractPack() &&
5973 (Mask == 0xffff || Mask == 0xffff0000))
5974 return SDValue();
5975 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005976 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005977 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005978 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005979 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005980 DAG.getConstant(Mask, MVT::i32));
5981 // Do not add new nodes to DAG combiner worklist.
5982 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005983 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005984 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005985 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005986 // The pack halfword instruction works better for masks that fit it,
5987 // so use that when it's available.
5988 if (Subtarget->hasT2ExtractPack() &&
5989 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5990 return SDValue();
5991 // 2b
5992 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005993 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005994 DAG.getConstant(lsb, MVT::i32));
5995 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005996 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005997 // Do not add new nodes to DAG combiner worklist.
5998 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005999 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006000 }
6001 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006002
Evan Cheng30fb13f2010-12-13 20:32:54 +00006003 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
6004 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
6005 ARM::isBitFieldInvertedMask(~Mask)) {
6006 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
6007 // where lsb(mask) == #shamt and masked bits of B are known zero.
6008 SDValue ShAmt = N00.getOperand(1);
6009 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
6010 unsigned LSB = CountTrailingZeros_32(Mask);
6011 if (ShAmtC != LSB)
6012 return SDValue();
6013
6014 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
6015 DAG.getConstant(~Mask, MVT::i32));
6016
6017 // Do not add new nodes to DAG combiner worklist.
6018 DCI.CombineTo(N, Res, false);
6019 }
6020
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006021 return SDValue();
6022}
6023
Evan Chengbf188ae2011-06-15 01:12:31 +00006024/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
6025/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00006026static SDValue PerformBFICombine(SDNode *N,
6027 TargetLowering::DAGCombinerInfo &DCI) {
6028 SDValue N1 = N->getOperand(1);
6029 if (N1.getOpcode() == ISD::AND) {
6030 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6031 if (!N11C)
6032 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006033 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
6034 unsigned LSB = CountTrailingZeros_32(~InvMask);
6035 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
6036 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00006037 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006038 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00006039 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
6040 N->getOperand(0), N1.getOperand(0),
6041 N->getOperand(2));
6042 }
6043 return SDValue();
6044}
6045
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006046/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
6047/// ARMISD::VMOVRRD.
6048static SDValue PerformVMOVRRDCombine(SDNode *N,
6049 TargetLowering::DAGCombinerInfo &DCI) {
6050 // vmovrrd(vmovdrr x, y) -> x,y
6051 SDValue InDouble = N->getOperand(0);
6052 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6053 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006054
6055 // vmovrrd(load f64) -> (load i32), (load i32)
6056 SDNode *InNode = InDouble.getNode();
6057 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6058 InNode->getValueType(0) == MVT::f64 &&
6059 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6060 !cast<LoadSDNode>(InNode)->isVolatile()) {
6061 // TODO: Should this be done for non-FrameIndex operands?
6062 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6063
6064 SelectionDAG &DAG = DCI.DAG;
6065 DebugLoc DL = LD->getDebugLoc();
6066 SDValue BasePtr = LD->getBasePtr();
6067 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6068 LD->getPointerInfo(), LD->isVolatile(),
6069 LD->isNonTemporal(), LD->getAlignment());
6070
6071 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6072 DAG.getConstant(4, MVT::i32));
6073 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6074 LD->getPointerInfo(), LD->isVolatile(),
6075 LD->isNonTemporal(),
6076 std::min(4U, LD->getAlignment() / 2));
6077
6078 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6079 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6080 DCI.RemoveFromWorklist(LD);
6081 DAG.DeleteNode(LD);
6082 return Result;
6083 }
6084
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006085 return SDValue();
6086}
6087
6088/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6089/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6090static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6091 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6092 SDValue Op0 = N->getOperand(0);
6093 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006094 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006095 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006096 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006097 Op1 = Op1.getOperand(0);
6098 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6099 Op0.getNode() == Op1.getNode() &&
6100 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006101 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006102 N->getValueType(0), Op0.getOperand(0));
6103 return SDValue();
6104}
6105
Bob Wilson31600902010-12-21 06:43:19 +00006106/// PerformSTORECombine - Target-specific dag combine xforms for
6107/// ISD::STORE.
6108static SDValue PerformSTORECombine(SDNode *N,
6109 TargetLowering::DAGCombinerInfo &DCI) {
6110 // Bitcast an i64 store extracted from a vector to f64.
6111 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6112 StoreSDNode *St = cast<StoreSDNode>(N);
6113 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006114 if (!ISD::isNormalStore(St) || St->isVolatile())
6115 return SDValue();
6116
6117 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6118 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6119 SelectionDAG &DAG = DCI.DAG;
6120 DebugLoc DL = St->getDebugLoc();
6121 SDValue BasePtr = St->getBasePtr();
6122 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6123 StVal.getNode()->getOperand(0), BasePtr,
6124 St->getPointerInfo(), St->isVolatile(),
6125 St->isNonTemporal(), St->getAlignment());
6126
6127 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6128 DAG.getConstant(4, MVT::i32));
6129 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6130 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6131 St->isNonTemporal(),
6132 std::min(4U, St->getAlignment() / 2));
6133 }
6134
6135 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006136 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6137 return SDValue();
6138
6139 SelectionDAG &DAG = DCI.DAG;
6140 DebugLoc dl = StVal.getDebugLoc();
6141 SDValue IntVec = StVal.getOperand(0);
6142 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6143 IntVec.getValueType().getVectorNumElements());
6144 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6145 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6146 Vec, StVal.getOperand(1));
6147 dl = N->getDebugLoc();
6148 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6149 // Make the DAGCombiner fold the bitcasts.
6150 DCI.AddToWorklist(Vec.getNode());
6151 DCI.AddToWorklist(ExtElt.getNode());
6152 DCI.AddToWorklist(V.getNode());
6153 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6154 St->getPointerInfo(), St->isVolatile(),
6155 St->isNonTemporal(), St->getAlignment(),
6156 St->getTBAAInfo());
6157}
6158
6159/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6160/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6161/// i64 vector to have f64 elements, since the value can then be loaded
6162/// directly into a VFP register.
6163static bool hasNormalLoadOperand(SDNode *N) {
6164 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6165 for (unsigned i = 0; i < NumElts; ++i) {
6166 SDNode *Elt = N->getOperand(i).getNode();
6167 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6168 return true;
6169 }
6170 return false;
6171}
6172
Bob Wilson75f02882010-09-17 22:59:05 +00006173/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6174/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006175static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6176 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006177 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6178 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6179 // into a pair of GPRs, which is fine when the value is used as a scalar,
6180 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006181 SelectionDAG &DAG = DCI.DAG;
6182 if (N->getNumOperands() == 2) {
6183 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6184 if (RV.getNode())
6185 return RV;
6186 }
Bob Wilson75f02882010-09-17 22:59:05 +00006187
Bob Wilson31600902010-12-21 06:43:19 +00006188 // Load i64 elements as f64 values so that type legalization does not split
6189 // them up into i32 values.
6190 EVT VT = N->getValueType(0);
6191 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6192 return SDValue();
6193 DebugLoc dl = N->getDebugLoc();
6194 SmallVector<SDValue, 8> Ops;
6195 unsigned NumElts = VT.getVectorNumElements();
6196 for (unsigned i = 0; i < NumElts; ++i) {
6197 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6198 Ops.push_back(V);
6199 // Make the DAGCombiner fold the bitcast.
6200 DCI.AddToWorklist(V.getNode());
6201 }
6202 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6203 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6204 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6205}
6206
6207/// PerformInsertEltCombine - Target-specific dag combine xforms for
6208/// ISD::INSERT_VECTOR_ELT.
6209static SDValue PerformInsertEltCombine(SDNode *N,
6210 TargetLowering::DAGCombinerInfo &DCI) {
6211 // Bitcast an i64 load inserted into a vector to f64.
6212 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6213 EVT VT = N->getValueType(0);
6214 SDNode *Elt = N->getOperand(1).getNode();
6215 if (VT.getVectorElementType() != MVT::i64 ||
6216 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6217 return SDValue();
6218
6219 SelectionDAG &DAG = DCI.DAG;
6220 DebugLoc dl = N->getDebugLoc();
6221 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6222 VT.getVectorNumElements());
6223 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6224 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6225 // Make the DAGCombiner fold the bitcasts.
6226 DCI.AddToWorklist(Vec.getNode());
6227 DCI.AddToWorklist(V.getNode());
6228 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6229 Vec, V, N->getOperand(2));
6230 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006231}
6232
Bob Wilsonf20700c2010-10-27 20:38:28 +00006233/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6234/// ISD::VECTOR_SHUFFLE.
6235static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6236 // The LLVM shufflevector instruction does not require the shuffle mask
6237 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6238 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6239 // operands do not match the mask length, they are extended by concatenating
6240 // them with undef vectors. That is probably the right thing for other
6241 // targets, but for NEON it is better to concatenate two double-register
6242 // size vector operands into a single quad-register size vector. Do that
6243 // transformation here:
6244 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6245 // shuffle(concat(v1, v2), undef)
6246 SDValue Op0 = N->getOperand(0);
6247 SDValue Op1 = N->getOperand(1);
6248 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6249 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6250 Op0.getNumOperands() != 2 ||
6251 Op1.getNumOperands() != 2)
6252 return SDValue();
6253 SDValue Concat0Op1 = Op0.getOperand(1);
6254 SDValue Concat1Op1 = Op1.getOperand(1);
6255 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6256 Concat1Op1.getOpcode() != ISD::UNDEF)
6257 return SDValue();
6258 // Skip the transformation if any of the types are illegal.
6259 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6260 EVT VT = N->getValueType(0);
6261 if (!TLI.isTypeLegal(VT) ||
6262 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6263 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6264 return SDValue();
6265
6266 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6267 Op0.getOperand(0), Op1.getOperand(0));
6268 // Translate the shuffle mask.
6269 SmallVector<int, 16> NewMask;
6270 unsigned NumElts = VT.getVectorNumElements();
6271 unsigned HalfElts = NumElts/2;
6272 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6273 for (unsigned n = 0; n < NumElts; ++n) {
6274 int MaskElt = SVN->getMaskElt(n);
6275 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006276 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006277 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006278 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006279 NewElt = HalfElts + MaskElt - NumElts;
6280 NewMask.push_back(NewElt);
6281 }
6282 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6283 DAG.getUNDEF(VT), NewMask.data());
6284}
6285
Bob Wilson1c3ef902011-02-07 17:43:21 +00006286/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6287/// NEON load/store intrinsics to merge base address updates.
6288static SDValue CombineBaseUpdate(SDNode *N,
6289 TargetLowering::DAGCombinerInfo &DCI) {
6290 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6291 return SDValue();
6292
6293 SelectionDAG &DAG = DCI.DAG;
6294 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6295 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6296 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6297 SDValue Addr = N->getOperand(AddrOpIdx);
6298
6299 // Search for a use of the address operand that is an increment.
6300 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6301 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6302 SDNode *User = *UI;
6303 if (User->getOpcode() != ISD::ADD ||
6304 UI.getUse().getResNo() != Addr.getResNo())
6305 continue;
6306
6307 // Check that the add is independent of the load/store. Otherwise, folding
6308 // it would create a cycle.
6309 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6310 continue;
6311
6312 // Find the new opcode for the updating load/store.
6313 bool isLoad = true;
6314 bool isLaneOp = false;
6315 unsigned NewOpc = 0;
6316 unsigned NumVecs = 0;
6317 if (isIntrinsic) {
6318 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6319 switch (IntNo) {
6320 default: assert(0 && "unexpected intrinsic for Neon base update");
6321 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6322 NumVecs = 1; break;
6323 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6324 NumVecs = 2; break;
6325 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6326 NumVecs = 3; break;
6327 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6328 NumVecs = 4; break;
6329 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6330 NumVecs = 2; isLaneOp = true; break;
6331 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6332 NumVecs = 3; isLaneOp = true; break;
6333 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6334 NumVecs = 4; isLaneOp = true; break;
6335 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6336 NumVecs = 1; isLoad = false; break;
6337 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6338 NumVecs = 2; isLoad = false; break;
6339 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6340 NumVecs = 3; isLoad = false; break;
6341 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6342 NumVecs = 4; isLoad = false; break;
6343 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6344 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6345 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6346 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6347 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6348 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6349 }
6350 } else {
6351 isLaneOp = true;
6352 switch (N->getOpcode()) {
6353 default: assert(0 && "unexpected opcode for Neon base update");
6354 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6355 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6356 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6357 }
6358 }
6359
6360 // Find the size of memory referenced by the load/store.
6361 EVT VecTy;
6362 if (isLoad)
6363 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006364 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006365 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6366 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6367 if (isLaneOp)
6368 NumBytes /= VecTy.getVectorNumElements();
6369
6370 // If the increment is a constant, it must match the memory ref size.
6371 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6372 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6373 uint64_t IncVal = CInc->getZExtValue();
6374 if (IncVal != NumBytes)
6375 continue;
6376 } else if (NumBytes >= 3 * 16) {
6377 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6378 // separate instructions that make it harder to use a non-constant update.
6379 continue;
6380 }
6381
6382 // Create the new updating load/store node.
6383 EVT Tys[6];
6384 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6385 unsigned n;
6386 for (n = 0; n < NumResultVecs; ++n)
6387 Tys[n] = VecTy;
6388 Tys[n++] = MVT::i32;
6389 Tys[n] = MVT::Other;
6390 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6391 SmallVector<SDValue, 8> Ops;
6392 Ops.push_back(N->getOperand(0)); // incoming chain
6393 Ops.push_back(N->getOperand(AddrOpIdx));
6394 Ops.push_back(Inc);
6395 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6396 Ops.push_back(N->getOperand(i));
6397 }
6398 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6399 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6400 Ops.data(), Ops.size(),
6401 MemInt->getMemoryVT(),
6402 MemInt->getMemOperand());
6403
6404 // Update the uses.
6405 std::vector<SDValue> NewResults;
6406 for (unsigned i = 0; i < NumResultVecs; ++i) {
6407 NewResults.push_back(SDValue(UpdN.getNode(), i));
6408 }
6409 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6410 DCI.CombineTo(N, NewResults);
6411 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6412
6413 break;
Owen Anderson76706012011-04-05 21:48:57 +00006414 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006415 return SDValue();
6416}
6417
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006418/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6419/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6420/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6421/// return true.
6422static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6423 SelectionDAG &DAG = DCI.DAG;
6424 EVT VT = N->getValueType(0);
6425 // vldN-dup instructions only support 64-bit vectors for N > 1.
6426 if (!VT.is64BitVector())
6427 return false;
6428
6429 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6430 SDNode *VLD = N->getOperand(0).getNode();
6431 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6432 return false;
6433 unsigned NumVecs = 0;
6434 unsigned NewOpc = 0;
6435 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6436 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6437 NumVecs = 2;
6438 NewOpc = ARMISD::VLD2DUP;
6439 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6440 NumVecs = 3;
6441 NewOpc = ARMISD::VLD3DUP;
6442 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6443 NumVecs = 4;
6444 NewOpc = ARMISD::VLD4DUP;
6445 } else {
6446 return false;
6447 }
6448
6449 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6450 // numbers match the load.
6451 unsigned VLDLaneNo =
6452 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6453 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6454 UI != UE; ++UI) {
6455 // Ignore uses of the chain result.
6456 if (UI.getUse().getResNo() == NumVecs)
6457 continue;
6458 SDNode *User = *UI;
6459 if (User->getOpcode() != ARMISD::VDUPLANE ||
6460 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6461 return false;
6462 }
6463
6464 // Create the vldN-dup node.
6465 EVT Tys[5];
6466 unsigned n;
6467 for (n = 0; n < NumVecs; ++n)
6468 Tys[n] = VT;
6469 Tys[n] = MVT::Other;
6470 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6471 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6472 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6473 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6474 Ops, 2, VLDMemInt->getMemoryVT(),
6475 VLDMemInt->getMemOperand());
6476
6477 // Update the uses.
6478 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6479 UI != UE; ++UI) {
6480 unsigned ResNo = UI.getUse().getResNo();
6481 // Ignore uses of the chain result.
6482 if (ResNo == NumVecs)
6483 continue;
6484 SDNode *User = *UI;
6485 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6486 }
6487
6488 // Now the vldN-lane intrinsic is dead except for its chain result.
6489 // Update uses of the chain.
6490 std::vector<SDValue> VLDDupResults;
6491 for (unsigned n = 0; n < NumVecs; ++n)
6492 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6493 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6494 DCI.CombineTo(VLD, VLDDupResults);
6495
6496 return true;
6497}
6498
Bob Wilson9e82bf12010-07-14 01:22:12 +00006499/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6500/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006501static SDValue PerformVDUPLANECombine(SDNode *N,
6502 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006503 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006504
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006505 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6506 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6507 if (CombineVLDDUP(N, DCI))
6508 return SDValue(N, 0);
6509
6510 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6511 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006512 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006513 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006514 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006515 return SDValue();
6516
6517 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6518 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6519 // The canonical VMOV for a zero vector uses a 32-bit element size.
6520 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6521 unsigned EltBits;
6522 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6523 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006524 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006525 if (EltSize > VT.getVectorElementType().getSizeInBits())
6526 return SDValue();
6527
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006528 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006529}
6530
Eric Christopherfa6f5912011-06-29 21:10:36 +00006531// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00006532// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
6533static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
6534{
Chad Rosier118c9a02011-06-28 17:26:57 +00006535 integerPart cN;
6536 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00006537 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
6538 I != E; I++) {
6539 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
6540 if (!C)
6541 return false;
6542
Eric Christopherfa6f5912011-06-29 21:10:36 +00006543 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00006544 APFloat APF = C->getValueAPF();
6545 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
6546 != APFloat::opOK || !isExact)
6547 return false;
6548
6549 c0 = (I == 0) ? cN : c0;
6550 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
6551 return false;
6552 }
6553 C = c0;
6554 return true;
6555}
6556
6557/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
6558/// can replace combinations of VMUL and VCVT (floating-point to integer)
6559/// when the VMUL has a constant operand that is a power of 2.
6560///
6561/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6562/// vmul.f32 d16, d17, d16
6563/// vcvt.s32.f32 d16, d16
6564/// becomes:
6565/// vcvt.s32.f32 d16, d16, #3
6566static SDValue PerformVCVTCombine(SDNode *N,
6567 TargetLowering::DAGCombinerInfo &DCI,
6568 const ARMSubtarget *Subtarget) {
6569 SelectionDAG &DAG = DCI.DAG;
6570 SDValue Op = N->getOperand(0);
6571
6572 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
6573 Op.getOpcode() != ISD::FMUL)
6574 return SDValue();
6575
6576 uint64_t C;
6577 SDValue N0 = Op->getOperand(0);
6578 SDValue ConstVec = Op->getOperand(1);
6579 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
6580
Eric Christopherfa6f5912011-06-29 21:10:36 +00006581 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00006582 !isConstVecPow2(ConstVec, isSigned, C))
6583 return SDValue();
6584
6585 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
6586 Intrinsic::arm_neon_vcvtfp2fxu;
6587 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6588 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006589 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00006590 DAG.getConstant(Log2_64(C), MVT::i32));
6591}
6592
6593/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
6594/// can replace combinations of VCVT (integer to floating-point) and VDIV
6595/// when the VDIV has a constant operand that is a power of 2.
6596///
6597/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6598/// vcvt.f32.s32 d16, d16
6599/// vdiv.f32 d16, d17, d16
6600/// becomes:
6601/// vcvt.f32.s32 d16, d16, #3
6602static SDValue PerformVDIVCombine(SDNode *N,
6603 TargetLowering::DAGCombinerInfo &DCI,
6604 const ARMSubtarget *Subtarget) {
6605 SelectionDAG &DAG = DCI.DAG;
6606 SDValue Op = N->getOperand(0);
6607 unsigned OpOpcode = Op.getNode()->getOpcode();
6608
6609 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
6610 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
6611 return SDValue();
6612
6613 uint64_t C;
6614 SDValue ConstVec = N->getOperand(1);
6615 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
6616
6617 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
6618 !isConstVecPow2(ConstVec, isSigned, C))
6619 return SDValue();
6620
Eric Christopherfa6f5912011-06-29 21:10:36 +00006621 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00006622 Intrinsic::arm_neon_vcvtfxu2fp;
6623 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6624 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006625 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00006626 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
6627}
6628
6629/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00006630/// operand of a vector shift operation, where all the elements of the
6631/// build_vector must have the same constant integer value.
6632static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6633 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006634 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006635 Op = Op.getOperand(0);
6636 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6637 APInt SplatBits, SplatUndef;
6638 unsigned SplatBitSize;
6639 bool HasAnyUndefs;
6640 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6641 HasAnyUndefs, ElementBits) ||
6642 SplatBitSize > ElementBits)
6643 return false;
6644 Cnt = SplatBits.getSExtValue();
6645 return true;
6646}
6647
6648/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6649/// operand of a vector shift left operation. That value must be in the range:
6650/// 0 <= Value < ElementBits for a left shift; or
6651/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006652static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006653 assert(VT.isVector() && "vector shift count is not a vector type");
6654 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6655 if (! getVShiftImm(Op, ElementBits, Cnt))
6656 return false;
6657 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6658}
6659
6660/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6661/// operand of a vector shift right operation. For a shift opcode, the value
6662/// is positive, but for an intrinsic the value count must be negative. The
6663/// absolute value must be in the range:
6664/// 1 <= |Value| <= ElementBits for a right shift; or
6665/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006666static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006667 int64_t &Cnt) {
6668 assert(VT.isVector() && "vector shift count is not a vector type");
6669 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6670 if (! getVShiftImm(Op, ElementBits, Cnt))
6671 return false;
6672 if (isIntrinsic)
6673 Cnt = -Cnt;
6674 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6675}
6676
6677/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6678static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6679 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6680 switch (IntNo) {
6681 default:
6682 // Don't do anything for most intrinsics.
6683 break;
6684
6685 // Vector shifts: check for immediate versions and lower them.
6686 // Note: This is done during DAG combining instead of DAG legalizing because
6687 // the build_vectors for 64-bit vector element shift counts are generally
6688 // not legal, and it is hard to see their values after they get legalized to
6689 // loads from a constant pool.
6690 case Intrinsic::arm_neon_vshifts:
6691 case Intrinsic::arm_neon_vshiftu:
6692 case Intrinsic::arm_neon_vshiftls:
6693 case Intrinsic::arm_neon_vshiftlu:
6694 case Intrinsic::arm_neon_vshiftn:
6695 case Intrinsic::arm_neon_vrshifts:
6696 case Intrinsic::arm_neon_vrshiftu:
6697 case Intrinsic::arm_neon_vrshiftn:
6698 case Intrinsic::arm_neon_vqshifts:
6699 case Intrinsic::arm_neon_vqshiftu:
6700 case Intrinsic::arm_neon_vqshiftsu:
6701 case Intrinsic::arm_neon_vqshiftns:
6702 case Intrinsic::arm_neon_vqshiftnu:
6703 case Intrinsic::arm_neon_vqshiftnsu:
6704 case Intrinsic::arm_neon_vqrshiftns:
6705 case Intrinsic::arm_neon_vqrshiftnu:
6706 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006707 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006708 int64_t Cnt;
6709 unsigned VShiftOpc = 0;
6710
6711 switch (IntNo) {
6712 case Intrinsic::arm_neon_vshifts:
6713 case Intrinsic::arm_neon_vshiftu:
6714 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6715 VShiftOpc = ARMISD::VSHL;
6716 break;
6717 }
6718 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6719 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6720 ARMISD::VSHRs : ARMISD::VSHRu);
6721 break;
6722 }
6723 return SDValue();
6724
6725 case Intrinsic::arm_neon_vshiftls:
6726 case Intrinsic::arm_neon_vshiftlu:
6727 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6728 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006729 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006730
6731 case Intrinsic::arm_neon_vrshifts:
6732 case Intrinsic::arm_neon_vrshiftu:
6733 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6734 break;
6735 return SDValue();
6736
6737 case Intrinsic::arm_neon_vqshifts:
6738 case Intrinsic::arm_neon_vqshiftu:
6739 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6740 break;
6741 return SDValue();
6742
6743 case Intrinsic::arm_neon_vqshiftsu:
6744 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6745 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006746 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006747
6748 case Intrinsic::arm_neon_vshiftn:
6749 case Intrinsic::arm_neon_vrshiftn:
6750 case Intrinsic::arm_neon_vqshiftns:
6751 case Intrinsic::arm_neon_vqshiftnu:
6752 case Intrinsic::arm_neon_vqshiftnsu:
6753 case Intrinsic::arm_neon_vqrshiftns:
6754 case Intrinsic::arm_neon_vqrshiftnu:
6755 case Intrinsic::arm_neon_vqrshiftnsu:
6756 // Narrowing shifts require an immediate right shift.
6757 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6758 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006759 llvm_unreachable("invalid shift count for narrowing vector shift "
6760 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006761
6762 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006763 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006764 }
6765
6766 switch (IntNo) {
6767 case Intrinsic::arm_neon_vshifts:
6768 case Intrinsic::arm_neon_vshiftu:
6769 // Opcode already set above.
6770 break;
6771 case Intrinsic::arm_neon_vshiftls:
6772 case Intrinsic::arm_neon_vshiftlu:
6773 if (Cnt == VT.getVectorElementType().getSizeInBits())
6774 VShiftOpc = ARMISD::VSHLLi;
6775 else
6776 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6777 ARMISD::VSHLLs : ARMISD::VSHLLu);
6778 break;
6779 case Intrinsic::arm_neon_vshiftn:
6780 VShiftOpc = ARMISD::VSHRN; break;
6781 case Intrinsic::arm_neon_vrshifts:
6782 VShiftOpc = ARMISD::VRSHRs; break;
6783 case Intrinsic::arm_neon_vrshiftu:
6784 VShiftOpc = ARMISD::VRSHRu; break;
6785 case Intrinsic::arm_neon_vrshiftn:
6786 VShiftOpc = ARMISD::VRSHRN; break;
6787 case Intrinsic::arm_neon_vqshifts:
6788 VShiftOpc = ARMISD::VQSHLs; break;
6789 case Intrinsic::arm_neon_vqshiftu:
6790 VShiftOpc = ARMISD::VQSHLu; break;
6791 case Intrinsic::arm_neon_vqshiftsu:
6792 VShiftOpc = ARMISD::VQSHLsu; break;
6793 case Intrinsic::arm_neon_vqshiftns:
6794 VShiftOpc = ARMISD::VQSHRNs; break;
6795 case Intrinsic::arm_neon_vqshiftnu:
6796 VShiftOpc = ARMISD::VQSHRNu; break;
6797 case Intrinsic::arm_neon_vqshiftnsu:
6798 VShiftOpc = ARMISD::VQSHRNsu; break;
6799 case Intrinsic::arm_neon_vqrshiftns:
6800 VShiftOpc = ARMISD::VQRSHRNs; break;
6801 case Intrinsic::arm_neon_vqrshiftnu:
6802 VShiftOpc = ARMISD::VQRSHRNu; break;
6803 case Intrinsic::arm_neon_vqrshiftnsu:
6804 VShiftOpc = ARMISD::VQRSHRNsu; break;
6805 }
6806
6807 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006808 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006809 }
6810
6811 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006812 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006813 int64_t Cnt;
6814 unsigned VShiftOpc = 0;
6815
6816 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6817 VShiftOpc = ARMISD::VSLI;
6818 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6819 VShiftOpc = ARMISD::VSRI;
6820 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006821 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006822 }
6823
6824 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6825 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006826 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006827 }
6828
6829 case Intrinsic::arm_neon_vqrshifts:
6830 case Intrinsic::arm_neon_vqrshiftu:
6831 // No immediate versions of these to check for.
6832 break;
6833 }
6834
6835 return SDValue();
6836}
6837
6838/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6839/// lowers them. As with the vector shift intrinsics, this is done during DAG
6840/// combining instead of DAG legalizing because the build_vectors for 64-bit
6841/// vector element shift counts are generally not legal, and it is hard to see
6842/// their values after they get legalized to loads from a constant pool.
6843static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6844 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006845 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006846
6847 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006848 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6849 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006850 return SDValue();
6851
6852 assert(ST->hasNEON() && "unexpected vector shift");
6853 int64_t Cnt;
6854
6855 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006856 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006857
6858 case ISD::SHL:
6859 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6860 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006861 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006862 break;
6863
6864 case ISD::SRA:
6865 case ISD::SRL:
6866 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6867 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6868 ARMISD::VSHRs : ARMISD::VSHRu);
6869 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006870 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006871 }
6872 }
6873 return SDValue();
6874}
6875
6876/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6877/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6878static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6879 const ARMSubtarget *ST) {
6880 SDValue N0 = N->getOperand(0);
6881
6882 // Check for sign- and zero-extensions of vector extract operations of 8-
6883 // and 16-bit vector elements. NEON supports these directly. They are
6884 // handled during DAG combining because type legalization will promote them
6885 // to 32-bit types and it is messy to recognize the operations after that.
6886 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6887 SDValue Vec = N0.getOperand(0);
6888 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006889 EVT VT = N->getValueType(0);
6890 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006891 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6892
Owen Anderson825b72b2009-08-11 20:47:22 +00006893 if (VT == MVT::i32 &&
6894 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006895 TLI.isTypeLegal(Vec.getValueType()) &&
6896 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006897
6898 unsigned Opc = 0;
6899 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006900 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006901 case ISD::SIGN_EXTEND:
6902 Opc = ARMISD::VGETLANEs;
6903 break;
6904 case ISD::ZERO_EXTEND:
6905 case ISD::ANY_EXTEND:
6906 Opc = ARMISD::VGETLANEu;
6907 break;
6908 }
6909 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6910 }
6911 }
6912
6913 return SDValue();
6914}
6915
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006916/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6917/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6918static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6919 const ARMSubtarget *ST) {
6920 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006921 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006922 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6923 // a NaN; only do the transformation when it matches that behavior.
6924
6925 // For now only do this when using NEON for FP operations; if using VFP, it
6926 // is not obvious that the benefit outweighs the cost of switching to the
6927 // NEON pipeline.
6928 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6929 N->getValueType(0) != MVT::f32)
6930 return SDValue();
6931
6932 SDValue CondLHS = N->getOperand(0);
6933 SDValue CondRHS = N->getOperand(1);
6934 SDValue LHS = N->getOperand(2);
6935 SDValue RHS = N->getOperand(3);
6936 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6937
6938 unsigned Opcode = 0;
6939 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006940 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006941 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006942 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006943 IsReversed = true ; // x CC y ? y : x
6944 } else {
6945 return SDValue();
6946 }
6947
Bob Wilsone742bb52010-02-24 22:15:53 +00006948 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006949 switch (CC) {
6950 default: break;
6951 case ISD::SETOLT:
6952 case ISD::SETOLE:
6953 case ISD::SETLT:
6954 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006955 case ISD::SETULT:
6956 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006957 // If LHS is NaN, an ordered comparison will be false and the result will
6958 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6959 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6960 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6961 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6962 break;
6963 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6964 // will return -0, so vmin can only be used for unsafe math or if one of
6965 // the operands is known to be nonzero.
6966 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6967 !UnsafeFPMath &&
6968 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6969 break;
6970 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006971 break;
6972
6973 case ISD::SETOGT:
6974 case ISD::SETOGE:
6975 case ISD::SETGT:
6976 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006977 case ISD::SETUGT:
6978 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006979 // If LHS is NaN, an ordered comparison will be false and the result will
6980 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6981 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6982 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6983 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6984 break;
6985 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6986 // will return +0, so vmax can only be used for unsafe math or if one of
6987 // the operands is known to be nonzero.
6988 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6989 !UnsafeFPMath &&
6990 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6991 break;
6992 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006993 break;
6994 }
6995
6996 if (!Opcode)
6997 return SDValue();
6998 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6999}
7000
Evan Chenge721f5c2011-07-13 00:42:17 +00007001/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
7002SDValue
7003ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
7004 SDValue Cmp = N->getOperand(4);
7005 if (Cmp.getOpcode() != ARMISD::CMPZ)
7006 // Only looking at EQ and NE cases.
7007 return SDValue();
7008
7009 EVT VT = N->getValueType(0);
7010 DebugLoc dl = N->getDebugLoc();
7011 SDValue LHS = Cmp.getOperand(0);
7012 SDValue RHS = Cmp.getOperand(1);
7013 SDValue FalseVal = N->getOperand(0);
7014 SDValue TrueVal = N->getOperand(1);
7015 SDValue ARMcc = N->getOperand(2);
7016 ARMCC::CondCodes CC = (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
7017
7018 // Simplify
7019 // mov r1, r0
7020 // cmp r1, x
7021 // mov r0, y
7022 // moveq r0, x
7023 // to
7024 // cmp r0, x
7025 // movne r0, y
7026 //
7027 // mov r1, r0
7028 // cmp r1, x
7029 // mov r0, x
7030 // movne r0, y
7031 // to
7032 // cmp r0, x
7033 // movne r0, y
7034 /// FIXME: Turn this into a target neutral optimization?
7035 SDValue Res;
7036 if (CC == ARMCC::NE && FalseVal == RHS) {
7037 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
7038 N->getOperand(3), Cmp);
7039 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
7040 SDValue ARMcc;
7041 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
7042 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
7043 N->getOperand(3), NewCmp);
7044 }
7045
7046 if (Res.getNode()) {
7047 APInt KnownZero, KnownOne;
7048 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
7049 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
7050 // Capture demanded bits information that would be otherwise lost.
7051 if (KnownZero == 0xfffffffe)
7052 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7053 DAG.getValueType(MVT::i1));
7054 else if (KnownZero == 0xffffff00)
7055 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7056 DAG.getValueType(MVT::i8));
7057 else if (KnownZero == 0xffff0000)
7058 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7059 DAG.getValueType(MVT::i16));
7060 }
7061
7062 return Res;
7063}
7064
Dan Gohman475871a2008-07-27 21:46:04 +00007065SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007066 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007067 switch (N->getOpcode()) {
7068 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00007069 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007070 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007071 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007072 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00007073 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00007074 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00007075 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007076 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00007077 case ISD::STORE: return PerformSTORECombine(N, DCI);
7078 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
7079 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00007080 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007081 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00007082 case ISD::FP_TO_SINT:
7083 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
7084 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007085 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00007086 case ISD::SHL:
7087 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007088 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00007089 case ISD::SIGN_EXTEND:
7090 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007091 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
7092 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00007093 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00007094 case ARMISD::VLD2DUP:
7095 case ARMISD::VLD3DUP:
7096 case ARMISD::VLD4DUP:
7097 return CombineBaseUpdate(N, DCI);
7098 case ISD::INTRINSIC_VOID:
7099 case ISD::INTRINSIC_W_CHAIN:
7100 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7101 case Intrinsic::arm_neon_vld1:
7102 case Intrinsic::arm_neon_vld2:
7103 case Intrinsic::arm_neon_vld3:
7104 case Intrinsic::arm_neon_vld4:
7105 case Intrinsic::arm_neon_vld2lane:
7106 case Intrinsic::arm_neon_vld3lane:
7107 case Intrinsic::arm_neon_vld4lane:
7108 case Intrinsic::arm_neon_vst1:
7109 case Intrinsic::arm_neon_vst2:
7110 case Intrinsic::arm_neon_vst3:
7111 case Intrinsic::arm_neon_vst4:
7112 case Intrinsic::arm_neon_vst2lane:
7113 case Intrinsic::arm_neon_vst3lane:
7114 case Intrinsic::arm_neon_vst4lane:
7115 return CombineBaseUpdate(N, DCI);
7116 default: break;
7117 }
7118 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007119 }
Dan Gohman475871a2008-07-27 21:46:04 +00007120 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007121}
7122
Evan Cheng31959b12011-02-02 01:06:55 +00007123bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
7124 EVT VT) const {
7125 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
7126}
7127
Bill Wendlingaf566342009-08-15 21:21:19 +00007128bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00007129 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00007130 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00007131
7132 switch (VT.getSimpleVT().SimpleTy) {
7133 default:
7134 return false;
7135 case MVT::i8:
7136 case MVT::i16:
7137 case MVT::i32:
7138 return true;
7139 // FIXME: VLD1 etc with standard alignment is legal.
7140 }
7141}
7142
Evan Chenge6c835f2009-08-14 20:09:37 +00007143static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
7144 if (V < 0)
7145 return false;
7146
7147 unsigned Scale = 1;
7148 switch (VT.getSimpleVT().SimpleTy) {
7149 default: return false;
7150 case MVT::i1:
7151 case MVT::i8:
7152 // Scale == 1;
7153 break;
7154 case MVT::i16:
7155 // Scale == 2;
7156 Scale = 2;
7157 break;
7158 case MVT::i32:
7159 // Scale == 4;
7160 Scale = 4;
7161 break;
7162 }
7163
7164 if ((V & (Scale - 1)) != 0)
7165 return false;
7166 V /= Scale;
7167 return V == (V & ((1LL << 5) - 1));
7168}
7169
7170static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
7171 const ARMSubtarget *Subtarget) {
7172 bool isNeg = false;
7173 if (V < 0) {
7174 isNeg = true;
7175 V = - V;
7176 }
7177
7178 switch (VT.getSimpleVT().SimpleTy) {
7179 default: return false;
7180 case MVT::i1:
7181 case MVT::i8:
7182 case MVT::i16:
7183 case MVT::i32:
7184 // + imm12 or - imm8
7185 if (isNeg)
7186 return V == (V & ((1LL << 8) - 1));
7187 return V == (V & ((1LL << 12) - 1));
7188 case MVT::f32:
7189 case MVT::f64:
7190 // Same as ARM mode. FIXME: NEON?
7191 if (!Subtarget->hasVFP2())
7192 return false;
7193 if ((V & 3) != 0)
7194 return false;
7195 V >>= 2;
7196 return V == (V & ((1LL << 8) - 1));
7197 }
7198}
7199
Evan Chengb01fad62007-03-12 23:30:29 +00007200/// isLegalAddressImmediate - Return true if the integer value can be used
7201/// as the offset of the target addressing mode for load / store of the
7202/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00007203static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007204 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00007205 if (V == 0)
7206 return true;
7207
Evan Cheng65011532009-03-09 19:15:00 +00007208 if (!VT.isSimple())
7209 return false;
7210
Evan Chenge6c835f2009-08-14 20:09:37 +00007211 if (Subtarget->isThumb1Only())
7212 return isLegalT1AddressImmediate(V, VT);
7213 else if (Subtarget->isThumb2())
7214 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007215
Evan Chenge6c835f2009-08-14 20:09:37 +00007216 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007217 if (V < 0)
7218 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007219 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007220 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007221 case MVT::i1:
7222 case MVT::i8:
7223 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007224 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007225 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007226 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007227 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007228 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007229 case MVT::f32:
7230 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007231 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007232 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007233 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007234 return false;
7235 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007236 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007237 }
Evan Chenga8e29892007-01-19 07:51:42 +00007238}
7239
Evan Chenge6c835f2009-08-14 20:09:37 +00007240bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7241 EVT VT) const {
7242 int Scale = AM.Scale;
7243 if (Scale < 0)
7244 return false;
7245
7246 switch (VT.getSimpleVT().SimpleTy) {
7247 default: return false;
7248 case MVT::i1:
7249 case MVT::i8:
7250 case MVT::i16:
7251 case MVT::i32:
7252 if (Scale == 1)
7253 return true;
7254 // r + r << imm
7255 Scale = Scale & ~1;
7256 return Scale == 2 || Scale == 4 || Scale == 8;
7257 case MVT::i64:
7258 // r + r
7259 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7260 return true;
7261 return false;
7262 case MVT::isVoid:
7263 // Note, we allow "void" uses (basically, uses that aren't loads or
7264 // stores), because arm allows folding a scale into many arithmetic
7265 // operations. This should be made more precise and revisited later.
7266
7267 // Allow r << imm, but the imm has to be a multiple of two.
7268 if (Scale & 1) return false;
7269 return isPowerOf2_32(Scale);
7270 }
7271}
7272
Chris Lattner37caf8c2007-04-09 23:33:39 +00007273/// isLegalAddressingMode - Return true if the addressing mode represented
7274/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007275bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007276 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007277 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007278 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007279 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007280
Chris Lattner37caf8c2007-04-09 23:33:39 +00007281 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007282 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007283 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007284
Chris Lattner37caf8c2007-04-09 23:33:39 +00007285 switch (AM.Scale) {
7286 case 0: // no scale reg, must be "r+i" or "r", or "i".
7287 break;
7288 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007289 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007290 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007291 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007292 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007293 // ARM doesn't support any R+R*scale+imm addr modes.
7294 if (AM.BaseOffs)
7295 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007296
Bob Wilson2c7dab12009-04-08 17:55:28 +00007297 if (!VT.isSimple())
7298 return false;
7299
Evan Chenge6c835f2009-08-14 20:09:37 +00007300 if (Subtarget->isThumb2())
7301 return isLegalT2ScaledAddressingMode(AM, VT);
7302
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007303 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007304 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007305 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007306 case MVT::i1:
7307 case MVT::i8:
7308 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007309 if (Scale < 0) Scale = -Scale;
7310 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007311 return true;
7312 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007313 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007315 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007316 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007317 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007318 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007319 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007320
Owen Anderson825b72b2009-08-11 20:47:22 +00007321 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007322 // Note, we allow "void" uses (basically, uses that aren't loads or
7323 // stores), because arm allows folding a scale into many arithmetic
7324 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007325
Chris Lattner37caf8c2007-04-09 23:33:39 +00007326 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007327 if (Scale & 1) return false;
7328 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007329 }
7330 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007331 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007332 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007333}
7334
Evan Cheng77e47512009-11-11 19:05:52 +00007335/// isLegalICmpImmediate - Return true if the specified immediate is legal
7336/// icmp immediate, that is the target has icmp instructions which can compare
7337/// a register against the immediate without having to materialize the
7338/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007339bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007340 if (!Subtarget->isThumb())
7341 return ARM_AM::getSOImmVal(Imm) != -1;
7342 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007343 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007344 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007345}
7346
Dan Gohmancca82142011-05-03 00:46:49 +00007347/// isLegalAddImmediate - Return true if the specified immediate is legal
7348/// add immediate, that is the target has add instructions which can add
7349/// a register with the immediate without having to materialize the
7350/// immediate into a register.
7351bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7352 return ARM_AM::getSOImmVal(Imm) != -1;
7353}
7354
Owen Andersone50ed302009-08-10 22:56:29 +00007355static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007356 bool isSEXTLoad, SDValue &Base,
7357 SDValue &Offset, bool &isInc,
7358 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007359 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7360 return false;
7361
Owen Anderson825b72b2009-08-11 20:47:22 +00007362 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007363 // AddressingMode 3
7364 Base = Ptr->getOperand(0);
7365 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007366 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007367 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007368 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007369 isInc = false;
7370 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7371 return true;
7372 }
7373 }
7374 isInc = (Ptr->getOpcode() == ISD::ADD);
7375 Offset = Ptr->getOperand(1);
7376 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007377 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007378 // AddressingMode 2
7379 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007380 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007381 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007382 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007383 isInc = false;
7384 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7385 Base = Ptr->getOperand(0);
7386 return true;
7387 }
7388 }
7389
7390 if (Ptr->getOpcode() == ISD::ADD) {
7391 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00007392 ARM_AM::ShiftOpc ShOpcVal=
7393 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00007394 if (ShOpcVal != ARM_AM::no_shift) {
7395 Base = Ptr->getOperand(1);
7396 Offset = Ptr->getOperand(0);
7397 } else {
7398 Base = Ptr->getOperand(0);
7399 Offset = Ptr->getOperand(1);
7400 }
7401 return true;
7402 }
7403
7404 isInc = (Ptr->getOpcode() == ISD::ADD);
7405 Base = Ptr->getOperand(0);
7406 Offset = Ptr->getOperand(1);
7407 return true;
7408 }
7409
Jim Grosbache5165492009-11-09 00:11:35 +00007410 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007411 return false;
7412}
7413
Owen Andersone50ed302009-08-10 22:56:29 +00007414static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007415 bool isSEXTLoad, SDValue &Base,
7416 SDValue &Offset, bool &isInc,
7417 SelectionDAG &DAG) {
7418 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7419 return false;
7420
7421 Base = Ptr->getOperand(0);
7422 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7423 int RHSC = (int)RHS->getZExtValue();
7424 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7425 assert(Ptr->getOpcode() == ISD::ADD);
7426 isInc = false;
7427 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7428 return true;
7429 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7430 isInc = Ptr->getOpcode() == ISD::ADD;
7431 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7432 return true;
7433 }
7434 }
7435
7436 return false;
7437}
7438
Evan Chenga8e29892007-01-19 07:51:42 +00007439/// getPreIndexedAddressParts - returns true by value, base pointer and
7440/// offset pointer and addressing mode by reference if the node's address
7441/// can be legally represented as pre-indexed load / store address.
7442bool
Dan Gohman475871a2008-07-27 21:46:04 +00007443ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7444 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007445 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007446 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007447 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007448 return false;
7449
Owen Andersone50ed302009-08-10 22:56:29 +00007450 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007451 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007452 bool isSEXTLoad = false;
7453 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7454 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007455 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007456 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7457 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7458 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007459 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007460 } else
7461 return false;
7462
7463 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007464 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007465 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007466 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7467 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007468 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007469 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007470 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007471 if (!isLegal)
7472 return false;
7473
7474 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7475 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007476}
7477
7478/// getPostIndexedAddressParts - returns true by value, base pointer and
7479/// offset pointer and addressing mode by reference if this node can be
7480/// combined with a load / store to form a post-indexed load / store.
7481bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007482 SDValue &Base,
7483 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007484 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007485 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007486 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007487 return false;
7488
Owen Andersone50ed302009-08-10 22:56:29 +00007489 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007490 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007491 bool isSEXTLoad = false;
7492 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007493 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007494 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007495 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7496 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007497 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007498 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007499 } else
7500 return false;
7501
7502 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007503 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007504 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007505 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007506 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007507 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007508 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7509 isInc, DAG);
7510 if (!isLegal)
7511 return false;
7512
Evan Cheng28dad2a2010-05-18 21:31:17 +00007513 if (Ptr != Base) {
7514 // Swap base ptr and offset to catch more post-index load / store when
7515 // it's legal. In Thumb2 mode, offset must be an immediate.
7516 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7517 !Subtarget->isThumb2())
7518 std::swap(Base, Offset);
7519
7520 // Post-indexed load / store update the base pointer.
7521 if (Ptr != Base)
7522 return false;
7523 }
7524
Evan Chenge88d5ce2009-07-02 07:28:31 +00007525 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7526 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007527}
7528
Dan Gohman475871a2008-07-27 21:46:04 +00007529void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007530 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007531 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007532 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007533 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007534 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007535 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007536 switch (Op.getOpcode()) {
7537 default: break;
7538 case ARMISD::CMOV: {
7539 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007540 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007541 if (KnownZero == 0 && KnownOne == 0) return;
7542
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007543 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007544 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7545 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007546 KnownZero &= KnownZeroRHS;
7547 KnownOne &= KnownOneRHS;
7548 return;
7549 }
7550 }
7551}
7552
7553//===----------------------------------------------------------------------===//
7554// ARM Inline Assembly Support
7555//===----------------------------------------------------------------------===//
7556
Evan Cheng55d42002011-01-08 01:24:27 +00007557bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7558 // Looking for "rev" which is V6+.
7559 if (!Subtarget->hasV6Ops())
7560 return false;
7561
7562 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7563 std::string AsmStr = IA->getAsmString();
7564 SmallVector<StringRef, 4> AsmPieces;
7565 SplitString(AsmStr, AsmPieces, ";\n");
7566
7567 switch (AsmPieces.size()) {
7568 default: return false;
7569 case 1:
7570 AsmStr = AsmPieces[0];
7571 AsmPieces.clear();
7572 SplitString(AsmStr, AsmPieces, " \t,");
7573
7574 // rev $0, $1
7575 if (AsmPieces.size() == 3 &&
7576 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7577 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007578 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00007579 if (Ty && Ty->getBitWidth() == 32)
7580 return IntrinsicLowering::LowerToByteSwap(CI);
7581 }
7582 break;
7583 }
7584
7585 return false;
7586}
7587
Evan Chenga8e29892007-01-19 07:51:42 +00007588/// getConstraintType - Given a constraint letter, return the type of
7589/// constraint it is for this target.
7590ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007591ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7592 if (Constraint.size() == 1) {
7593 switch (Constraint[0]) {
7594 default: break;
7595 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007596 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00007597 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007598 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007599 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00007600 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00007601 // An address with a single base register. Due to the way we
7602 // currently handle addresses it is the same as an 'r' memory constraint.
7603 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007604 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007605 } else if (Constraint.size() == 2) {
7606 switch (Constraint[0]) {
7607 default: break;
7608 // All 'U+' constraints are addresses.
7609 case 'U': return C_Memory;
7610 }
Evan Chenga8e29892007-01-19 07:51:42 +00007611 }
Chris Lattner4234f572007-03-25 02:14:49 +00007612 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007613}
7614
John Thompson44ab89e2010-10-29 17:29:13 +00007615/// Examine constraint type and operand type and determine a weight value.
7616/// This object must already have been set up with the operand type
7617/// and the current alternative constraint selected.
7618TargetLowering::ConstraintWeight
7619ARMTargetLowering::getSingleConstraintMatchWeight(
7620 AsmOperandInfo &info, const char *constraint) const {
7621 ConstraintWeight weight = CW_Invalid;
7622 Value *CallOperandVal = info.CallOperandVal;
7623 // If we don't have a value, we can't do a match,
7624 // but allow it at the lowest weight.
7625 if (CallOperandVal == NULL)
7626 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007627 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007628 // Look at the constraint type.
7629 switch (*constraint) {
7630 default:
7631 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7632 break;
7633 case 'l':
7634 if (type->isIntegerTy()) {
7635 if (Subtarget->isThumb())
7636 weight = CW_SpecificReg;
7637 else
7638 weight = CW_Register;
7639 }
7640 break;
7641 case 'w':
7642 if (type->isFloatingPointTy())
7643 weight = CW_Register;
7644 break;
7645 }
7646 return weight;
7647}
7648
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007649typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7650RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007651ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007652 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007653 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007654 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007655 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007656 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007657 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007658 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007659 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007660 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007661 case 'h': // High regs or no regs.
7662 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007663 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007664 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007665 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007666 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007667 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007668 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007669 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007670 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007671 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007672 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007673 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007674 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007675 case 'x':
7676 if (VT == MVT::f32)
7677 return RCPair(0U, ARM::SPR_8RegisterClass);
7678 if (VT.getSizeInBits() == 64)
7679 return RCPair(0U, ARM::DPR_8RegisterClass);
7680 if (VT.getSizeInBits() == 128)
7681 return RCPair(0U, ARM::QPR_8RegisterClass);
7682 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007683 case 't':
7684 if (VT == MVT::f32)
7685 return RCPair(0U, ARM::SPRRegisterClass);
7686 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007687 }
7688 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007689 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007690 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007691
Evan Chenga8e29892007-01-19 07:51:42 +00007692 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7693}
7694
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007695/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7696/// vector. If it is invalid, don't add anything to Ops.
7697void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007698 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007699 std::vector<SDValue>&Ops,
7700 SelectionDAG &DAG) const {
7701 SDValue Result(0, 0);
7702
Eric Christopher100c8332011-06-02 23:16:42 +00007703 // Currently only support length 1 constraints.
7704 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007705
Eric Christopher100c8332011-06-02 23:16:42 +00007706 char ConstraintLetter = Constraint[0];
7707 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007708 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007709 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007710 case 'I': case 'J': case 'K': case 'L':
7711 case 'M': case 'N': case 'O':
7712 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7713 if (!C)
7714 return;
7715
7716 int64_t CVal64 = C->getSExtValue();
7717 int CVal = (int) CVal64;
7718 // None of these constraints allow values larger than 32 bits. Check
7719 // that the value fits in an int.
7720 if (CVal != CVal64)
7721 return;
7722
Eric Christopher100c8332011-06-02 23:16:42 +00007723 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007724 case 'j':
7725 // Constant suitable for movw, must be between 0 and
7726 // 65535.
7727 if (Subtarget->hasV6T2Ops())
7728 if (CVal >= 0 && CVal <= 65535)
7729 break;
7730 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007731 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007732 if (Subtarget->isThumb1Only()) {
7733 // This must be a constant between 0 and 255, for ADD
7734 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007735 if (CVal >= 0 && CVal <= 255)
7736 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007737 } else if (Subtarget->isThumb2()) {
7738 // A constant that can be used as an immediate value in a
7739 // data-processing instruction.
7740 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7741 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007742 } else {
7743 // A constant that can be used as an immediate value in a
7744 // data-processing instruction.
7745 if (ARM_AM::getSOImmVal(CVal) != -1)
7746 break;
7747 }
7748 return;
7749
7750 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007751 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007752 // This must be a constant between -255 and -1, for negated ADD
7753 // immediates. This can be used in GCC with an "n" modifier that
7754 // prints the negated value, for use with SUB instructions. It is
7755 // not useful otherwise but is implemented for compatibility.
7756 if (CVal >= -255 && CVal <= -1)
7757 break;
7758 } else {
7759 // This must be a constant between -4095 and 4095. It is not clear
7760 // what this constraint is intended for. Implemented for
7761 // compatibility with GCC.
7762 if (CVal >= -4095 && CVal <= 4095)
7763 break;
7764 }
7765 return;
7766
7767 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007768 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007769 // A 32-bit value where only one byte has a nonzero value. Exclude
7770 // zero to match GCC. This constraint is used by GCC internally for
7771 // constants that can be loaded with a move/shift combination.
7772 // It is not useful otherwise but is implemented for compatibility.
7773 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7774 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007775 } else if (Subtarget->isThumb2()) {
7776 // A constant whose bitwise inverse can be used as an immediate
7777 // value in a data-processing instruction. This can be used in GCC
7778 // with a "B" modifier that prints the inverted value, for use with
7779 // BIC and MVN instructions. It is not useful otherwise but is
7780 // implemented for compatibility.
7781 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7782 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007783 } else {
7784 // A constant whose bitwise inverse can be used as an immediate
7785 // value in a data-processing instruction. This can be used in GCC
7786 // with a "B" modifier that prints the inverted value, for use with
7787 // BIC and MVN instructions. It is not useful otherwise but is
7788 // implemented for compatibility.
7789 if (ARM_AM::getSOImmVal(~CVal) != -1)
7790 break;
7791 }
7792 return;
7793
7794 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007795 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007796 // This must be a constant between -7 and 7,
7797 // for 3-operand ADD/SUB immediate instructions.
7798 if (CVal >= -7 && CVal < 7)
7799 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007800 } else if (Subtarget->isThumb2()) {
7801 // A constant whose negation can be used as an immediate value in a
7802 // data-processing instruction. This can be used in GCC with an "n"
7803 // modifier that prints the negated value, for use with SUB
7804 // instructions. It is not useful otherwise but is implemented for
7805 // compatibility.
7806 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7807 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007808 } else {
7809 // A constant whose negation can be used as an immediate value in a
7810 // data-processing instruction. This can be used in GCC with an "n"
7811 // modifier that prints the negated value, for use with SUB
7812 // instructions. It is not useful otherwise but is implemented for
7813 // compatibility.
7814 if (ARM_AM::getSOImmVal(-CVal) != -1)
7815 break;
7816 }
7817 return;
7818
7819 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007820 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007821 // This must be a multiple of 4 between 0 and 1020, for
7822 // ADD sp + immediate.
7823 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7824 break;
7825 } else {
7826 // A power of two or a constant between 0 and 32. This is used in
7827 // GCC for the shift amount on shifted register operands, but it is
7828 // useful in general for any shift amounts.
7829 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7830 break;
7831 }
7832 return;
7833
7834 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007835 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007836 // This must be a constant between 0 and 31, for shift amounts.
7837 if (CVal >= 0 && CVal <= 31)
7838 break;
7839 }
7840 return;
7841
7842 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007843 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007844 // This must be a multiple of 4 between -508 and 508, for
7845 // ADD/SUB sp = sp + immediate.
7846 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7847 break;
7848 }
7849 return;
7850 }
7851 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7852 break;
7853 }
7854
7855 if (Result.getNode()) {
7856 Ops.push_back(Result);
7857 return;
7858 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007859 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007860}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007861
7862bool
7863ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7864 // The ARM target isn't yet aware of offsets.
7865 return false;
7866}
Evan Cheng39382422009-10-28 01:44:26 +00007867
7868int ARM::getVFPf32Imm(const APFloat &FPImm) {
7869 APInt Imm = FPImm.bitcastToAPInt();
7870 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7871 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7872 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7873
7874 // We can handle 4 bits of mantissa.
7875 // mantissa = (16+UInt(e:f:g:h))/16.
7876 if (Mantissa & 0x7ffff)
7877 return -1;
7878 Mantissa >>= 19;
7879 if ((Mantissa & 0xf) != Mantissa)
7880 return -1;
7881
7882 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7883 if (Exp < -3 || Exp > 4)
7884 return -1;
7885 Exp = ((Exp+3) & 0x7) ^ 4;
7886
7887 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7888}
7889
7890int ARM::getVFPf64Imm(const APFloat &FPImm) {
7891 APInt Imm = FPImm.bitcastToAPInt();
7892 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7893 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7894 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7895
7896 // We can handle 4 bits of mantissa.
7897 // mantissa = (16+UInt(e:f:g:h))/16.
7898 if (Mantissa & 0xffffffffffffLL)
7899 return -1;
7900 Mantissa >>= 48;
7901 if ((Mantissa & 0xf) != Mantissa)
7902 return -1;
7903
7904 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7905 if (Exp < -3 || Exp > 4)
7906 return -1;
7907 Exp = ((Exp+3) & 0x7) ^ 4;
7908
7909 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7910}
7911
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007912bool ARM::isBitFieldInvertedMask(unsigned v) {
7913 if (v == 0xffffffff)
7914 return 0;
7915 // there can be 1's on either or both "outsides", all the "inside"
7916 // bits must be 0's
7917 unsigned int lsb = 0, msb = 31;
7918 while (v & (1 << msb)) --msb;
7919 while (v & (1 << lsb)) ++lsb;
7920 for (unsigned int i = lsb; i <= msb; ++i) {
7921 if (v & (1 << i))
7922 return 0;
7923 }
7924 return 1;
7925}
7926
Evan Cheng39382422009-10-28 01:44:26 +00007927/// isFPImmLegal - Returns true if the target can instruction select the
7928/// specified FP immediate natively. If false, the legalizer will
7929/// materialize the FP immediate as a load from a constant pool.
7930bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7931 if (!Subtarget->hasVFP3())
7932 return false;
7933 if (VT == MVT::f32)
7934 return ARM::getVFPf32Imm(Imm) != -1;
7935 if (VT == MVT::f64)
7936 return ARM::getVFPf64Imm(Imm) != -1;
7937 return false;
7938}
Bob Wilson65ffec42010-09-21 17:56:22 +00007939
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007940/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007941/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7942/// specified in the intrinsic calls.
7943bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7944 const CallInst &I,
7945 unsigned Intrinsic) const {
7946 switch (Intrinsic) {
7947 case Intrinsic::arm_neon_vld1:
7948 case Intrinsic::arm_neon_vld2:
7949 case Intrinsic::arm_neon_vld3:
7950 case Intrinsic::arm_neon_vld4:
7951 case Intrinsic::arm_neon_vld2lane:
7952 case Intrinsic::arm_neon_vld3lane:
7953 case Intrinsic::arm_neon_vld4lane: {
7954 Info.opc = ISD::INTRINSIC_W_CHAIN;
7955 // Conservatively set memVT to the entire set of vectors loaded.
7956 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7957 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7958 Info.ptrVal = I.getArgOperand(0);
7959 Info.offset = 0;
7960 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7961 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7962 Info.vol = false; // volatile loads with NEON intrinsics not supported
7963 Info.readMem = true;
7964 Info.writeMem = false;
7965 return true;
7966 }
7967 case Intrinsic::arm_neon_vst1:
7968 case Intrinsic::arm_neon_vst2:
7969 case Intrinsic::arm_neon_vst3:
7970 case Intrinsic::arm_neon_vst4:
7971 case Intrinsic::arm_neon_vst2lane:
7972 case Intrinsic::arm_neon_vst3lane:
7973 case Intrinsic::arm_neon_vst4lane: {
7974 Info.opc = ISD::INTRINSIC_VOID;
7975 // Conservatively set memVT to the entire set of vectors stored.
7976 unsigned NumElts = 0;
7977 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007978 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00007979 if (!ArgTy->isVectorTy())
7980 break;
7981 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7982 }
7983 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7984 Info.ptrVal = I.getArgOperand(0);
7985 Info.offset = 0;
7986 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7987 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7988 Info.vol = false; // volatile stores with NEON intrinsics not supported
7989 Info.readMem = false;
7990 Info.writeMem = true;
7991 return true;
7992 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007993 case Intrinsic::arm_strexd: {
7994 Info.opc = ISD::INTRINSIC_W_CHAIN;
7995 Info.memVT = MVT::i64;
7996 Info.ptrVal = I.getArgOperand(2);
7997 Info.offset = 0;
7998 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007999 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008000 Info.readMem = false;
8001 Info.writeMem = true;
8002 return true;
8003 }
8004 case Intrinsic::arm_ldrexd: {
8005 Info.opc = ISD::INTRINSIC_W_CHAIN;
8006 Info.memVT = MVT::i64;
8007 Info.ptrVal = I.getArgOperand(0);
8008 Info.offset = 0;
8009 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008010 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008011 Info.readMem = true;
8012 Info.writeMem = false;
8013 return true;
8014 }
Bob Wilson65ffec42010-09-21 17:56:22 +00008015 default:
8016 break;
8017 }
8018
8019 return false;
8020}