blob: 46b42996834015839160648b475ddfdfa9dbe85d [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);
Jim Grosbach68741be2010-06-18 22:35:32 +0000605 } else {
606 // Set them all for expansion, which will force libcalls.
607 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000608 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000609 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
610 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
611 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000612 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
613 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
614 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000615 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
616 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
617 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
618 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
624 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
625 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
626 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
627 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
628 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
629 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
630 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
631 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
632 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000633 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i8, Expand);
634 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i16, Expand);
635 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
636 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i8, Expand);
637 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i16, Expand);
638 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
639 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i8, Expand);
640 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i16, Expand);
641 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
642 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i8, Expand);
643 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i16, Expand);
644 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000645 // Since the libcalls include locking, fold in the fences
646 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000647 }
648 // 64-bit versions are always libcalls (for now)
649 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000650 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000651 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
652 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
653 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
654 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
655 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
656 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000657
Evan Cheng416941d2010-11-04 05:19:35 +0000658 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000659
Eli Friedmana2c6f452010-06-26 04:36:50 +0000660 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
661 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
663 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000664 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000666
Nate Begemand1fb5832010-08-03 21:31:55 +0000667 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000668 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
669 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000670 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000671 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
672 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000673
674 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000676 if (Subtarget->isTargetDarwin()) {
677 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
678 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000679 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000680 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000681 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000682
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 setOperationAction(ISD::SETCC, MVT::i32, Expand);
684 setOperationAction(ISD::SETCC, MVT::f32, Expand);
685 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000686 setOperationAction(ISD::SELECT, MVT::i32, Custom);
687 setOperationAction(ISD::SELECT, MVT::f32, Custom);
688 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
690 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
691 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000692
Owen Anderson825b72b2009-08-11 20:47:22 +0000693 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
694 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
695 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
696 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
697 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000698
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000699 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::FSIN, MVT::f64, Expand);
701 setOperationAction(ISD::FSIN, MVT::f32, Expand);
702 setOperationAction(ISD::FCOS, MVT::f32, Expand);
703 setOperationAction(ISD::FCOS, MVT::f64, Expand);
704 setOperationAction(ISD::FREM, MVT::f64, Expand);
705 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000706 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
708 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000709 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 setOperationAction(ISD::FPOW, MVT::f64, Expand);
711 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000712
Cameron Zwarich33390842011-07-08 21:39:21 +0000713 setOperationAction(ISD::FMA, MVT::f64, Expand);
714 setOperationAction(ISD::FMA, MVT::f32, Expand);
715
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000716 // Various VFP goodness
717 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000718 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
719 if (Subtarget->hasVFP2()) {
720 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
721 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
722 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
723 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
724 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000725 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000726 if (!Subtarget->hasFP16()) {
727 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
728 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000729 }
Evan Cheng110cf482008-04-01 01:50:16 +0000730 }
Evan Chenga8e29892007-01-19 07:51:42 +0000731
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000732 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000733 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000734 setTargetDAGCombine(ISD::ADD);
735 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000736 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000737
Owen Anderson080c0922010-11-05 19:27:46 +0000738 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000739 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000740 if (Subtarget->hasNEON())
741 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000742
Evan Chenga8e29892007-01-19 07:51:42 +0000743 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000744
Evan Chengf7d87ee2010-05-21 00:43:17 +0000745 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
746 setSchedulingPreference(Sched::RegPressure);
747 else
748 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000749
Evan Cheng05219282011-01-06 06:52:41 +0000750 //// temporary - rewrite interface to use type
751 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000752
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000753 // On ARM arguments smaller than 4 bytes are extended, so all arguments
754 // are at least 4 bytes aligned.
755 setMinStackArgumentAlignment(4);
756
Evan Chengfff606d2010-09-24 19:07:23 +0000757 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000758
759 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000760}
761
Andrew Trick32cec0a2011-01-19 02:35:27 +0000762// FIXME: It might make sense to define the representative register class as the
763// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
764// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
765// SPR's representative would be DPR_VFP2. This should work well if register
766// pressure tracking were modified such that a register use would increment the
767// pressure of the register class's representative and all of it's super
768// classes' representatives transitively. We have not implemented this because
769// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000770// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000771// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000772std::pair<const TargetRegisterClass*, uint8_t>
773ARMTargetLowering::findRepresentativeClass(EVT VT) const{
774 const TargetRegisterClass *RRC = 0;
775 uint8_t Cost = 1;
776 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000777 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000778 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000779 // Use DPR as representative register class for all floating point
780 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
781 // the cost is 1 for both f32 and f64.
782 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000783 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000784 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000785 // When NEON is used for SP, only half of the register file is available
786 // because operations that define both SP and DP results will be constrained
787 // to the VFP2 class (D0-D15). We currently model this constraint prior to
788 // coalescing by double-counting the SP regs. See the FIXME above.
789 if (Subtarget->useNEONForSinglePrecisionFP())
790 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000791 break;
792 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
793 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000794 RRC = ARM::DPRRegisterClass;
795 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000796 break;
797 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000798 RRC = ARM::DPRRegisterClass;
799 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000800 break;
801 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000802 RRC = ARM::DPRRegisterClass;
803 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000804 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000805 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000806 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000807}
808
Evan Chenga8e29892007-01-19 07:51:42 +0000809const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
810 switch (Opcode) {
811 default: return 0;
812 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000813 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000814 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000815 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
816 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000817 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000818 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
819 case ARMISD::tCALL: return "ARMISD::tCALL";
820 case ARMISD::BRCOND: return "ARMISD::BRCOND";
821 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000822 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000823 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
824 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
825 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000826 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000827 case ARMISD::CMPFP: return "ARMISD::CMPFP";
828 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000829 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000830 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
831 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000832
Jim Grosbach3482c802010-01-18 19:58:49 +0000833 case ARMISD::RBIT: return "ARMISD::RBIT";
834
Bob Wilson76a312b2010-03-19 22:51:32 +0000835 case ARMISD::FTOSI: return "ARMISD::FTOSI";
836 case ARMISD::FTOUI: return "ARMISD::FTOUI";
837 case ARMISD::SITOF: return "ARMISD::SITOF";
838 case ARMISD::UITOF: return "ARMISD::UITOF";
839
Evan Chenga8e29892007-01-19 07:51:42 +0000840 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
841 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
842 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000843
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000844 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
845 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000846
Evan Chengc5942082009-10-28 06:55:03 +0000847 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
848 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000849 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000850
Dale Johannesen51e28e62010-06-03 21:09:53 +0000851 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000852
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000853 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000854
Evan Cheng86198642009-08-07 00:34:42 +0000855 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
856
Jim Grosbach3728e962009-12-10 00:11:09 +0000857 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000858 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000859
Evan Chengdfed19f2010-11-03 06:34:55 +0000860 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
861
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000863 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000864 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000865 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
866 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000867 case ARMISD::VCGEU: return "ARMISD::VCGEU";
868 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000869 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
870 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000871 case ARMISD::VCGTU: return "ARMISD::VCGTU";
872 case ARMISD::VTST: return "ARMISD::VTST";
873
874 case ARMISD::VSHL: return "ARMISD::VSHL";
875 case ARMISD::VSHRs: return "ARMISD::VSHRs";
876 case ARMISD::VSHRu: return "ARMISD::VSHRu";
877 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
878 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
879 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
880 case ARMISD::VSHRN: return "ARMISD::VSHRN";
881 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
882 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
883 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
884 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
885 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
886 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
887 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
888 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
889 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
890 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
891 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
892 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
893 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
894 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000895 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000896 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000897 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000898 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000899 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000900 case ARMISD::VREV64: return "ARMISD::VREV64";
901 case ARMISD::VREV32: return "ARMISD::VREV32";
902 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000903 case ARMISD::VZIP: return "ARMISD::VZIP";
904 case ARMISD::VUZP: return "ARMISD::VUZP";
905 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000906 case ARMISD::VTBL1: return "ARMISD::VTBL1";
907 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000908 case ARMISD::VMULLs: return "ARMISD::VMULLs";
909 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000910 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000911 case ARMISD::FMAX: return "ARMISD::FMAX";
912 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000913 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000914 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
915 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000916 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000917 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
918 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
919 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000920 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
921 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
922 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
923 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
924 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
925 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
926 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
927 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
928 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
929 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
930 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
931 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
932 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
933 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
934 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
935 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
936 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000937 }
938}
939
Evan Cheng06b666c2010-05-15 02:18:07 +0000940/// getRegClassFor - Return the register class that should be used for the
941/// specified value type.
942TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
943 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
944 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
945 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000946 if (Subtarget->hasNEON()) {
947 if (VT == MVT::v4i64)
948 return ARM::QQPRRegisterClass;
949 else if (VT == MVT::v8i64)
950 return ARM::QQQQPRRegisterClass;
951 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000952 return TargetLowering::getRegClassFor(VT);
953}
954
Eric Christopherab695882010-07-21 22:26:11 +0000955// Create a fast isel object.
956FastISel *
957ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
958 return ARM::createFastISel(funcInfo);
959}
960
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000961/// getMaximalGlobalOffset - Returns the maximal possible offset which can
962/// be used for loads / stores from the global.
963unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
964 return (Subtarget->isThumb1Only() ? 127 : 4095);
965}
966
Evan Cheng1cc39842010-05-20 23:26:43 +0000967Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000968 unsigned NumVals = N->getNumValues();
969 if (!NumVals)
970 return Sched::RegPressure;
971
972 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000973 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000974 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000975 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000976 if (VT.isFloatingPoint() || VT.isVector())
977 return Sched::Latency;
978 }
Evan Chengc10f5432010-05-28 23:25:23 +0000979
980 if (!N->isMachineOpcode())
981 return Sched::RegPressure;
982
983 // Load are scheduled for latency even if there instruction itinerary
984 // is not available.
985 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000986 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000987
Evan Chenge837dea2011-06-28 19:10:37 +0000988 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +0000989 return Sched::RegPressure;
990 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +0000991 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000992 return Sched::Latency;
993
Evan Cheng1cc39842010-05-20 23:26:43 +0000994 return Sched::RegPressure;
995}
996
Evan Chenga8e29892007-01-19 07:51:42 +0000997//===----------------------------------------------------------------------===//
998// Lowering Code
999//===----------------------------------------------------------------------===//
1000
Evan Chenga8e29892007-01-19 07:51:42 +00001001/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1002static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1003 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001004 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001005 case ISD::SETNE: return ARMCC::NE;
1006 case ISD::SETEQ: return ARMCC::EQ;
1007 case ISD::SETGT: return ARMCC::GT;
1008 case ISD::SETGE: return ARMCC::GE;
1009 case ISD::SETLT: return ARMCC::LT;
1010 case ISD::SETLE: return ARMCC::LE;
1011 case ISD::SETUGT: return ARMCC::HI;
1012 case ISD::SETUGE: return ARMCC::HS;
1013 case ISD::SETULT: return ARMCC::LO;
1014 case ISD::SETULE: return ARMCC::LS;
1015 }
1016}
1017
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001018/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1019static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001020 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001021 CondCode2 = ARMCC::AL;
1022 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001023 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001024 case ISD::SETEQ:
1025 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1026 case ISD::SETGT:
1027 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1028 case ISD::SETGE:
1029 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1030 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001031 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001032 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1033 case ISD::SETO: CondCode = ARMCC::VC; break;
1034 case ISD::SETUO: CondCode = ARMCC::VS; break;
1035 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1036 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1037 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1038 case ISD::SETLT:
1039 case ISD::SETULT: CondCode = ARMCC::LT; break;
1040 case ISD::SETLE:
1041 case ISD::SETULE: CondCode = ARMCC::LE; break;
1042 case ISD::SETNE:
1043 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1044 }
Evan Chenga8e29892007-01-19 07:51:42 +00001045}
1046
Bob Wilson1f595bb2009-04-17 19:07:39 +00001047//===----------------------------------------------------------------------===//
1048// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001049//===----------------------------------------------------------------------===//
1050
1051#include "ARMGenCallingConv.inc"
1052
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001053/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1054/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001055CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001056 bool Return,
1057 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001058 switch (CC) {
1059 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001060 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001061 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001062 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001063 if (!Subtarget->isAAPCS_ABI())
1064 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1065 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1066 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1067 }
1068 // Fallthrough
1069 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001070 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001071 if (!Subtarget->isAAPCS_ABI())
1072 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1073 else if (Subtarget->hasVFP2() &&
1074 FloatABIType == FloatABI::Hard && !isVarArg)
1075 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1076 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1077 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001078 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001079 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001080 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001081 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001082 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001083 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001084 }
1085}
1086
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087/// LowerCallResult - Lower the result values of a call into the
1088/// appropriate copies out of appropriate physical registers.
1089SDValue
1090ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001091 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092 const SmallVectorImpl<ISD::InputArg> &Ins,
1093 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001094 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001095
Bob Wilson1f595bb2009-04-17 19:07:39 +00001096 // Assign locations to each value returned by this call.
1097 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001098 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1099 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001100 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001101 CCAssignFnForNode(CallConv, /* Return*/ true,
1102 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001103
1104 // Copy all of the result registers out of their specified physreg.
1105 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1106 CCValAssign VA = RVLocs[i];
1107
Bob Wilson80915242009-04-25 00:33:20 +00001108 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001109 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001110 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001111 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001113 Chain = Lo.getValue(1);
1114 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001116 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001117 InFlag);
1118 Chain = Hi.getValue(1);
1119 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001120 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001121
Owen Anderson825b72b2009-08-11 20:47:22 +00001122 if (VA.getLocVT() == MVT::v2f64) {
1123 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1124 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1125 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001126
1127 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001128 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001129 Chain = Lo.getValue(1);
1130 InFlag = Lo.getValue(2);
1131 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001133 Chain = Hi.getValue(1);
1134 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001135 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001136 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1137 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001138 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001139 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001140 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1141 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001142 Chain = Val.getValue(1);
1143 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001144 }
Bob Wilson80915242009-04-25 00:33:20 +00001145
1146 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001147 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001148 case CCValAssign::Full: break;
1149 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001150 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001151 break;
1152 }
1153
Dan Gohman98ca4f22009-08-05 01:29:28 +00001154 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001155 }
1156
Dan Gohman98ca4f22009-08-05 01:29:28 +00001157 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001158}
1159
Bob Wilsondee46d72009-04-17 20:35:10 +00001160/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001161SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001162ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1163 SDValue StackPtr, SDValue Arg,
1164 DebugLoc dl, SelectionDAG &DAG,
1165 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001166 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 unsigned LocMemOffset = VA.getLocMemOffset();
1168 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1169 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001170 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001171 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001172 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001173}
1174
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001176 SDValue Chain, SDValue &Arg,
1177 RegsToPassVector &RegsToPass,
1178 CCValAssign &VA, CCValAssign &NextVA,
1179 SDValue &StackPtr,
1180 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001181 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001182
Jim Grosbache5165492009-11-09 00:11:35 +00001183 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001184 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001185 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1186
1187 if (NextVA.isRegLoc())
1188 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1189 else {
1190 assert(NextVA.isMemLoc());
1191 if (StackPtr.getNode() == 0)
1192 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1193
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1195 dl, DAG, NextVA,
1196 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001197 }
1198}
1199
Dan Gohman98ca4f22009-08-05 01:29:28 +00001200/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001201/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1202/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001203SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001204ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001205 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001206 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001208 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001209 const SmallVectorImpl<ISD::InputArg> &Ins,
1210 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001211 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001212 MachineFunction &MF = DAG.getMachineFunction();
1213 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1214 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001215 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001216 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001217 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001218 if (isTailCall) {
1219 // Check if it's really possible to do a tail call.
1220 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1221 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001222 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001223 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1224 // detected sibcalls.
1225 if (isTailCall) {
1226 ++NumTailCalls;
1227 IsSibCall = true;
1228 }
1229 }
Evan Chenga8e29892007-01-19 07:51:42 +00001230
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 // Analyze operands of the call, assigning locations to each operand.
1232 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001233 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1234 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001236 CCAssignFnForNode(CallConv, /* Return*/ false,
1237 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001238
Bob Wilson1f595bb2009-04-17 19:07:39 +00001239 // Get a count of how many bytes are to be pushed on the stack.
1240 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001241
Dale Johannesen51e28e62010-06-03 21:09:53 +00001242 // For tail calls, memory operands are available in our caller's stack.
1243 if (IsSibCall)
1244 NumBytes = 0;
1245
Evan Chenga8e29892007-01-19 07:51:42 +00001246 // Adjust the stack pointer for the new arguments...
1247 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001248 if (!IsSibCall)
1249 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001250
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001251 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001252
Bob Wilson5bafff32009-06-22 23:27:02 +00001253 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001255
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001257 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1259 i != e;
1260 ++i, ++realArgIdx) {
1261 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001262 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001263 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001264 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001265
Bob Wilson1f595bb2009-04-17 19:07:39 +00001266 // Promote the value if needed.
1267 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001268 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001269 case CCValAssign::Full: break;
1270 case CCValAssign::SExt:
1271 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1272 break;
1273 case CCValAssign::ZExt:
1274 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1275 break;
1276 case CCValAssign::AExt:
1277 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1278 break;
1279 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001281 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001282 }
1283
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001284 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001285 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001286 if (VA.getLocVT() == MVT::v2f64) {
1287 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1288 DAG.getConstant(0, MVT::i32));
1289 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1290 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001291
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001293 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1294
1295 VA = ArgLocs[++i]; // skip ahead to next loc
1296 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001297 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001298 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1299 } else {
1300 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001301
Dan Gohman98ca4f22009-08-05 01:29:28 +00001302 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1303 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001304 }
1305 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001306 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001307 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001308 }
1309 } else if (VA.isRegLoc()) {
1310 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001311 } else if (isByVal) {
1312 assert(VA.isMemLoc());
1313 unsigned offset = 0;
1314
1315 // True if this byval aggregate will be split between registers
1316 // and memory.
1317 if (CCInfo.isFirstByValRegValid()) {
1318 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1319 unsigned int i, j;
1320 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1321 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1322 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1323 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1324 MachinePointerInfo(),
1325 false, false, 0);
1326 MemOpChains.push_back(Load.getValue(1));
1327 RegsToPass.push_back(std::make_pair(j, Load));
1328 }
1329 offset = ARM::R4 - CCInfo.getFirstByValReg();
1330 CCInfo.clearFirstByValReg();
1331 }
1332
1333 unsigned LocMemOffset = VA.getLocMemOffset();
1334 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1335 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1336 StkPtrOff);
1337 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1338 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1339 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1340 MVT::i32);
1341 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1342 Flags.getByValAlign(),
1343 /*isVolatile=*/false,
1344 /*AlwaysInline=*/false,
1345 MachinePointerInfo(0),
1346 MachinePointerInfo(0)));
1347
1348 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001349 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001350
Dan Gohman98ca4f22009-08-05 01:29:28 +00001351 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1352 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001353 }
Evan Chenga8e29892007-01-19 07:51:42 +00001354 }
1355
1356 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001357 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001358 &MemOpChains[0], MemOpChains.size());
1359
1360 // Build a sequence of copy-to-reg nodes chained together with token chain
1361 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001362 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001363 // Tail call byval lowering might overwrite argument registers so in case of
1364 // tail call optimization the copies to registers are lowered later.
1365 if (!isTailCall)
1366 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1367 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1368 RegsToPass[i].second, InFlag);
1369 InFlag = Chain.getValue(1);
1370 }
Evan Chenga8e29892007-01-19 07:51:42 +00001371
Dale Johannesen51e28e62010-06-03 21:09:53 +00001372 // For tail calls lower the arguments to the 'real' stack slot.
1373 if (isTailCall) {
1374 // Force all the incoming stack arguments to be loaded from the stack
1375 // before any new outgoing arguments are stored to the stack, because the
1376 // outgoing stack slots may alias the incoming argument stack slots, and
1377 // the alias isn't otherwise explicit. This is slightly more conservative
1378 // than necessary, because it means that each store effectively depends
1379 // on every argument instead of just those arguments it would clobber.
1380
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001381 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001382 InFlag = SDValue();
1383 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1384 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1385 RegsToPass[i].second, InFlag);
1386 InFlag = Chain.getValue(1);
1387 }
1388 InFlag =SDValue();
1389 }
1390
Bill Wendling056292f2008-09-16 21:48:12 +00001391 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1392 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1393 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001394 bool isDirect = false;
1395 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001396 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001397 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001398
1399 if (EnableARMLongCalls) {
1400 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1401 && "long-calls with non-static relocation model!");
1402 // Handle a global address or an external symbol. If it's not one of
1403 // those, the target's already in a register, so we don't need to do
1404 // anything extra.
1405 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001406 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001407 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001408 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001409 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1410 ARMPCLabelIndex,
1411 ARMCP::CPValue, 0);
1412 // Get the address of the callee into a register
1413 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1414 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1415 Callee = DAG.getLoad(getPointerTy(), dl,
1416 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001417 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001418 false, false, 0);
1419 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1420 const char *Sym = S->getSymbol();
1421
1422 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001423 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001424 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1425 Sym, ARMPCLabelIndex, 0);
1426 // Get the address of the callee into a register
1427 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1428 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1429 Callee = DAG.getLoad(getPointerTy(), dl,
1430 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001431 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001432 false, false, 0);
1433 }
1434 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001435 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001436 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001437 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001438 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001439 getTargetMachine().getRelocationModel() != Reloc::Static;
1440 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001441 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001442 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001443 // tBX takes a register source operand.
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();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001446 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001447 ARMPCLabelIndex,
1448 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001449 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001450 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001451 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001452 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001453 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001454 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001455 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001456 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001457 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001458 } else {
1459 // On ELF targets for PIC code, direct calls should go through the PLT
1460 unsigned OpFlags = 0;
1461 if (Subtarget->isTargetELF() &&
1462 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1463 OpFlags = ARMII::MO_PLT;
1464 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1465 }
Bill Wendling056292f2008-09-16 21:48:12 +00001466 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001467 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001468 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001469 getTargetMachine().getRelocationModel() != Reloc::Static;
1470 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001471 // tBX takes a register source operand.
1472 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001473 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001474 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001475 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001476 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001477 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001479 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001480 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001481 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001482 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001483 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001484 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001485 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001486 } else {
1487 unsigned OpFlags = 0;
1488 // On ELF targets for PIC code, direct calls should go through the PLT
1489 if (Subtarget->isTargetELF() &&
1490 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1491 OpFlags = ARMII::MO_PLT;
1492 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1493 }
Evan Chenga8e29892007-01-19 07:51:42 +00001494 }
1495
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001496 // FIXME: handle tail calls differently.
1497 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001498 if (Subtarget->isThumb()) {
1499 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001500 CallOpc = ARMISD::CALL_NOLINK;
1501 else
1502 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1503 } else {
1504 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001505 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1506 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001507 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001508
Dan Gohman475871a2008-07-27 21:46:04 +00001509 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001510 Ops.push_back(Chain);
1511 Ops.push_back(Callee);
1512
1513 // Add argument registers to the end of the list so that they are known live
1514 // into the call.
1515 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1516 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1517 RegsToPass[i].second.getValueType()));
1518
Gabor Greifba36cb52008-08-28 21:40:38 +00001519 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001520 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001521
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001522 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001523 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001524 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001525
Duncan Sands4bdcb612008-07-02 17:40:58 +00001526 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001527 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001528 InFlag = Chain.getValue(1);
1529
Chris Lattnere563bbc2008-10-11 22:08:30 +00001530 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1531 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001532 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001533 InFlag = Chain.getValue(1);
1534
Bob Wilson1f595bb2009-04-17 19:07:39 +00001535 // Handle result values, copying them out of physregs into vregs that we
1536 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1538 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001539}
1540
Stuart Hastingsf222e592011-02-28 17:17:53 +00001541/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001542/// on the stack. Remember the next parameter register to allocate,
1543/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001544/// this.
1545void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001546llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1547 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1548 assert((State->getCallOrPrologue() == Prologue ||
1549 State->getCallOrPrologue() == Call) &&
1550 "unhandled ParmContext");
1551 if ((!State->isFirstByValRegValid()) &&
1552 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1553 State->setFirstByValReg(reg);
1554 // At a call site, a byval parameter that is split between
1555 // registers and memory needs its size truncated here. In a
1556 // function prologue, such byval parameters are reassembled in
1557 // memory, and are not truncated.
1558 if (State->getCallOrPrologue() == Call) {
1559 unsigned excess = 4 * (ARM::R4 - reg);
1560 assert(size >= excess && "expected larger existing stack allocation");
1561 size -= excess;
1562 }
1563 }
1564 // Confiscate any remaining parameter registers to preclude their
1565 // assignment to subsequent parameters.
1566 while (State->AllocateReg(GPRArgRegs, 4))
1567 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001568}
1569
Dale Johannesen51e28e62010-06-03 21:09:53 +00001570/// MatchingStackOffset - Return true if the given stack call argument is
1571/// already available in the same position (relatively) of the caller's
1572/// incoming argument stack.
1573static
1574bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1575 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1576 const ARMInstrInfo *TII) {
1577 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1578 int FI = INT_MAX;
1579 if (Arg.getOpcode() == ISD::CopyFromReg) {
1580 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001581 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001582 return false;
1583 MachineInstr *Def = MRI->getVRegDef(VR);
1584 if (!Def)
1585 return false;
1586 if (!Flags.isByVal()) {
1587 if (!TII->isLoadFromStackSlot(Def, FI))
1588 return false;
1589 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001590 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001591 }
1592 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1593 if (Flags.isByVal())
1594 // ByVal argument is passed in as a pointer but it's now being
1595 // dereferenced. e.g.
1596 // define @foo(%struct.X* %A) {
1597 // tail call @bar(%struct.X* byval %A)
1598 // }
1599 return false;
1600 SDValue Ptr = Ld->getBasePtr();
1601 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1602 if (!FINode)
1603 return false;
1604 FI = FINode->getIndex();
1605 } else
1606 return false;
1607
1608 assert(FI != INT_MAX);
1609 if (!MFI->isFixedObjectIndex(FI))
1610 return false;
1611 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1612}
1613
1614/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1615/// for tail call optimization. Targets which want to do tail call
1616/// optimization should implement this function.
1617bool
1618ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1619 CallingConv::ID CalleeCC,
1620 bool isVarArg,
1621 bool isCalleeStructRet,
1622 bool isCallerStructRet,
1623 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001624 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001625 const SmallVectorImpl<ISD::InputArg> &Ins,
1626 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001627 const Function *CallerF = DAG.getMachineFunction().getFunction();
1628 CallingConv::ID CallerCC = CallerF->getCallingConv();
1629 bool CCMatch = CallerCC == CalleeCC;
1630
1631 // Look for obvious safe cases to perform tail call optimization that do not
1632 // require ABI changes. This is what gcc calls sibcall.
1633
Jim Grosbach7616b642010-06-16 23:45:49 +00001634 // Do not sibcall optimize vararg calls unless the call site is not passing
1635 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636 if (isVarArg && !Outs.empty())
1637 return false;
1638
1639 // Also avoid sibcall optimization if either caller or callee uses struct
1640 // return semantics.
1641 if (isCalleeStructRet || isCallerStructRet)
1642 return false;
1643
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001644 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001645 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1646 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1647 // support in the assembler and linker to be used. This would need to be
1648 // fixed to fully support tail calls in Thumb1.
1649 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001650 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1651 // LR. This means if we need to reload LR, it takes an extra instructions,
1652 // which outweighs the value of the tail call; but here we don't know yet
1653 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001654 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001655 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001656
1657 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1658 // but we need to make sure there are enough registers; the only valid
1659 // registers are the 4 used for parameters. We don't currently do this
1660 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001661 if (Subtarget->isThumb1Only())
1662 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001663
Dale Johannesen51e28e62010-06-03 21:09:53 +00001664 // If the calling conventions do not match, then we'd better make sure the
1665 // results are returned in the same way as what the caller expects.
1666 if (!CCMatch) {
1667 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001668 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1669 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001670 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1671
1672 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001673 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1674 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001675 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1676
1677 if (RVLocs1.size() != RVLocs2.size())
1678 return false;
1679 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1680 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1681 return false;
1682 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1683 return false;
1684 if (RVLocs1[i].isRegLoc()) {
1685 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1686 return false;
1687 } else {
1688 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1689 return false;
1690 }
1691 }
1692 }
1693
1694 // If the callee takes no arguments then go on to check the results of the
1695 // call.
1696 if (!Outs.empty()) {
1697 // Check if stack adjustment is needed. For now, do not do this if any
1698 // argument is passed on the stack.
1699 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001700 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1701 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001702 CCInfo.AnalyzeCallOperands(Outs,
1703 CCAssignFnForNode(CalleeCC, false, isVarArg));
1704 if (CCInfo.getNextStackOffset()) {
1705 MachineFunction &MF = DAG.getMachineFunction();
1706
1707 // Check if the arguments are already laid out in the right way as
1708 // the caller's fixed stack objects.
1709 MachineFrameInfo *MFI = MF.getFrameInfo();
1710 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1711 const ARMInstrInfo *TII =
1712 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001713 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1714 i != e;
1715 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001716 CCValAssign &VA = ArgLocs[i];
1717 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001718 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001719 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001720 if (VA.getLocInfo() == CCValAssign::Indirect)
1721 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001722 if (VA.needsCustom()) {
1723 // f64 and vector types are split into multiple registers or
1724 // register/stack-slot combinations. The types will not match
1725 // the registers; give up on memory f64 refs until we figure
1726 // out what to do about this.
1727 if (!VA.isRegLoc())
1728 return false;
1729 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001730 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001731 if (RegVT == MVT::v2f64) {
1732 if (!ArgLocs[++i].isRegLoc())
1733 return false;
1734 if (!ArgLocs[++i].isRegLoc())
1735 return false;
1736 }
1737 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001738 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1739 MFI, MRI, TII))
1740 return false;
1741 }
1742 }
1743 }
1744 }
1745
1746 return true;
1747}
1748
Dan Gohman98ca4f22009-08-05 01:29:28 +00001749SDValue
1750ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001751 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001753 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001754 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001755
Bob Wilsondee46d72009-04-17 20:35:10 +00001756 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001758
Bob Wilsondee46d72009-04-17 20:35:10 +00001759 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001760 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1761 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001762
Dan Gohman98ca4f22009-08-05 01:29:28 +00001763 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001764 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1765 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001766
1767 // If this is the first return lowered for this function, add
1768 // the regs to the liveout set for the function.
1769 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1770 for (unsigned i = 0; i != RVLocs.size(); ++i)
1771 if (RVLocs[i].isRegLoc())
1772 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001773 }
1774
Bob Wilson1f595bb2009-04-17 19:07:39 +00001775 SDValue Flag;
1776
1777 // Copy the result values into the output registers.
1778 for (unsigned i = 0, realRVLocIdx = 0;
1779 i != RVLocs.size();
1780 ++i, ++realRVLocIdx) {
1781 CCValAssign &VA = RVLocs[i];
1782 assert(VA.isRegLoc() && "Can only return in registers!");
1783
Dan Gohmanc9403652010-07-07 15:54:55 +00001784 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001785
1786 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001787 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001788 case CCValAssign::Full: break;
1789 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001790 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001791 break;
1792 }
1793
Bob Wilson1f595bb2009-04-17 19:07:39 +00001794 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001796 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1798 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001799 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001800 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001801
1802 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1803 Flag = Chain.getValue(1);
1804 VA = RVLocs[++i]; // skip ahead to next loc
1805 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1806 HalfGPRs.getValue(1), Flag);
1807 Flag = Chain.getValue(1);
1808 VA = RVLocs[++i]; // skip ahead to next loc
1809
1810 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1812 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001813 }
1814 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1815 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001816 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001817 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001819 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001820 VA = RVLocs[++i]; // skip ahead to next loc
1821 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1822 Flag);
1823 } else
1824 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1825
Bob Wilsondee46d72009-04-17 20:35:10 +00001826 // Guarantee that all emitted copies are
1827 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001828 Flag = Chain.getValue(1);
1829 }
1830
1831 SDValue result;
1832 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001834 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001835 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001836
1837 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001838}
1839
Evan Cheng3d2125c2010-11-30 23:55:39 +00001840bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1841 if (N->getNumValues() != 1)
1842 return false;
1843 if (!N->hasNUsesOfValue(1, 0))
1844 return false;
1845
1846 unsigned NumCopies = 0;
1847 SDNode* Copies[2];
1848 SDNode *Use = *N->use_begin();
1849 if (Use->getOpcode() == ISD::CopyToReg) {
1850 Copies[NumCopies++] = Use;
1851 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1852 // f64 returned in a pair of GPRs.
1853 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1854 UI != UE; ++UI) {
1855 if (UI->getOpcode() != ISD::CopyToReg)
1856 return false;
1857 Copies[UI.getUse().getResNo()] = *UI;
1858 ++NumCopies;
1859 }
1860 } else if (Use->getOpcode() == ISD::BITCAST) {
1861 // f32 returned in a single GPR.
1862 if (!Use->hasNUsesOfValue(1, 0))
1863 return false;
1864 Use = *Use->use_begin();
1865 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1866 return false;
1867 Copies[NumCopies++] = Use;
1868 } else {
1869 return false;
1870 }
1871
1872 if (NumCopies != 1 && NumCopies != 2)
1873 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001874
1875 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001876 for (unsigned i = 0; i < NumCopies; ++i) {
1877 SDNode *Copy = Copies[i];
1878 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1879 UI != UE; ++UI) {
1880 if (UI->getOpcode() == ISD::CopyToReg) {
1881 SDNode *Use = *UI;
1882 if (Use == Copies[0] || Use == Copies[1])
1883 continue;
1884 return false;
1885 }
1886 if (UI->getOpcode() != ARMISD::RET_FLAG)
1887 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001888 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001889 }
1890 }
1891
Evan Cheng1bf891a2010-12-01 22:59:46 +00001892 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001893}
1894
Evan Cheng485fafc2011-03-21 01:19:09 +00001895bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1896 if (!EnableARMTailCalls)
1897 return false;
1898
1899 if (!CI->isTailCall())
1900 return false;
1901
1902 return !Subtarget->isThumb1Only();
1903}
1904
Bob Wilsonb62d2572009-11-03 00:02:05 +00001905// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1906// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1907// one of the above mentioned nodes. It has to be wrapped because otherwise
1908// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1909// be used to form addressing mode. These wrapped nodes will be selected
1910// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001911static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001912 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001913 // FIXME there is no actual debug info here
1914 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001915 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001916 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001917 if (CP->isMachineConstantPoolEntry())
1918 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1919 CP->getAlignment());
1920 else
1921 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1922 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001923 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001924}
1925
Jim Grosbache1102ca2010-07-19 17:20:38 +00001926unsigned ARMTargetLowering::getJumpTableEncoding() const {
1927 return MachineJumpTableInfo::EK_Inline;
1928}
1929
Dan Gohmand858e902010-04-17 15:26:15 +00001930SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1931 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001932 MachineFunction &MF = DAG.getMachineFunction();
1933 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1934 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001935 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001936 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001937 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001938 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1939 SDValue CPAddr;
1940 if (RelocM == Reloc::Static) {
1941 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1942 } else {
1943 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001944 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001945 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1946 ARMCP::CPBlockAddress,
1947 PCAdj);
1948 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1949 }
1950 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1951 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001952 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001953 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001954 if (RelocM == Reloc::Static)
1955 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001956 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001957 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001958}
1959
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001960// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001961SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001962ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001963 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001964 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001965 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001966 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001967 MachineFunction &MF = DAG.getMachineFunction();
1968 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001969 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001970 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001971 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001972 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001973 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001975 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001976 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001977 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001978 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001979
Evan Chenge7e0d622009-11-06 22:24:13 +00001980 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001981 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001982
1983 // call __tls_get_addr.
1984 ArgListTy Args;
1985 ArgListEntry Entry;
1986 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001987 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001988 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001989 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001990 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001991 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00001992 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001993 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001994 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001995 return CallResult.first;
1996}
1997
1998// Lower ISD::GlobalTLSAddress using the "initial exec" or
1999// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002000SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002001ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002002 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002003 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002004 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002005 SDValue Offset;
2006 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002007 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002008 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002009 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002010
Chris Lattner4fb63d02009-07-15 04:12:33 +00002011 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002012 MachineFunction &MF = DAG.getMachineFunction();
2013 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002014 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002015 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002016 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2017 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002018 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002019 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002020 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002022 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002023 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002024 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002025 Chain = Offset.getValue(1);
2026
Evan Chenge7e0d622009-11-06 22:24:13 +00002027 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002028 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002029
Evan Cheng9eda6892009-10-31 03:39:36 +00002030 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002031 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002032 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002033 } else {
2034 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002035 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002036 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002037 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002038 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002039 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002040 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002041 }
2042
2043 // The address of the thread local variable is the add of the thread
2044 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002045 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002046}
2047
Dan Gohman475871a2008-07-27 21:46:04 +00002048SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002049ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002050 // TODO: implement the "local dynamic" model
2051 assert(Subtarget->isTargetELF() &&
2052 "TLS not implemented for non-ELF targets");
2053 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2054 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2055 // otherwise use the "Local Exec" TLS Model
2056 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2057 return LowerToTLSGeneralDynamicModel(GA, DAG);
2058 else
2059 return LowerToTLSExecModels(GA, DAG);
2060}
2061
Dan Gohman475871a2008-07-27 21:46:04 +00002062SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002063 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002064 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002065 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002066 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002067 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2068 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002069 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002070 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002071 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002072 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002073 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002074 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002075 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002076 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002077 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002078 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002079 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002080 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002081 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002082 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002083 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002084 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002085 }
2086
2087 // If we have T2 ops, we can materialize the address directly via movt/movw
2088 // pair. This is always cheaper.
2089 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002090 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002091 // FIXME: Once remat is capable of dealing with instructions with register
2092 // operands, expand this into two nodes.
2093 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2094 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002095 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002096 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2097 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2098 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2099 MachinePointerInfo::getConstantPool(),
2100 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002101 }
2102}
2103
Dan Gohman475871a2008-07-27 21:46:04 +00002104SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002105 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002106 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002107 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002108 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002109 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002110 MachineFunction &MF = DAG.getMachineFunction();
2111 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2112
Evan Cheng4abce0c2011-05-27 20:11:27 +00002113 // FIXME: Enable this for static codegen when tool issues are fixed.
2114 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002115 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002116 // FIXME: Once remat is capable of dealing with instructions with register
2117 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002118 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002119 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2120 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2121
Evan Cheng53519f02011-01-21 18:55:51 +00002122 unsigned Wrapper = (RelocM == Reloc::PIC_)
2123 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2124 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002125 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002126 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2127 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2128 MachinePointerInfo::getGOT(), false, false, 0);
2129 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002130 }
2131
2132 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002133 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002134 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002135 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002136 } else {
2137 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002138 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2139 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002140 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002141 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002142 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002143 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002144
Evan Cheng9eda6892009-10-31 03:39:36 +00002145 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002146 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002147 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002148 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002149
2150 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002151 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002152 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002153 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002154
Evan Cheng63476a82009-09-03 07:04:02 +00002155 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002156 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002157 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002158
2159 return Result;
2160}
2161
Dan Gohman475871a2008-07-27 21:46:04 +00002162SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002163 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002164 assert(Subtarget->isTargetELF() &&
2165 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002166 MachineFunction &MF = DAG.getMachineFunction();
2167 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002168 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002169 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002170 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002171 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002172 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2173 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002174 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002175 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002177 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002178 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002179 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002180 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002181 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002182}
2183
Jim Grosbach0e0da732009-05-12 23:59:14 +00002184SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002185ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2186 const {
2187 DebugLoc dl = Op.getDebugLoc();
2188 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002189 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002190}
2191
2192SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002193ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2194 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002195 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002196 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2197 Op.getOperand(1), Val);
2198}
2199
2200SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002201ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2202 DebugLoc dl = Op.getDebugLoc();
2203 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2204 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2205}
2206
2207SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002208ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002209 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002210 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002211 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002212 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002213 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002214 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002215 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002216 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2217 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002218 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002219 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002220 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002221 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002222 EVT PtrVT = getPointerTy();
2223 DebugLoc dl = Op.getDebugLoc();
2224 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2225 SDValue CPAddr;
2226 unsigned PCAdj = (RelocM != Reloc::PIC_)
2227 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002228 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002229 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2230 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002231 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002232 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002233 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002234 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002235 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002236 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002237
2238 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002239 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002240 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2241 }
2242 return Result;
2243 }
Evan Cheng92e39162011-03-29 23:06:19 +00002244 case Intrinsic::arm_neon_vmulls:
2245 case Intrinsic::arm_neon_vmullu: {
2246 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2247 ? ARMISD::VMULLs : ARMISD::VMULLu;
2248 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2249 Op.getOperand(1), Op.getOperand(2));
2250 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002251 }
2252}
2253
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002254static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002255 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002256 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002257 if (!Subtarget->hasDataBarrier()) {
2258 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2259 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2260 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002261 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002262 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002263 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002264 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002265 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002266
2267 SDValue Op5 = Op.getOperand(5);
2268 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2269 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2270 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2271 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2272
2273 ARM_MB::MemBOpt DMBOpt;
2274 if (isDeviceBarrier)
2275 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2276 else
2277 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2278 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2279 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002280}
2281
Eli Friedman14648462011-07-27 22:21:52 +00002282
2283static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2284 const ARMSubtarget *Subtarget) {
2285 // FIXME: handle "fence singlethread" more efficiently.
2286 DebugLoc dl = Op.getDebugLoc();
2287 if (!Subtarget->hasDataBarrier()) {
2288 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2289 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2290 // here.
2291 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2292 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
2293 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
2294 DAG.getConstant(0, MVT::i32));
2295 }
2296
2297 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
2298 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
2299
2300 ARM_MB::MemBOpt DMBOpt;
2301 if (FenceOrdering == Release)
2302 DMBOpt = ARM_MB::ISHST;
2303 else
2304 DMBOpt = ARM_MB::ISH;
2305 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2306 DAG.getConstant(DMBOpt, MVT::i32));
2307}
2308
Evan Chengdfed19f2010-11-03 06:34:55 +00002309static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2310 const ARMSubtarget *Subtarget) {
2311 // ARM pre v5TE and Thumb1 does not have preload instructions.
2312 if (!(Subtarget->isThumb2() ||
2313 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2314 // Just preserve the chain.
2315 return Op.getOperand(0);
2316
2317 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002318 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2319 if (!isRead &&
2320 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2321 // ARMv7 with MP extension has PLDW.
2322 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002323
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002324 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2325 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002326 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002327 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002328 isData = ~isData & 1;
2329 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002330
2331 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002332 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2333 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002334}
2335
Dan Gohman1e93df62010-04-17 14:41:14 +00002336static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2337 MachineFunction &MF = DAG.getMachineFunction();
2338 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2339
Evan Chenga8e29892007-01-19 07:51:42 +00002340 // vastart just stores the address of the VarArgsFrameIndex slot into the
2341 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002342 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002343 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002344 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002345 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002346 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2347 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002348}
2349
Dan Gohman475871a2008-07-27 21:46:04 +00002350SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002351ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2352 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002353 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002354 MachineFunction &MF = DAG.getMachineFunction();
2355 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2356
2357 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002358 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002359 RC = ARM::tGPRRegisterClass;
2360 else
2361 RC = ARM::GPRRegisterClass;
2362
2363 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002364 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002365 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002366
2367 SDValue ArgValue2;
2368 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002369 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002370 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002371
2372 // Create load node to retrieve arguments from the stack.
2373 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002374 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002375 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002376 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002377 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002378 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002379 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002380 }
2381
Jim Grosbache5165492009-11-09 00:11:35 +00002382 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002383}
2384
Stuart Hastingsc7315872011-04-20 16:47:52 +00002385void
2386ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2387 unsigned &VARegSize, unsigned &VARegSaveSize)
2388 const {
2389 unsigned NumGPRs;
2390 if (CCInfo.isFirstByValRegValid())
2391 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2392 else {
2393 unsigned int firstUnalloced;
2394 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2395 sizeof(GPRArgRegs) /
2396 sizeof(GPRArgRegs[0]));
2397 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2398 }
2399
2400 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2401 VARegSize = NumGPRs * 4;
2402 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2403}
2404
2405// The remaining GPRs hold either the beginning of variable-argument
2406// data, or the beginning of an aggregate passed by value (usuall
2407// byval). Either way, we allocate stack slots adjacent to the data
2408// provided by our caller, and store the unallocated registers there.
2409// If this is a variadic function, the va_list pointer will begin with
2410// these values; otherwise, this reassembles a (byval) structure that
2411// was split between registers and memory.
2412void
2413ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2414 DebugLoc dl, SDValue &Chain,
2415 unsigned ArgOffset) const {
2416 MachineFunction &MF = DAG.getMachineFunction();
2417 MachineFrameInfo *MFI = MF.getFrameInfo();
2418 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2419 unsigned firstRegToSaveIndex;
2420 if (CCInfo.isFirstByValRegValid())
2421 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2422 else {
2423 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2424 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2425 }
2426
2427 unsigned VARegSize, VARegSaveSize;
2428 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2429 if (VARegSaveSize) {
2430 // If this function is vararg, store any remaining integer argument regs
2431 // to their spots on the stack so that they may be loaded by deferencing
2432 // the result of va_next.
2433 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002434 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2435 ArgOffset + VARegSaveSize
2436 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002437 false));
2438 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2439 getPointerTy());
2440
2441 SmallVector<SDValue, 4> MemOps;
2442 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2443 TargetRegisterClass *RC;
2444 if (AFI->isThumb1OnlyFunction())
2445 RC = ARM::tGPRRegisterClass;
2446 else
2447 RC = ARM::GPRRegisterClass;
2448
2449 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2450 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2451 SDValue Store =
2452 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002453 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002454 false, false, 0);
2455 MemOps.push_back(Store);
2456 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2457 DAG.getConstant(4, getPointerTy()));
2458 }
2459 if (!MemOps.empty())
2460 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2461 &MemOps[0], MemOps.size());
2462 } else
2463 // This will point to the next argument passed via stack.
2464 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2465}
2466
Bob Wilson5bafff32009-06-22 23:27:02 +00002467SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002468ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002469 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002470 const SmallVectorImpl<ISD::InputArg>
2471 &Ins,
2472 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002473 SmallVectorImpl<SDValue> &InVals)
2474 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002475 MachineFunction &MF = DAG.getMachineFunction();
2476 MachineFrameInfo *MFI = MF.getFrameInfo();
2477
Bob Wilson1f595bb2009-04-17 19:07:39 +00002478 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2479
2480 // Assign locations to all of the incoming arguments.
2481 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002482 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2483 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002484 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002485 CCAssignFnForNode(CallConv, /* Return*/ false,
2486 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002487
2488 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002489 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002490
Stuart Hastingsf222e592011-02-28 17:17:53 +00002491 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002492 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2493 CCValAssign &VA = ArgLocs[i];
2494
Bob Wilsondee46d72009-04-17 20:35:10 +00002495 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002496 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002497 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002498
Bob Wilson1f595bb2009-04-17 19:07:39 +00002499 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002500 // f64 and vector types are split up into multiple registers or
2501 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002502 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002503 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002504 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002505 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002506 SDValue ArgValue2;
2507 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002508 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002509 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2510 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002511 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002512 false, false, 0);
2513 } else {
2514 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2515 Chain, DAG, dl);
2516 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002517 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2518 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002519 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002520 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002521 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2522 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002523 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002524
Bob Wilson5bafff32009-06-22 23:27:02 +00002525 } else {
2526 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002527
Owen Anderson825b72b2009-08-11 20:47:22 +00002528 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002529 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002530 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002531 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002532 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002533 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002534 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002535 RC = (AFI->isThumb1OnlyFunction() ?
2536 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002537 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002538 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002539
2540 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002541 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002542 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002543 }
2544
2545 // If this is an 8 or 16-bit value, it is really passed promoted
2546 // to 32 bits. Insert an assert[sz]ext to capture this, then
2547 // truncate to the right size.
2548 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002549 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002550 case CCValAssign::Full: break;
2551 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002552 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002553 break;
2554 case CCValAssign::SExt:
2555 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2556 DAG.getValueType(VA.getValVT()));
2557 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2558 break;
2559 case CCValAssign::ZExt:
2560 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2561 DAG.getValueType(VA.getValVT()));
2562 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2563 break;
2564 }
2565
Dan Gohman98ca4f22009-08-05 01:29:28 +00002566 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002567
2568 } else { // VA.isRegLoc()
2569
2570 // sanity check
2571 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002572 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002573
Stuart Hastingsf222e592011-02-28 17:17:53 +00002574 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002575
Stuart Hastingsf222e592011-02-28 17:17:53 +00002576 // Some Ins[] entries become multiple ArgLoc[] entries.
2577 // Process them only once.
2578 if (index != lastInsIndex)
2579 {
2580 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002581 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002582 // This can be changed with more analysis.
2583 // In case of tail call optimization mark all arguments mutable.
2584 // Since they could be overwritten by lowering of arguments in case of
2585 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002586 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002587 unsigned VARegSize, VARegSaveSize;
2588 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2589 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2590 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002591 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002592 int FI = MFI->CreateFixedObject(Bytes,
2593 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002594 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2595 } else {
2596 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2597 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002598
Stuart Hastingsf222e592011-02-28 17:17:53 +00002599 // Create load nodes to retrieve arguments from the stack.
2600 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2601 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2602 MachinePointerInfo::getFixedStack(FI),
2603 false, false, 0));
2604 }
2605 lastInsIndex = index;
2606 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002607 }
2608 }
2609
2610 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002611 if (isVarArg)
2612 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002613
Dan Gohman98ca4f22009-08-05 01:29:28 +00002614 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002615}
2616
2617/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002618static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002619 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002620 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002621 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002622 // Maybe this has already been legalized into the constant pool?
2623 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002624 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002625 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002626 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002627 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002628 }
2629 }
2630 return false;
2631}
2632
Evan Chenga8e29892007-01-19 07:51:42 +00002633/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2634/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002635SDValue
2636ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002637 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002638 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002639 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002640 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002641 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002642 // Constant does not fit, try adjusting it by one?
2643 switch (CC) {
2644 default: break;
2645 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002646 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002647 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002648 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002649 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002650 }
2651 break;
2652 case ISD::SETULT:
2653 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002654 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002655 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002656 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002657 }
2658 break;
2659 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002660 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002661 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002662 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002663 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002664 }
2665 break;
2666 case ISD::SETULE:
2667 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002668 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002669 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002671 }
2672 break;
2673 }
2674 }
2675 }
2676
2677 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002678 ARMISD::NodeType CompareType;
2679 switch (CondCode) {
2680 default:
2681 CompareType = ARMISD::CMP;
2682 break;
2683 case ARMCC::EQ:
2684 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002685 // Uses only Z Flag
2686 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002687 break;
2688 }
Evan Cheng218977b2010-07-13 19:27:42 +00002689 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002690 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002691}
2692
2693/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002694SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002695ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002696 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002697 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002698 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002699 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002700 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002701 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2702 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002703}
2704
Bob Wilson79f56c92011-03-08 01:17:20 +00002705/// duplicateCmp - Glue values can have only one use, so this function
2706/// duplicates a comparison node.
2707SDValue
2708ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2709 unsigned Opc = Cmp.getOpcode();
2710 DebugLoc DL = Cmp.getDebugLoc();
2711 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2712 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2713
2714 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2715 Cmp = Cmp.getOperand(0);
2716 Opc = Cmp.getOpcode();
2717 if (Opc == ARMISD::CMPFP)
2718 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2719 else {
2720 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2721 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2722 }
2723 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2724}
2725
Bill Wendlingde2b1512010-08-11 08:43:16 +00002726SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2727 SDValue Cond = Op.getOperand(0);
2728 SDValue SelectTrue = Op.getOperand(1);
2729 SDValue SelectFalse = Op.getOperand(2);
2730 DebugLoc dl = Op.getDebugLoc();
2731
2732 // Convert:
2733 //
2734 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2735 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2736 //
2737 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2738 const ConstantSDNode *CMOVTrue =
2739 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2740 const ConstantSDNode *CMOVFalse =
2741 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2742
2743 if (CMOVTrue && CMOVFalse) {
2744 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2745 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2746
2747 SDValue True;
2748 SDValue False;
2749 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2750 True = SelectTrue;
2751 False = SelectFalse;
2752 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2753 True = SelectFalse;
2754 False = SelectTrue;
2755 }
2756
2757 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002758 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002759 SDValue ARMcc = Cond.getOperand(2);
2760 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002761 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002762 assert(True.getValueType() == VT);
2763 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002764 }
2765 }
2766 }
2767
2768 return DAG.getSelectCC(dl, Cond,
2769 DAG.getConstant(0, Cond.getValueType()),
2770 SelectTrue, SelectFalse, ISD::SETNE);
2771}
2772
Dan Gohmand858e902010-04-17 15:26:15 +00002773SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002774 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002775 SDValue LHS = Op.getOperand(0);
2776 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002777 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002778 SDValue TrueVal = Op.getOperand(2);
2779 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002780 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002781
Owen Anderson825b72b2009-08-11 20:47:22 +00002782 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002783 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002784 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002785 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Evan Chenge721f5c2011-07-13 00:42:17 +00002786 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002787 }
2788
2789 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002790 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002791
Evan Cheng218977b2010-07-13 19:27:42 +00002792 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2793 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002794 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002795 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002796 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002797 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002798 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002799 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002800 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002801 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002802 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002803 }
2804 return Result;
2805}
2806
Evan Cheng218977b2010-07-13 19:27:42 +00002807/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2808/// to morph to an integer compare sequence.
2809static bool canChangeToInt(SDValue Op, bool &SeenZero,
2810 const ARMSubtarget *Subtarget) {
2811 SDNode *N = Op.getNode();
2812 if (!N->hasOneUse())
2813 // Otherwise it requires moving the value from fp to integer registers.
2814 return false;
2815 if (!N->getNumValues())
2816 return false;
2817 EVT VT = Op.getValueType();
2818 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2819 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2820 // vmrs are very slow, e.g. cortex-a8.
2821 return false;
2822
2823 if (isFloatingPointZero(Op)) {
2824 SeenZero = true;
2825 return true;
2826 }
2827 return ISD::isNormalLoad(N);
2828}
2829
2830static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2831 if (isFloatingPointZero(Op))
2832 return DAG.getConstant(0, MVT::i32);
2833
2834 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2835 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002836 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002837 Ld->isVolatile(), Ld->isNonTemporal(),
2838 Ld->getAlignment());
2839
2840 llvm_unreachable("Unknown VFP cmp argument!");
2841}
2842
2843static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2844 SDValue &RetVal1, SDValue &RetVal2) {
2845 if (isFloatingPointZero(Op)) {
2846 RetVal1 = DAG.getConstant(0, MVT::i32);
2847 RetVal2 = DAG.getConstant(0, MVT::i32);
2848 return;
2849 }
2850
2851 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2852 SDValue Ptr = Ld->getBasePtr();
2853 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2854 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002855 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002856 Ld->isVolatile(), Ld->isNonTemporal(),
2857 Ld->getAlignment());
2858
2859 EVT PtrType = Ptr.getValueType();
2860 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2861 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2862 PtrType, Ptr, DAG.getConstant(4, PtrType));
2863 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2864 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002865 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002866 Ld->isVolatile(), Ld->isNonTemporal(),
2867 NewAlign);
2868 return;
2869 }
2870
2871 llvm_unreachable("Unknown VFP cmp argument!");
2872}
2873
2874/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2875/// f32 and even f64 comparisons to integer ones.
2876SDValue
2877ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2878 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002879 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002880 SDValue LHS = Op.getOperand(2);
2881 SDValue RHS = Op.getOperand(3);
2882 SDValue Dest = Op.getOperand(4);
2883 DebugLoc dl = Op.getDebugLoc();
2884
2885 bool SeenZero = false;
2886 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2887 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002888 // If one of the operand is zero, it's safe to ignore the NaN case since
2889 // we only care about equality comparisons.
2890 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002891 // If unsafe fp math optimization is enabled and there are no other uses of
2892 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002893 // to an integer comparison.
2894 if (CC == ISD::SETOEQ)
2895 CC = ISD::SETEQ;
2896 else if (CC == ISD::SETUNE)
2897 CC = ISD::SETNE;
2898
2899 SDValue ARMcc;
2900 if (LHS.getValueType() == MVT::f32) {
2901 LHS = bitcastf32Toi32(LHS, DAG);
2902 RHS = bitcastf32Toi32(RHS, DAG);
2903 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2904 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2905 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2906 Chain, Dest, ARMcc, CCR, Cmp);
2907 }
2908
2909 SDValue LHS1, LHS2;
2910 SDValue RHS1, RHS2;
2911 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2912 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2913 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2914 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002915 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002916 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2917 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2918 }
2919
2920 return SDValue();
2921}
2922
2923SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2924 SDValue Chain = Op.getOperand(0);
2925 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2926 SDValue LHS = Op.getOperand(2);
2927 SDValue RHS = Op.getOperand(3);
2928 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002929 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002930
Owen Anderson825b72b2009-08-11 20:47:22 +00002931 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002932 SDValue ARMcc;
2933 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002934 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002935 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002936 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002937 }
2938
Owen Anderson825b72b2009-08-11 20:47:22 +00002939 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002940
2941 if (UnsafeFPMath &&
2942 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2943 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2944 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2945 if (Result.getNode())
2946 return Result;
2947 }
2948
Evan Chenga8e29892007-01-19 07:51:42 +00002949 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002950 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002951
Evan Cheng218977b2010-07-13 19:27:42 +00002952 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2953 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002954 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002955 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002956 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002957 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002958 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002959 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2960 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002961 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002962 }
2963 return Res;
2964}
2965
Dan Gohmand858e902010-04-17 15:26:15 +00002966SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002967 SDValue Chain = Op.getOperand(0);
2968 SDValue Table = Op.getOperand(1);
2969 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002970 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002971
Owen Andersone50ed302009-08-10 22:56:29 +00002972 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002973 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2974 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002975 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002976 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002978 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2979 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002980 if (Subtarget->isThumb2()) {
2981 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2982 // which does another jump to the destination. This also makes it easier
2983 // to translate it to TBB / TBH later.
2984 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002985 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002986 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002987 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002988 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002989 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002990 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002991 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002992 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002993 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002994 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002995 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002996 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002997 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002998 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002999 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003000 }
Evan Chenga8e29892007-01-19 07:51:42 +00003001}
3002
Bob Wilson76a312b2010-03-19 22:51:32 +00003003static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3004 DebugLoc dl = Op.getDebugLoc();
3005 unsigned Opc;
3006
3007 switch (Op.getOpcode()) {
3008 default:
3009 assert(0 && "Invalid opcode!");
3010 case ISD::FP_TO_SINT:
3011 Opc = ARMISD::FTOSI;
3012 break;
3013 case ISD::FP_TO_UINT:
3014 Opc = ARMISD::FTOUI;
3015 break;
3016 }
3017 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003018 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003019}
3020
Cameron Zwarich3007d332011-03-29 21:41:55 +00003021static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3022 EVT VT = Op.getValueType();
3023 DebugLoc dl = Op.getDebugLoc();
3024
3025 EVT OperandVT = Op.getOperand(0).getValueType();
3026 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
3027 if (VT != MVT::v4f32)
3028 return DAG.UnrollVectorOp(Op.getNode());
3029
3030 unsigned CastOpc;
3031 unsigned Opc;
3032 switch (Op.getOpcode()) {
3033 default:
3034 assert(0 && "Invalid opcode!");
3035 case ISD::SINT_TO_FP:
3036 CastOpc = ISD::SIGN_EXTEND;
3037 Opc = ISD::SINT_TO_FP;
3038 break;
3039 case ISD::UINT_TO_FP:
3040 CastOpc = ISD::ZERO_EXTEND;
3041 Opc = ISD::UINT_TO_FP;
3042 break;
3043 }
3044
3045 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3046 return DAG.getNode(Opc, dl, VT, Op);
3047}
3048
Bob Wilson76a312b2010-03-19 22:51:32 +00003049static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3050 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003051 if (VT.isVector())
3052 return LowerVectorINT_TO_FP(Op, DAG);
3053
Bob Wilson76a312b2010-03-19 22:51:32 +00003054 DebugLoc dl = Op.getDebugLoc();
3055 unsigned Opc;
3056
3057 switch (Op.getOpcode()) {
3058 default:
3059 assert(0 && "Invalid opcode!");
3060 case ISD::SINT_TO_FP:
3061 Opc = ARMISD::SITOF;
3062 break;
3063 case ISD::UINT_TO_FP:
3064 Opc = ARMISD::UITOF;
3065 break;
3066 }
3067
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003068 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003069 return DAG.getNode(Opc, dl, VT, Op);
3070}
3071
Evan Cheng515fe3a2010-07-08 02:08:50 +00003072SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003073 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003074 SDValue Tmp0 = Op.getOperand(0);
3075 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003076 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003077 EVT VT = Op.getValueType();
3078 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003079 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3080 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3081 bool UseNEON = !InGPR && Subtarget->hasNEON();
3082
3083 if (UseNEON) {
3084 // Use VBSL to copy the sign bit.
3085 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3086 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3087 DAG.getTargetConstant(EncodedVal, MVT::i32));
3088 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3089 if (VT == MVT::f64)
3090 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3091 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3092 DAG.getConstant(32, MVT::i32));
3093 else /*if (VT == MVT::f32)*/
3094 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3095 if (SrcVT == MVT::f32) {
3096 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3097 if (VT == MVT::f64)
3098 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3099 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3100 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003101 } else if (VT == MVT::f32)
3102 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3103 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3104 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003105 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3106 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3107
3108 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3109 MVT::i32);
3110 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3111 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3112 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003113
Evan Chenge573fb32011-02-23 02:24:55 +00003114 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3115 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3116 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003117 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003118 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3119 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3120 DAG.getConstant(0, MVT::i32));
3121 } else {
3122 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3123 }
3124
3125 return Res;
3126 }
Evan Chengc143dd42011-02-11 02:28:55 +00003127
3128 // Bitcast operand 1 to i32.
3129 if (SrcVT == MVT::f64)
3130 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3131 &Tmp1, 1).getValue(1);
3132 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3133
Evan Chenge573fb32011-02-23 02:24:55 +00003134 // Or in the signbit with integer operations.
3135 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3136 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3137 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3138 if (VT == MVT::f32) {
3139 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3140 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3141 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3142 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003143 }
3144
Evan Chenge573fb32011-02-23 02:24:55 +00003145 // f64: Or the high part with signbit and then combine two parts.
3146 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3147 &Tmp0, 1);
3148 SDValue Lo = Tmp0.getValue(0);
3149 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3150 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3151 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003152}
3153
Evan Cheng2457f2c2010-05-22 01:47:14 +00003154SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3155 MachineFunction &MF = DAG.getMachineFunction();
3156 MachineFrameInfo *MFI = MF.getFrameInfo();
3157 MFI->setReturnAddressIsTaken(true);
3158
3159 EVT VT = Op.getValueType();
3160 DebugLoc dl = Op.getDebugLoc();
3161 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3162 if (Depth) {
3163 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3164 SDValue Offset = DAG.getConstant(4, MVT::i32);
3165 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3166 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003167 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003168 }
3169
3170 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003171 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003172 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3173}
3174
Dan Gohmand858e902010-04-17 15:26:15 +00003175SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003176 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3177 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003178
Owen Andersone50ed302009-08-10 22:56:29 +00003179 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003180 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3181 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003182 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003183 ? ARM::R7 : ARM::R11;
3184 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3185 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003186 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3187 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003188 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003189 return FrameAddr;
3190}
3191
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003192/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003193/// expand a bit convert where either the source or destination type is i64 to
3194/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3195/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3196/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003197static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003198 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3199 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003200 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003201
Bob Wilson9f3f0612010-04-17 05:30:19 +00003202 // This function is only supposed to be called for i64 types, either as the
3203 // source or destination of the bit convert.
3204 EVT SrcVT = Op.getValueType();
3205 EVT DstVT = N->getValueType(0);
3206 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003207 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003208
Bob Wilson9f3f0612010-04-17 05:30:19 +00003209 // Turn i64->f64 into VMOVDRR.
3210 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003211 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3212 DAG.getConstant(0, MVT::i32));
3213 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3214 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003215 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003216 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003217 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003218
Jim Grosbache5165492009-11-09 00:11:35 +00003219 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003220 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3221 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3222 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3223 // Merge the pieces into a single i64 value.
3224 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3225 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003226
Bob Wilson9f3f0612010-04-17 05:30:19 +00003227 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003228}
3229
Bob Wilson5bafff32009-06-22 23:27:02 +00003230/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003231/// Zero vectors are used to represent vector negation and in those cases
3232/// will be implemented with the NEON VNEG instruction. However, VNEG does
3233/// not support i64 elements, so sometimes the zero vectors will need to be
3234/// explicitly constructed. Regardless, use a canonical VMOV to create the
3235/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003236static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003237 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003238 // The canonical modified immediate encoding of a zero vector is....0!
3239 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3240 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3241 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003242 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003243}
3244
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003245/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3246/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003247SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3248 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003249 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3250 EVT VT = Op.getValueType();
3251 unsigned VTBits = VT.getSizeInBits();
3252 DebugLoc dl = Op.getDebugLoc();
3253 SDValue ShOpLo = Op.getOperand(0);
3254 SDValue ShOpHi = Op.getOperand(1);
3255 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003256 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003257 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003258
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003259 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3260
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003261 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3262 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3263 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3264 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3265 DAG.getConstant(VTBits, MVT::i32));
3266 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3267 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003268 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003269
3270 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3271 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003272 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003273 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003274 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003275 CCR, Cmp);
3276
3277 SDValue Ops[2] = { Lo, Hi };
3278 return DAG.getMergeValues(Ops, 2, dl);
3279}
3280
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003281/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3282/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003283SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3284 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003285 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3286 EVT VT = Op.getValueType();
3287 unsigned VTBits = VT.getSizeInBits();
3288 DebugLoc dl = Op.getDebugLoc();
3289 SDValue ShOpLo = Op.getOperand(0);
3290 SDValue ShOpHi = Op.getOperand(1);
3291 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003292 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003293
3294 assert(Op.getOpcode() == ISD::SHL_PARTS);
3295 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3296 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3297 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3298 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3299 DAG.getConstant(VTBits, MVT::i32));
3300 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3301 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3302
3303 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3304 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3305 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003306 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003307 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003308 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003309 CCR, Cmp);
3310
3311 SDValue Ops[2] = { Lo, Hi };
3312 return DAG.getMergeValues(Ops, 2, dl);
3313}
3314
Jim Grosbach4725ca72010-09-08 03:54:02 +00003315SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003316 SelectionDAG &DAG) const {
3317 // The rounding mode is in bits 23:22 of the FPSCR.
3318 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3319 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3320 // so that the shift + and get folded into a bitfield extract.
3321 DebugLoc dl = Op.getDebugLoc();
3322 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3323 DAG.getConstant(Intrinsic::arm_get_fpscr,
3324 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003325 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003326 DAG.getConstant(1U << 22, MVT::i32));
3327 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3328 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003329 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003330 DAG.getConstant(3, MVT::i32));
3331}
3332
Jim Grosbach3482c802010-01-18 19:58:49 +00003333static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3334 const ARMSubtarget *ST) {
3335 EVT VT = N->getValueType(0);
3336 DebugLoc dl = N->getDebugLoc();
3337
3338 if (!ST->hasV6T2Ops())
3339 return SDValue();
3340
3341 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3342 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3343}
3344
Bob Wilson5bafff32009-06-22 23:27:02 +00003345static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3346 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003347 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003348 DebugLoc dl = N->getDebugLoc();
3349
Bob Wilsond5448bb2010-11-18 21:16:28 +00003350 if (!VT.isVector())
3351 return SDValue();
3352
Bob Wilson5bafff32009-06-22 23:27:02 +00003353 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003354 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003355
Bob Wilsond5448bb2010-11-18 21:16:28 +00003356 // Left shifts translate directly to the vshiftu intrinsic.
3357 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003358 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003359 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3360 N->getOperand(0), N->getOperand(1));
3361
3362 assert((N->getOpcode() == ISD::SRA ||
3363 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3364
3365 // NEON uses the same intrinsics for both left and right shifts. For
3366 // right shifts, the shift amounts are negative, so negate the vector of
3367 // shift amounts.
3368 EVT ShiftVT = N->getOperand(1).getValueType();
3369 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3370 getZeroVector(ShiftVT, DAG, dl),
3371 N->getOperand(1));
3372 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3373 Intrinsic::arm_neon_vshifts :
3374 Intrinsic::arm_neon_vshiftu);
3375 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3376 DAG.getConstant(vshiftInt, MVT::i32),
3377 N->getOperand(0), NegatedCount);
3378}
3379
3380static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3381 const ARMSubtarget *ST) {
3382 EVT VT = N->getValueType(0);
3383 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003384
Eli Friedmance392eb2009-08-22 03:13:10 +00003385 // We can get here for a node like i32 = ISD::SHL i32, i64
3386 if (VT != MVT::i64)
3387 return SDValue();
3388
3389 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003390 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003391
Chris Lattner27a6c732007-11-24 07:07:01 +00003392 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3393 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003394 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003395 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003396
Chris Lattner27a6c732007-11-24 07:07:01 +00003397 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003398 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003399
Chris Lattner27a6c732007-11-24 07:07:01 +00003400 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003402 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003403 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003404 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003405
Chris Lattner27a6c732007-11-24 07:07:01 +00003406 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3407 // captures the result into a carry flag.
3408 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003409 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003410
Chris Lattner27a6c732007-11-24 07:07:01 +00003411 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003412 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003413
Chris Lattner27a6c732007-11-24 07:07:01 +00003414 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003415 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003416}
3417
Bob Wilson5bafff32009-06-22 23:27:02 +00003418static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3419 SDValue TmpOp0, TmpOp1;
3420 bool Invert = false;
3421 bool Swap = false;
3422 unsigned Opc = 0;
3423
3424 SDValue Op0 = Op.getOperand(0);
3425 SDValue Op1 = Op.getOperand(1);
3426 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003427 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003428 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3429 DebugLoc dl = Op.getDebugLoc();
3430
3431 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3432 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003433 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003434 case ISD::SETUNE:
3435 case ISD::SETNE: Invert = true; // Fallthrough
3436 case ISD::SETOEQ:
3437 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3438 case ISD::SETOLT:
3439 case ISD::SETLT: Swap = true; // Fallthrough
3440 case ISD::SETOGT:
3441 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3442 case ISD::SETOLE:
3443 case ISD::SETLE: Swap = true; // Fallthrough
3444 case ISD::SETOGE:
3445 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3446 case ISD::SETUGE: Swap = true; // Fallthrough
3447 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3448 case ISD::SETUGT: Swap = true; // Fallthrough
3449 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3450 case ISD::SETUEQ: Invert = true; // Fallthrough
3451 case ISD::SETONE:
3452 // Expand this to (OLT | OGT).
3453 TmpOp0 = Op0;
3454 TmpOp1 = Op1;
3455 Opc = ISD::OR;
3456 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3457 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3458 break;
3459 case ISD::SETUO: Invert = true; // Fallthrough
3460 case ISD::SETO:
3461 // Expand this to (OLT | OGE).
3462 TmpOp0 = Op0;
3463 TmpOp1 = Op1;
3464 Opc = ISD::OR;
3465 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3466 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3467 break;
3468 }
3469 } else {
3470 // Integer comparisons.
3471 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003472 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003473 case ISD::SETNE: Invert = true;
3474 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3475 case ISD::SETLT: Swap = true;
3476 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3477 case ISD::SETLE: Swap = true;
3478 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3479 case ISD::SETULT: Swap = true;
3480 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3481 case ISD::SETULE: Swap = true;
3482 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3483 }
3484
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003485 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003486 if (Opc == ARMISD::VCEQ) {
3487
3488 SDValue AndOp;
3489 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3490 AndOp = Op0;
3491 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3492 AndOp = Op1;
3493
3494 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003495 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003496 AndOp = AndOp.getOperand(0);
3497
3498 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3499 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003500 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3501 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003502 Invert = !Invert;
3503 }
3504 }
3505 }
3506
3507 if (Swap)
3508 std::swap(Op0, Op1);
3509
Owen Andersonc24cb352010-11-08 23:21:22 +00003510 // If one of the operands is a constant vector zero, attempt to fold the
3511 // comparison to a specialized compare-against-zero form.
3512 SDValue SingleOp;
3513 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3514 SingleOp = Op0;
3515 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3516 if (Opc == ARMISD::VCGE)
3517 Opc = ARMISD::VCLEZ;
3518 else if (Opc == ARMISD::VCGT)
3519 Opc = ARMISD::VCLTZ;
3520 SingleOp = Op1;
3521 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003522
Owen Andersonc24cb352010-11-08 23:21:22 +00003523 SDValue Result;
3524 if (SingleOp.getNode()) {
3525 switch (Opc) {
3526 case ARMISD::VCEQ:
3527 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3528 case ARMISD::VCGE:
3529 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3530 case ARMISD::VCLEZ:
3531 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3532 case ARMISD::VCGT:
3533 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3534 case ARMISD::VCLTZ:
3535 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3536 default:
3537 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3538 }
3539 } else {
3540 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3541 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003542
3543 if (Invert)
3544 Result = DAG.getNOT(dl, Result, VT);
3545
3546 return Result;
3547}
3548
Bob Wilsond3c42842010-06-14 22:19:57 +00003549/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3550/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003551/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003552static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3553 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003554 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003555 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003556
Bob Wilson827b2102010-06-15 19:05:35 +00003557 // SplatBitSize is set to the smallest size that splats the vector, so a
3558 // zero vector will always have SplatBitSize == 8. However, NEON modified
3559 // immediate instructions others than VMOV do not support the 8-bit encoding
3560 // of a zero vector, and the default encoding of zero is supposed to be the
3561 // 32-bit version.
3562 if (SplatBits == 0)
3563 SplatBitSize = 32;
3564
Bob Wilson5bafff32009-06-22 23:27:02 +00003565 switch (SplatBitSize) {
3566 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003567 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003568 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003569 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003570 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003571 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003572 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003573 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003574 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003575
3576 case 16:
3577 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003578 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003579 if ((SplatBits & ~0xff) == 0) {
3580 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003581 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003582 Imm = SplatBits;
3583 break;
3584 }
3585 if ((SplatBits & ~0xff00) == 0) {
3586 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003587 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003588 Imm = SplatBits >> 8;
3589 break;
3590 }
3591 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003592
3593 case 32:
3594 // NEON's 32-bit VMOV supports splat values where:
3595 // * only one byte is nonzero, or
3596 // * the least significant byte is 0xff and the second byte is nonzero, or
3597 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003598 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003599 if ((SplatBits & ~0xff) == 0) {
3600 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003601 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003602 Imm = SplatBits;
3603 break;
3604 }
3605 if ((SplatBits & ~0xff00) == 0) {
3606 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003607 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003608 Imm = SplatBits >> 8;
3609 break;
3610 }
3611 if ((SplatBits & ~0xff0000) == 0) {
3612 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003613 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003614 Imm = SplatBits >> 16;
3615 break;
3616 }
3617 if ((SplatBits & ~0xff000000) == 0) {
3618 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003619 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003620 Imm = SplatBits >> 24;
3621 break;
3622 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003623
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003624 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3625 if (type == OtherModImm) return SDValue();
3626
Bob Wilson5bafff32009-06-22 23:27:02 +00003627 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003628 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3629 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003630 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003631 Imm = SplatBits >> 8;
3632 SplatBits |= 0xff;
3633 break;
3634 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003635
3636 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003637 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3638 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003639 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003640 Imm = SplatBits >> 16;
3641 SplatBits |= 0xffff;
3642 break;
3643 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003644
3645 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3646 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3647 // VMOV.I32. A (very) minor optimization would be to replicate the value
3648 // and fall through here to test for a valid 64-bit splat. But, then the
3649 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003650 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003651
3652 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003653 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003654 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003655 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003656 uint64_t BitMask = 0xff;
3657 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003658 unsigned ImmMask = 1;
3659 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003660 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003661 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003662 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003663 Imm |= ImmMask;
3664 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003665 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003666 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003667 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003668 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003669 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003670 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003671 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003672 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003673 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003674 break;
3675 }
3676
Bob Wilson1a913ed2010-06-11 21:34:50 +00003677 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003678 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003679 return SDValue();
3680 }
3681
Bob Wilsoncba270d2010-07-13 21:16:48 +00003682 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3683 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003684}
3685
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003686static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3687 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003688 unsigned NumElts = VT.getVectorNumElements();
3689 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003690
3691 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3692 if (M[0] < 0)
3693 return false;
3694
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003695 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003696
3697 // If this is a VEXT shuffle, the immediate value is the index of the first
3698 // element. The other shuffle indices must be the successive elements after
3699 // the first one.
3700 unsigned ExpectedElt = Imm;
3701 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003702 // Increment the expected index. If it wraps around, it may still be
3703 // a VEXT but the source vectors must be swapped.
3704 ExpectedElt += 1;
3705 if (ExpectedElt == NumElts * 2) {
3706 ExpectedElt = 0;
3707 ReverseVEXT = true;
3708 }
3709
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003710 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003711 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003712 return false;
3713 }
3714
3715 // Adjust the index value if the source operands will be swapped.
3716 if (ReverseVEXT)
3717 Imm -= NumElts;
3718
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003719 return true;
3720}
3721
Bob Wilson8bb9e482009-07-26 00:39:34 +00003722/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3723/// instruction with the specified blocksize. (The order of the elements
3724/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003725static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3726 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003727 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3728 "Only possible block sizes for VREV are: 16, 32, 64");
3729
Bob Wilson8bb9e482009-07-26 00:39:34 +00003730 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003731 if (EltSz == 64)
3732 return false;
3733
3734 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003735 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003736 // If the first shuffle index is UNDEF, be optimistic.
3737 if (M[0] < 0)
3738 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003739
3740 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3741 return false;
3742
3743 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003744 if (M[i] < 0) continue; // ignore UNDEF indices
3745 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003746 return false;
3747 }
3748
3749 return true;
3750}
3751
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003752static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3753 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3754 // range, then 0 is placed into the resulting vector. So pretty much any mask
3755 // of 8 elements can work here.
3756 return VT == MVT::v8i8 && M.size() == 8;
3757}
3758
Bob Wilsonc692cb72009-08-21 20:54:19 +00003759static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3760 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003761 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3762 if (EltSz == 64)
3763 return false;
3764
Bob Wilsonc692cb72009-08-21 20:54:19 +00003765 unsigned NumElts = VT.getVectorNumElements();
3766 WhichResult = (M[0] == 0 ? 0 : 1);
3767 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003768 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3769 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003770 return false;
3771 }
3772 return true;
3773}
3774
Bob Wilson324f4f12009-12-03 06:40:55 +00003775/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3776/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3777/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3778static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3779 unsigned &WhichResult) {
3780 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3781 if (EltSz == 64)
3782 return false;
3783
3784 unsigned NumElts = VT.getVectorNumElements();
3785 WhichResult = (M[0] == 0 ? 0 : 1);
3786 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003787 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3788 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003789 return false;
3790 }
3791 return true;
3792}
3793
Bob Wilsonc692cb72009-08-21 20:54:19 +00003794static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3795 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003796 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3797 if (EltSz == 64)
3798 return false;
3799
Bob Wilsonc692cb72009-08-21 20:54:19 +00003800 unsigned NumElts = VT.getVectorNumElements();
3801 WhichResult = (M[0] == 0 ? 0 : 1);
3802 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003803 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003804 if ((unsigned) M[i] != 2 * i + WhichResult)
3805 return false;
3806 }
3807
3808 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003809 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003810 return false;
3811
3812 return true;
3813}
3814
Bob Wilson324f4f12009-12-03 06:40:55 +00003815/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3816/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3817/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3818static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3819 unsigned &WhichResult) {
3820 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3821 if (EltSz == 64)
3822 return false;
3823
3824 unsigned Half = VT.getVectorNumElements() / 2;
3825 WhichResult = (M[0] == 0 ? 0 : 1);
3826 for (unsigned j = 0; j != 2; ++j) {
3827 unsigned Idx = WhichResult;
3828 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003829 int MIdx = M[i + j * Half];
3830 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003831 return false;
3832 Idx += 2;
3833 }
3834 }
3835
3836 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3837 if (VT.is64BitVector() && EltSz == 32)
3838 return false;
3839
3840 return true;
3841}
3842
Bob Wilsonc692cb72009-08-21 20:54:19 +00003843static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3844 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003845 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3846 if (EltSz == 64)
3847 return false;
3848
Bob Wilsonc692cb72009-08-21 20:54:19 +00003849 unsigned NumElts = VT.getVectorNumElements();
3850 WhichResult = (M[0] == 0 ? 0 : 1);
3851 unsigned Idx = WhichResult * NumElts / 2;
3852 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003853 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3854 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003855 return false;
3856 Idx += 1;
3857 }
3858
3859 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003860 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003861 return false;
3862
3863 return true;
3864}
3865
Bob Wilson324f4f12009-12-03 06:40:55 +00003866/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3867/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3868/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3869static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3870 unsigned &WhichResult) {
3871 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3872 if (EltSz == 64)
3873 return false;
3874
3875 unsigned NumElts = VT.getVectorNumElements();
3876 WhichResult = (M[0] == 0 ? 0 : 1);
3877 unsigned Idx = WhichResult * NumElts / 2;
3878 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003879 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3880 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003881 return false;
3882 Idx += 1;
3883 }
3884
3885 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3886 if (VT.is64BitVector() && EltSz == 32)
3887 return false;
3888
3889 return true;
3890}
3891
Dale Johannesenf630c712010-07-29 20:10:08 +00003892// If N is an integer constant that can be moved into a register in one
3893// instruction, return an SDValue of such a constant (will become a MOV
3894// instruction). Otherwise return null.
3895static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3896 const ARMSubtarget *ST, DebugLoc dl) {
3897 uint64_t Val;
3898 if (!isa<ConstantSDNode>(N))
3899 return SDValue();
3900 Val = cast<ConstantSDNode>(N)->getZExtValue();
3901
3902 if (ST->isThumb1Only()) {
3903 if (Val <= 255 || ~Val <= 255)
3904 return DAG.getConstant(Val, MVT::i32);
3905 } else {
3906 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3907 return DAG.getConstant(Val, MVT::i32);
3908 }
3909 return SDValue();
3910}
3911
Bob Wilson5bafff32009-06-22 23:27:02 +00003912// If this is a case we can't handle, return null and let the default
3913// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003914SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3915 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003916 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003917 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003918 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003919
3920 APInt SplatBits, SplatUndef;
3921 unsigned SplatBitSize;
3922 bool HasAnyUndefs;
3923 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003924 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003925 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003926 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003927 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003928 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003929 DAG, VmovVT, VT.is128BitVector(),
3930 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003931 if (Val.getNode()) {
3932 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003933 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003934 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003935
3936 // Try an immediate VMVN.
3937 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3938 ((1LL << SplatBitSize) - 1));
3939 Val = isNEONModifiedImm(NegatedImm,
3940 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003941 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003942 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003943 if (Val.getNode()) {
3944 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003945 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003946 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003947 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003948 }
3949
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003950 // Scan through the operands to see if only one value is used.
3951 unsigned NumElts = VT.getVectorNumElements();
3952 bool isOnlyLowElement = true;
3953 bool usesOnlyOneValue = true;
3954 bool isConstant = true;
3955 SDValue Value;
3956 for (unsigned i = 0; i < NumElts; ++i) {
3957 SDValue V = Op.getOperand(i);
3958 if (V.getOpcode() == ISD::UNDEF)
3959 continue;
3960 if (i > 0)
3961 isOnlyLowElement = false;
3962 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3963 isConstant = false;
3964
3965 if (!Value.getNode())
3966 Value = V;
3967 else if (V != Value)
3968 usesOnlyOneValue = false;
3969 }
3970
3971 if (!Value.getNode())
3972 return DAG.getUNDEF(VT);
3973
3974 if (isOnlyLowElement)
3975 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3976
Dale Johannesenf630c712010-07-29 20:10:08 +00003977 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3978
Dale Johannesen575cd142010-10-19 20:00:17 +00003979 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3980 // i32 and try again.
3981 if (usesOnlyOneValue && EltSize <= 32) {
3982 if (!isConstant)
3983 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3984 if (VT.getVectorElementType().isFloatingPoint()) {
3985 SmallVector<SDValue, 8> Ops;
3986 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003987 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003988 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003989 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3990 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003991 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3992 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003993 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003994 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003995 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3996 if (Val.getNode())
3997 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003998 }
3999
4000 // If all elements are constants and the case above didn't get hit, fall back
4001 // to the default expansion, which will generate a load from the constant
4002 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004003 if (isConstant)
4004 return SDValue();
4005
Bob Wilson11a1dff2011-01-07 21:37:30 +00004006 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4007 if (NumElts >= 4) {
4008 SDValue shuffle = ReconstructShuffle(Op, DAG);
4009 if (shuffle != SDValue())
4010 return shuffle;
4011 }
4012
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004013 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004014 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4015 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004016 if (EltSize >= 32) {
4017 // Do the expansion with floating-point types, since that is what the VFP
4018 // registers are defined to use, and since i64 is not legal.
4019 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4020 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004021 SmallVector<SDValue, 8> Ops;
4022 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004023 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004024 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004025 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004026 }
4027
4028 return SDValue();
4029}
4030
Bob Wilson11a1dff2011-01-07 21:37:30 +00004031// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004032// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004033SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4034 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004035 DebugLoc dl = Op.getDebugLoc();
4036 EVT VT = Op.getValueType();
4037 unsigned NumElts = VT.getVectorNumElements();
4038
4039 SmallVector<SDValue, 2> SourceVecs;
4040 SmallVector<unsigned, 2> MinElts;
4041 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004042
Bob Wilson11a1dff2011-01-07 21:37:30 +00004043 for (unsigned i = 0; i < NumElts; ++i) {
4044 SDValue V = Op.getOperand(i);
4045 if (V.getOpcode() == ISD::UNDEF)
4046 continue;
4047 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4048 // A shuffle can only come from building a vector from various
4049 // elements of other vectors.
4050 return SDValue();
4051 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004052
Bob Wilson11a1dff2011-01-07 21:37:30 +00004053 // Record this extraction against the appropriate vector if possible...
4054 SDValue SourceVec = V.getOperand(0);
4055 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4056 bool FoundSource = false;
4057 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4058 if (SourceVecs[j] == SourceVec) {
4059 if (MinElts[j] > EltNo)
4060 MinElts[j] = EltNo;
4061 if (MaxElts[j] < EltNo)
4062 MaxElts[j] = EltNo;
4063 FoundSource = true;
4064 break;
4065 }
4066 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004067
Bob Wilson11a1dff2011-01-07 21:37:30 +00004068 // Or record a new source if not...
4069 if (!FoundSource) {
4070 SourceVecs.push_back(SourceVec);
4071 MinElts.push_back(EltNo);
4072 MaxElts.push_back(EltNo);
4073 }
4074 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004075
Bob Wilson11a1dff2011-01-07 21:37:30 +00004076 // Currently only do something sane when at most two source vectors
4077 // involved.
4078 if (SourceVecs.size() > 2)
4079 return SDValue();
4080
4081 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4082 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004083
Bob Wilson11a1dff2011-01-07 21:37:30 +00004084 // This loop extracts the usage patterns of the source vectors
4085 // and prepares appropriate SDValues for a shuffle if possible.
4086 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4087 if (SourceVecs[i].getValueType() == VT) {
4088 // No VEXT necessary
4089 ShuffleSrcs[i] = SourceVecs[i];
4090 VEXTOffsets[i] = 0;
4091 continue;
4092 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4093 // It probably isn't worth padding out a smaller vector just to
4094 // break it down again in a shuffle.
4095 return SDValue();
4096 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004097
Bob Wilson11a1dff2011-01-07 21:37:30 +00004098 // Since only 64-bit and 128-bit vectors are legal on ARM and
4099 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004100 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4101 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004102
Bob Wilson11a1dff2011-01-07 21:37:30 +00004103 if (MaxElts[i] - MinElts[i] >= NumElts) {
4104 // Span too large for a VEXT to cope
4105 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004106 }
4107
Bob Wilson11a1dff2011-01-07 21:37:30 +00004108 if (MinElts[i] >= NumElts) {
4109 // The extraction can just take the second half
4110 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004111 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4112 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004113 DAG.getIntPtrConstant(NumElts));
4114 } else if (MaxElts[i] < NumElts) {
4115 // The extraction can just take the first half
4116 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004117 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4118 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004119 DAG.getIntPtrConstant(0));
4120 } else {
4121 // An actual VEXT is needed
4122 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004123 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4124 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004125 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004126 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4127 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004128 DAG.getIntPtrConstant(NumElts));
4129 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4130 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4131 }
4132 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004133
Bob Wilson11a1dff2011-01-07 21:37:30 +00004134 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004135
Bob Wilson11a1dff2011-01-07 21:37:30 +00004136 for (unsigned i = 0; i < NumElts; ++i) {
4137 SDValue Entry = Op.getOperand(i);
4138 if (Entry.getOpcode() == ISD::UNDEF) {
4139 Mask.push_back(-1);
4140 continue;
4141 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004142
Bob Wilson11a1dff2011-01-07 21:37:30 +00004143 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004144 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4145 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004146 if (ExtractVec == SourceVecs[0]) {
4147 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4148 } else {
4149 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4150 }
4151 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004152
Bob Wilson11a1dff2011-01-07 21:37:30 +00004153 // Final check before we try to produce nonsense...
4154 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004155 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4156 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004157
Bob Wilson11a1dff2011-01-07 21:37:30 +00004158 return SDValue();
4159}
4160
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004161/// isShuffleMaskLegal - Targets can use this to indicate that they only
4162/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4163/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4164/// are assumed to be legal.
4165bool
4166ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4167 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004168 if (VT.getVectorNumElements() == 4 &&
4169 (VT.is128BitVector() || VT.is64BitVector())) {
4170 unsigned PFIndexes[4];
4171 for (unsigned i = 0; i != 4; ++i) {
4172 if (M[i] < 0)
4173 PFIndexes[i] = 8;
4174 else
4175 PFIndexes[i] = M[i];
4176 }
4177
4178 // Compute the index in the perfect shuffle table.
4179 unsigned PFTableIndex =
4180 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4181 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4182 unsigned Cost = (PFEntry >> 30);
4183
4184 if (Cost <= 4)
4185 return true;
4186 }
4187
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004188 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004189 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004190
Bob Wilson53dd2452010-06-07 23:53:38 +00004191 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4192 return (EltSize >= 32 ||
4193 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004194 isVREVMask(M, VT, 64) ||
4195 isVREVMask(M, VT, 32) ||
4196 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004197 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004198 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004199 isVTRNMask(M, VT, WhichResult) ||
4200 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004201 isVZIPMask(M, VT, WhichResult) ||
4202 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4203 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4204 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004205}
4206
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004207/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4208/// the specified operations to build the shuffle.
4209static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4210 SDValue RHS, SelectionDAG &DAG,
4211 DebugLoc dl) {
4212 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4213 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4214 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4215
4216 enum {
4217 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4218 OP_VREV,
4219 OP_VDUP0,
4220 OP_VDUP1,
4221 OP_VDUP2,
4222 OP_VDUP3,
4223 OP_VEXT1,
4224 OP_VEXT2,
4225 OP_VEXT3,
4226 OP_VUZPL, // VUZP, left result
4227 OP_VUZPR, // VUZP, right result
4228 OP_VZIPL, // VZIP, left result
4229 OP_VZIPR, // VZIP, right result
4230 OP_VTRNL, // VTRN, left result
4231 OP_VTRNR // VTRN, right result
4232 };
4233
4234 if (OpNum == OP_COPY) {
4235 if (LHSID == (1*9+2)*9+3) return LHS;
4236 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4237 return RHS;
4238 }
4239
4240 SDValue OpLHS, OpRHS;
4241 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4242 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4243 EVT VT = OpLHS.getValueType();
4244
4245 switch (OpNum) {
4246 default: llvm_unreachable("Unknown shuffle opcode!");
4247 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004248 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004249 if (VT.getVectorElementType() == MVT::i32 ||
4250 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004251 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4252 // vrev <4 x i16> -> VREV32
4253 if (VT.getVectorElementType() == MVT::i16)
4254 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4255 // vrev <4 x i8> -> VREV16
4256 assert(VT.getVectorElementType() == MVT::i8);
4257 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004258 case OP_VDUP0:
4259 case OP_VDUP1:
4260 case OP_VDUP2:
4261 case OP_VDUP3:
4262 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004263 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004264 case OP_VEXT1:
4265 case OP_VEXT2:
4266 case OP_VEXT3:
4267 return DAG.getNode(ARMISD::VEXT, dl, VT,
4268 OpLHS, OpRHS,
4269 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4270 case OP_VUZPL:
4271 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004272 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004273 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4274 case OP_VZIPL:
4275 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004276 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004277 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4278 case OP_VTRNL:
4279 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004280 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4281 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004282 }
4283}
4284
Bill Wendling69a05a72011-03-14 23:02:38 +00004285static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4286 SmallVectorImpl<int> &ShuffleMask,
4287 SelectionDAG &DAG) {
4288 // Check to see if we can use the VTBL instruction.
4289 SDValue V1 = Op.getOperand(0);
4290 SDValue V2 = Op.getOperand(1);
4291 DebugLoc DL = Op.getDebugLoc();
4292
4293 SmallVector<SDValue, 8> VTBLMask;
4294 for (SmallVectorImpl<int>::iterator
4295 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4296 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4297
4298 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4299 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4300 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4301 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004302
Owen Anderson76706012011-04-05 21:48:57 +00004303 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004304 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4305 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004306}
4307
Bob Wilson5bafff32009-06-22 23:27:02 +00004308static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004309 SDValue V1 = Op.getOperand(0);
4310 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004311 DebugLoc dl = Op.getDebugLoc();
4312 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004313 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004314 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004315
Bob Wilson28865062009-08-13 02:13:04 +00004316 // Convert shuffles that are directly supported on NEON to target-specific
4317 // DAG nodes, instead of keeping them as shuffles and matching them again
4318 // during code selection. This is more efficient and avoids the possibility
4319 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004320 // FIXME: floating-point vectors should be canonicalized to integer vectors
4321 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004322 SVN->getMask(ShuffleMask);
4323
Bob Wilson53dd2452010-06-07 23:53:38 +00004324 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4325 if (EltSize <= 32) {
4326 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4327 int Lane = SVN->getSplatIndex();
4328 // If this is undef splat, generate it via "just" vdup, if possible.
4329 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004330
Bob Wilson53dd2452010-06-07 23:53:38 +00004331 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4332 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4333 }
4334 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4335 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004336 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004337
4338 bool ReverseVEXT;
4339 unsigned Imm;
4340 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4341 if (ReverseVEXT)
4342 std::swap(V1, V2);
4343 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4344 DAG.getConstant(Imm, MVT::i32));
4345 }
4346
4347 if (isVREVMask(ShuffleMask, VT, 64))
4348 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4349 if (isVREVMask(ShuffleMask, VT, 32))
4350 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4351 if (isVREVMask(ShuffleMask, VT, 16))
4352 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4353
4354 // Check for Neon shuffles that modify both input vectors in place.
4355 // If both results are used, i.e., if there are two shuffles with the same
4356 // source operands and with masks corresponding to both results of one of
4357 // these operations, DAG memoization will ensure that a single node is
4358 // used for both shuffles.
4359 unsigned WhichResult;
4360 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4361 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4362 V1, V2).getValue(WhichResult);
4363 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4364 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4365 V1, V2).getValue(WhichResult);
4366 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4367 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4368 V1, V2).getValue(WhichResult);
4369
4370 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4371 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4372 V1, V1).getValue(WhichResult);
4373 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4374 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4375 V1, V1).getValue(WhichResult);
4376 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4377 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4378 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004379 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004380
Bob Wilsonc692cb72009-08-21 20:54:19 +00004381 // If the shuffle is not directly supported and it has 4 elements, use
4382 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004383 unsigned NumElts = VT.getVectorNumElements();
4384 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004385 unsigned PFIndexes[4];
4386 for (unsigned i = 0; i != 4; ++i) {
4387 if (ShuffleMask[i] < 0)
4388 PFIndexes[i] = 8;
4389 else
4390 PFIndexes[i] = ShuffleMask[i];
4391 }
4392
4393 // Compute the index in the perfect shuffle table.
4394 unsigned PFTableIndex =
4395 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004396 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4397 unsigned Cost = (PFEntry >> 30);
4398
4399 if (Cost <= 4)
4400 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4401 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004402
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004403 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004404 if (EltSize >= 32) {
4405 // Do the expansion with floating-point types, since that is what the VFP
4406 // registers are defined to use, and since i64 is not legal.
4407 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4408 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004409 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4410 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004411 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004412 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004413 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004414 Ops.push_back(DAG.getUNDEF(EltVT));
4415 else
4416 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4417 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4418 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4419 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004420 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004421 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004422 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004423 }
4424
Bill Wendling69a05a72011-03-14 23:02:38 +00004425 if (VT == MVT::v8i8) {
4426 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4427 if (NewOp.getNode())
4428 return NewOp;
4429 }
4430
Bob Wilson22cac0d2009-08-14 05:16:33 +00004431 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004432}
4433
Bob Wilson5bafff32009-06-22 23:27:02 +00004434static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004435 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004436 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004437 if (!isa<ConstantSDNode>(Lane))
4438 return SDValue();
4439
4440 SDValue Vec = Op.getOperand(0);
4441 if (Op.getValueType() == MVT::i32 &&
4442 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4443 DebugLoc dl = Op.getDebugLoc();
4444 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4445 }
4446
4447 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004448}
4449
Bob Wilsona6d65862009-08-03 20:36:38 +00004450static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4451 // The only time a CONCAT_VECTORS operation can have legal types is when
4452 // two 64-bit vectors are concatenated to a 128-bit vector.
4453 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4454 "unexpected CONCAT_VECTORS");
4455 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004456 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004457 SDValue Op0 = Op.getOperand(0);
4458 SDValue Op1 = Op.getOperand(1);
4459 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004460 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004461 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004462 DAG.getIntPtrConstant(0));
4463 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004464 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004465 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004466 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004467 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004468}
4469
Bob Wilson626613d2010-11-23 19:38:38 +00004470/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4471/// element has been zero/sign-extended, depending on the isSigned parameter,
4472/// from an integer type half its size.
4473static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4474 bool isSigned) {
4475 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4476 EVT VT = N->getValueType(0);
4477 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4478 SDNode *BVN = N->getOperand(0).getNode();
4479 if (BVN->getValueType(0) != MVT::v4i32 ||
4480 BVN->getOpcode() != ISD::BUILD_VECTOR)
4481 return false;
4482 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4483 unsigned HiElt = 1 - LoElt;
4484 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4485 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4486 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4487 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4488 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4489 return false;
4490 if (isSigned) {
4491 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4492 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4493 return true;
4494 } else {
4495 if (Hi0->isNullValue() && Hi1->isNullValue())
4496 return true;
4497 }
4498 return false;
4499 }
4500
4501 if (N->getOpcode() != ISD::BUILD_VECTOR)
4502 return false;
4503
4504 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4505 SDNode *Elt = N->getOperand(i).getNode();
4506 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4507 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4508 unsigned HalfSize = EltSize / 2;
4509 if (isSigned) {
4510 int64_t SExtVal = C->getSExtValue();
4511 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4512 return false;
4513 } else {
4514 if ((C->getZExtValue() >> HalfSize) != 0)
4515 return false;
4516 }
4517 continue;
4518 }
4519 return false;
4520 }
4521
4522 return true;
4523}
4524
4525/// isSignExtended - Check if a node is a vector value that is sign-extended
4526/// or a constant BUILD_VECTOR with sign-extended elements.
4527static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4528 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4529 return true;
4530 if (isExtendedBUILD_VECTOR(N, DAG, true))
4531 return true;
4532 return false;
4533}
4534
4535/// isZeroExtended - Check if a node is a vector value that is zero-extended
4536/// or a constant BUILD_VECTOR with zero-extended elements.
4537static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4538 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4539 return true;
4540 if (isExtendedBUILD_VECTOR(N, DAG, false))
4541 return true;
4542 return false;
4543}
4544
4545/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4546/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004547static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4548 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4549 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004550 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4551 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4552 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4553 LD->isNonTemporal(), LD->getAlignment());
4554 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4555 // have been legalized as a BITCAST from v4i32.
4556 if (N->getOpcode() == ISD::BITCAST) {
4557 SDNode *BVN = N->getOperand(0).getNode();
4558 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4559 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4560 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4561 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4562 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4563 }
4564 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4565 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4566 EVT VT = N->getValueType(0);
4567 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4568 unsigned NumElts = VT.getVectorNumElements();
4569 MVT TruncVT = MVT::getIntegerVT(EltSize);
4570 SmallVector<SDValue, 8> Ops;
4571 for (unsigned i = 0; i != NumElts; ++i) {
4572 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4573 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004574 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004575 }
4576 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4577 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004578}
4579
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004580static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4581 unsigned Opcode = N->getOpcode();
4582 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4583 SDNode *N0 = N->getOperand(0).getNode();
4584 SDNode *N1 = N->getOperand(1).getNode();
4585 return N0->hasOneUse() && N1->hasOneUse() &&
4586 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4587 }
4588 return false;
4589}
4590
4591static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4592 unsigned Opcode = N->getOpcode();
4593 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4594 SDNode *N0 = N->getOperand(0).getNode();
4595 SDNode *N1 = N->getOperand(1).getNode();
4596 return N0->hasOneUse() && N1->hasOneUse() &&
4597 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4598 }
4599 return false;
4600}
4601
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004602static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4603 // Multiplications are only custom-lowered for 128-bit vectors so that
4604 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4605 EVT VT = Op.getValueType();
4606 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4607 SDNode *N0 = Op.getOperand(0).getNode();
4608 SDNode *N1 = Op.getOperand(1).getNode();
4609 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004610 bool isMLA = false;
4611 bool isN0SExt = isSignExtended(N0, DAG);
4612 bool isN1SExt = isSignExtended(N1, DAG);
4613 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004614 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004615 else {
4616 bool isN0ZExt = isZeroExtended(N0, DAG);
4617 bool isN1ZExt = isZeroExtended(N1, DAG);
4618 if (isN0ZExt && isN1ZExt)
4619 NewOpc = ARMISD::VMULLu;
4620 else if (isN1SExt || isN1ZExt) {
4621 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4622 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4623 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4624 NewOpc = ARMISD::VMULLs;
4625 isMLA = true;
4626 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4627 NewOpc = ARMISD::VMULLu;
4628 isMLA = true;
4629 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4630 std::swap(N0, N1);
4631 NewOpc = ARMISD::VMULLu;
4632 isMLA = true;
4633 }
4634 }
4635
4636 if (!NewOpc) {
4637 if (VT == MVT::v2i64)
4638 // Fall through to expand this. It is not legal.
4639 return SDValue();
4640 else
4641 // Other vector multiplications are legal.
4642 return Op;
4643 }
4644 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004645
4646 // Legalize to a VMULL instruction.
4647 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004648 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004649 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004650 if (!isMLA) {
4651 Op0 = SkipExtension(N0, DAG);
4652 assert(Op0.getValueType().is64BitVector() &&
4653 Op1.getValueType().is64BitVector() &&
4654 "unexpected types for extended operands to VMULL");
4655 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4656 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004657
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004658 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4659 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4660 // vmull q0, d4, d6
4661 // vmlal q0, d5, d6
4662 // is faster than
4663 // vaddl q0, d4, d5
4664 // vmovl q1, d6
4665 // vmul q0, q0, q1
4666 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4667 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4668 EVT Op1VT = Op1.getValueType();
4669 return DAG.getNode(N0->getOpcode(), DL, VT,
4670 DAG.getNode(NewOpc, DL, VT,
4671 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4672 DAG.getNode(NewOpc, DL, VT,
4673 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004674}
4675
Owen Anderson76706012011-04-05 21:48:57 +00004676static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004677LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4678 // Convert to float
4679 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4680 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4681 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4682 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4683 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4684 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4685 // Get reciprocal estimate.
4686 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004687 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004688 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4689 // Because char has a smaller range than uchar, we can actually get away
4690 // without any newton steps. This requires that we use a weird bias
4691 // of 0xb000, however (again, this has been exhaustively tested).
4692 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4693 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4694 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4695 Y = DAG.getConstant(0xb000, MVT::i32);
4696 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4697 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4698 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4699 // Convert back to short.
4700 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4701 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4702 return X;
4703}
4704
Owen Anderson76706012011-04-05 21:48:57 +00004705static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004706LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4707 SDValue N2;
4708 // Convert to float.
4709 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4710 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4711 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4712 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4713 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4714 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004715
Nate Begeman7973f352011-02-11 20:53:29 +00004716 // Use reciprocal estimate and one refinement step.
4717 // float4 recip = vrecpeq_f32(yf);
4718 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004719 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004720 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004721 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004722 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4723 N1, N2);
4724 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4725 // Because short has a smaller range than ushort, we can actually get away
4726 // with only a single newton step. This requires that we use a weird bias
4727 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004728 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004729 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4730 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004731 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004732 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4733 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4734 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4735 // Convert back to integer and return.
4736 // return vmovn_s32(vcvt_s32_f32(result));
4737 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4738 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4739 return N0;
4740}
4741
4742static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4743 EVT VT = Op.getValueType();
4744 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4745 "unexpected type for custom-lowering ISD::SDIV");
4746
4747 DebugLoc dl = Op.getDebugLoc();
4748 SDValue N0 = Op.getOperand(0);
4749 SDValue N1 = Op.getOperand(1);
4750 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004751
Nate Begeman7973f352011-02-11 20:53:29 +00004752 if (VT == MVT::v8i8) {
4753 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4754 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004755
Nate Begeman7973f352011-02-11 20:53:29 +00004756 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4757 DAG.getIntPtrConstant(4));
4758 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004759 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004760 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4761 DAG.getIntPtrConstant(0));
4762 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4763 DAG.getIntPtrConstant(0));
4764
4765 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4766 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4767
4768 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4769 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004770
Nate Begeman7973f352011-02-11 20:53:29 +00004771 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4772 return N0;
4773 }
4774 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4775}
4776
4777static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4778 EVT VT = Op.getValueType();
4779 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4780 "unexpected type for custom-lowering ISD::UDIV");
4781
4782 DebugLoc dl = Op.getDebugLoc();
4783 SDValue N0 = Op.getOperand(0);
4784 SDValue N1 = Op.getOperand(1);
4785 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004786
Nate Begeman7973f352011-02-11 20:53:29 +00004787 if (VT == MVT::v8i8) {
4788 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4789 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004790
Nate Begeman7973f352011-02-11 20:53:29 +00004791 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4792 DAG.getIntPtrConstant(4));
4793 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004794 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004795 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4796 DAG.getIntPtrConstant(0));
4797 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4798 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004799
Nate Begeman7973f352011-02-11 20:53:29 +00004800 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4801 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004802
Nate Begeman7973f352011-02-11 20:53:29 +00004803 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4804 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004805
4806 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004807 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4808 N0);
4809 return N0;
4810 }
Owen Anderson76706012011-04-05 21:48:57 +00004811
Nate Begeman7973f352011-02-11 20:53:29 +00004812 // v4i16 sdiv ... Convert to float.
4813 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4814 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4815 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4816 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4817 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004818 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004819
4820 // Use reciprocal estimate and two refinement steps.
4821 // float4 recip = vrecpeq_f32(yf);
4822 // recip *= vrecpsq_f32(yf, recip);
4823 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004824 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004825 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004826 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004827 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004828 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004829 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004830 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004831 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004832 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004833 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4834 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4835 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4836 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004837 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004838 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4839 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4840 N1 = DAG.getConstant(2, MVT::i32);
4841 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4842 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4843 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4844 // Convert back to integer and return.
4845 // return vmovn_u32(vcvt_s32_f32(result));
4846 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4847 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4848 return N0;
4849}
4850
Dan Gohmand858e902010-04-17 15:26:15 +00004851SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004852 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004853 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004854 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004855 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004856 case ISD::GlobalAddress:
4857 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4858 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004859 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004860 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004861 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4862 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004863 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004864 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004865 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00004866 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004867 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004868 case ISD::SINT_TO_FP:
4869 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4870 case ISD::FP_TO_SINT:
4871 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004872 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004873 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004874 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004875 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004876 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004877 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004878 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004879 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4880 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004881 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004882 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004883 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004884 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004885 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004886 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004887 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004888 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004889 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004890 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004891 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004892 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004893 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004894 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004895 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004896 case ISD::SDIV: return LowerSDIV(Op, DAG);
4897 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004898 }
Dan Gohman475871a2008-07-27 21:46:04 +00004899 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004900}
4901
Duncan Sands1607f052008-12-01 11:39:25 +00004902/// ReplaceNodeResults - Replace the results of node with an illegal result
4903/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004904void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4905 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004906 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004907 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004908 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004909 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004910 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004911 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004912 case ISD::BITCAST:
4913 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004914 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004915 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004916 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004917 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004918 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004919 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004920 if (Res.getNode())
4921 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004922}
Chris Lattner27a6c732007-11-24 07:07:01 +00004923
Evan Chenga8e29892007-01-19 07:51:42 +00004924//===----------------------------------------------------------------------===//
4925// ARM Scheduler Hooks
4926//===----------------------------------------------------------------------===//
4927
4928MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004929ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4930 MachineBasicBlock *BB,
4931 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004932 unsigned dest = MI->getOperand(0).getReg();
4933 unsigned ptr = MI->getOperand(1).getReg();
4934 unsigned oldval = MI->getOperand(2).getReg();
4935 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004936 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4937 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004938 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004939
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004940 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4941 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004942 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004943 : ARM::GPRRegisterClass);
4944
4945 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004946 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4947 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4948 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004949 }
4950
Jim Grosbach5278eb82009-12-11 01:42:04 +00004951 unsigned ldrOpc, strOpc;
4952 switch (Size) {
4953 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004954 case 1:
4955 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004956 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004957 break;
4958 case 2:
4959 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4960 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4961 break;
4962 case 4:
4963 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4964 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4965 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004966 }
4967
4968 MachineFunction *MF = BB->getParent();
4969 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4970 MachineFunction::iterator It = BB;
4971 ++It; // insert the new blocks after the current block
4972
4973 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4974 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4975 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4976 MF->insert(It, loop1MBB);
4977 MF->insert(It, loop2MBB);
4978 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004979
4980 // Transfer the remainder of BB and its successor edges to exitMBB.
4981 exitMBB->splice(exitMBB->begin(), BB,
4982 llvm::next(MachineBasicBlock::iterator(MI)),
4983 BB->end());
4984 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004985
4986 // thisMBB:
4987 // ...
4988 // fallthrough --> loop1MBB
4989 BB->addSuccessor(loop1MBB);
4990
4991 // loop1MBB:
4992 // ldrex dest, [ptr]
4993 // cmp dest, oldval
4994 // bne exitMBB
4995 BB = loop1MBB;
4996 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004997 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004998 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004999 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5000 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005001 BB->addSuccessor(loop2MBB);
5002 BB->addSuccessor(exitMBB);
5003
5004 // loop2MBB:
5005 // strex scratch, newval, [ptr]
5006 // cmp scratch, #0
5007 // bne loop1MBB
5008 BB = loop2MBB;
5009 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
5010 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005011 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005012 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005013 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5014 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005015 BB->addSuccessor(loop1MBB);
5016 BB->addSuccessor(exitMBB);
5017
5018 // exitMBB:
5019 // ...
5020 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005021
Dan Gohman14152b42010-07-06 20:24:04 +00005022 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005023
Jim Grosbach5278eb82009-12-11 01:42:04 +00005024 return BB;
5025}
5026
5027MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005028ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5029 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005030 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5031 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5032
5033 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005034 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005035 MachineFunction::iterator It = BB;
5036 ++It;
5037
5038 unsigned dest = MI->getOperand(0).getReg();
5039 unsigned ptr = MI->getOperand(1).getReg();
5040 unsigned incr = MI->getOperand(2).getReg();
5041 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005042 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005043
5044 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5045 if (isThumb2) {
5046 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5047 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5048 }
5049
Jim Grosbachc3c23542009-12-14 04:22:04 +00005050 unsigned ldrOpc, strOpc;
5051 switch (Size) {
5052 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005053 case 1:
5054 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005055 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005056 break;
5057 case 2:
5058 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5059 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5060 break;
5061 case 4:
5062 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5063 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5064 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005065 }
5066
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005067 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5068 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5069 MF->insert(It, loopMBB);
5070 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005071
5072 // Transfer the remainder of BB and its successor edges to exitMBB.
5073 exitMBB->splice(exitMBB->begin(), BB,
5074 llvm::next(MachineBasicBlock::iterator(MI)),
5075 BB->end());
5076 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005077
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005078 TargetRegisterClass *TRC =
5079 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5080 unsigned scratch = MRI.createVirtualRegister(TRC);
5081 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005082
5083 // thisMBB:
5084 // ...
5085 // fallthrough --> loopMBB
5086 BB->addSuccessor(loopMBB);
5087
5088 // loopMBB:
5089 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005090 // <binop> scratch2, dest, incr
5091 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005092 // cmp scratch, #0
5093 // bne- loopMBB
5094 // fallthrough --> exitMBB
5095 BB = loopMBB;
5096 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005097 if (BinOpcode) {
5098 // operand order needs to go the other way for NAND
5099 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5100 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5101 addReg(incr).addReg(dest)).addReg(0);
5102 else
5103 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5104 addReg(dest).addReg(incr)).addReg(0);
5105 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005106
5107 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5108 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005109 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005110 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005111 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5112 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005113
5114 BB->addSuccessor(loopMBB);
5115 BB->addSuccessor(exitMBB);
5116
5117 // exitMBB:
5118 // ...
5119 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005120
Dan Gohman14152b42010-07-06 20:24:04 +00005121 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005122
Jim Grosbachc3c23542009-12-14 04:22:04 +00005123 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005124}
5125
Jim Grosbachf7da8822011-04-26 19:44:18 +00005126MachineBasicBlock *
5127ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5128 MachineBasicBlock *BB,
5129 unsigned Size,
5130 bool signExtend,
5131 ARMCC::CondCodes Cond) const {
5132 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5133
5134 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5135 MachineFunction *MF = BB->getParent();
5136 MachineFunction::iterator It = BB;
5137 ++It;
5138
5139 unsigned dest = MI->getOperand(0).getReg();
5140 unsigned ptr = MI->getOperand(1).getReg();
5141 unsigned incr = MI->getOperand(2).getReg();
5142 unsigned oldval = dest;
5143 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005144 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005145
5146 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5147 if (isThumb2) {
5148 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5149 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5150 }
5151
Jim Grosbachf7da8822011-04-26 19:44:18 +00005152 unsigned ldrOpc, strOpc, extendOpc;
5153 switch (Size) {
5154 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5155 case 1:
5156 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5157 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005158 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005159 break;
5160 case 2:
5161 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5162 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005163 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005164 break;
5165 case 4:
5166 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5167 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5168 extendOpc = 0;
5169 break;
5170 }
5171
5172 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5173 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5174 MF->insert(It, loopMBB);
5175 MF->insert(It, exitMBB);
5176
5177 // Transfer the remainder of BB and its successor edges to exitMBB.
5178 exitMBB->splice(exitMBB->begin(), BB,
5179 llvm::next(MachineBasicBlock::iterator(MI)),
5180 BB->end());
5181 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5182
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005183 TargetRegisterClass *TRC =
5184 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5185 unsigned scratch = MRI.createVirtualRegister(TRC);
5186 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005187
5188 // thisMBB:
5189 // ...
5190 // fallthrough --> loopMBB
5191 BB->addSuccessor(loopMBB);
5192
5193 // loopMBB:
5194 // ldrex dest, ptr
5195 // (sign extend dest, if required)
5196 // cmp dest, incr
5197 // cmov.cond scratch2, dest, incr
5198 // strex scratch, scratch2, ptr
5199 // cmp scratch, #0
5200 // bne- loopMBB
5201 // fallthrough --> exitMBB
5202 BB = loopMBB;
5203 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5204
5205 // Sign extend the value, if necessary.
5206 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005207 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005208 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5209 .addReg(dest)
5210 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005211 }
5212
5213 // Build compare and cmov instructions.
5214 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5215 .addReg(oldval).addReg(incr));
5216 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5217 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5218
5219 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5220 .addReg(ptr));
5221 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5222 .addReg(scratch).addImm(0));
5223 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5224 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5225
5226 BB->addSuccessor(loopMBB);
5227 BB->addSuccessor(exitMBB);
5228
5229 // exitMBB:
5230 // ...
5231 BB = exitMBB;
5232
5233 MI->eraseFromParent(); // The instruction is gone now.
5234
5235 return BB;
5236}
5237
Evan Cheng218977b2010-07-13 19:27:42 +00005238static
5239MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5240 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5241 E = MBB->succ_end(); I != E; ++I)
5242 if (*I != Succ)
5243 return *I;
5244 llvm_unreachable("Expecting a BB with two successors!");
5245}
5246
Andrew Trick1c3af772011-04-23 03:55:32 +00005247// FIXME: This opcode table should obviously be expressed in the target
5248// description. We probably just need a "machine opcode" value in the pseudo
5249// instruction. But the ideal solution maybe to simply remove the "S" version
5250// of the opcode altogether.
5251struct AddSubFlagsOpcodePair {
5252 unsigned PseudoOpc;
5253 unsigned MachineOpc;
5254};
5255
5256static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5257 {ARM::ADCSri, ARM::ADCri},
5258 {ARM::ADCSrr, ARM::ADCrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005259 {ARM::ADCSrsi, ARM::ADCrsi},
5260 {ARM::ADCSrsr, ARM::ADCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005261 {ARM::SBCSri, ARM::SBCri},
5262 {ARM::SBCSrr, ARM::SBCrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005263 {ARM::SBCSrsi, ARM::SBCrsi},
5264 {ARM::SBCSrsr, ARM::SBCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005265 {ARM::RSBSri, ARM::RSBri},
5266 {ARM::RSBSrr, ARM::RSBrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005267 {ARM::RSBSrsi, ARM::RSBrsi},
5268 {ARM::RSBSrsr, ARM::RSBrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005269 {ARM::RSCSri, ARM::RSCri},
Owen Anderson92a20222011-07-21 18:54:16 +00005270 {ARM::RSCSrsi, ARM::RSCrsi},
5271 {ARM::RSCSrsr, ARM::RSCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005272 {ARM::t2ADCSri, ARM::t2ADCri},
5273 {ARM::t2ADCSrr, ARM::t2ADCrr},
5274 {ARM::t2ADCSrs, ARM::t2ADCrs},
5275 {ARM::t2SBCSri, ARM::t2SBCri},
5276 {ARM::t2SBCSrr, ARM::t2SBCrr},
5277 {ARM::t2SBCSrs, ARM::t2SBCrs},
5278 {ARM::t2RSBSri, ARM::t2RSBri},
5279 {ARM::t2RSBSrs, ARM::t2RSBrs},
5280};
5281
5282// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5283// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5284//
5285// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5286// position to be recognized by the target descrition as the 'S' bit.
5287bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5288 MachineBasicBlock *BB) const {
5289 unsigned OldOpc = MI->getOpcode();
5290 unsigned NewOpc = 0;
5291
5292 // This is only called for instructions that need remapping, so iterating over
5293 // the tiny opcode table is not costly.
5294 static const int NPairs =
5295 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5296 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5297 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5298 if (OldOpc == Pair->PseudoOpc) {
5299 NewOpc = Pair->MachineOpc;
5300 break;
5301 }
5302 }
5303 if (!NewOpc)
5304 return false;
5305
5306 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5307 DebugLoc dl = MI->getDebugLoc();
5308 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5309 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5310 MIB.addOperand(MI->getOperand(i));
5311 AddDefaultPred(MIB);
5312 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5313 MI->eraseFromParent();
5314 return true;
5315}
5316
Jim Grosbache801dc42009-12-12 01:40:06 +00005317MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005318ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005319 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005320 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005321 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005322 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005323 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005324 default: {
5325 if (RemapAddSubWithFlags(MI, BB))
5326 return BB;
5327
Jim Grosbach5278eb82009-12-11 01:42:04 +00005328 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005329 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005330 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005331 case ARM::ATOMIC_LOAD_ADD_I8:
5332 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5333 case ARM::ATOMIC_LOAD_ADD_I16:
5334 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5335 case ARM::ATOMIC_LOAD_ADD_I32:
5336 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005337
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005338 case ARM::ATOMIC_LOAD_AND_I8:
5339 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5340 case ARM::ATOMIC_LOAD_AND_I16:
5341 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5342 case ARM::ATOMIC_LOAD_AND_I32:
5343 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005344
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005345 case ARM::ATOMIC_LOAD_OR_I8:
5346 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5347 case ARM::ATOMIC_LOAD_OR_I16:
5348 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5349 case ARM::ATOMIC_LOAD_OR_I32:
5350 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005351
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005352 case ARM::ATOMIC_LOAD_XOR_I8:
5353 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5354 case ARM::ATOMIC_LOAD_XOR_I16:
5355 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5356 case ARM::ATOMIC_LOAD_XOR_I32:
5357 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005358
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005359 case ARM::ATOMIC_LOAD_NAND_I8:
5360 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5361 case ARM::ATOMIC_LOAD_NAND_I16:
5362 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5363 case ARM::ATOMIC_LOAD_NAND_I32:
5364 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005365
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005366 case ARM::ATOMIC_LOAD_SUB_I8:
5367 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5368 case ARM::ATOMIC_LOAD_SUB_I16:
5369 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5370 case ARM::ATOMIC_LOAD_SUB_I32:
5371 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005372
Jim Grosbachf7da8822011-04-26 19:44:18 +00005373 case ARM::ATOMIC_LOAD_MIN_I8:
5374 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5375 case ARM::ATOMIC_LOAD_MIN_I16:
5376 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5377 case ARM::ATOMIC_LOAD_MIN_I32:
5378 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5379
5380 case ARM::ATOMIC_LOAD_MAX_I8:
5381 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5382 case ARM::ATOMIC_LOAD_MAX_I16:
5383 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5384 case ARM::ATOMIC_LOAD_MAX_I32:
5385 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5386
5387 case ARM::ATOMIC_LOAD_UMIN_I8:
5388 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5389 case ARM::ATOMIC_LOAD_UMIN_I16:
5390 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5391 case ARM::ATOMIC_LOAD_UMIN_I32:
5392 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5393
5394 case ARM::ATOMIC_LOAD_UMAX_I8:
5395 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5396 case ARM::ATOMIC_LOAD_UMAX_I16:
5397 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5398 case ARM::ATOMIC_LOAD_UMAX_I32:
5399 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5400
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005401 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5402 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5403 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005404
5405 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5406 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5407 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005408
Evan Cheng007ea272009-08-12 05:17:19 +00005409 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005410 // To "insert" a SELECT_CC instruction, we actually have to insert the
5411 // diamond control-flow pattern. The incoming instruction knows the
5412 // destination vreg to set, the condition code register to branch on, the
5413 // true/false values to select between, and a branch opcode to use.
5414 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005415 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005416 ++It;
5417
5418 // thisMBB:
5419 // ...
5420 // TrueVal = ...
5421 // cmpTY ccX, r1, r2
5422 // bCC copy1MBB
5423 // fallthrough --> copy0MBB
5424 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005425 MachineFunction *F = BB->getParent();
5426 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5427 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005428 F->insert(It, copy0MBB);
5429 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005430
5431 // Transfer the remainder of BB and its successor edges to sinkMBB.
5432 sinkMBB->splice(sinkMBB->begin(), BB,
5433 llvm::next(MachineBasicBlock::iterator(MI)),
5434 BB->end());
5435 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5436
Dan Gohman258c58c2010-07-06 15:49:48 +00005437 BB->addSuccessor(copy0MBB);
5438 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005439
Dan Gohman14152b42010-07-06 20:24:04 +00005440 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5441 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5442
Evan Chenga8e29892007-01-19 07:51:42 +00005443 // copy0MBB:
5444 // %FalseValue = ...
5445 // # fallthrough to sinkMBB
5446 BB = copy0MBB;
5447
5448 // Update machine-CFG edges
5449 BB->addSuccessor(sinkMBB);
5450
5451 // sinkMBB:
5452 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5453 // ...
5454 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005455 BuildMI(*BB, BB->begin(), dl,
5456 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005457 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5458 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5459
Dan Gohman14152b42010-07-06 20:24:04 +00005460 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005461 return BB;
5462 }
Evan Cheng86198642009-08-07 00:34:42 +00005463
Evan Cheng218977b2010-07-13 19:27:42 +00005464 case ARM::BCCi64:
5465 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005466 // If there is an unconditional branch to the other successor, remove it.
5467 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005468
Evan Cheng218977b2010-07-13 19:27:42 +00005469 // Compare both parts that make up the double comparison separately for
5470 // equality.
5471 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5472
5473 unsigned LHS1 = MI->getOperand(1).getReg();
5474 unsigned LHS2 = MI->getOperand(2).getReg();
5475 if (RHSisZero) {
5476 AddDefaultPred(BuildMI(BB, dl,
5477 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5478 .addReg(LHS1).addImm(0));
5479 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5480 .addReg(LHS2).addImm(0)
5481 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5482 } else {
5483 unsigned RHS1 = MI->getOperand(3).getReg();
5484 unsigned RHS2 = MI->getOperand(4).getReg();
5485 AddDefaultPred(BuildMI(BB, dl,
5486 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5487 .addReg(LHS1).addReg(RHS1));
5488 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5489 .addReg(LHS2).addReg(RHS2)
5490 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5491 }
5492
5493 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5494 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5495 if (MI->getOperand(0).getImm() == ARMCC::NE)
5496 std::swap(destMBB, exitMBB);
5497
5498 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5499 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5500 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5501 .addMBB(exitMBB);
5502
5503 MI->eraseFromParent(); // The pseudo instruction is gone now.
5504 return BB;
5505 }
Evan Chenga8e29892007-01-19 07:51:42 +00005506 }
5507}
5508
5509//===----------------------------------------------------------------------===//
5510// ARM Optimization Hooks
5511//===----------------------------------------------------------------------===//
5512
Chris Lattnerd1980a52009-03-12 06:52:53 +00005513static
5514SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5515 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005516 SelectionDAG &DAG = DCI.DAG;
5517 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005518 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005519 unsigned Opc = N->getOpcode();
5520 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5521 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5522 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5523 ISD::CondCode CC = ISD::SETCC_INVALID;
5524
5525 if (isSlctCC) {
5526 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5527 } else {
5528 SDValue CCOp = Slct.getOperand(0);
5529 if (CCOp.getOpcode() == ISD::SETCC)
5530 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5531 }
5532
5533 bool DoXform = false;
5534 bool InvCC = false;
5535 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5536 "Bad input!");
5537
5538 if (LHS.getOpcode() == ISD::Constant &&
5539 cast<ConstantSDNode>(LHS)->isNullValue()) {
5540 DoXform = true;
5541 } else if (CC != ISD::SETCC_INVALID &&
5542 RHS.getOpcode() == ISD::Constant &&
5543 cast<ConstantSDNode>(RHS)->isNullValue()) {
5544 std::swap(LHS, RHS);
5545 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005546 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005547 Op0.getOperand(0).getValueType();
5548 bool isInt = OpVT.isInteger();
5549 CC = ISD::getSetCCInverse(CC, isInt);
5550
5551 if (!TLI.isCondCodeLegal(CC, OpVT))
5552 return SDValue(); // Inverse operator isn't legal.
5553
5554 DoXform = true;
5555 InvCC = true;
5556 }
5557
5558 if (DoXform) {
5559 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5560 if (isSlctCC)
5561 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5562 Slct.getOperand(0), Slct.getOperand(1), CC);
5563 SDValue CCOp = Slct.getOperand(0);
5564 if (InvCC)
5565 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5566 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5567 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5568 CCOp, OtherOp, Result);
5569 }
5570 return SDValue();
5571}
5572
Eric Christopherfa6f5912011-06-29 21:10:36 +00005573// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00005574// (only after legalization).
5575static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5576 TargetLowering::DAGCombinerInfo &DCI,
5577 const ARMSubtarget *Subtarget) {
5578
5579 // Only perform optimization if after legalize, and if NEON is available. We
5580 // also expected both operands to be BUILD_VECTORs.
5581 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5582 || N0.getOpcode() != ISD::BUILD_VECTOR
5583 || N1.getOpcode() != ISD::BUILD_VECTOR)
5584 return SDValue();
5585
5586 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5587 EVT VT = N->getValueType(0);
5588 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5589 return SDValue();
5590
5591 // Check that the vector operands are of the right form.
5592 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5593 // operands, where N is the size of the formed vector.
5594 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5595 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005596
5597 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005598 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005599 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005600 SDValue Vec = N0->getOperand(0)->getOperand(0);
5601 SDNode *V = Vec.getNode();
5602 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005603
Eric Christopherfa6f5912011-06-29 21:10:36 +00005604 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00005605 // check to see if each of their operands are an EXTRACT_VECTOR with
5606 // the same vector and appropriate index.
5607 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5608 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5609 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00005610
Tanya Lattner189531f2011-06-14 23:48:48 +00005611 SDValue ExtVec0 = N0->getOperand(i);
5612 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005613
Tanya Lattner189531f2011-06-14 23:48:48 +00005614 // First operand is the vector, verify its the same.
5615 if (V != ExtVec0->getOperand(0).getNode() ||
5616 V != ExtVec1->getOperand(0).getNode())
5617 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00005618
Tanya Lattner189531f2011-06-14 23:48:48 +00005619 // Second is the constant, verify its correct.
5620 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5621 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00005622
Tanya Lattner189531f2011-06-14 23:48:48 +00005623 // For the constant, we want to see all the even or all the odd.
5624 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5625 || C1->getZExtValue() != nextIndex+1)
5626 return SDValue();
5627
5628 // Increment index.
5629 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005630 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00005631 return SDValue();
5632 }
5633
5634 // Create VPADDL node.
5635 SelectionDAG &DAG = DCI.DAG;
5636 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00005637
5638 // Build operand list.
5639 SmallVector<SDValue, 8> Ops;
5640 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5641 TLI.getPointerTy()));
5642
5643 // Input is the vector.
5644 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005645
Tanya Lattner189531f2011-06-14 23:48:48 +00005646 // Get widened type and narrowed type.
5647 MVT widenType;
5648 unsigned numElem = VT.getVectorNumElements();
5649 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5650 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5651 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5652 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5653 default:
5654 assert(0 && "Invalid vector element type for padd optimization.");
5655 }
5656
5657 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5658 widenType, &Ops[0], Ops.size());
5659 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5660}
5661
Bob Wilson3d5792a2010-07-29 20:34:14 +00005662/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5663/// operands N0 and N1. This is a helper for PerformADDCombine that is
5664/// called with the default operands, and if that fails, with commuted
5665/// operands.
5666static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005667 TargetLowering::DAGCombinerInfo &DCI,
5668 const ARMSubtarget *Subtarget){
5669
5670 // Attempt to create vpaddl for this add.
5671 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5672 if (Result.getNode())
5673 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005674
Chris Lattnerd1980a52009-03-12 06:52:53 +00005675 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5676 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5677 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5678 if (Result.getNode()) return Result;
5679 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005680 return SDValue();
5681}
5682
Bob Wilson3d5792a2010-07-29 20:34:14 +00005683/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5684///
5685static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005686 TargetLowering::DAGCombinerInfo &DCI,
5687 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005688 SDValue N0 = N->getOperand(0);
5689 SDValue N1 = N->getOperand(1);
5690
5691 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005692 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005693 if (Result.getNode())
5694 return Result;
5695
5696 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005697 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005698}
5699
Chris Lattnerd1980a52009-03-12 06:52:53 +00005700/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005701///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005702static SDValue PerformSUBCombine(SDNode *N,
5703 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005704 SDValue N0 = N->getOperand(0);
5705 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005706
Chris Lattnerd1980a52009-03-12 06:52:53 +00005707 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5708 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5709 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5710 if (Result.getNode()) return Result;
5711 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005712
Chris Lattnerd1980a52009-03-12 06:52:53 +00005713 return SDValue();
5714}
5715
Evan Cheng463d3582011-03-31 19:38:48 +00005716/// PerformVMULCombine
5717/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5718/// special multiplier accumulator forwarding.
5719/// vmul d3, d0, d2
5720/// vmla d3, d1, d2
5721/// is faster than
5722/// vadd d3, d0, d1
5723/// vmul d3, d3, d2
5724static SDValue PerformVMULCombine(SDNode *N,
5725 TargetLowering::DAGCombinerInfo &DCI,
5726 const ARMSubtarget *Subtarget) {
5727 if (!Subtarget->hasVMLxForwarding())
5728 return SDValue();
5729
5730 SelectionDAG &DAG = DCI.DAG;
5731 SDValue N0 = N->getOperand(0);
5732 SDValue N1 = N->getOperand(1);
5733 unsigned Opcode = N0.getOpcode();
5734 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5735 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00005736 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00005737 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5738 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5739 return SDValue();
5740 std::swap(N0, N1);
5741 }
5742
5743 EVT VT = N->getValueType(0);
5744 DebugLoc DL = N->getDebugLoc();
5745 SDValue N00 = N0->getOperand(0);
5746 SDValue N01 = N0->getOperand(1);
5747 return DAG.getNode(Opcode, DL, VT,
5748 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5749 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5750}
5751
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005752static SDValue PerformMULCombine(SDNode *N,
5753 TargetLowering::DAGCombinerInfo &DCI,
5754 const ARMSubtarget *Subtarget) {
5755 SelectionDAG &DAG = DCI.DAG;
5756
5757 if (Subtarget->isThumb1Only())
5758 return SDValue();
5759
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005760 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5761 return SDValue();
5762
5763 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005764 if (VT.is64BitVector() || VT.is128BitVector())
5765 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005766 if (VT != MVT::i32)
5767 return SDValue();
5768
5769 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5770 if (!C)
5771 return SDValue();
5772
5773 uint64_t MulAmt = C->getZExtValue();
5774 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5775 ShiftAmt = ShiftAmt & (32 - 1);
5776 SDValue V = N->getOperand(0);
5777 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005778
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005779 SDValue Res;
5780 MulAmt >>= ShiftAmt;
5781 if (isPowerOf2_32(MulAmt - 1)) {
5782 // (mul x, 2^N + 1) => (add (shl x, N), x)
5783 Res = DAG.getNode(ISD::ADD, DL, VT,
5784 V, DAG.getNode(ISD::SHL, DL, VT,
5785 V, DAG.getConstant(Log2_32(MulAmt-1),
5786 MVT::i32)));
5787 } else if (isPowerOf2_32(MulAmt + 1)) {
5788 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5789 Res = DAG.getNode(ISD::SUB, DL, VT,
5790 DAG.getNode(ISD::SHL, DL, VT,
5791 V, DAG.getConstant(Log2_32(MulAmt+1),
5792 MVT::i32)),
5793 V);
5794 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005795 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005796
5797 if (ShiftAmt != 0)
5798 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5799 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005800
5801 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005802 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005803 return SDValue();
5804}
5805
Owen Anderson080c0922010-11-05 19:27:46 +00005806static SDValue PerformANDCombine(SDNode *N,
5807 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005808
Owen Anderson080c0922010-11-05 19:27:46 +00005809 // Attempt to use immediate-form VBIC
5810 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5811 DebugLoc dl = N->getDebugLoc();
5812 EVT VT = N->getValueType(0);
5813 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005814
Tanya Lattner0433b212011-04-07 15:24:20 +00005815 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5816 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005817
Owen Anderson080c0922010-11-05 19:27:46 +00005818 APInt SplatBits, SplatUndef;
5819 unsigned SplatBitSize;
5820 bool HasAnyUndefs;
5821 if (BVN &&
5822 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5823 if (SplatBitSize <= 64) {
5824 EVT VbicVT;
5825 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5826 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005827 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005828 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005829 if (Val.getNode()) {
5830 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005831 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005832 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005833 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005834 }
5835 }
5836 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005837
Owen Anderson080c0922010-11-05 19:27:46 +00005838 return SDValue();
5839}
5840
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005841/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5842static SDValue PerformORCombine(SDNode *N,
5843 TargetLowering::DAGCombinerInfo &DCI,
5844 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005845 // Attempt to use immediate-form VORR
5846 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5847 DebugLoc dl = N->getDebugLoc();
5848 EVT VT = N->getValueType(0);
5849 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005850
Tanya Lattner0433b212011-04-07 15:24:20 +00005851 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5852 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005853
Owen Anderson60f48702010-11-03 23:15:26 +00005854 APInt SplatBits, SplatUndef;
5855 unsigned SplatBitSize;
5856 bool HasAnyUndefs;
5857 if (BVN && Subtarget->hasNEON() &&
5858 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5859 if (SplatBitSize <= 64) {
5860 EVT VorrVT;
5861 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5862 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005863 DAG, VorrVT, VT.is128BitVector(),
5864 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005865 if (Val.getNode()) {
5866 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005867 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005868 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005869 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005870 }
5871 }
5872 }
5873
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005874 SDValue N0 = N->getOperand(0);
5875 if (N0.getOpcode() != ISD::AND)
5876 return SDValue();
5877 SDValue N1 = N->getOperand(1);
5878
5879 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5880 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5881 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5882 APInt SplatUndef;
5883 unsigned SplatBitSize;
5884 bool HasAnyUndefs;
5885
5886 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5887 APInt SplatBits0;
5888 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5889 HasAnyUndefs) && !HasAnyUndefs) {
5890 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5891 APInt SplatBits1;
5892 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5893 HasAnyUndefs) && !HasAnyUndefs &&
5894 SplatBits0 == ~SplatBits1) {
5895 // Canonicalize the vector type to make instruction selection simpler.
5896 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5897 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5898 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005899 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005900 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5901 }
5902 }
5903 }
5904
Jim Grosbach54238562010-07-17 03:30:54 +00005905 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5906 // reasonable.
5907
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005908 // BFI is only available on V6T2+
5909 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5910 return SDValue();
5911
Jim Grosbach54238562010-07-17 03:30:54 +00005912 DebugLoc DL = N->getDebugLoc();
5913 // 1) or (and A, mask), val => ARMbfi A, val, mask
5914 // iff (val & mask) == val
5915 //
5916 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5917 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005918 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005919 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005920 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005921 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005922
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005923 if (VT != MVT::i32)
5924 return SDValue();
5925
Evan Cheng30fb13f2010-12-13 20:32:54 +00005926 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005927
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005928 // The value and the mask need to be constants so we can verify this is
5929 // actually a bitfield set. If the mask is 0xffff, we can do better
5930 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005931 SDValue MaskOp = N0.getOperand(1);
5932 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5933 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005934 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005935 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005936 if (Mask == 0xffff)
5937 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005938 SDValue Res;
5939 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005940 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5941 if (N1C) {
5942 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005943 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005944 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005945
Evan Chenga9688c42010-12-11 04:11:38 +00005946 if (ARM::isBitFieldInvertedMask(Mask)) {
5947 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005948
Evan Cheng30fb13f2010-12-13 20:32:54 +00005949 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005950 DAG.getConstant(Val, MVT::i32),
5951 DAG.getConstant(Mask, MVT::i32));
5952
5953 // Do not add new nodes to DAG combiner worklist.
5954 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005955 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005956 }
Jim Grosbach54238562010-07-17 03:30:54 +00005957 } else if (N1.getOpcode() == ISD::AND) {
5958 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005959 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5960 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005961 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005962 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005963
Eric Christopher29aeed12011-03-26 01:21:03 +00005964 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5965 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005966 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005967 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005968 // The pack halfword instruction works better for masks that fit it,
5969 // so use that when it's available.
5970 if (Subtarget->hasT2ExtractPack() &&
5971 (Mask == 0xffff || Mask == 0xffff0000))
5972 return SDValue();
5973 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005974 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005975 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005976 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005977 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005978 DAG.getConstant(Mask, MVT::i32));
5979 // Do not add new nodes to DAG combiner worklist.
5980 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005981 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005982 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005983 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005984 // The pack halfword instruction works better for masks that fit it,
5985 // so use that when it's available.
5986 if (Subtarget->hasT2ExtractPack() &&
5987 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5988 return SDValue();
5989 // 2b
5990 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005991 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005992 DAG.getConstant(lsb, MVT::i32));
5993 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005994 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005995 // Do not add new nodes to DAG combiner worklist.
5996 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005997 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005998 }
5999 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006000
Evan Cheng30fb13f2010-12-13 20:32:54 +00006001 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
6002 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
6003 ARM::isBitFieldInvertedMask(~Mask)) {
6004 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
6005 // where lsb(mask) == #shamt and masked bits of B are known zero.
6006 SDValue ShAmt = N00.getOperand(1);
6007 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
6008 unsigned LSB = CountTrailingZeros_32(Mask);
6009 if (ShAmtC != LSB)
6010 return SDValue();
6011
6012 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
6013 DAG.getConstant(~Mask, MVT::i32));
6014
6015 // Do not add new nodes to DAG combiner worklist.
6016 DCI.CombineTo(N, Res, false);
6017 }
6018
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006019 return SDValue();
6020}
6021
Evan Chengbf188ae2011-06-15 01:12:31 +00006022/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
6023/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00006024static SDValue PerformBFICombine(SDNode *N,
6025 TargetLowering::DAGCombinerInfo &DCI) {
6026 SDValue N1 = N->getOperand(1);
6027 if (N1.getOpcode() == ISD::AND) {
6028 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6029 if (!N11C)
6030 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006031 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
6032 unsigned LSB = CountTrailingZeros_32(~InvMask);
6033 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
6034 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00006035 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006036 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00006037 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
6038 N->getOperand(0), N1.getOperand(0),
6039 N->getOperand(2));
6040 }
6041 return SDValue();
6042}
6043
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006044/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
6045/// ARMISD::VMOVRRD.
6046static SDValue PerformVMOVRRDCombine(SDNode *N,
6047 TargetLowering::DAGCombinerInfo &DCI) {
6048 // vmovrrd(vmovdrr x, y) -> x,y
6049 SDValue InDouble = N->getOperand(0);
6050 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6051 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006052
6053 // vmovrrd(load f64) -> (load i32), (load i32)
6054 SDNode *InNode = InDouble.getNode();
6055 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6056 InNode->getValueType(0) == MVT::f64 &&
6057 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6058 !cast<LoadSDNode>(InNode)->isVolatile()) {
6059 // TODO: Should this be done for non-FrameIndex operands?
6060 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6061
6062 SelectionDAG &DAG = DCI.DAG;
6063 DebugLoc DL = LD->getDebugLoc();
6064 SDValue BasePtr = LD->getBasePtr();
6065 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6066 LD->getPointerInfo(), LD->isVolatile(),
6067 LD->isNonTemporal(), LD->getAlignment());
6068
6069 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6070 DAG.getConstant(4, MVT::i32));
6071 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6072 LD->getPointerInfo(), LD->isVolatile(),
6073 LD->isNonTemporal(),
6074 std::min(4U, LD->getAlignment() / 2));
6075
6076 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6077 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6078 DCI.RemoveFromWorklist(LD);
6079 DAG.DeleteNode(LD);
6080 return Result;
6081 }
6082
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006083 return SDValue();
6084}
6085
6086/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6087/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6088static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6089 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6090 SDValue Op0 = N->getOperand(0);
6091 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006092 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006093 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006094 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006095 Op1 = Op1.getOperand(0);
6096 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6097 Op0.getNode() == Op1.getNode() &&
6098 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006099 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006100 N->getValueType(0), Op0.getOperand(0));
6101 return SDValue();
6102}
6103
Bob Wilson31600902010-12-21 06:43:19 +00006104/// PerformSTORECombine - Target-specific dag combine xforms for
6105/// ISD::STORE.
6106static SDValue PerformSTORECombine(SDNode *N,
6107 TargetLowering::DAGCombinerInfo &DCI) {
6108 // Bitcast an i64 store extracted from a vector to f64.
6109 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6110 StoreSDNode *St = cast<StoreSDNode>(N);
6111 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006112 if (!ISD::isNormalStore(St) || St->isVolatile())
6113 return SDValue();
6114
6115 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6116 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6117 SelectionDAG &DAG = DCI.DAG;
6118 DebugLoc DL = St->getDebugLoc();
6119 SDValue BasePtr = St->getBasePtr();
6120 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6121 StVal.getNode()->getOperand(0), BasePtr,
6122 St->getPointerInfo(), St->isVolatile(),
6123 St->isNonTemporal(), St->getAlignment());
6124
6125 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6126 DAG.getConstant(4, MVT::i32));
6127 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6128 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6129 St->isNonTemporal(),
6130 std::min(4U, St->getAlignment() / 2));
6131 }
6132
6133 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006134 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6135 return SDValue();
6136
6137 SelectionDAG &DAG = DCI.DAG;
6138 DebugLoc dl = StVal.getDebugLoc();
6139 SDValue IntVec = StVal.getOperand(0);
6140 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6141 IntVec.getValueType().getVectorNumElements());
6142 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6143 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6144 Vec, StVal.getOperand(1));
6145 dl = N->getDebugLoc();
6146 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6147 // Make the DAGCombiner fold the bitcasts.
6148 DCI.AddToWorklist(Vec.getNode());
6149 DCI.AddToWorklist(ExtElt.getNode());
6150 DCI.AddToWorklist(V.getNode());
6151 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6152 St->getPointerInfo(), St->isVolatile(),
6153 St->isNonTemporal(), St->getAlignment(),
6154 St->getTBAAInfo());
6155}
6156
6157/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6158/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6159/// i64 vector to have f64 elements, since the value can then be loaded
6160/// directly into a VFP register.
6161static bool hasNormalLoadOperand(SDNode *N) {
6162 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6163 for (unsigned i = 0; i < NumElts; ++i) {
6164 SDNode *Elt = N->getOperand(i).getNode();
6165 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6166 return true;
6167 }
6168 return false;
6169}
6170
Bob Wilson75f02882010-09-17 22:59:05 +00006171/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6172/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006173static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6174 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006175 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6176 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6177 // into a pair of GPRs, which is fine when the value is used as a scalar,
6178 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006179 SelectionDAG &DAG = DCI.DAG;
6180 if (N->getNumOperands() == 2) {
6181 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6182 if (RV.getNode())
6183 return RV;
6184 }
Bob Wilson75f02882010-09-17 22:59:05 +00006185
Bob Wilson31600902010-12-21 06:43:19 +00006186 // Load i64 elements as f64 values so that type legalization does not split
6187 // them up into i32 values.
6188 EVT VT = N->getValueType(0);
6189 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6190 return SDValue();
6191 DebugLoc dl = N->getDebugLoc();
6192 SmallVector<SDValue, 8> Ops;
6193 unsigned NumElts = VT.getVectorNumElements();
6194 for (unsigned i = 0; i < NumElts; ++i) {
6195 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6196 Ops.push_back(V);
6197 // Make the DAGCombiner fold the bitcast.
6198 DCI.AddToWorklist(V.getNode());
6199 }
6200 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6201 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6202 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6203}
6204
6205/// PerformInsertEltCombine - Target-specific dag combine xforms for
6206/// ISD::INSERT_VECTOR_ELT.
6207static SDValue PerformInsertEltCombine(SDNode *N,
6208 TargetLowering::DAGCombinerInfo &DCI) {
6209 // Bitcast an i64 load inserted into a vector to f64.
6210 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6211 EVT VT = N->getValueType(0);
6212 SDNode *Elt = N->getOperand(1).getNode();
6213 if (VT.getVectorElementType() != MVT::i64 ||
6214 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6215 return SDValue();
6216
6217 SelectionDAG &DAG = DCI.DAG;
6218 DebugLoc dl = N->getDebugLoc();
6219 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6220 VT.getVectorNumElements());
6221 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6222 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6223 // Make the DAGCombiner fold the bitcasts.
6224 DCI.AddToWorklist(Vec.getNode());
6225 DCI.AddToWorklist(V.getNode());
6226 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6227 Vec, V, N->getOperand(2));
6228 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006229}
6230
Bob Wilsonf20700c2010-10-27 20:38:28 +00006231/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6232/// ISD::VECTOR_SHUFFLE.
6233static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6234 // The LLVM shufflevector instruction does not require the shuffle mask
6235 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6236 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6237 // operands do not match the mask length, they are extended by concatenating
6238 // them with undef vectors. That is probably the right thing for other
6239 // targets, but for NEON it is better to concatenate two double-register
6240 // size vector operands into a single quad-register size vector. Do that
6241 // transformation here:
6242 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6243 // shuffle(concat(v1, v2), undef)
6244 SDValue Op0 = N->getOperand(0);
6245 SDValue Op1 = N->getOperand(1);
6246 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6247 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6248 Op0.getNumOperands() != 2 ||
6249 Op1.getNumOperands() != 2)
6250 return SDValue();
6251 SDValue Concat0Op1 = Op0.getOperand(1);
6252 SDValue Concat1Op1 = Op1.getOperand(1);
6253 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6254 Concat1Op1.getOpcode() != ISD::UNDEF)
6255 return SDValue();
6256 // Skip the transformation if any of the types are illegal.
6257 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6258 EVT VT = N->getValueType(0);
6259 if (!TLI.isTypeLegal(VT) ||
6260 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6261 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6262 return SDValue();
6263
6264 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6265 Op0.getOperand(0), Op1.getOperand(0));
6266 // Translate the shuffle mask.
6267 SmallVector<int, 16> NewMask;
6268 unsigned NumElts = VT.getVectorNumElements();
6269 unsigned HalfElts = NumElts/2;
6270 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6271 for (unsigned n = 0; n < NumElts; ++n) {
6272 int MaskElt = SVN->getMaskElt(n);
6273 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006274 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006275 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006276 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006277 NewElt = HalfElts + MaskElt - NumElts;
6278 NewMask.push_back(NewElt);
6279 }
6280 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6281 DAG.getUNDEF(VT), NewMask.data());
6282}
6283
Bob Wilson1c3ef902011-02-07 17:43:21 +00006284/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6285/// NEON load/store intrinsics to merge base address updates.
6286static SDValue CombineBaseUpdate(SDNode *N,
6287 TargetLowering::DAGCombinerInfo &DCI) {
6288 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6289 return SDValue();
6290
6291 SelectionDAG &DAG = DCI.DAG;
6292 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6293 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6294 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6295 SDValue Addr = N->getOperand(AddrOpIdx);
6296
6297 // Search for a use of the address operand that is an increment.
6298 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6299 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6300 SDNode *User = *UI;
6301 if (User->getOpcode() != ISD::ADD ||
6302 UI.getUse().getResNo() != Addr.getResNo())
6303 continue;
6304
6305 // Check that the add is independent of the load/store. Otherwise, folding
6306 // it would create a cycle.
6307 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6308 continue;
6309
6310 // Find the new opcode for the updating load/store.
6311 bool isLoad = true;
6312 bool isLaneOp = false;
6313 unsigned NewOpc = 0;
6314 unsigned NumVecs = 0;
6315 if (isIntrinsic) {
6316 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6317 switch (IntNo) {
6318 default: assert(0 && "unexpected intrinsic for Neon base update");
6319 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6320 NumVecs = 1; break;
6321 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6322 NumVecs = 2; break;
6323 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6324 NumVecs = 3; break;
6325 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6326 NumVecs = 4; break;
6327 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6328 NumVecs = 2; isLaneOp = true; break;
6329 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6330 NumVecs = 3; isLaneOp = true; break;
6331 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6332 NumVecs = 4; isLaneOp = true; break;
6333 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6334 NumVecs = 1; isLoad = false; break;
6335 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6336 NumVecs = 2; isLoad = false; break;
6337 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6338 NumVecs = 3; isLoad = false; break;
6339 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6340 NumVecs = 4; isLoad = false; break;
6341 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6342 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6343 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6344 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6345 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6346 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6347 }
6348 } else {
6349 isLaneOp = true;
6350 switch (N->getOpcode()) {
6351 default: assert(0 && "unexpected opcode for Neon base update");
6352 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6353 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6354 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6355 }
6356 }
6357
6358 // Find the size of memory referenced by the load/store.
6359 EVT VecTy;
6360 if (isLoad)
6361 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006362 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006363 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6364 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6365 if (isLaneOp)
6366 NumBytes /= VecTy.getVectorNumElements();
6367
6368 // If the increment is a constant, it must match the memory ref size.
6369 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6370 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6371 uint64_t IncVal = CInc->getZExtValue();
6372 if (IncVal != NumBytes)
6373 continue;
6374 } else if (NumBytes >= 3 * 16) {
6375 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6376 // separate instructions that make it harder to use a non-constant update.
6377 continue;
6378 }
6379
6380 // Create the new updating load/store node.
6381 EVT Tys[6];
6382 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6383 unsigned n;
6384 for (n = 0; n < NumResultVecs; ++n)
6385 Tys[n] = VecTy;
6386 Tys[n++] = MVT::i32;
6387 Tys[n] = MVT::Other;
6388 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6389 SmallVector<SDValue, 8> Ops;
6390 Ops.push_back(N->getOperand(0)); // incoming chain
6391 Ops.push_back(N->getOperand(AddrOpIdx));
6392 Ops.push_back(Inc);
6393 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6394 Ops.push_back(N->getOperand(i));
6395 }
6396 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6397 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6398 Ops.data(), Ops.size(),
6399 MemInt->getMemoryVT(),
6400 MemInt->getMemOperand());
6401
6402 // Update the uses.
6403 std::vector<SDValue> NewResults;
6404 for (unsigned i = 0; i < NumResultVecs; ++i) {
6405 NewResults.push_back(SDValue(UpdN.getNode(), i));
6406 }
6407 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6408 DCI.CombineTo(N, NewResults);
6409 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6410
6411 break;
Owen Anderson76706012011-04-05 21:48:57 +00006412 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006413 return SDValue();
6414}
6415
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006416/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6417/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6418/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6419/// return true.
6420static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6421 SelectionDAG &DAG = DCI.DAG;
6422 EVT VT = N->getValueType(0);
6423 // vldN-dup instructions only support 64-bit vectors for N > 1.
6424 if (!VT.is64BitVector())
6425 return false;
6426
6427 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6428 SDNode *VLD = N->getOperand(0).getNode();
6429 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6430 return false;
6431 unsigned NumVecs = 0;
6432 unsigned NewOpc = 0;
6433 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6434 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6435 NumVecs = 2;
6436 NewOpc = ARMISD::VLD2DUP;
6437 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6438 NumVecs = 3;
6439 NewOpc = ARMISD::VLD3DUP;
6440 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6441 NumVecs = 4;
6442 NewOpc = ARMISD::VLD4DUP;
6443 } else {
6444 return false;
6445 }
6446
6447 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6448 // numbers match the load.
6449 unsigned VLDLaneNo =
6450 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6451 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6452 UI != UE; ++UI) {
6453 // Ignore uses of the chain result.
6454 if (UI.getUse().getResNo() == NumVecs)
6455 continue;
6456 SDNode *User = *UI;
6457 if (User->getOpcode() != ARMISD::VDUPLANE ||
6458 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6459 return false;
6460 }
6461
6462 // Create the vldN-dup node.
6463 EVT Tys[5];
6464 unsigned n;
6465 for (n = 0; n < NumVecs; ++n)
6466 Tys[n] = VT;
6467 Tys[n] = MVT::Other;
6468 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6469 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6470 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6471 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6472 Ops, 2, VLDMemInt->getMemoryVT(),
6473 VLDMemInt->getMemOperand());
6474
6475 // Update the uses.
6476 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6477 UI != UE; ++UI) {
6478 unsigned ResNo = UI.getUse().getResNo();
6479 // Ignore uses of the chain result.
6480 if (ResNo == NumVecs)
6481 continue;
6482 SDNode *User = *UI;
6483 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6484 }
6485
6486 // Now the vldN-lane intrinsic is dead except for its chain result.
6487 // Update uses of the chain.
6488 std::vector<SDValue> VLDDupResults;
6489 for (unsigned n = 0; n < NumVecs; ++n)
6490 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6491 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6492 DCI.CombineTo(VLD, VLDDupResults);
6493
6494 return true;
6495}
6496
Bob Wilson9e82bf12010-07-14 01:22:12 +00006497/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6498/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006499static SDValue PerformVDUPLANECombine(SDNode *N,
6500 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006501 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006502
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006503 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6504 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6505 if (CombineVLDDUP(N, DCI))
6506 return SDValue(N, 0);
6507
6508 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6509 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006510 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006511 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006512 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006513 return SDValue();
6514
6515 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6516 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6517 // The canonical VMOV for a zero vector uses a 32-bit element size.
6518 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6519 unsigned EltBits;
6520 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6521 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006522 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006523 if (EltSize > VT.getVectorElementType().getSizeInBits())
6524 return SDValue();
6525
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006526 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006527}
6528
Eric Christopherfa6f5912011-06-29 21:10:36 +00006529// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00006530// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
6531static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
6532{
Chad Rosier118c9a02011-06-28 17:26:57 +00006533 integerPart cN;
6534 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00006535 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
6536 I != E; I++) {
6537 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
6538 if (!C)
6539 return false;
6540
Eric Christopherfa6f5912011-06-29 21:10:36 +00006541 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00006542 APFloat APF = C->getValueAPF();
6543 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
6544 != APFloat::opOK || !isExact)
6545 return false;
6546
6547 c0 = (I == 0) ? cN : c0;
6548 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
6549 return false;
6550 }
6551 C = c0;
6552 return true;
6553}
6554
6555/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
6556/// can replace combinations of VMUL and VCVT (floating-point to integer)
6557/// when the VMUL has a constant operand that is a power of 2.
6558///
6559/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6560/// vmul.f32 d16, d17, d16
6561/// vcvt.s32.f32 d16, d16
6562/// becomes:
6563/// vcvt.s32.f32 d16, d16, #3
6564static SDValue PerformVCVTCombine(SDNode *N,
6565 TargetLowering::DAGCombinerInfo &DCI,
6566 const ARMSubtarget *Subtarget) {
6567 SelectionDAG &DAG = DCI.DAG;
6568 SDValue Op = N->getOperand(0);
6569
6570 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
6571 Op.getOpcode() != ISD::FMUL)
6572 return SDValue();
6573
6574 uint64_t C;
6575 SDValue N0 = Op->getOperand(0);
6576 SDValue ConstVec = Op->getOperand(1);
6577 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
6578
Eric Christopherfa6f5912011-06-29 21:10:36 +00006579 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00006580 !isConstVecPow2(ConstVec, isSigned, C))
6581 return SDValue();
6582
6583 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
6584 Intrinsic::arm_neon_vcvtfp2fxu;
6585 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6586 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006587 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00006588 DAG.getConstant(Log2_64(C), MVT::i32));
6589}
6590
6591/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
6592/// can replace combinations of VCVT (integer to floating-point) and VDIV
6593/// when the VDIV has a constant operand that is a power of 2.
6594///
6595/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6596/// vcvt.f32.s32 d16, d16
6597/// vdiv.f32 d16, d17, d16
6598/// becomes:
6599/// vcvt.f32.s32 d16, d16, #3
6600static SDValue PerformVDIVCombine(SDNode *N,
6601 TargetLowering::DAGCombinerInfo &DCI,
6602 const ARMSubtarget *Subtarget) {
6603 SelectionDAG &DAG = DCI.DAG;
6604 SDValue Op = N->getOperand(0);
6605 unsigned OpOpcode = Op.getNode()->getOpcode();
6606
6607 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
6608 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
6609 return SDValue();
6610
6611 uint64_t C;
6612 SDValue ConstVec = N->getOperand(1);
6613 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
6614
6615 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
6616 !isConstVecPow2(ConstVec, isSigned, C))
6617 return SDValue();
6618
Eric Christopherfa6f5912011-06-29 21:10:36 +00006619 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00006620 Intrinsic::arm_neon_vcvtfxu2fp;
6621 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6622 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006623 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00006624 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
6625}
6626
6627/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00006628/// operand of a vector shift operation, where all the elements of the
6629/// build_vector must have the same constant integer value.
6630static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6631 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006632 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006633 Op = Op.getOperand(0);
6634 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6635 APInt SplatBits, SplatUndef;
6636 unsigned SplatBitSize;
6637 bool HasAnyUndefs;
6638 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6639 HasAnyUndefs, ElementBits) ||
6640 SplatBitSize > ElementBits)
6641 return false;
6642 Cnt = SplatBits.getSExtValue();
6643 return true;
6644}
6645
6646/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6647/// operand of a vector shift left operation. That value must be in the range:
6648/// 0 <= Value < ElementBits for a left shift; or
6649/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006650static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006651 assert(VT.isVector() && "vector shift count is not a vector type");
6652 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6653 if (! getVShiftImm(Op, ElementBits, Cnt))
6654 return false;
6655 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6656}
6657
6658/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6659/// operand of a vector shift right operation. For a shift opcode, the value
6660/// is positive, but for an intrinsic the value count must be negative. The
6661/// absolute value must be in the range:
6662/// 1 <= |Value| <= ElementBits for a right shift; or
6663/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006664static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006665 int64_t &Cnt) {
6666 assert(VT.isVector() && "vector shift count is not a vector type");
6667 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6668 if (! getVShiftImm(Op, ElementBits, Cnt))
6669 return false;
6670 if (isIntrinsic)
6671 Cnt = -Cnt;
6672 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6673}
6674
6675/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6676static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6677 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6678 switch (IntNo) {
6679 default:
6680 // Don't do anything for most intrinsics.
6681 break;
6682
6683 // Vector shifts: check for immediate versions and lower them.
6684 // Note: This is done during DAG combining instead of DAG legalizing because
6685 // the build_vectors for 64-bit vector element shift counts are generally
6686 // not legal, and it is hard to see their values after they get legalized to
6687 // loads from a constant pool.
6688 case Intrinsic::arm_neon_vshifts:
6689 case Intrinsic::arm_neon_vshiftu:
6690 case Intrinsic::arm_neon_vshiftls:
6691 case Intrinsic::arm_neon_vshiftlu:
6692 case Intrinsic::arm_neon_vshiftn:
6693 case Intrinsic::arm_neon_vrshifts:
6694 case Intrinsic::arm_neon_vrshiftu:
6695 case Intrinsic::arm_neon_vrshiftn:
6696 case Intrinsic::arm_neon_vqshifts:
6697 case Intrinsic::arm_neon_vqshiftu:
6698 case Intrinsic::arm_neon_vqshiftsu:
6699 case Intrinsic::arm_neon_vqshiftns:
6700 case Intrinsic::arm_neon_vqshiftnu:
6701 case Intrinsic::arm_neon_vqshiftnsu:
6702 case Intrinsic::arm_neon_vqrshiftns:
6703 case Intrinsic::arm_neon_vqrshiftnu:
6704 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006705 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006706 int64_t Cnt;
6707 unsigned VShiftOpc = 0;
6708
6709 switch (IntNo) {
6710 case Intrinsic::arm_neon_vshifts:
6711 case Intrinsic::arm_neon_vshiftu:
6712 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6713 VShiftOpc = ARMISD::VSHL;
6714 break;
6715 }
6716 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6717 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6718 ARMISD::VSHRs : ARMISD::VSHRu);
6719 break;
6720 }
6721 return SDValue();
6722
6723 case Intrinsic::arm_neon_vshiftls:
6724 case Intrinsic::arm_neon_vshiftlu:
6725 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6726 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006727 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006728
6729 case Intrinsic::arm_neon_vrshifts:
6730 case Intrinsic::arm_neon_vrshiftu:
6731 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6732 break;
6733 return SDValue();
6734
6735 case Intrinsic::arm_neon_vqshifts:
6736 case Intrinsic::arm_neon_vqshiftu:
6737 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6738 break;
6739 return SDValue();
6740
6741 case Intrinsic::arm_neon_vqshiftsu:
6742 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6743 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006744 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006745
6746 case Intrinsic::arm_neon_vshiftn:
6747 case Intrinsic::arm_neon_vrshiftn:
6748 case Intrinsic::arm_neon_vqshiftns:
6749 case Intrinsic::arm_neon_vqshiftnu:
6750 case Intrinsic::arm_neon_vqshiftnsu:
6751 case Intrinsic::arm_neon_vqrshiftns:
6752 case Intrinsic::arm_neon_vqrshiftnu:
6753 case Intrinsic::arm_neon_vqrshiftnsu:
6754 // Narrowing shifts require an immediate right shift.
6755 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6756 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006757 llvm_unreachable("invalid shift count for narrowing vector shift "
6758 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006759
6760 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006761 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006762 }
6763
6764 switch (IntNo) {
6765 case Intrinsic::arm_neon_vshifts:
6766 case Intrinsic::arm_neon_vshiftu:
6767 // Opcode already set above.
6768 break;
6769 case Intrinsic::arm_neon_vshiftls:
6770 case Intrinsic::arm_neon_vshiftlu:
6771 if (Cnt == VT.getVectorElementType().getSizeInBits())
6772 VShiftOpc = ARMISD::VSHLLi;
6773 else
6774 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6775 ARMISD::VSHLLs : ARMISD::VSHLLu);
6776 break;
6777 case Intrinsic::arm_neon_vshiftn:
6778 VShiftOpc = ARMISD::VSHRN; break;
6779 case Intrinsic::arm_neon_vrshifts:
6780 VShiftOpc = ARMISD::VRSHRs; break;
6781 case Intrinsic::arm_neon_vrshiftu:
6782 VShiftOpc = ARMISD::VRSHRu; break;
6783 case Intrinsic::arm_neon_vrshiftn:
6784 VShiftOpc = ARMISD::VRSHRN; break;
6785 case Intrinsic::arm_neon_vqshifts:
6786 VShiftOpc = ARMISD::VQSHLs; break;
6787 case Intrinsic::arm_neon_vqshiftu:
6788 VShiftOpc = ARMISD::VQSHLu; break;
6789 case Intrinsic::arm_neon_vqshiftsu:
6790 VShiftOpc = ARMISD::VQSHLsu; break;
6791 case Intrinsic::arm_neon_vqshiftns:
6792 VShiftOpc = ARMISD::VQSHRNs; break;
6793 case Intrinsic::arm_neon_vqshiftnu:
6794 VShiftOpc = ARMISD::VQSHRNu; break;
6795 case Intrinsic::arm_neon_vqshiftnsu:
6796 VShiftOpc = ARMISD::VQSHRNsu; break;
6797 case Intrinsic::arm_neon_vqrshiftns:
6798 VShiftOpc = ARMISD::VQRSHRNs; break;
6799 case Intrinsic::arm_neon_vqrshiftnu:
6800 VShiftOpc = ARMISD::VQRSHRNu; break;
6801 case Intrinsic::arm_neon_vqrshiftnsu:
6802 VShiftOpc = ARMISD::VQRSHRNsu; break;
6803 }
6804
6805 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006806 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006807 }
6808
6809 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006810 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006811 int64_t Cnt;
6812 unsigned VShiftOpc = 0;
6813
6814 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6815 VShiftOpc = ARMISD::VSLI;
6816 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6817 VShiftOpc = ARMISD::VSRI;
6818 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006819 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006820 }
6821
6822 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6823 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006824 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006825 }
6826
6827 case Intrinsic::arm_neon_vqrshifts:
6828 case Intrinsic::arm_neon_vqrshiftu:
6829 // No immediate versions of these to check for.
6830 break;
6831 }
6832
6833 return SDValue();
6834}
6835
6836/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6837/// lowers them. As with the vector shift intrinsics, this is done during DAG
6838/// combining instead of DAG legalizing because the build_vectors for 64-bit
6839/// vector element shift counts are generally not legal, and it is hard to see
6840/// their values after they get legalized to loads from a constant pool.
6841static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6842 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006843 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006844
6845 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006846 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6847 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006848 return SDValue();
6849
6850 assert(ST->hasNEON() && "unexpected vector shift");
6851 int64_t Cnt;
6852
6853 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006854 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006855
6856 case ISD::SHL:
6857 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6858 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006859 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006860 break;
6861
6862 case ISD::SRA:
6863 case ISD::SRL:
6864 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6865 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6866 ARMISD::VSHRs : ARMISD::VSHRu);
6867 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006868 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006869 }
6870 }
6871 return SDValue();
6872}
6873
6874/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6875/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6876static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6877 const ARMSubtarget *ST) {
6878 SDValue N0 = N->getOperand(0);
6879
6880 // Check for sign- and zero-extensions of vector extract operations of 8-
6881 // and 16-bit vector elements. NEON supports these directly. They are
6882 // handled during DAG combining because type legalization will promote them
6883 // to 32-bit types and it is messy to recognize the operations after that.
6884 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6885 SDValue Vec = N0.getOperand(0);
6886 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006887 EVT VT = N->getValueType(0);
6888 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006889 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6890
Owen Anderson825b72b2009-08-11 20:47:22 +00006891 if (VT == MVT::i32 &&
6892 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006893 TLI.isTypeLegal(Vec.getValueType()) &&
6894 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006895
6896 unsigned Opc = 0;
6897 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006898 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006899 case ISD::SIGN_EXTEND:
6900 Opc = ARMISD::VGETLANEs;
6901 break;
6902 case ISD::ZERO_EXTEND:
6903 case ISD::ANY_EXTEND:
6904 Opc = ARMISD::VGETLANEu;
6905 break;
6906 }
6907 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6908 }
6909 }
6910
6911 return SDValue();
6912}
6913
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006914/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6915/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6916static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6917 const ARMSubtarget *ST) {
6918 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006919 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006920 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6921 // a NaN; only do the transformation when it matches that behavior.
6922
6923 // For now only do this when using NEON for FP operations; if using VFP, it
6924 // is not obvious that the benefit outweighs the cost of switching to the
6925 // NEON pipeline.
6926 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6927 N->getValueType(0) != MVT::f32)
6928 return SDValue();
6929
6930 SDValue CondLHS = N->getOperand(0);
6931 SDValue CondRHS = N->getOperand(1);
6932 SDValue LHS = N->getOperand(2);
6933 SDValue RHS = N->getOperand(3);
6934 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6935
6936 unsigned Opcode = 0;
6937 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006938 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006939 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006940 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006941 IsReversed = true ; // x CC y ? y : x
6942 } else {
6943 return SDValue();
6944 }
6945
Bob Wilsone742bb52010-02-24 22:15:53 +00006946 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006947 switch (CC) {
6948 default: break;
6949 case ISD::SETOLT:
6950 case ISD::SETOLE:
6951 case ISD::SETLT:
6952 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006953 case ISD::SETULT:
6954 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006955 // If LHS is NaN, an ordered comparison will be false and the result will
6956 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6957 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6958 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6959 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6960 break;
6961 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6962 // will return -0, so vmin can only be used for unsafe math or if one of
6963 // the operands is known to be nonzero.
6964 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6965 !UnsafeFPMath &&
6966 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6967 break;
6968 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006969 break;
6970
6971 case ISD::SETOGT:
6972 case ISD::SETOGE:
6973 case ISD::SETGT:
6974 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006975 case ISD::SETUGT:
6976 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006977 // If LHS is NaN, an ordered comparison will be false and the result will
6978 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6979 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6980 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6981 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6982 break;
6983 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6984 // will return +0, so vmax can only be used for unsafe math or if one of
6985 // the operands is known to be nonzero.
6986 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6987 !UnsafeFPMath &&
6988 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6989 break;
6990 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006991 break;
6992 }
6993
6994 if (!Opcode)
6995 return SDValue();
6996 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6997}
6998
Evan Chenge721f5c2011-07-13 00:42:17 +00006999/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
7000SDValue
7001ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
7002 SDValue Cmp = N->getOperand(4);
7003 if (Cmp.getOpcode() != ARMISD::CMPZ)
7004 // Only looking at EQ and NE cases.
7005 return SDValue();
7006
7007 EVT VT = N->getValueType(0);
7008 DebugLoc dl = N->getDebugLoc();
7009 SDValue LHS = Cmp.getOperand(0);
7010 SDValue RHS = Cmp.getOperand(1);
7011 SDValue FalseVal = N->getOperand(0);
7012 SDValue TrueVal = N->getOperand(1);
7013 SDValue ARMcc = N->getOperand(2);
7014 ARMCC::CondCodes CC = (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
7015
7016 // Simplify
7017 // mov r1, r0
7018 // cmp r1, x
7019 // mov r0, y
7020 // moveq r0, x
7021 // to
7022 // cmp r0, x
7023 // movne r0, y
7024 //
7025 // mov r1, r0
7026 // cmp r1, x
7027 // mov r0, x
7028 // movne r0, y
7029 // to
7030 // cmp r0, x
7031 // movne r0, y
7032 /// FIXME: Turn this into a target neutral optimization?
7033 SDValue Res;
7034 if (CC == ARMCC::NE && FalseVal == RHS) {
7035 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
7036 N->getOperand(3), Cmp);
7037 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
7038 SDValue ARMcc;
7039 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
7040 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
7041 N->getOperand(3), NewCmp);
7042 }
7043
7044 if (Res.getNode()) {
7045 APInt KnownZero, KnownOne;
7046 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
7047 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
7048 // Capture demanded bits information that would be otherwise lost.
7049 if (KnownZero == 0xfffffffe)
7050 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7051 DAG.getValueType(MVT::i1));
7052 else if (KnownZero == 0xffffff00)
7053 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7054 DAG.getValueType(MVT::i8));
7055 else if (KnownZero == 0xffff0000)
7056 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7057 DAG.getValueType(MVT::i16));
7058 }
7059
7060 return Res;
7061}
7062
Dan Gohman475871a2008-07-27 21:46:04 +00007063SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007064 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007065 switch (N->getOpcode()) {
7066 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00007067 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007068 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007069 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007070 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00007071 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00007072 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00007073 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007074 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00007075 case ISD::STORE: return PerformSTORECombine(N, DCI);
7076 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
7077 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00007078 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007079 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00007080 case ISD::FP_TO_SINT:
7081 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
7082 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007083 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00007084 case ISD::SHL:
7085 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007086 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00007087 case ISD::SIGN_EXTEND:
7088 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007089 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
7090 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00007091 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00007092 case ARMISD::VLD2DUP:
7093 case ARMISD::VLD3DUP:
7094 case ARMISD::VLD4DUP:
7095 return CombineBaseUpdate(N, DCI);
7096 case ISD::INTRINSIC_VOID:
7097 case ISD::INTRINSIC_W_CHAIN:
7098 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7099 case Intrinsic::arm_neon_vld1:
7100 case Intrinsic::arm_neon_vld2:
7101 case Intrinsic::arm_neon_vld3:
7102 case Intrinsic::arm_neon_vld4:
7103 case Intrinsic::arm_neon_vld2lane:
7104 case Intrinsic::arm_neon_vld3lane:
7105 case Intrinsic::arm_neon_vld4lane:
7106 case Intrinsic::arm_neon_vst1:
7107 case Intrinsic::arm_neon_vst2:
7108 case Intrinsic::arm_neon_vst3:
7109 case Intrinsic::arm_neon_vst4:
7110 case Intrinsic::arm_neon_vst2lane:
7111 case Intrinsic::arm_neon_vst3lane:
7112 case Intrinsic::arm_neon_vst4lane:
7113 return CombineBaseUpdate(N, DCI);
7114 default: break;
7115 }
7116 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007117 }
Dan Gohman475871a2008-07-27 21:46:04 +00007118 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007119}
7120
Evan Cheng31959b12011-02-02 01:06:55 +00007121bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
7122 EVT VT) const {
7123 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
7124}
7125
Bill Wendlingaf566342009-08-15 21:21:19 +00007126bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00007127 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00007128 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00007129
7130 switch (VT.getSimpleVT().SimpleTy) {
7131 default:
7132 return false;
7133 case MVT::i8:
7134 case MVT::i16:
7135 case MVT::i32:
7136 return true;
7137 // FIXME: VLD1 etc with standard alignment is legal.
7138 }
7139}
7140
Evan Chenge6c835f2009-08-14 20:09:37 +00007141static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
7142 if (V < 0)
7143 return false;
7144
7145 unsigned Scale = 1;
7146 switch (VT.getSimpleVT().SimpleTy) {
7147 default: return false;
7148 case MVT::i1:
7149 case MVT::i8:
7150 // Scale == 1;
7151 break;
7152 case MVT::i16:
7153 // Scale == 2;
7154 Scale = 2;
7155 break;
7156 case MVT::i32:
7157 // Scale == 4;
7158 Scale = 4;
7159 break;
7160 }
7161
7162 if ((V & (Scale - 1)) != 0)
7163 return false;
7164 V /= Scale;
7165 return V == (V & ((1LL << 5) - 1));
7166}
7167
7168static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
7169 const ARMSubtarget *Subtarget) {
7170 bool isNeg = false;
7171 if (V < 0) {
7172 isNeg = true;
7173 V = - V;
7174 }
7175
7176 switch (VT.getSimpleVT().SimpleTy) {
7177 default: return false;
7178 case MVT::i1:
7179 case MVT::i8:
7180 case MVT::i16:
7181 case MVT::i32:
7182 // + imm12 or - imm8
7183 if (isNeg)
7184 return V == (V & ((1LL << 8) - 1));
7185 return V == (V & ((1LL << 12) - 1));
7186 case MVT::f32:
7187 case MVT::f64:
7188 // Same as ARM mode. FIXME: NEON?
7189 if (!Subtarget->hasVFP2())
7190 return false;
7191 if ((V & 3) != 0)
7192 return false;
7193 V >>= 2;
7194 return V == (V & ((1LL << 8) - 1));
7195 }
7196}
7197
Evan Chengb01fad62007-03-12 23:30:29 +00007198/// isLegalAddressImmediate - Return true if the integer value can be used
7199/// as the offset of the target addressing mode for load / store of the
7200/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00007201static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007202 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00007203 if (V == 0)
7204 return true;
7205
Evan Cheng65011532009-03-09 19:15:00 +00007206 if (!VT.isSimple())
7207 return false;
7208
Evan Chenge6c835f2009-08-14 20:09:37 +00007209 if (Subtarget->isThumb1Only())
7210 return isLegalT1AddressImmediate(V, VT);
7211 else if (Subtarget->isThumb2())
7212 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007213
Evan Chenge6c835f2009-08-14 20:09:37 +00007214 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007215 if (V < 0)
7216 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007217 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007218 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007219 case MVT::i1:
7220 case MVT::i8:
7221 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007222 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007223 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007224 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007225 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007226 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007227 case MVT::f32:
7228 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007229 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007230 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007231 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007232 return false;
7233 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007234 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007235 }
Evan Chenga8e29892007-01-19 07:51:42 +00007236}
7237
Evan Chenge6c835f2009-08-14 20:09:37 +00007238bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7239 EVT VT) const {
7240 int Scale = AM.Scale;
7241 if (Scale < 0)
7242 return false;
7243
7244 switch (VT.getSimpleVT().SimpleTy) {
7245 default: return false;
7246 case MVT::i1:
7247 case MVT::i8:
7248 case MVT::i16:
7249 case MVT::i32:
7250 if (Scale == 1)
7251 return true;
7252 // r + r << imm
7253 Scale = Scale & ~1;
7254 return Scale == 2 || Scale == 4 || Scale == 8;
7255 case MVT::i64:
7256 // r + r
7257 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7258 return true;
7259 return false;
7260 case MVT::isVoid:
7261 // Note, we allow "void" uses (basically, uses that aren't loads or
7262 // stores), because arm allows folding a scale into many arithmetic
7263 // operations. This should be made more precise and revisited later.
7264
7265 // Allow r << imm, but the imm has to be a multiple of two.
7266 if (Scale & 1) return false;
7267 return isPowerOf2_32(Scale);
7268 }
7269}
7270
Chris Lattner37caf8c2007-04-09 23:33:39 +00007271/// isLegalAddressingMode - Return true if the addressing mode represented
7272/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007273bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007274 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007275 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007276 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007277 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007278
Chris Lattner37caf8c2007-04-09 23:33:39 +00007279 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007280 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007281 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007282
Chris Lattner37caf8c2007-04-09 23:33:39 +00007283 switch (AM.Scale) {
7284 case 0: // no scale reg, must be "r+i" or "r", or "i".
7285 break;
7286 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007287 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007288 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007289 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007290 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007291 // ARM doesn't support any R+R*scale+imm addr modes.
7292 if (AM.BaseOffs)
7293 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007294
Bob Wilson2c7dab12009-04-08 17:55:28 +00007295 if (!VT.isSimple())
7296 return false;
7297
Evan Chenge6c835f2009-08-14 20:09:37 +00007298 if (Subtarget->isThumb2())
7299 return isLegalT2ScaledAddressingMode(AM, VT);
7300
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007301 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007302 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007303 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007304 case MVT::i1:
7305 case MVT::i8:
7306 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007307 if (Scale < 0) Scale = -Scale;
7308 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007309 return true;
7310 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007311 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007312 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007313 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007314 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007315 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007316 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007317 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007318
Owen Anderson825b72b2009-08-11 20:47:22 +00007319 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007320 // Note, we allow "void" uses (basically, uses that aren't loads or
7321 // stores), because arm allows folding a scale into many arithmetic
7322 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007323
Chris Lattner37caf8c2007-04-09 23:33:39 +00007324 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007325 if (Scale & 1) return false;
7326 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007327 }
7328 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007329 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007330 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007331}
7332
Evan Cheng77e47512009-11-11 19:05:52 +00007333/// isLegalICmpImmediate - Return true if the specified immediate is legal
7334/// icmp immediate, that is the target has icmp instructions which can compare
7335/// a register against the immediate without having to materialize the
7336/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007337bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007338 if (!Subtarget->isThumb())
7339 return ARM_AM::getSOImmVal(Imm) != -1;
7340 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007341 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007342 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007343}
7344
Dan Gohmancca82142011-05-03 00:46:49 +00007345/// isLegalAddImmediate - Return true if the specified immediate is legal
7346/// add immediate, that is the target has add instructions which can add
7347/// a register with the immediate without having to materialize the
7348/// immediate into a register.
7349bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7350 return ARM_AM::getSOImmVal(Imm) != -1;
7351}
7352
Owen Andersone50ed302009-08-10 22:56:29 +00007353static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007354 bool isSEXTLoad, SDValue &Base,
7355 SDValue &Offset, bool &isInc,
7356 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007357 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7358 return false;
7359
Owen Anderson825b72b2009-08-11 20:47:22 +00007360 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007361 // AddressingMode 3
7362 Base = Ptr->getOperand(0);
7363 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007364 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007365 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007366 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007367 isInc = false;
7368 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7369 return true;
7370 }
7371 }
7372 isInc = (Ptr->getOpcode() == ISD::ADD);
7373 Offset = Ptr->getOperand(1);
7374 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007375 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007376 // AddressingMode 2
7377 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007378 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007379 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007380 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007381 isInc = false;
7382 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7383 Base = Ptr->getOperand(0);
7384 return true;
7385 }
7386 }
7387
7388 if (Ptr->getOpcode() == ISD::ADD) {
7389 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00007390 ARM_AM::ShiftOpc ShOpcVal=
7391 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00007392 if (ShOpcVal != ARM_AM::no_shift) {
7393 Base = Ptr->getOperand(1);
7394 Offset = Ptr->getOperand(0);
7395 } else {
7396 Base = Ptr->getOperand(0);
7397 Offset = Ptr->getOperand(1);
7398 }
7399 return true;
7400 }
7401
7402 isInc = (Ptr->getOpcode() == ISD::ADD);
7403 Base = Ptr->getOperand(0);
7404 Offset = Ptr->getOperand(1);
7405 return true;
7406 }
7407
Jim Grosbache5165492009-11-09 00:11:35 +00007408 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007409 return false;
7410}
7411
Owen Andersone50ed302009-08-10 22:56:29 +00007412static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007413 bool isSEXTLoad, SDValue &Base,
7414 SDValue &Offset, bool &isInc,
7415 SelectionDAG &DAG) {
7416 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7417 return false;
7418
7419 Base = Ptr->getOperand(0);
7420 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7421 int RHSC = (int)RHS->getZExtValue();
7422 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7423 assert(Ptr->getOpcode() == ISD::ADD);
7424 isInc = false;
7425 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7426 return true;
7427 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7428 isInc = Ptr->getOpcode() == ISD::ADD;
7429 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7430 return true;
7431 }
7432 }
7433
7434 return false;
7435}
7436
Evan Chenga8e29892007-01-19 07:51:42 +00007437/// getPreIndexedAddressParts - returns true by value, base pointer and
7438/// offset pointer and addressing mode by reference if the node's address
7439/// can be legally represented as pre-indexed load / store address.
7440bool
Dan Gohman475871a2008-07-27 21:46:04 +00007441ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7442 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007443 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007444 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007445 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007446 return false;
7447
Owen Andersone50ed302009-08-10 22:56:29 +00007448 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007449 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007450 bool isSEXTLoad = false;
7451 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7452 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007453 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007454 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7455 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7456 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007457 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007458 } else
7459 return false;
7460
7461 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007462 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007463 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007464 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7465 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007466 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007467 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007468 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007469 if (!isLegal)
7470 return false;
7471
7472 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7473 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007474}
7475
7476/// getPostIndexedAddressParts - returns true by value, base pointer and
7477/// offset pointer and addressing mode by reference if this node can be
7478/// combined with a load / store to form a post-indexed load / store.
7479bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007480 SDValue &Base,
7481 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007482 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007483 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007484 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007485 return false;
7486
Owen Andersone50ed302009-08-10 22:56:29 +00007487 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007488 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007489 bool isSEXTLoad = false;
7490 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007491 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007492 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007493 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7494 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007495 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007496 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007497 } else
7498 return false;
7499
7500 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007501 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007502 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007503 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007504 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007505 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007506 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7507 isInc, DAG);
7508 if (!isLegal)
7509 return false;
7510
Evan Cheng28dad2a2010-05-18 21:31:17 +00007511 if (Ptr != Base) {
7512 // Swap base ptr and offset to catch more post-index load / store when
7513 // it's legal. In Thumb2 mode, offset must be an immediate.
7514 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7515 !Subtarget->isThumb2())
7516 std::swap(Base, Offset);
7517
7518 // Post-indexed load / store update the base pointer.
7519 if (Ptr != Base)
7520 return false;
7521 }
7522
Evan Chenge88d5ce2009-07-02 07:28:31 +00007523 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7524 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007525}
7526
Dan Gohman475871a2008-07-27 21:46:04 +00007527void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007528 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007529 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007530 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007531 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007532 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007533 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007534 switch (Op.getOpcode()) {
7535 default: break;
7536 case ARMISD::CMOV: {
7537 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007538 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007539 if (KnownZero == 0 && KnownOne == 0) return;
7540
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007541 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007542 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7543 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007544 KnownZero &= KnownZeroRHS;
7545 KnownOne &= KnownOneRHS;
7546 return;
7547 }
7548 }
7549}
7550
7551//===----------------------------------------------------------------------===//
7552// ARM Inline Assembly Support
7553//===----------------------------------------------------------------------===//
7554
Evan Cheng55d42002011-01-08 01:24:27 +00007555bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7556 // Looking for "rev" which is V6+.
7557 if (!Subtarget->hasV6Ops())
7558 return false;
7559
7560 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7561 std::string AsmStr = IA->getAsmString();
7562 SmallVector<StringRef, 4> AsmPieces;
7563 SplitString(AsmStr, AsmPieces, ";\n");
7564
7565 switch (AsmPieces.size()) {
7566 default: return false;
7567 case 1:
7568 AsmStr = AsmPieces[0];
7569 AsmPieces.clear();
7570 SplitString(AsmStr, AsmPieces, " \t,");
7571
7572 // rev $0, $1
7573 if (AsmPieces.size() == 3 &&
7574 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7575 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007576 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00007577 if (Ty && Ty->getBitWidth() == 32)
7578 return IntrinsicLowering::LowerToByteSwap(CI);
7579 }
7580 break;
7581 }
7582
7583 return false;
7584}
7585
Evan Chenga8e29892007-01-19 07:51:42 +00007586/// getConstraintType - Given a constraint letter, return the type of
7587/// constraint it is for this target.
7588ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007589ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7590 if (Constraint.size() == 1) {
7591 switch (Constraint[0]) {
7592 default: break;
7593 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007594 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00007595 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007596 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007597 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00007598 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00007599 // An address with a single base register. Due to the way we
7600 // currently handle addresses it is the same as an 'r' memory constraint.
7601 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007602 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007603 } else if (Constraint.size() == 2) {
7604 switch (Constraint[0]) {
7605 default: break;
7606 // All 'U+' constraints are addresses.
7607 case 'U': return C_Memory;
7608 }
Evan Chenga8e29892007-01-19 07:51:42 +00007609 }
Chris Lattner4234f572007-03-25 02:14:49 +00007610 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007611}
7612
John Thompson44ab89e2010-10-29 17:29:13 +00007613/// Examine constraint type and operand type and determine a weight value.
7614/// This object must already have been set up with the operand type
7615/// and the current alternative constraint selected.
7616TargetLowering::ConstraintWeight
7617ARMTargetLowering::getSingleConstraintMatchWeight(
7618 AsmOperandInfo &info, const char *constraint) const {
7619 ConstraintWeight weight = CW_Invalid;
7620 Value *CallOperandVal = info.CallOperandVal;
7621 // If we don't have a value, we can't do a match,
7622 // but allow it at the lowest weight.
7623 if (CallOperandVal == NULL)
7624 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007625 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007626 // Look at the constraint type.
7627 switch (*constraint) {
7628 default:
7629 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7630 break;
7631 case 'l':
7632 if (type->isIntegerTy()) {
7633 if (Subtarget->isThumb())
7634 weight = CW_SpecificReg;
7635 else
7636 weight = CW_Register;
7637 }
7638 break;
7639 case 'w':
7640 if (type->isFloatingPointTy())
7641 weight = CW_Register;
7642 break;
7643 }
7644 return weight;
7645}
7646
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007647typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7648RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007649ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007650 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007651 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007652 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007653 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007654 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007655 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007656 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007657 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007658 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007659 case 'h': // High regs or no regs.
7660 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007661 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007662 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007663 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007664 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007665 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007666 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007667 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007668 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007669 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007670 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007671 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007672 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007673 case 'x':
7674 if (VT == MVT::f32)
7675 return RCPair(0U, ARM::SPR_8RegisterClass);
7676 if (VT.getSizeInBits() == 64)
7677 return RCPair(0U, ARM::DPR_8RegisterClass);
7678 if (VT.getSizeInBits() == 128)
7679 return RCPair(0U, ARM::QPR_8RegisterClass);
7680 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007681 case 't':
7682 if (VT == MVT::f32)
7683 return RCPair(0U, ARM::SPRRegisterClass);
7684 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007685 }
7686 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007687 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007688 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007689
Evan Chenga8e29892007-01-19 07:51:42 +00007690 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7691}
7692
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007693/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7694/// vector. If it is invalid, don't add anything to Ops.
7695void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007696 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007697 std::vector<SDValue>&Ops,
7698 SelectionDAG &DAG) const {
7699 SDValue Result(0, 0);
7700
Eric Christopher100c8332011-06-02 23:16:42 +00007701 // Currently only support length 1 constraints.
7702 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007703
Eric Christopher100c8332011-06-02 23:16:42 +00007704 char ConstraintLetter = Constraint[0];
7705 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007706 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007707 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007708 case 'I': case 'J': case 'K': case 'L':
7709 case 'M': case 'N': case 'O':
7710 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7711 if (!C)
7712 return;
7713
7714 int64_t CVal64 = C->getSExtValue();
7715 int CVal = (int) CVal64;
7716 // None of these constraints allow values larger than 32 bits. Check
7717 // that the value fits in an int.
7718 if (CVal != CVal64)
7719 return;
7720
Eric Christopher100c8332011-06-02 23:16:42 +00007721 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007722 case 'j':
7723 // Constant suitable for movw, must be between 0 and
7724 // 65535.
7725 if (Subtarget->hasV6T2Ops())
7726 if (CVal >= 0 && CVal <= 65535)
7727 break;
7728 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007729 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007730 if (Subtarget->isThumb1Only()) {
7731 // This must be a constant between 0 and 255, for ADD
7732 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007733 if (CVal >= 0 && CVal <= 255)
7734 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007735 } else if (Subtarget->isThumb2()) {
7736 // A constant that can be used as an immediate value in a
7737 // data-processing instruction.
7738 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7739 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007740 } else {
7741 // A constant that can be used as an immediate value in a
7742 // data-processing instruction.
7743 if (ARM_AM::getSOImmVal(CVal) != -1)
7744 break;
7745 }
7746 return;
7747
7748 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007749 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007750 // This must be a constant between -255 and -1, for negated ADD
7751 // immediates. This can be used in GCC with an "n" modifier that
7752 // prints the negated value, for use with SUB instructions. It is
7753 // not useful otherwise but is implemented for compatibility.
7754 if (CVal >= -255 && CVal <= -1)
7755 break;
7756 } else {
7757 // This must be a constant between -4095 and 4095. It is not clear
7758 // what this constraint is intended for. Implemented for
7759 // compatibility with GCC.
7760 if (CVal >= -4095 && CVal <= 4095)
7761 break;
7762 }
7763 return;
7764
7765 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007766 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007767 // A 32-bit value where only one byte has a nonzero value. Exclude
7768 // zero to match GCC. This constraint is used by GCC internally for
7769 // constants that can be loaded with a move/shift combination.
7770 // It is not useful otherwise but is implemented for compatibility.
7771 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7772 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007773 } else if (Subtarget->isThumb2()) {
7774 // A constant whose bitwise inverse can be used as an immediate
7775 // value in a data-processing instruction. This can be used in GCC
7776 // with a "B" modifier that prints the inverted value, for use with
7777 // BIC and MVN instructions. It is not useful otherwise but is
7778 // implemented for compatibility.
7779 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7780 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007781 } else {
7782 // A constant whose bitwise inverse can be used as an immediate
7783 // value in a data-processing instruction. This can be used in GCC
7784 // with a "B" modifier that prints the inverted value, for use with
7785 // BIC and MVN instructions. It is not useful otherwise but is
7786 // implemented for compatibility.
7787 if (ARM_AM::getSOImmVal(~CVal) != -1)
7788 break;
7789 }
7790 return;
7791
7792 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007793 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007794 // This must be a constant between -7 and 7,
7795 // for 3-operand ADD/SUB immediate instructions.
7796 if (CVal >= -7 && CVal < 7)
7797 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007798 } else if (Subtarget->isThumb2()) {
7799 // A constant whose negation can be used as an immediate value in a
7800 // data-processing instruction. This can be used in GCC with an "n"
7801 // modifier that prints the negated value, for use with SUB
7802 // instructions. It is not useful otherwise but is implemented for
7803 // compatibility.
7804 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7805 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007806 } else {
7807 // A constant whose negation can be used as an immediate value in a
7808 // data-processing instruction. This can be used in GCC with an "n"
7809 // modifier that prints the negated value, for use with SUB
7810 // instructions. It is not useful otherwise but is implemented for
7811 // compatibility.
7812 if (ARM_AM::getSOImmVal(-CVal) != -1)
7813 break;
7814 }
7815 return;
7816
7817 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007818 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007819 // This must be a multiple of 4 between 0 and 1020, for
7820 // ADD sp + immediate.
7821 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7822 break;
7823 } else {
7824 // A power of two or a constant between 0 and 32. This is used in
7825 // GCC for the shift amount on shifted register operands, but it is
7826 // useful in general for any shift amounts.
7827 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7828 break;
7829 }
7830 return;
7831
7832 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007833 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007834 // This must be a constant between 0 and 31, for shift amounts.
7835 if (CVal >= 0 && CVal <= 31)
7836 break;
7837 }
7838 return;
7839
7840 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007841 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007842 // This must be a multiple of 4 between -508 and 508, for
7843 // ADD/SUB sp = sp + immediate.
7844 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7845 break;
7846 }
7847 return;
7848 }
7849 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7850 break;
7851 }
7852
7853 if (Result.getNode()) {
7854 Ops.push_back(Result);
7855 return;
7856 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007857 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007858}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007859
7860bool
7861ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7862 // The ARM target isn't yet aware of offsets.
7863 return false;
7864}
Evan Cheng39382422009-10-28 01:44:26 +00007865
7866int ARM::getVFPf32Imm(const APFloat &FPImm) {
7867 APInt Imm = FPImm.bitcastToAPInt();
7868 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7869 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7870 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7871
7872 // We can handle 4 bits of mantissa.
7873 // mantissa = (16+UInt(e:f:g:h))/16.
7874 if (Mantissa & 0x7ffff)
7875 return -1;
7876 Mantissa >>= 19;
7877 if ((Mantissa & 0xf) != Mantissa)
7878 return -1;
7879
7880 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7881 if (Exp < -3 || Exp > 4)
7882 return -1;
7883 Exp = ((Exp+3) & 0x7) ^ 4;
7884
7885 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7886}
7887
7888int ARM::getVFPf64Imm(const APFloat &FPImm) {
7889 APInt Imm = FPImm.bitcastToAPInt();
7890 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7891 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7892 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7893
7894 // We can handle 4 bits of mantissa.
7895 // mantissa = (16+UInt(e:f:g:h))/16.
7896 if (Mantissa & 0xffffffffffffLL)
7897 return -1;
7898 Mantissa >>= 48;
7899 if ((Mantissa & 0xf) != Mantissa)
7900 return -1;
7901
7902 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7903 if (Exp < -3 || Exp > 4)
7904 return -1;
7905 Exp = ((Exp+3) & 0x7) ^ 4;
7906
7907 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7908}
7909
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007910bool ARM::isBitFieldInvertedMask(unsigned v) {
7911 if (v == 0xffffffff)
7912 return 0;
7913 // there can be 1's on either or both "outsides", all the "inside"
7914 // bits must be 0's
7915 unsigned int lsb = 0, msb = 31;
7916 while (v & (1 << msb)) --msb;
7917 while (v & (1 << lsb)) ++lsb;
7918 for (unsigned int i = lsb; i <= msb; ++i) {
7919 if (v & (1 << i))
7920 return 0;
7921 }
7922 return 1;
7923}
7924
Evan Cheng39382422009-10-28 01:44:26 +00007925/// isFPImmLegal - Returns true if the target can instruction select the
7926/// specified FP immediate natively. If false, the legalizer will
7927/// materialize the FP immediate as a load from a constant pool.
7928bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7929 if (!Subtarget->hasVFP3())
7930 return false;
7931 if (VT == MVT::f32)
7932 return ARM::getVFPf32Imm(Imm) != -1;
7933 if (VT == MVT::f64)
7934 return ARM::getVFPf64Imm(Imm) != -1;
7935 return false;
7936}
Bob Wilson65ffec42010-09-21 17:56:22 +00007937
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007938/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007939/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7940/// specified in the intrinsic calls.
7941bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7942 const CallInst &I,
7943 unsigned Intrinsic) const {
7944 switch (Intrinsic) {
7945 case Intrinsic::arm_neon_vld1:
7946 case Intrinsic::arm_neon_vld2:
7947 case Intrinsic::arm_neon_vld3:
7948 case Intrinsic::arm_neon_vld4:
7949 case Intrinsic::arm_neon_vld2lane:
7950 case Intrinsic::arm_neon_vld3lane:
7951 case Intrinsic::arm_neon_vld4lane: {
7952 Info.opc = ISD::INTRINSIC_W_CHAIN;
7953 // Conservatively set memVT to the entire set of vectors loaded.
7954 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7955 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7956 Info.ptrVal = I.getArgOperand(0);
7957 Info.offset = 0;
7958 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7959 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7960 Info.vol = false; // volatile loads with NEON intrinsics not supported
7961 Info.readMem = true;
7962 Info.writeMem = false;
7963 return true;
7964 }
7965 case Intrinsic::arm_neon_vst1:
7966 case Intrinsic::arm_neon_vst2:
7967 case Intrinsic::arm_neon_vst3:
7968 case Intrinsic::arm_neon_vst4:
7969 case Intrinsic::arm_neon_vst2lane:
7970 case Intrinsic::arm_neon_vst3lane:
7971 case Intrinsic::arm_neon_vst4lane: {
7972 Info.opc = ISD::INTRINSIC_VOID;
7973 // Conservatively set memVT to the entire set of vectors stored.
7974 unsigned NumElts = 0;
7975 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007976 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00007977 if (!ArgTy->isVectorTy())
7978 break;
7979 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7980 }
7981 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7982 Info.ptrVal = I.getArgOperand(0);
7983 Info.offset = 0;
7984 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7985 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7986 Info.vol = false; // volatile stores with NEON intrinsics not supported
7987 Info.readMem = false;
7988 Info.writeMem = true;
7989 return true;
7990 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007991 case Intrinsic::arm_strexd: {
7992 Info.opc = ISD::INTRINSIC_W_CHAIN;
7993 Info.memVT = MVT::i64;
7994 Info.ptrVal = I.getArgOperand(2);
7995 Info.offset = 0;
7996 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007997 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007998 Info.readMem = false;
7999 Info.writeMem = true;
8000 return true;
8001 }
8002 case Intrinsic::arm_ldrexd: {
8003 Info.opc = ISD::INTRINSIC_W_CHAIN;
8004 Info.memVT = MVT::i64;
8005 Info.ptrVal = I.getArgOperand(0);
8006 Info.offset = 0;
8007 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008008 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008009 Info.readMem = true;
8010 Info.writeMem = false;
8011 return true;
8012 }
Bob Wilson65ffec42010-09-21 17:56:22 +00008013 default:
8014 break;
8015 }
8016
8017 return false;
8018}