blob: ca8f2db96e8e8371d0053c6f825d718f5f679b77 [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.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000599 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000600 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000601 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000602 // membarrier needs custom lowering; the rest are legal and handled
603 // normally.
604 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000605 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000606 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
607 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000608 } else {
609 // Set them all for expansion, which will force libcalls.
610 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000611 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000612 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000613 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000614 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000615 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000616 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000617 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000618 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000619 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000620 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000621 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000622 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000623 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman069e2ed2011-08-26 02:59:24 +0000624 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
625 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000626 // Since the libcalls include locking, fold in the fences
627 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000628 }
Evan Chenga8e29892007-01-19 07:51:42 +0000629
Evan Cheng416941d2010-11-04 05:19:35 +0000630 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000631
Eli Friedmana2c6f452010-06-26 04:36:50 +0000632 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
633 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
635 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000636 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000638
Nate Begemand1fb5832010-08-03 21:31:55 +0000639 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000640 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
641 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000642 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000643 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
644 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000645
646 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000648 if (Subtarget->isTargetDarwin()) {
649 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
650 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000651 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000652 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000653 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000654
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::SETCC, MVT::i32, Expand);
656 setOperationAction(ISD::SETCC, MVT::f32, Expand);
657 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000658 setOperationAction(ISD::SELECT, MVT::i32, Custom);
659 setOperationAction(ISD::SELECT, MVT::f32, Custom);
660 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
662 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
663 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000664
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
666 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
667 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
668 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
669 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000670
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000671 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::FSIN, MVT::f64, Expand);
673 setOperationAction(ISD::FSIN, MVT::f32, Expand);
674 setOperationAction(ISD::FCOS, MVT::f32, Expand);
675 setOperationAction(ISD::FCOS, MVT::f64, Expand);
676 setOperationAction(ISD::FREM, MVT::f64, Expand);
677 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000678 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
680 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000681 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 setOperationAction(ISD::FPOW, MVT::f64, Expand);
683 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000684
Cameron Zwarich33390842011-07-08 21:39:21 +0000685 setOperationAction(ISD::FMA, MVT::f64, Expand);
686 setOperationAction(ISD::FMA, MVT::f32, Expand);
687
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000688 // Various VFP goodness
689 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000690 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
691 if (Subtarget->hasVFP2()) {
692 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
693 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
694 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
695 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
696 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000697 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000698 if (!Subtarget->hasFP16()) {
699 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
700 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000701 }
Evan Cheng110cf482008-04-01 01:50:16 +0000702 }
Evan Chenga8e29892007-01-19 07:51:42 +0000703
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000704 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000705 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000706 setTargetDAGCombine(ISD::ADD);
707 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000708 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000709
Owen Anderson080c0922010-11-05 19:27:46 +0000710 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000711 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000712 if (Subtarget->hasNEON())
713 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000714
Evan Chenga8e29892007-01-19 07:51:42 +0000715 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000716
Evan Chengf7d87ee2010-05-21 00:43:17 +0000717 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
718 setSchedulingPreference(Sched::RegPressure);
719 else
720 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000721
Evan Cheng05219282011-01-06 06:52:41 +0000722 //// temporary - rewrite interface to use type
723 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000724
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000725 // On ARM arguments smaller than 4 bytes are extended, so all arguments
726 // are at least 4 bytes aligned.
727 setMinStackArgumentAlignment(4);
728
Evan Chengfff606d2010-09-24 19:07:23 +0000729 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000730
731 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000732}
733
Andrew Trick32cec0a2011-01-19 02:35:27 +0000734// FIXME: It might make sense to define the representative register class as the
735// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
736// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
737// SPR's representative would be DPR_VFP2. This should work well if register
738// pressure tracking were modified such that a register use would increment the
739// pressure of the register class's representative and all of it's super
740// classes' representatives transitively. We have not implemented this because
741// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000742// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000743// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000744std::pair<const TargetRegisterClass*, uint8_t>
745ARMTargetLowering::findRepresentativeClass(EVT VT) const{
746 const TargetRegisterClass *RRC = 0;
747 uint8_t Cost = 1;
748 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000749 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000750 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000751 // Use DPR as representative register class for all floating point
752 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
753 // the cost is 1 for both f32 and f64.
754 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000755 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000756 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000757 // When NEON is used for SP, only half of the register file is available
758 // because operations that define both SP and DP results will be constrained
759 // to the VFP2 class (D0-D15). We currently model this constraint prior to
760 // coalescing by double-counting the SP regs. See the FIXME above.
761 if (Subtarget->useNEONForSinglePrecisionFP())
762 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000763 break;
764 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
765 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000766 RRC = ARM::DPRRegisterClass;
767 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000768 break;
769 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000770 RRC = ARM::DPRRegisterClass;
771 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000772 break;
773 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000774 RRC = ARM::DPRRegisterClass;
775 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000776 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000777 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000778 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000779}
780
Evan Chenga8e29892007-01-19 07:51:42 +0000781const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
782 switch (Opcode) {
783 default: return 0;
784 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000785 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000786 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000787 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
788 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000789 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000790 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
791 case ARMISD::tCALL: return "ARMISD::tCALL";
792 case ARMISD::BRCOND: return "ARMISD::BRCOND";
793 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000794 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000795 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
796 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
797 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000798 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000799 case ARMISD::CMPFP: return "ARMISD::CMPFP";
800 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000801 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000802 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
803 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000804
Jim Grosbach3482c802010-01-18 19:58:49 +0000805 case ARMISD::RBIT: return "ARMISD::RBIT";
806
Bob Wilson76a312b2010-03-19 22:51:32 +0000807 case ARMISD::FTOSI: return "ARMISD::FTOSI";
808 case ARMISD::FTOUI: return "ARMISD::FTOUI";
809 case ARMISD::SITOF: return "ARMISD::SITOF";
810 case ARMISD::UITOF: return "ARMISD::UITOF";
811
Evan Chenga8e29892007-01-19 07:51:42 +0000812 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
813 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
814 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000815
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000816 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
817 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000818
Evan Chengc5942082009-10-28 06:55:03 +0000819 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
820 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000821 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000822
Dale Johannesen51e28e62010-06-03 21:09:53 +0000823 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000824
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000825 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000826
Evan Cheng86198642009-08-07 00:34:42 +0000827 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
828
Jim Grosbach3728e962009-12-10 00:11:09 +0000829 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000830 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000831
Evan Chengdfed19f2010-11-03 06:34:55 +0000832 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
833
Bob Wilson5bafff32009-06-22 23:27:02 +0000834 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000835 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000836 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000837 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
838 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000839 case ARMISD::VCGEU: return "ARMISD::VCGEU";
840 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000841 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
842 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000843 case ARMISD::VCGTU: return "ARMISD::VCGTU";
844 case ARMISD::VTST: return "ARMISD::VTST";
845
846 case ARMISD::VSHL: return "ARMISD::VSHL";
847 case ARMISD::VSHRs: return "ARMISD::VSHRs";
848 case ARMISD::VSHRu: return "ARMISD::VSHRu";
849 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
850 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
851 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
852 case ARMISD::VSHRN: return "ARMISD::VSHRN";
853 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
854 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
855 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
856 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
857 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
858 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
859 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
860 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
861 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
862 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
863 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
864 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
865 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
866 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000867 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000868 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000869 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000870 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000871 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000872 case ARMISD::VREV64: return "ARMISD::VREV64";
873 case ARMISD::VREV32: return "ARMISD::VREV32";
874 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000875 case ARMISD::VZIP: return "ARMISD::VZIP";
876 case ARMISD::VUZP: return "ARMISD::VUZP";
877 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000878 case ARMISD::VTBL1: return "ARMISD::VTBL1";
879 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000880 case ARMISD::VMULLs: return "ARMISD::VMULLs";
881 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000882 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000883 case ARMISD::FMAX: return "ARMISD::FMAX";
884 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000885 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000886 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
887 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000888 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000889 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
890 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
891 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000892 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
893 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
894 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
895 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
896 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
897 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
898 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
899 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
900 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
901 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
902 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
903 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
904 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
905 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
906 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
907 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
908 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000909 }
910}
911
Evan Cheng06b666c2010-05-15 02:18:07 +0000912/// getRegClassFor - Return the register class that should be used for the
913/// specified value type.
914TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
915 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
916 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
917 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000918 if (Subtarget->hasNEON()) {
919 if (VT == MVT::v4i64)
920 return ARM::QQPRRegisterClass;
921 else if (VT == MVT::v8i64)
922 return ARM::QQQQPRRegisterClass;
923 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000924 return TargetLowering::getRegClassFor(VT);
925}
926
Eric Christopherab695882010-07-21 22:26:11 +0000927// Create a fast isel object.
928FastISel *
929ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
930 return ARM::createFastISel(funcInfo);
931}
932
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000933/// getMaximalGlobalOffset - Returns the maximal possible offset which can
934/// be used for loads / stores from the global.
935unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
936 return (Subtarget->isThumb1Only() ? 127 : 4095);
937}
938
Evan Cheng1cc39842010-05-20 23:26:43 +0000939Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000940 unsigned NumVals = N->getNumValues();
941 if (!NumVals)
942 return Sched::RegPressure;
943
944 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000945 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000946 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000947 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000948 if (VT.isFloatingPoint() || VT.isVector())
949 return Sched::Latency;
950 }
Evan Chengc10f5432010-05-28 23:25:23 +0000951
952 if (!N->isMachineOpcode())
953 return Sched::RegPressure;
954
955 // Load are scheduled for latency even if there instruction itinerary
956 // is not available.
957 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000958 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000959
Evan Chenge837dea2011-06-28 19:10:37 +0000960 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +0000961 return Sched::RegPressure;
962 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +0000963 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000964 return Sched::Latency;
965
Evan Cheng1cc39842010-05-20 23:26:43 +0000966 return Sched::RegPressure;
967}
968
Evan Chenga8e29892007-01-19 07:51:42 +0000969//===----------------------------------------------------------------------===//
970// Lowering Code
971//===----------------------------------------------------------------------===//
972
Evan Chenga8e29892007-01-19 07:51:42 +0000973/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
974static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
975 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000976 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000977 case ISD::SETNE: return ARMCC::NE;
978 case ISD::SETEQ: return ARMCC::EQ;
979 case ISD::SETGT: return ARMCC::GT;
980 case ISD::SETGE: return ARMCC::GE;
981 case ISD::SETLT: return ARMCC::LT;
982 case ISD::SETLE: return ARMCC::LE;
983 case ISD::SETUGT: return ARMCC::HI;
984 case ISD::SETUGE: return ARMCC::HS;
985 case ISD::SETULT: return ARMCC::LO;
986 case ISD::SETULE: return ARMCC::LS;
987 }
988}
989
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000990/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
991static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000992 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000993 CondCode2 = ARMCC::AL;
994 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000995 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000996 case ISD::SETEQ:
997 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
998 case ISD::SETGT:
999 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1000 case ISD::SETGE:
1001 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1002 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001003 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001004 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1005 case ISD::SETO: CondCode = ARMCC::VC; break;
1006 case ISD::SETUO: CondCode = ARMCC::VS; break;
1007 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1008 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1009 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1010 case ISD::SETLT:
1011 case ISD::SETULT: CondCode = ARMCC::LT; break;
1012 case ISD::SETLE:
1013 case ISD::SETULE: CondCode = ARMCC::LE; break;
1014 case ISD::SETNE:
1015 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1016 }
Evan Chenga8e29892007-01-19 07:51:42 +00001017}
1018
Bob Wilson1f595bb2009-04-17 19:07:39 +00001019//===----------------------------------------------------------------------===//
1020// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001021//===----------------------------------------------------------------------===//
1022
1023#include "ARMGenCallingConv.inc"
1024
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001025/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1026/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001027CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001028 bool Return,
1029 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001030 switch (CC) {
1031 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001032 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001033 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001034 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001035 if (!Subtarget->isAAPCS_ABI())
1036 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1037 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1038 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1039 }
1040 // Fallthrough
1041 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001042 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001043 if (!Subtarget->isAAPCS_ABI())
1044 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1045 else if (Subtarget->hasVFP2() &&
1046 FloatABIType == FloatABI::Hard && !isVarArg)
1047 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1048 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1049 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001050 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001051 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001052 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001053 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001054 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001055 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001056 }
1057}
1058
Dan Gohman98ca4f22009-08-05 01:29:28 +00001059/// LowerCallResult - Lower the result values of a call into the
1060/// appropriate copies out of appropriate physical registers.
1061SDValue
1062ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001063 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001064 const SmallVectorImpl<ISD::InputArg> &Ins,
1065 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001066 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001067
Bob Wilson1f595bb2009-04-17 19:07:39 +00001068 // Assign locations to each value returned by this call.
1069 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001070 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1071 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001072 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001073 CCAssignFnForNode(CallConv, /* Return*/ true,
1074 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001075
1076 // Copy all of the result registers out of their specified physreg.
1077 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1078 CCValAssign VA = RVLocs[i];
1079
Bob Wilson80915242009-04-25 00:33:20 +00001080 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001081 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001082 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001083 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001084 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001085 Chain = Lo.getValue(1);
1086 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001087 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001088 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001089 InFlag);
1090 Chain = Hi.getValue(1);
1091 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001092 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001093
Owen Anderson825b72b2009-08-11 20:47:22 +00001094 if (VA.getLocVT() == MVT::v2f64) {
1095 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1096 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1097 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001098
1099 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001100 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001101 Chain = Lo.getValue(1);
1102 InFlag = Lo.getValue(2);
1103 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001104 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001105 Chain = Hi.getValue(1);
1106 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001107 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001108 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1109 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001110 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001111 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001112 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1113 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001114 Chain = Val.getValue(1);
1115 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001116 }
Bob Wilson80915242009-04-25 00:33:20 +00001117
1118 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001119 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001120 case CCValAssign::Full: break;
1121 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001122 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001123 break;
1124 }
1125
Dan Gohman98ca4f22009-08-05 01:29:28 +00001126 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001127 }
1128
Dan Gohman98ca4f22009-08-05 01:29:28 +00001129 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001130}
1131
Bob Wilsondee46d72009-04-17 20:35:10 +00001132/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001133SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001134ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1135 SDValue StackPtr, SDValue Arg,
1136 DebugLoc dl, SelectionDAG &DAG,
1137 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001138 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001139 unsigned LocMemOffset = VA.getLocMemOffset();
1140 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1141 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001143 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001144 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001145}
1146
Dan Gohman98ca4f22009-08-05 01:29:28 +00001147void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001148 SDValue Chain, SDValue &Arg,
1149 RegsToPassVector &RegsToPass,
1150 CCValAssign &VA, CCValAssign &NextVA,
1151 SDValue &StackPtr,
1152 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001153 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001154
Jim Grosbache5165492009-11-09 00:11:35 +00001155 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001156 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001157 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1158
1159 if (NextVA.isRegLoc())
1160 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1161 else {
1162 assert(NextVA.isMemLoc());
1163 if (StackPtr.getNode() == 0)
1164 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1165
Dan Gohman98ca4f22009-08-05 01:29:28 +00001166 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1167 dl, DAG, NextVA,
1168 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001169 }
1170}
1171
Dan Gohman98ca4f22009-08-05 01:29:28 +00001172/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001173/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1174/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001176ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001177 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001178 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001180 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001181 const SmallVectorImpl<ISD::InputArg> &Ins,
1182 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001183 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001184 MachineFunction &MF = DAG.getMachineFunction();
1185 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1186 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001187 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001188 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001189 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001190 if (isTailCall) {
1191 // Check if it's really possible to do a tail call.
1192 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1193 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001194 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001195 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1196 // detected sibcalls.
1197 if (isTailCall) {
1198 ++NumTailCalls;
1199 IsSibCall = true;
1200 }
1201 }
Evan Chenga8e29892007-01-19 07:51:42 +00001202
Bob Wilson1f595bb2009-04-17 19:07:39 +00001203 // Analyze operands of the call, assigning locations to each operand.
1204 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001205 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1206 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001208 CCAssignFnForNode(CallConv, /* Return*/ false,
1209 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001210
Bob Wilson1f595bb2009-04-17 19:07:39 +00001211 // Get a count of how many bytes are to be pushed on the stack.
1212 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001213
Dale Johannesen51e28e62010-06-03 21:09:53 +00001214 // For tail calls, memory operands are available in our caller's stack.
1215 if (IsSibCall)
1216 NumBytes = 0;
1217
Evan Chenga8e29892007-01-19 07:51:42 +00001218 // Adjust the stack pointer for the new arguments...
1219 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001220 if (!IsSibCall)
1221 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001222
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001223 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001224
Bob Wilson5bafff32009-06-22 23:27:02 +00001225 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001226 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001227
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001229 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1231 i != e;
1232 ++i, ++realArgIdx) {
1233 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001234 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001236 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001237
Bob Wilson1f595bb2009-04-17 19:07:39 +00001238 // Promote the value if needed.
1239 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001240 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241 case CCValAssign::Full: break;
1242 case CCValAssign::SExt:
1243 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1244 break;
1245 case CCValAssign::ZExt:
1246 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1247 break;
1248 case CCValAssign::AExt:
1249 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1250 break;
1251 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001252 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001253 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001254 }
1255
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001256 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001258 if (VA.getLocVT() == MVT::v2f64) {
1259 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1260 DAG.getConstant(0, MVT::i32));
1261 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1262 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001263
Dan Gohman98ca4f22009-08-05 01:29:28 +00001264 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001265 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1266
1267 VA = ArgLocs[++i]; // skip ahead to next loc
1268 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001269 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001270 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1271 } else {
1272 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001273
Dan Gohman98ca4f22009-08-05 01:29:28 +00001274 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1275 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001276 }
1277 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001279 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001280 }
1281 } else if (VA.isRegLoc()) {
1282 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001283 } else if (isByVal) {
1284 assert(VA.isMemLoc());
1285 unsigned offset = 0;
1286
1287 // True if this byval aggregate will be split between registers
1288 // and memory.
1289 if (CCInfo.isFirstByValRegValid()) {
1290 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1291 unsigned int i, j;
1292 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1293 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1294 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1295 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1296 MachinePointerInfo(),
1297 false, false, 0);
1298 MemOpChains.push_back(Load.getValue(1));
1299 RegsToPass.push_back(std::make_pair(j, Load));
1300 }
1301 offset = ARM::R4 - CCInfo.getFirstByValReg();
1302 CCInfo.clearFirstByValReg();
1303 }
1304
1305 unsigned LocMemOffset = VA.getLocMemOffset();
1306 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1307 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1308 StkPtrOff);
1309 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1310 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1311 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1312 MVT::i32);
1313 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1314 Flags.getByValAlign(),
1315 /*isVolatile=*/false,
1316 /*AlwaysInline=*/false,
1317 MachinePointerInfo(0),
1318 MachinePointerInfo(0)));
1319
1320 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001321 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001322
Dan Gohman98ca4f22009-08-05 01:29:28 +00001323 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1324 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001325 }
Evan Chenga8e29892007-01-19 07:51:42 +00001326 }
1327
1328 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001329 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001330 &MemOpChains[0], MemOpChains.size());
1331
1332 // Build a sequence of copy-to-reg nodes chained together with token chain
1333 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001334 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001335 // Tail call byval lowering might overwrite argument registers so in case of
1336 // tail call optimization the copies to registers are lowered later.
1337 if (!isTailCall)
1338 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1339 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1340 RegsToPass[i].second, InFlag);
1341 InFlag = Chain.getValue(1);
1342 }
Evan Chenga8e29892007-01-19 07:51:42 +00001343
Dale Johannesen51e28e62010-06-03 21:09:53 +00001344 // For tail calls lower the arguments to the 'real' stack slot.
1345 if (isTailCall) {
1346 // Force all the incoming stack arguments to be loaded from the stack
1347 // before any new outgoing arguments are stored to the stack, because the
1348 // outgoing stack slots may alias the incoming argument stack slots, and
1349 // the alias isn't otherwise explicit. This is slightly more conservative
1350 // than necessary, because it means that each store effectively depends
1351 // on every argument instead of just those arguments it would clobber.
1352
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001353 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001354 InFlag = SDValue();
1355 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1356 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1357 RegsToPass[i].second, InFlag);
1358 InFlag = Chain.getValue(1);
1359 }
1360 InFlag =SDValue();
1361 }
1362
Bill Wendling056292f2008-09-16 21:48:12 +00001363 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1364 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1365 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001366 bool isDirect = false;
1367 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001368 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001369 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001370
1371 if (EnableARMLongCalls) {
1372 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1373 && "long-calls with non-static relocation model!");
1374 // Handle a global address or an external symbol. If it's not one of
1375 // those, the target's already in a register, so we don't need to do
1376 // anything extra.
1377 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001378 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001379 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001380 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001381 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1382 ARMPCLabelIndex,
1383 ARMCP::CPValue, 0);
1384 // Get the address of the callee into a register
1385 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1386 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1387 Callee = DAG.getLoad(getPointerTy(), dl,
1388 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001389 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001390 false, false, 0);
1391 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1392 const char *Sym = S->getSymbol();
1393
1394 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001395 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001396 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1397 Sym, ARMPCLabelIndex, 0);
1398 // Get the address of the callee into a register
1399 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1400 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1401 Callee = DAG.getLoad(getPointerTy(), dl,
1402 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001403 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001404 false, false, 0);
1405 }
1406 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001407 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001408 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001409 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001410 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001411 getTargetMachine().getRelocationModel() != Reloc::Static;
1412 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001413 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001414 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001415 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001416 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001417 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001418 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001419 ARMPCLabelIndex,
1420 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001421 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001422 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001423 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001424 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001425 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001426 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001427 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001428 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001429 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001430 } else {
1431 // On ELF targets for PIC code, direct calls should go through the PLT
1432 unsigned OpFlags = 0;
1433 if (Subtarget->isTargetELF() &&
1434 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1435 OpFlags = ARMII::MO_PLT;
1436 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1437 }
Bill Wendling056292f2008-09-16 21:48:12 +00001438 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001439 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001440 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001441 getTargetMachine().getRelocationModel() != Reloc::Static;
1442 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001443 // tBX takes a register source operand.
1444 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001445 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001446 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001447 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001448 Sym, ARMPCLabelIndex, 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);
Dale Johannesen33c960f2009-02-04 20:06:27 +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 unsigned OpFlags = 0;
1460 // On ELF targets for PIC code, direct calls should go through the PLT
1461 if (Subtarget->isTargetELF() &&
1462 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1463 OpFlags = ARMII::MO_PLT;
1464 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1465 }
Evan Chenga8e29892007-01-19 07:51:42 +00001466 }
1467
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001468 // FIXME: handle tail calls differently.
1469 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001470 if (Subtarget->isThumb()) {
1471 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001472 CallOpc = ARMISD::CALL_NOLINK;
1473 else
1474 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1475 } else {
1476 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001477 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1478 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001479 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001480
Dan Gohman475871a2008-07-27 21:46:04 +00001481 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001482 Ops.push_back(Chain);
1483 Ops.push_back(Callee);
1484
1485 // Add argument registers to the end of the list so that they are known live
1486 // into the call.
1487 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1488 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1489 RegsToPass[i].second.getValueType()));
1490
Gabor Greifba36cb52008-08-28 21:40:38 +00001491 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001492 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001493
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001494 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001495 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001496 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001497
Duncan Sands4bdcb612008-07-02 17:40:58 +00001498 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001499 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001500 InFlag = Chain.getValue(1);
1501
Chris Lattnere563bbc2008-10-11 22:08:30 +00001502 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1503 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001504 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001505 InFlag = Chain.getValue(1);
1506
Bob Wilson1f595bb2009-04-17 19:07:39 +00001507 // Handle result values, copying them out of physregs into vregs that we
1508 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001509 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1510 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001511}
1512
Stuart Hastingsf222e592011-02-28 17:17:53 +00001513/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001514/// on the stack. Remember the next parameter register to allocate,
1515/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001516/// this.
1517void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001518llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1519 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1520 assert((State->getCallOrPrologue() == Prologue ||
1521 State->getCallOrPrologue() == Call) &&
1522 "unhandled ParmContext");
1523 if ((!State->isFirstByValRegValid()) &&
1524 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1525 State->setFirstByValReg(reg);
1526 // At a call site, a byval parameter that is split between
1527 // registers and memory needs its size truncated here. In a
1528 // function prologue, such byval parameters are reassembled in
1529 // memory, and are not truncated.
1530 if (State->getCallOrPrologue() == Call) {
1531 unsigned excess = 4 * (ARM::R4 - reg);
1532 assert(size >= excess && "expected larger existing stack allocation");
1533 size -= excess;
1534 }
1535 }
1536 // Confiscate any remaining parameter registers to preclude their
1537 // assignment to subsequent parameters.
1538 while (State->AllocateReg(GPRArgRegs, 4))
1539 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001540}
1541
Dale Johannesen51e28e62010-06-03 21:09:53 +00001542/// MatchingStackOffset - Return true if the given stack call argument is
1543/// already available in the same position (relatively) of the caller's
1544/// incoming argument stack.
1545static
1546bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1547 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1548 const ARMInstrInfo *TII) {
1549 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1550 int FI = INT_MAX;
1551 if (Arg.getOpcode() == ISD::CopyFromReg) {
1552 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001553 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001554 return false;
1555 MachineInstr *Def = MRI->getVRegDef(VR);
1556 if (!Def)
1557 return false;
1558 if (!Flags.isByVal()) {
1559 if (!TII->isLoadFromStackSlot(Def, FI))
1560 return false;
1561 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001562 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001563 }
1564 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1565 if (Flags.isByVal())
1566 // ByVal argument is passed in as a pointer but it's now being
1567 // dereferenced. e.g.
1568 // define @foo(%struct.X* %A) {
1569 // tail call @bar(%struct.X* byval %A)
1570 // }
1571 return false;
1572 SDValue Ptr = Ld->getBasePtr();
1573 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1574 if (!FINode)
1575 return false;
1576 FI = FINode->getIndex();
1577 } else
1578 return false;
1579
1580 assert(FI != INT_MAX);
1581 if (!MFI->isFixedObjectIndex(FI))
1582 return false;
1583 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1584}
1585
1586/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1587/// for tail call optimization. Targets which want to do tail call
1588/// optimization should implement this function.
1589bool
1590ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1591 CallingConv::ID CalleeCC,
1592 bool isVarArg,
1593 bool isCalleeStructRet,
1594 bool isCallerStructRet,
1595 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001596 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001597 const SmallVectorImpl<ISD::InputArg> &Ins,
1598 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001599 const Function *CallerF = DAG.getMachineFunction().getFunction();
1600 CallingConv::ID CallerCC = CallerF->getCallingConv();
1601 bool CCMatch = CallerCC == CalleeCC;
1602
1603 // Look for obvious safe cases to perform tail call optimization that do not
1604 // require ABI changes. This is what gcc calls sibcall.
1605
Jim Grosbach7616b642010-06-16 23:45:49 +00001606 // Do not sibcall optimize vararg calls unless the call site is not passing
1607 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001608 if (isVarArg && !Outs.empty())
1609 return false;
1610
1611 // Also avoid sibcall optimization if either caller or callee uses struct
1612 // return semantics.
1613 if (isCalleeStructRet || isCallerStructRet)
1614 return false;
1615
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001616 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001617 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1618 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1619 // support in the assembler and linker to be used. This would need to be
1620 // fixed to fully support tail calls in Thumb1.
1621 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001622 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1623 // LR. This means if we need to reload LR, it takes an extra instructions,
1624 // which outweighs the value of the tail call; but here we don't know yet
1625 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001626 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001627 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001628
1629 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1630 // but we need to make sure there are enough registers; the only valid
1631 // registers are the 4 used for parameters. We don't currently do this
1632 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001633 if (Subtarget->isThumb1Only())
1634 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001635
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636 // If the calling conventions do not match, then we'd better make sure the
1637 // results are returned in the same way as what the caller expects.
1638 if (!CCMatch) {
1639 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001640 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1641 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001642 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1643
1644 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001645 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1646 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001647 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1648
1649 if (RVLocs1.size() != RVLocs2.size())
1650 return false;
1651 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1652 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1653 return false;
1654 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1655 return false;
1656 if (RVLocs1[i].isRegLoc()) {
1657 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1658 return false;
1659 } else {
1660 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1661 return false;
1662 }
1663 }
1664 }
1665
1666 // If the callee takes no arguments then go on to check the results of the
1667 // call.
1668 if (!Outs.empty()) {
1669 // Check if stack adjustment is needed. For now, do not do this if any
1670 // argument is passed on the stack.
1671 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001672 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1673 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001674 CCInfo.AnalyzeCallOperands(Outs,
1675 CCAssignFnForNode(CalleeCC, false, isVarArg));
1676 if (CCInfo.getNextStackOffset()) {
1677 MachineFunction &MF = DAG.getMachineFunction();
1678
1679 // Check if the arguments are already laid out in the right way as
1680 // the caller's fixed stack objects.
1681 MachineFrameInfo *MFI = MF.getFrameInfo();
1682 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1683 const ARMInstrInfo *TII =
1684 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001685 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1686 i != e;
1687 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001688 CCValAssign &VA = ArgLocs[i];
1689 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001690 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001691 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001692 if (VA.getLocInfo() == CCValAssign::Indirect)
1693 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001694 if (VA.needsCustom()) {
1695 // f64 and vector types are split into multiple registers or
1696 // register/stack-slot combinations. The types will not match
1697 // the registers; give up on memory f64 refs until we figure
1698 // out what to do about this.
1699 if (!VA.isRegLoc())
1700 return false;
1701 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001702 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001703 if (RegVT == MVT::v2f64) {
1704 if (!ArgLocs[++i].isRegLoc())
1705 return false;
1706 if (!ArgLocs[++i].isRegLoc())
1707 return false;
1708 }
1709 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001710 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1711 MFI, MRI, TII))
1712 return false;
1713 }
1714 }
1715 }
1716 }
1717
1718 return true;
1719}
1720
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721SDValue
1722ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001723 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001725 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001726 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001727
Bob Wilsondee46d72009-04-17 20:35:10 +00001728 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001729 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001730
Bob Wilsondee46d72009-04-17 20:35:10 +00001731 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001732 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1733 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001734
Dan Gohman98ca4f22009-08-05 01:29:28 +00001735 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001736 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1737 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001738
1739 // If this is the first return lowered for this function, add
1740 // the regs to the liveout set for the function.
1741 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1742 for (unsigned i = 0; i != RVLocs.size(); ++i)
1743 if (RVLocs[i].isRegLoc())
1744 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001745 }
1746
Bob Wilson1f595bb2009-04-17 19:07:39 +00001747 SDValue Flag;
1748
1749 // Copy the result values into the output registers.
1750 for (unsigned i = 0, realRVLocIdx = 0;
1751 i != RVLocs.size();
1752 ++i, ++realRVLocIdx) {
1753 CCValAssign &VA = RVLocs[i];
1754 assert(VA.isRegLoc() && "Can only return in registers!");
1755
Dan Gohmanc9403652010-07-07 15:54:55 +00001756 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757
1758 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001759 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001760 case CCValAssign::Full: break;
1761 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001762 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001763 break;
1764 }
1765
Bob Wilson1f595bb2009-04-17 19:07:39 +00001766 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001768 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1770 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001771 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001773
1774 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1775 Flag = Chain.getValue(1);
1776 VA = RVLocs[++i]; // skip ahead to next loc
1777 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1778 HalfGPRs.getValue(1), Flag);
1779 Flag = Chain.getValue(1);
1780 VA = RVLocs[++i]; // skip ahead to next loc
1781
1782 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001783 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1784 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001785 }
1786 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1787 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001788 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001790 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001791 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001792 VA = RVLocs[++i]; // skip ahead to next loc
1793 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1794 Flag);
1795 } else
1796 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1797
Bob Wilsondee46d72009-04-17 20:35:10 +00001798 // Guarantee that all emitted copies are
1799 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001800 Flag = Chain.getValue(1);
1801 }
1802
1803 SDValue result;
1804 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001806 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001808
1809 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001810}
1811
Evan Cheng3d2125c2010-11-30 23:55:39 +00001812bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1813 if (N->getNumValues() != 1)
1814 return false;
1815 if (!N->hasNUsesOfValue(1, 0))
1816 return false;
1817
1818 unsigned NumCopies = 0;
1819 SDNode* Copies[2];
1820 SDNode *Use = *N->use_begin();
1821 if (Use->getOpcode() == ISD::CopyToReg) {
1822 Copies[NumCopies++] = Use;
1823 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1824 // f64 returned in a pair of GPRs.
1825 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1826 UI != UE; ++UI) {
1827 if (UI->getOpcode() != ISD::CopyToReg)
1828 return false;
1829 Copies[UI.getUse().getResNo()] = *UI;
1830 ++NumCopies;
1831 }
1832 } else if (Use->getOpcode() == ISD::BITCAST) {
1833 // f32 returned in a single GPR.
1834 if (!Use->hasNUsesOfValue(1, 0))
1835 return false;
1836 Use = *Use->use_begin();
1837 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1838 return false;
1839 Copies[NumCopies++] = Use;
1840 } else {
1841 return false;
1842 }
1843
1844 if (NumCopies != 1 && NumCopies != 2)
1845 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001846
1847 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001848 for (unsigned i = 0; i < NumCopies; ++i) {
1849 SDNode *Copy = Copies[i];
1850 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1851 UI != UE; ++UI) {
1852 if (UI->getOpcode() == ISD::CopyToReg) {
1853 SDNode *Use = *UI;
1854 if (Use == Copies[0] || Use == Copies[1])
1855 continue;
1856 return false;
1857 }
1858 if (UI->getOpcode() != ARMISD::RET_FLAG)
1859 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001860 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001861 }
1862 }
1863
Evan Cheng1bf891a2010-12-01 22:59:46 +00001864 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001865}
1866
Evan Cheng485fafc2011-03-21 01:19:09 +00001867bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1868 if (!EnableARMTailCalls)
1869 return false;
1870
1871 if (!CI->isTailCall())
1872 return false;
1873
1874 return !Subtarget->isThumb1Only();
1875}
1876
Bob Wilsonb62d2572009-11-03 00:02:05 +00001877// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1878// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1879// one of the above mentioned nodes. It has to be wrapped because otherwise
1880// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1881// be used to form addressing mode. These wrapped nodes will be selected
1882// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001883static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001884 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001885 // FIXME there is no actual debug info here
1886 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001887 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001888 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001889 if (CP->isMachineConstantPoolEntry())
1890 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1891 CP->getAlignment());
1892 else
1893 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1894 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001896}
1897
Jim Grosbache1102ca2010-07-19 17:20:38 +00001898unsigned ARMTargetLowering::getJumpTableEncoding() const {
1899 return MachineJumpTableInfo::EK_Inline;
1900}
1901
Dan Gohmand858e902010-04-17 15:26:15 +00001902SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1903 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001904 MachineFunction &MF = DAG.getMachineFunction();
1905 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1906 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001907 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001908 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001909 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001910 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1911 SDValue CPAddr;
1912 if (RelocM == Reloc::Static) {
1913 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1914 } else {
1915 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001916 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001917 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1918 ARMCP::CPBlockAddress,
1919 PCAdj);
1920 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1921 }
1922 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1923 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001924 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001925 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001926 if (RelocM == Reloc::Static)
1927 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001928 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001929 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001930}
1931
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001932// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001933SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001934ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001935 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001936 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001937 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001938 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001939 MachineFunction &MF = DAG.getMachineFunction();
1940 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001941 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001942 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001943 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001944 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001945 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001947 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001948 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001949 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001950 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001951
Evan Chenge7e0d622009-11-06 22:24:13 +00001952 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001953 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001954
1955 // call __tls_get_addr.
1956 ArgListTy Args;
1957 ArgListEntry Entry;
1958 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001959 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001960 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001961 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001962 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001963 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00001964 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001965 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001966 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001967 return CallResult.first;
1968}
1969
1970// Lower ISD::GlobalTLSAddress using the "initial exec" or
1971// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001972SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001973ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001974 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001975 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001976 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001977 SDValue Offset;
1978 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001979 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001980 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001981 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001982
Chris Lattner4fb63d02009-07-15 04:12:33 +00001983 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001984 MachineFunction &MF = DAG.getMachineFunction();
1985 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001986 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001987 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001988 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1989 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001990 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001991 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001992 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001994 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001995 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001996 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001997 Chain = Offset.getValue(1);
1998
Evan Chenge7e0d622009-11-06 22:24:13 +00001999 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002000 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002001
Evan Cheng9eda6892009-10-31 03:39:36 +00002002 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002003 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002004 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002005 } else {
2006 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002007 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002008 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002009 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002010 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002011 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002012 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002013 }
2014
2015 // The address of the thread local variable is the add of the thread
2016 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002017 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002018}
2019
Dan Gohman475871a2008-07-27 21:46:04 +00002020SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002021ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002022 // TODO: implement the "local dynamic" model
2023 assert(Subtarget->isTargetELF() &&
2024 "TLS not implemented for non-ELF targets");
2025 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2026 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2027 // otherwise use the "Local Exec" TLS Model
2028 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2029 return LowerToTLSGeneralDynamicModel(GA, DAG);
2030 else
2031 return LowerToTLSExecModels(GA, DAG);
2032}
2033
Dan Gohman475871a2008-07-27 21:46:04 +00002034SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002035 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002036 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002037 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002038 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002039 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2040 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002041 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002042 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002043 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002044 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002046 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002047 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002048 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002049 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002050 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002051 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002052 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002053 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002054 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002055 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002056 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002057 }
2058
2059 // If we have T2 ops, we can materialize the address directly via movt/movw
2060 // pair. This is always cheaper.
2061 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002062 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002063 // FIXME: Once remat is capable of dealing with instructions with register
2064 // operands, expand this into two nodes.
2065 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2066 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002067 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002068 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2069 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2070 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2071 MachinePointerInfo::getConstantPool(),
2072 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002073 }
2074}
2075
Dan Gohman475871a2008-07-27 21:46:04 +00002076SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002077 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002078 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002079 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002080 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002081 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002082 MachineFunction &MF = DAG.getMachineFunction();
2083 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2084
Evan Cheng4abce0c2011-05-27 20:11:27 +00002085 // FIXME: Enable this for static codegen when tool issues are fixed.
2086 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002087 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002088 // FIXME: Once remat is capable of dealing with instructions with register
2089 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002090 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002091 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2092 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2093
Evan Cheng53519f02011-01-21 18:55:51 +00002094 unsigned Wrapper = (RelocM == Reloc::PIC_)
2095 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2096 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002097 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002098 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2099 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2100 MachinePointerInfo::getGOT(), false, false, 0);
2101 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002102 }
2103
2104 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002105 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002106 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002107 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002108 } else {
2109 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002110 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2111 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002112 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002113 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002114 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002116
Evan Cheng9eda6892009-10-31 03:39:36 +00002117 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002118 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002119 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002120 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002121
2122 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002123 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002124 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002125 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002126
Evan Cheng63476a82009-09-03 07:04:02 +00002127 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002128 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002129 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002130
2131 return Result;
2132}
2133
Dan Gohman475871a2008-07-27 21:46:04 +00002134SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002135 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002136 assert(Subtarget->isTargetELF() &&
2137 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002138 MachineFunction &MF = DAG.getMachineFunction();
2139 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002140 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002141 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002142 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002143 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002144 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2145 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002146 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002147 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002148 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002149 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002150 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002151 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002152 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002153 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002154}
2155
Jim Grosbach0e0da732009-05-12 23:59:14 +00002156SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002157ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2158 const {
2159 DebugLoc dl = Op.getDebugLoc();
2160 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002161 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002162}
2163
2164SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002165ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2166 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002167 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002168 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2169 Op.getOperand(1), Val);
2170}
2171
2172SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002173ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2174 DebugLoc dl = Op.getDebugLoc();
2175 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2176 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2177}
2178
2179SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002180ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002181 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002182 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002183 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002184 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002185 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002186 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002187 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002188 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2189 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002190 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002191 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002192 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002193 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002194 EVT PtrVT = getPointerTy();
2195 DebugLoc dl = Op.getDebugLoc();
2196 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2197 SDValue CPAddr;
2198 unsigned PCAdj = (RelocM != Reloc::PIC_)
2199 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002200 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002201 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2202 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002203 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002204 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002205 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002206 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002207 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002208 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002209
2210 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002211 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002212 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2213 }
2214 return Result;
2215 }
Evan Cheng92e39162011-03-29 23:06:19 +00002216 case Intrinsic::arm_neon_vmulls:
2217 case Intrinsic::arm_neon_vmullu: {
2218 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2219 ? ARMISD::VMULLs : ARMISD::VMULLu;
2220 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2221 Op.getOperand(1), Op.getOperand(2));
2222 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002223 }
2224}
2225
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002226static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002227 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002228 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002229 if (!Subtarget->hasDataBarrier()) {
2230 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2231 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2232 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002233 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002234 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002235 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002236 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002237 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002238
2239 SDValue Op5 = Op.getOperand(5);
2240 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2241 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2242 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2243 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2244
2245 ARM_MB::MemBOpt DMBOpt;
2246 if (isDeviceBarrier)
2247 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2248 else
2249 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2250 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2251 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002252}
2253
Eli Friedman26689ac2011-08-03 21:06:02 +00002254
2255static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2256 const ARMSubtarget *Subtarget) {
2257 // FIXME: handle "fence singlethread" more efficiently.
2258 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002259 if (!Subtarget->hasDataBarrier()) {
2260 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2261 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2262 // here.
2263 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2264 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002265 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002266 DAG.getConstant(0, MVT::i32));
2267 }
2268
Eli Friedman26689ac2011-08-03 21:06:02 +00002269 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002270 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002271}
2272
Evan Chengdfed19f2010-11-03 06:34:55 +00002273static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2274 const ARMSubtarget *Subtarget) {
2275 // ARM pre v5TE and Thumb1 does not have preload instructions.
2276 if (!(Subtarget->isThumb2() ||
2277 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2278 // Just preserve the chain.
2279 return Op.getOperand(0);
2280
2281 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002282 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2283 if (!isRead &&
2284 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2285 // ARMv7 with MP extension has PLDW.
2286 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002287
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002288 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2289 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002290 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002291 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002292 isData = ~isData & 1;
2293 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002294
2295 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002296 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2297 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002298}
2299
Dan Gohman1e93df62010-04-17 14:41:14 +00002300static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2301 MachineFunction &MF = DAG.getMachineFunction();
2302 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2303
Evan Chenga8e29892007-01-19 07:51:42 +00002304 // vastart just stores the address of the VarArgsFrameIndex slot into the
2305 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002306 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002307 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002308 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002309 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002310 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2311 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002312}
2313
Dan Gohman475871a2008-07-27 21:46:04 +00002314SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002315ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2316 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002317 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002318 MachineFunction &MF = DAG.getMachineFunction();
2319 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2320
2321 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002322 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002323 RC = ARM::tGPRRegisterClass;
2324 else
2325 RC = ARM::GPRRegisterClass;
2326
2327 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002328 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002329 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002330
2331 SDValue ArgValue2;
2332 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002333 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002334 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002335
2336 // Create load node to retrieve arguments from the stack.
2337 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002338 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002339 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002340 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002341 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002342 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002343 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002344 }
2345
Jim Grosbache5165492009-11-09 00:11:35 +00002346 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002347}
2348
Stuart Hastingsc7315872011-04-20 16:47:52 +00002349void
2350ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2351 unsigned &VARegSize, unsigned &VARegSaveSize)
2352 const {
2353 unsigned NumGPRs;
2354 if (CCInfo.isFirstByValRegValid())
2355 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2356 else {
2357 unsigned int firstUnalloced;
2358 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2359 sizeof(GPRArgRegs) /
2360 sizeof(GPRArgRegs[0]));
2361 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2362 }
2363
2364 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2365 VARegSize = NumGPRs * 4;
2366 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2367}
2368
2369// The remaining GPRs hold either the beginning of variable-argument
2370// data, or the beginning of an aggregate passed by value (usuall
2371// byval). Either way, we allocate stack slots adjacent to the data
2372// provided by our caller, and store the unallocated registers there.
2373// If this is a variadic function, the va_list pointer will begin with
2374// these values; otherwise, this reassembles a (byval) structure that
2375// was split between registers and memory.
2376void
2377ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2378 DebugLoc dl, SDValue &Chain,
2379 unsigned ArgOffset) const {
2380 MachineFunction &MF = DAG.getMachineFunction();
2381 MachineFrameInfo *MFI = MF.getFrameInfo();
2382 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2383 unsigned firstRegToSaveIndex;
2384 if (CCInfo.isFirstByValRegValid())
2385 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2386 else {
2387 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2388 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2389 }
2390
2391 unsigned VARegSize, VARegSaveSize;
2392 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2393 if (VARegSaveSize) {
2394 // If this function is vararg, store any remaining integer argument regs
2395 // to their spots on the stack so that they may be loaded by deferencing
2396 // the result of va_next.
2397 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002398 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2399 ArgOffset + VARegSaveSize
2400 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002401 false));
2402 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2403 getPointerTy());
2404
2405 SmallVector<SDValue, 4> MemOps;
2406 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2407 TargetRegisterClass *RC;
2408 if (AFI->isThumb1OnlyFunction())
2409 RC = ARM::tGPRRegisterClass;
2410 else
2411 RC = ARM::GPRRegisterClass;
2412
2413 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2414 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2415 SDValue Store =
2416 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002417 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002418 false, false, 0);
2419 MemOps.push_back(Store);
2420 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2421 DAG.getConstant(4, getPointerTy()));
2422 }
2423 if (!MemOps.empty())
2424 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2425 &MemOps[0], MemOps.size());
2426 } else
2427 // This will point to the next argument passed via stack.
2428 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2429}
2430
Bob Wilson5bafff32009-06-22 23:27:02 +00002431SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002432ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002433 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002434 const SmallVectorImpl<ISD::InputArg>
2435 &Ins,
2436 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002437 SmallVectorImpl<SDValue> &InVals)
2438 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002439 MachineFunction &MF = DAG.getMachineFunction();
2440 MachineFrameInfo *MFI = MF.getFrameInfo();
2441
Bob Wilson1f595bb2009-04-17 19:07:39 +00002442 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2443
2444 // Assign locations to all of the incoming arguments.
2445 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002446 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2447 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002448 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002449 CCAssignFnForNode(CallConv, /* Return*/ false,
2450 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002451
2452 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002453 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002454
Stuart Hastingsf222e592011-02-28 17:17:53 +00002455 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002456 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2457 CCValAssign &VA = ArgLocs[i];
2458
Bob Wilsondee46d72009-04-17 20:35:10 +00002459 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002460 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002461 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002462
Bob Wilson1f595bb2009-04-17 19:07:39 +00002463 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002464 // f64 and vector types are split up into multiple registers or
2465 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002466 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002467 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002468 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002469 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002470 SDValue ArgValue2;
2471 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002472 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002473 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2474 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002475 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002476 false, false, 0);
2477 } else {
2478 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2479 Chain, DAG, dl);
2480 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002481 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2482 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002483 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002484 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002485 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2486 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002487 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002488
Bob Wilson5bafff32009-06-22 23:27:02 +00002489 } else {
2490 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002491
Owen Anderson825b72b2009-08-11 20:47:22 +00002492 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002493 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002494 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002495 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002496 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002497 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002498 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002499 RC = (AFI->isThumb1OnlyFunction() ?
2500 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002501 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002502 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002503
2504 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002505 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002506 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002507 }
2508
2509 // If this is an 8 or 16-bit value, it is really passed promoted
2510 // to 32 bits. Insert an assert[sz]ext to capture this, then
2511 // truncate to the right size.
2512 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002513 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002514 case CCValAssign::Full: break;
2515 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002516 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002517 break;
2518 case CCValAssign::SExt:
2519 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2520 DAG.getValueType(VA.getValVT()));
2521 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2522 break;
2523 case CCValAssign::ZExt:
2524 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2525 DAG.getValueType(VA.getValVT()));
2526 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2527 break;
2528 }
2529
Dan Gohman98ca4f22009-08-05 01:29:28 +00002530 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002531
2532 } else { // VA.isRegLoc()
2533
2534 // sanity check
2535 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002536 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002537
Stuart Hastingsf222e592011-02-28 17:17:53 +00002538 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002539
Stuart Hastingsf222e592011-02-28 17:17:53 +00002540 // Some Ins[] entries become multiple ArgLoc[] entries.
2541 // Process them only once.
2542 if (index != lastInsIndex)
2543 {
2544 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002545 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002546 // This can be changed with more analysis.
2547 // In case of tail call optimization mark all arguments mutable.
2548 // Since they could be overwritten by lowering of arguments in case of
2549 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002550 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002551 unsigned VARegSize, VARegSaveSize;
2552 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2553 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2554 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002555 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002556 int FI = MFI->CreateFixedObject(Bytes,
2557 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002558 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2559 } else {
2560 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2561 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002562
Stuart Hastingsf222e592011-02-28 17:17:53 +00002563 // Create load nodes to retrieve arguments from the stack.
2564 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2565 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2566 MachinePointerInfo::getFixedStack(FI),
2567 false, false, 0));
2568 }
2569 lastInsIndex = index;
2570 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002571 }
2572 }
2573
2574 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002575 if (isVarArg)
2576 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002577
Dan Gohman98ca4f22009-08-05 01:29:28 +00002578 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002579}
2580
2581/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002582static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002583 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002584 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002585 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002586 // Maybe this has already been legalized into the constant pool?
2587 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002588 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002589 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002590 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002591 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002592 }
2593 }
2594 return false;
2595}
2596
Evan Chenga8e29892007-01-19 07:51:42 +00002597/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2598/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002599SDValue
2600ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002601 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002602 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002603 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002604 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002605 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002606 // Constant does not fit, try adjusting it by one?
2607 switch (CC) {
2608 default: break;
2609 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002610 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002611 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002612 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002613 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002614 }
2615 break;
2616 case ISD::SETULT:
2617 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002618 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002619 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002620 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002621 }
2622 break;
2623 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002624 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002625 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002626 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002627 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002628 }
2629 break;
2630 case ISD::SETULE:
2631 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002632 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002633 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002634 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002635 }
2636 break;
2637 }
2638 }
2639 }
2640
2641 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002642 ARMISD::NodeType CompareType;
2643 switch (CondCode) {
2644 default:
2645 CompareType = ARMISD::CMP;
2646 break;
2647 case ARMCC::EQ:
2648 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002649 // Uses only Z Flag
2650 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002651 break;
2652 }
Evan Cheng218977b2010-07-13 19:27:42 +00002653 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002654 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002655}
2656
2657/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002658SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002659ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002660 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002661 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002662 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002663 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002664 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002665 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2666 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002667}
2668
Bob Wilson79f56c92011-03-08 01:17:20 +00002669/// duplicateCmp - Glue values can have only one use, so this function
2670/// duplicates a comparison node.
2671SDValue
2672ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2673 unsigned Opc = Cmp.getOpcode();
2674 DebugLoc DL = Cmp.getDebugLoc();
2675 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2676 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2677
2678 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2679 Cmp = Cmp.getOperand(0);
2680 Opc = Cmp.getOpcode();
2681 if (Opc == ARMISD::CMPFP)
2682 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2683 else {
2684 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2685 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2686 }
2687 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2688}
2689
Bill Wendlingde2b1512010-08-11 08:43:16 +00002690SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2691 SDValue Cond = Op.getOperand(0);
2692 SDValue SelectTrue = Op.getOperand(1);
2693 SDValue SelectFalse = Op.getOperand(2);
2694 DebugLoc dl = Op.getDebugLoc();
2695
2696 // Convert:
2697 //
2698 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2699 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2700 //
2701 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2702 const ConstantSDNode *CMOVTrue =
2703 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2704 const ConstantSDNode *CMOVFalse =
2705 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2706
2707 if (CMOVTrue && CMOVFalse) {
2708 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2709 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2710
2711 SDValue True;
2712 SDValue False;
2713 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2714 True = SelectTrue;
2715 False = SelectFalse;
2716 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2717 True = SelectFalse;
2718 False = SelectTrue;
2719 }
2720
2721 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002722 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002723 SDValue ARMcc = Cond.getOperand(2);
2724 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002725 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002726 assert(True.getValueType() == VT);
2727 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002728 }
2729 }
2730 }
2731
2732 return DAG.getSelectCC(dl, Cond,
2733 DAG.getConstant(0, Cond.getValueType()),
2734 SelectTrue, SelectFalse, ISD::SETNE);
2735}
2736
Dan Gohmand858e902010-04-17 15:26:15 +00002737SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002738 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002739 SDValue LHS = Op.getOperand(0);
2740 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002741 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002742 SDValue TrueVal = Op.getOperand(2);
2743 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002744 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002745
Owen Anderson825b72b2009-08-11 20:47:22 +00002746 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002747 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002748 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002749 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Evan Chenge721f5c2011-07-13 00:42:17 +00002750 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002751 }
2752
2753 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002754 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002755
Evan Cheng218977b2010-07-13 19:27:42 +00002756 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2757 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002758 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002759 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002760 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002761 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002762 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002763 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002764 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002765 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002766 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002767 }
2768 return Result;
2769}
2770
Evan Cheng218977b2010-07-13 19:27:42 +00002771/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2772/// to morph to an integer compare sequence.
2773static bool canChangeToInt(SDValue Op, bool &SeenZero,
2774 const ARMSubtarget *Subtarget) {
2775 SDNode *N = Op.getNode();
2776 if (!N->hasOneUse())
2777 // Otherwise it requires moving the value from fp to integer registers.
2778 return false;
2779 if (!N->getNumValues())
2780 return false;
2781 EVT VT = Op.getValueType();
2782 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2783 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2784 // vmrs are very slow, e.g. cortex-a8.
2785 return false;
2786
2787 if (isFloatingPointZero(Op)) {
2788 SeenZero = true;
2789 return true;
2790 }
2791 return ISD::isNormalLoad(N);
2792}
2793
2794static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2795 if (isFloatingPointZero(Op))
2796 return DAG.getConstant(0, MVT::i32);
2797
2798 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2799 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002800 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002801 Ld->isVolatile(), Ld->isNonTemporal(),
2802 Ld->getAlignment());
2803
2804 llvm_unreachable("Unknown VFP cmp argument!");
2805}
2806
2807static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2808 SDValue &RetVal1, SDValue &RetVal2) {
2809 if (isFloatingPointZero(Op)) {
2810 RetVal1 = DAG.getConstant(0, MVT::i32);
2811 RetVal2 = DAG.getConstant(0, MVT::i32);
2812 return;
2813 }
2814
2815 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2816 SDValue Ptr = Ld->getBasePtr();
2817 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2818 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002819 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002820 Ld->isVolatile(), Ld->isNonTemporal(),
2821 Ld->getAlignment());
2822
2823 EVT PtrType = Ptr.getValueType();
2824 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2825 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2826 PtrType, Ptr, DAG.getConstant(4, PtrType));
2827 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2828 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002829 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002830 Ld->isVolatile(), Ld->isNonTemporal(),
2831 NewAlign);
2832 return;
2833 }
2834
2835 llvm_unreachable("Unknown VFP cmp argument!");
2836}
2837
2838/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2839/// f32 and even f64 comparisons to integer ones.
2840SDValue
2841ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2842 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002843 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002844 SDValue LHS = Op.getOperand(2);
2845 SDValue RHS = Op.getOperand(3);
2846 SDValue Dest = Op.getOperand(4);
2847 DebugLoc dl = Op.getDebugLoc();
2848
2849 bool SeenZero = false;
2850 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2851 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002852 // If one of the operand is zero, it's safe to ignore the NaN case since
2853 // we only care about equality comparisons.
2854 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002855 // If unsafe fp math optimization is enabled and there are no other uses of
2856 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002857 // to an integer comparison.
2858 if (CC == ISD::SETOEQ)
2859 CC = ISD::SETEQ;
2860 else if (CC == ISD::SETUNE)
2861 CC = ISD::SETNE;
2862
2863 SDValue ARMcc;
2864 if (LHS.getValueType() == MVT::f32) {
2865 LHS = bitcastf32Toi32(LHS, DAG);
2866 RHS = bitcastf32Toi32(RHS, DAG);
2867 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2868 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2869 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2870 Chain, Dest, ARMcc, CCR, Cmp);
2871 }
2872
2873 SDValue LHS1, LHS2;
2874 SDValue RHS1, RHS2;
2875 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2876 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2877 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2878 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002879 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002880 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2881 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2882 }
2883
2884 return SDValue();
2885}
2886
2887SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2888 SDValue Chain = Op.getOperand(0);
2889 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2890 SDValue LHS = Op.getOperand(2);
2891 SDValue RHS = Op.getOperand(3);
2892 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002893 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002894
Owen Anderson825b72b2009-08-11 20:47:22 +00002895 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002896 SDValue ARMcc;
2897 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002899 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002900 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002901 }
2902
Owen Anderson825b72b2009-08-11 20:47:22 +00002903 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002904
2905 if (UnsafeFPMath &&
2906 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2907 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2908 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2909 if (Result.getNode())
2910 return Result;
2911 }
2912
Evan Chenga8e29892007-01-19 07:51:42 +00002913 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002914 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002915
Evan Cheng218977b2010-07-13 19:27:42 +00002916 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2917 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002918 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002919 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002920 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002921 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002922 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002923 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2924 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002925 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002926 }
2927 return Res;
2928}
2929
Dan Gohmand858e902010-04-17 15:26:15 +00002930SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002931 SDValue Chain = Op.getOperand(0);
2932 SDValue Table = Op.getOperand(1);
2933 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002934 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002935
Owen Andersone50ed302009-08-10 22:56:29 +00002936 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002937 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2938 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002939 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002940 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002941 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002942 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2943 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002944 if (Subtarget->isThumb2()) {
2945 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2946 // which does another jump to the destination. This also makes it easier
2947 // to translate it to TBB / TBH later.
2948 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002950 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002951 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002952 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002953 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002954 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002955 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002956 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002957 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002959 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002960 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002961 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002962 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002963 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002964 }
Evan Chenga8e29892007-01-19 07:51:42 +00002965}
2966
Bob Wilson76a312b2010-03-19 22:51:32 +00002967static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2968 DebugLoc dl = Op.getDebugLoc();
2969 unsigned Opc;
2970
2971 switch (Op.getOpcode()) {
2972 default:
2973 assert(0 && "Invalid opcode!");
2974 case ISD::FP_TO_SINT:
2975 Opc = ARMISD::FTOSI;
2976 break;
2977 case ISD::FP_TO_UINT:
2978 Opc = ARMISD::FTOUI;
2979 break;
2980 }
2981 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002982 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002983}
2984
Cameron Zwarich3007d332011-03-29 21:41:55 +00002985static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2986 EVT VT = Op.getValueType();
2987 DebugLoc dl = Op.getDebugLoc();
2988
Duncan Sands1f6a3292011-08-12 14:54:45 +00002989 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
2990 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00002991 if (VT != MVT::v4f32)
2992 return DAG.UnrollVectorOp(Op.getNode());
2993
2994 unsigned CastOpc;
2995 unsigned Opc;
2996 switch (Op.getOpcode()) {
2997 default:
2998 assert(0 && "Invalid opcode!");
2999 case ISD::SINT_TO_FP:
3000 CastOpc = ISD::SIGN_EXTEND;
3001 Opc = ISD::SINT_TO_FP;
3002 break;
3003 case ISD::UINT_TO_FP:
3004 CastOpc = ISD::ZERO_EXTEND;
3005 Opc = ISD::UINT_TO_FP;
3006 break;
3007 }
3008
3009 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3010 return DAG.getNode(Opc, dl, VT, Op);
3011}
3012
Bob Wilson76a312b2010-03-19 22:51:32 +00003013static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3014 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003015 if (VT.isVector())
3016 return LowerVectorINT_TO_FP(Op, DAG);
3017
Bob Wilson76a312b2010-03-19 22:51:32 +00003018 DebugLoc dl = Op.getDebugLoc();
3019 unsigned Opc;
3020
3021 switch (Op.getOpcode()) {
3022 default:
3023 assert(0 && "Invalid opcode!");
3024 case ISD::SINT_TO_FP:
3025 Opc = ARMISD::SITOF;
3026 break;
3027 case ISD::UINT_TO_FP:
3028 Opc = ARMISD::UITOF;
3029 break;
3030 }
3031
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003032 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003033 return DAG.getNode(Opc, dl, VT, Op);
3034}
3035
Evan Cheng515fe3a2010-07-08 02:08:50 +00003036SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003037 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003038 SDValue Tmp0 = Op.getOperand(0);
3039 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003040 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003041 EVT VT = Op.getValueType();
3042 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003043 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3044 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3045 bool UseNEON = !InGPR && Subtarget->hasNEON();
3046
3047 if (UseNEON) {
3048 // Use VBSL to copy the sign bit.
3049 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3050 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3051 DAG.getTargetConstant(EncodedVal, MVT::i32));
3052 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3053 if (VT == MVT::f64)
3054 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3055 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3056 DAG.getConstant(32, MVT::i32));
3057 else /*if (VT == MVT::f32)*/
3058 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3059 if (SrcVT == MVT::f32) {
3060 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3061 if (VT == MVT::f64)
3062 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3063 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3064 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003065 } else if (VT == MVT::f32)
3066 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3067 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3068 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003069 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3070 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3071
3072 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3073 MVT::i32);
3074 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3075 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3076 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003077
Evan Chenge573fb32011-02-23 02:24:55 +00003078 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3079 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3080 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003081 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003082 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3083 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3084 DAG.getConstant(0, MVT::i32));
3085 } else {
3086 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3087 }
3088
3089 return Res;
3090 }
Evan Chengc143dd42011-02-11 02:28:55 +00003091
3092 // Bitcast operand 1 to i32.
3093 if (SrcVT == MVT::f64)
3094 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3095 &Tmp1, 1).getValue(1);
3096 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3097
Evan Chenge573fb32011-02-23 02:24:55 +00003098 // Or in the signbit with integer operations.
3099 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3100 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3101 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3102 if (VT == MVT::f32) {
3103 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3104 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3105 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3106 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003107 }
3108
Evan Chenge573fb32011-02-23 02:24:55 +00003109 // f64: Or the high part with signbit and then combine two parts.
3110 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3111 &Tmp0, 1);
3112 SDValue Lo = Tmp0.getValue(0);
3113 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3114 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3115 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003116}
3117
Evan Cheng2457f2c2010-05-22 01:47:14 +00003118SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3119 MachineFunction &MF = DAG.getMachineFunction();
3120 MachineFrameInfo *MFI = MF.getFrameInfo();
3121 MFI->setReturnAddressIsTaken(true);
3122
3123 EVT VT = Op.getValueType();
3124 DebugLoc dl = Op.getDebugLoc();
3125 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3126 if (Depth) {
3127 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3128 SDValue Offset = DAG.getConstant(4, MVT::i32);
3129 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3130 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003131 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003132 }
3133
3134 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003135 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003136 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3137}
3138
Dan Gohmand858e902010-04-17 15:26:15 +00003139SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003140 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3141 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003142
Owen Andersone50ed302009-08-10 22:56:29 +00003143 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003144 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3145 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003146 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003147 ? ARM::R7 : ARM::R11;
3148 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3149 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003150 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3151 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003152 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003153 return FrameAddr;
3154}
3155
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003156/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003157/// expand a bit convert where either the source or destination type is i64 to
3158/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3159/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3160/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003161static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003162 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3163 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003164 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003165
Bob Wilson9f3f0612010-04-17 05:30:19 +00003166 // This function is only supposed to be called for i64 types, either as the
3167 // source or destination of the bit convert.
3168 EVT SrcVT = Op.getValueType();
3169 EVT DstVT = N->getValueType(0);
3170 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003171 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003172
Bob Wilson9f3f0612010-04-17 05:30:19 +00003173 // Turn i64->f64 into VMOVDRR.
3174 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003175 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3176 DAG.getConstant(0, MVT::i32));
3177 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3178 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003179 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003180 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003181 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003182
Jim Grosbache5165492009-11-09 00:11:35 +00003183 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003184 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3185 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3186 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3187 // Merge the pieces into a single i64 value.
3188 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3189 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003190
Bob Wilson9f3f0612010-04-17 05:30:19 +00003191 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003192}
3193
Bob Wilson5bafff32009-06-22 23:27:02 +00003194/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003195/// Zero vectors are used to represent vector negation and in those cases
3196/// will be implemented with the NEON VNEG instruction. However, VNEG does
3197/// not support i64 elements, so sometimes the zero vectors will need to be
3198/// explicitly constructed. Regardless, use a canonical VMOV to create the
3199/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003200static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003201 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003202 // The canonical modified immediate encoding of a zero vector is....0!
3203 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3204 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3205 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003206 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003207}
3208
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003209/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3210/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003211SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3212 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003213 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3214 EVT VT = Op.getValueType();
3215 unsigned VTBits = VT.getSizeInBits();
3216 DebugLoc dl = Op.getDebugLoc();
3217 SDValue ShOpLo = Op.getOperand(0);
3218 SDValue ShOpHi = Op.getOperand(1);
3219 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003220 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003221 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003222
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003223 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3224
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003225 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3226 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3227 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3228 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3229 DAG.getConstant(VTBits, MVT::i32));
3230 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3231 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003232 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003233
3234 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3235 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003236 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003237 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003238 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003239 CCR, Cmp);
3240
3241 SDValue Ops[2] = { Lo, Hi };
3242 return DAG.getMergeValues(Ops, 2, dl);
3243}
3244
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003245/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3246/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003247SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3248 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003249 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3250 EVT VT = Op.getValueType();
3251 unsigned VTBits = VT.getSizeInBits();
3252 DebugLoc dl = Op.getDebugLoc();
3253 SDValue ShOpLo = Op.getOperand(0);
3254 SDValue ShOpHi = Op.getOperand(1);
3255 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003256 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003257
3258 assert(Op.getOpcode() == ISD::SHL_PARTS);
3259 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3260 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3261 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3262 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3263 DAG.getConstant(VTBits, MVT::i32));
3264 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3265 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3266
3267 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3268 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3269 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003270 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003271 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003272 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003273 CCR, Cmp);
3274
3275 SDValue Ops[2] = { Lo, Hi };
3276 return DAG.getMergeValues(Ops, 2, dl);
3277}
3278
Jim Grosbach4725ca72010-09-08 03:54:02 +00003279SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003280 SelectionDAG &DAG) const {
3281 // The rounding mode is in bits 23:22 of the FPSCR.
3282 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3283 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3284 // so that the shift + and get folded into a bitfield extract.
3285 DebugLoc dl = Op.getDebugLoc();
3286 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3287 DAG.getConstant(Intrinsic::arm_get_fpscr,
3288 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003289 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003290 DAG.getConstant(1U << 22, MVT::i32));
3291 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3292 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003293 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003294 DAG.getConstant(3, MVT::i32));
3295}
3296
Jim Grosbach3482c802010-01-18 19:58:49 +00003297static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3298 const ARMSubtarget *ST) {
3299 EVT VT = N->getValueType(0);
3300 DebugLoc dl = N->getDebugLoc();
3301
3302 if (!ST->hasV6T2Ops())
3303 return SDValue();
3304
3305 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3306 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3307}
3308
Bob Wilson5bafff32009-06-22 23:27:02 +00003309static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3310 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003311 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003312 DebugLoc dl = N->getDebugLoc();
3313
Bob Wilsond5448bb2010-11-18 21:16:28 +00003314 if (!VT.isVector())
3315 return SDValue();
3316
Bob Wilson5bafff32009-06-22 23:27:02 +00003317 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003318 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003319
Bob Wilsond5448bb2010-11-18 21:16:28 +00003320 // Left shifts translate directly to the vshiftu intrinsic.
3321 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003322 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003323 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3324 N->getOperand(0), N->getOperand(1));
3325
3326 assert((N->getOpcode() == ISD::SRA ||
3327 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3328
3329 // NEON uses the same intrinsics for both left and right shifts. For
3330 // right shifts, the shift amounts are negative, so negate the vector of
3331 // shift amounts.
3332 EVT ShiftVT = N->getOperand(1).getValueType();
3333 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3334 getZeroVector(ShiftVT, DAG, dl),
3335 N->getOperand(1));
3336 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3337 Intrinsic::arm_neon_vshifts :
3338 Intrinsic::arm_neon_vshiftu);
3339 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3340 DAG.getConstant(vshiftInt, MVT::i32),
3341 N->getOperand(0), NegatedCount);
3342}
3343
3344static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3345 const ARMSubtarget *ST) {
3346 EVT VT = N->getValueType(0);
3347 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003348
Eli Friedmance392eb2009-08-22 03:13:10 +00003349 // We can get here for a node like i32 = ISD::SHL i32, i64
3350 if (VT != MVT::i64)
3351 return SDValue();
3352
3353 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003354 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003355
Chris Lattner27a6c732007-11-24 07:07:01 +00003356 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3357 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003358 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003359 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003360
Chris Lattner27a6c732007-11-24 07:07:01 +00003361 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003362 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003363
Chris Lattner27a6c732007-11-24 07:07:01 +00003364 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003366 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003368 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003369
Chris Lattner27a6c732007-11-24 07:07:01 +00003370 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3371 // captures the result into a carry flag.
3372 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003373 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003374
Chris Lattner27a6c732007-11-24 07:07:01 +00003375 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003376 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003377
Chris Lattner27a6c732007-11-24 07:07:01 +00003378 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003379 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003380}
3381
Bob Wilson5bafff32009-06-22 23:27:02 +00003382static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3383 SDValue TmpOp0, TmpOp1;
3384 bool Invert = false;
3385 bool Swap = false;
3386 unsigned Opc = 0;
3387
3388 SDValue Op0 = Op.getOperand(0);
3389 SDValue Op1 = Op.getOperand(1);
3390 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003391 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003392 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3393 DebugLoc dl = Op.getDebugLoc();
3394
3395 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3396 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003397 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003398 case ISD::SETUNE:
3399 case ISD::SETNE: Invert = true; // Fallthrough
3400 case ISD::SETOEQ:
3401 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3402 case ISD::SETOLT:
3403 case ISD::SETLT: Swap = true; // Fallthrough
3404 case ISD::SETOGT:
3405 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3406 case ISD::SETOLE:
3407 case ISD::SETLE: Swap = true; // Fallthrough
3408 case ISD::SETOGE:
3409 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3410 case ISD::SETUGE: Swap = true; // Fallthrough
3411 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3412 case ISD::SETUGT: Swap = true; // Fallthrough
3413 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3414 case ISD::SETUEQ: Invert = true; // Fallthrough
3415 case ISD::SETONE:
3416 // Expand this to (OLT | OGT).
3417 TmpOp0 = Op0;
3418 TmpOp1 = Op1;
3419 Opc = ISD::OR;
3420 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3421 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3422 break;
3423 case ISD::SETUO: Invert = true; // Fallthrough
3424 case ISD::SETO:
3425 // Expand this to (OLT | OGE).
3426 TmpOp0 = Op0;
3427 TmpOp1 = Op1;
3428 Opc = ISD::OR;
3429 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3430 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3431 break;
3432 }
3433 } else {
3434 // Integer comparisons.
3435 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003436 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003437 case ISD::SETNE: Invert = true;
3438 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3439 case ISD::SETLT: Swap = true;
3440 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3441 case ISD::SETLE: Swap = true;
3442 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3443 case ISD::SETULT: Swap = true;
3444 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3445 case ISD::SETULE: Swap = true;
3446 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3447 }
3448
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003449 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003450 if (Opc == ARMISD::VCEQ) {
3451
3452 SDValue AndOp;
3453 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3454 AndOp = Op0;
3455 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3456 AndOp = Op1;
3457
3458 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003459 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003460 AndOp = AndOp.getOperand(0);
3461
3462 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3463 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003464 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3465 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003466 Invert = !Invert;
3467 }
3468 }
3469 }
3470
3471 if (Swap)
3472 std::swap(Op0, Op1);
3473
Owen Andersonc24cb352010-11-08 23:21:22 +00003474 // If one of the operands is a constant vector zero, attempt to fold the
3475 // comparison to a specialized compare-against-zero form.
3476 SDValue SingleOp;
3477 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3478 SingleOp = Op0;
3479 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3480 if (Opc == ARMISD::VCGE)
3481 Opc = ARMISD::VCLEZ;
3482 else if (Opc == ARMISD::VCGT)
3483 Opc = ARMISD::VCLTZ;
3484 SingleOp = Op1;
3485 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003486
Owen Andersonc24cb352010-11-08 23:21:22 +00003487 SDValue Result;
3488 if (SingleOp.getNode()) {
3489 switch (Opc) {
3490 case ARMISD::VCEQ:
3491 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3492 case ARMISD::VCGE:
3493 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3494 case ARMISD::VCLEZ:
3495 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3496 case ARMISD::VCGT:
3497 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3498 case ARMISD::VCLTZ:
3499 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3500 default:
3501 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3502 }
3503 } else {
3504 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3505 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003506
3507 if (Invert)
3508 Result = DAG.getNOT(dl, Result, VT);
3509
3510 return Result;
3511}
3512
Bob Wilsond3c42842010-06-14 22:19:57 +00003513/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3514/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003515/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003516static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3517 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003518 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003519 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003520
Bob Wilson827b2102010-06-15 19:05:35 +00003521 // SplatBitSize is set to the smallest size that splats the vector, so a
3522 // zero vector will always have SplatBitSize == 8. However, NEON modified
3523 // immediate instructions others than VMOV do not support the 8-bit encoding
3524 // of a zero vector, and the default encoding of zero is supposed to be the
3525 // 32-bit version.
3526 if (SplatBits == 0)
3527 SplatBitSize = 32;
3528
Bob Wilson5bafff32009-06-22 23:27:02 +00003529 switch (SplatBitSize) {
3530 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003531 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003532 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003533 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003534 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003535 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003536 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003537 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003538 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003539
3540 case 16:
3541 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003542 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003543 if ((SplatBits & ~0xff) == 0) {
3544 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003545 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003546 Imm = SplatBits;
3547 break;
3548 }
3549 if ((SplatBits & ~0xff00) == 0) {
3550 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003551 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003552 Imm = SplatBits >> 8;
3553 break;
3554 }
3555 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003556
3557 case 32:
3558 // NEON's 32-bit VMOV supports splat values where:
3559 // * only one byte is nonzero, or
3560 // * the least significant byte is 0xff and the second byte is nonzero, or
3561 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003562 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003563 if ((SplatBits & ~0xff) == 0) {
3564 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003565 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003566 Imm = SplatBits;
3567 break;
3568 }
3569 if ((SplatBits & ~0xff00) == 0) {
3570 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003571 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003572 Imm = SplatBits >> 8;
3573 break;
3574 }
3575 if ((SplatBits & ~0xff0000) == 0) {
3576 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003577 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003578 Imm = SplatBits >> 16;
3579 break;
3580 }
3581 if ((SplatBits & ~0xff000000) == 0) {
3582 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003583 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003584 Imm = SplatBits >> 24;
3585 break;
3586 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003587
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003588 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3589 if (type == OtherModImm) return SDValue();
3590
Bob Wilson5bafff32009-06-22 23:27:02 +00003591 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003592 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3593 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003594 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003595 Imm = SplatBits >> 8;
3596 SplatBits |= 0xff;
3597 break;
3598 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003599
3600 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003601 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3602 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003603 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003604 Imm = SplatBits >> 16;
3605 SplatBits |= 0xffff;
3606 break;
3607 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003608
3609 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3610 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3611 // VMOV.I32. A (very) minor optimization would be to replicate the value
3612 // and fall through here to test for a valid 64-bit splat. But, then the
3613 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003614 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003615
3616 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003617 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003618 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003619 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003620 uint64_t BitMask = 0xff;
3621 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003622 unsigned ImmMask = 1;
3623 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003624 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003625 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003626 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003627 Imm |= ImmMask;
3628 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003629 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003630 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003631 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003632 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003633 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003634 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003635 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003636 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003637 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003638 break;
3639 }
3640
Bob Wilson1a913ed2010-06-11 21:34:50 +00003641 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003642 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003643 return SDValue();
3644 }
3645
Bob Wilsoncba270d2010-07-13 21:16:48 +00003646 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3647 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003648}
3649
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003650static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3651 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003652 unsigned NumElts = VT.getVectorNumElements();
3653 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003654
3655 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3656 if (M[0] < 0)
3657 return false;
3658
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003659 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003660
3661 // If this is a VEXT shuffle, the immediate value is the index of the first
3662 // element. The other shuffle indices must be the successive elements after
3663 // the first one.
3664 unsigned ExpectedElt = Imm;
3665 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003666 // Increment the expected index. If it wraps around, it may still be
3667 // a VEXT but the source vectors must be swapped.
3668 ExpectedElt += 1;
3669 if (ExpectedElt == NumElts * 2) {
3670 ExpectedElt = 0;
3671 ReverseVEXT = true;
3672 }
3673
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003674 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003675 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003676 return false;
3677 }
3678
3679 // Adjust the index value if the source operands will be swapped.
3680 if (ReverseVEXT)
3681 Imm -= NumElts;
3682
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003683 return true;
3684}
3685
Bob Wilson8bb9e482009-07-26 00:39:34 +00003686/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3687/// instruction with the specified blocksize. (The order of the elements
3688/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003689static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3690 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003691 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3692 "Only possible block sizes for VREV are: 16, 32, 64");
3693
Bob Wilson8bb9e482009-07-26 00:39:34 +00003694 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003695 if (EltSz == 64)
3696 return false;
3697
3698 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003699 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003700 // If the first shuffle index is UNDEF, be optimistic.
3701 if (M[0] < 0)
3702 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003703
3704 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3705 return false;
3706
3707 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003708 if (M[i] < 0) continue; // ignore UNDEF indices
3709 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003710 return false;
3711 }
3712
3713 return true;
3714}
3715
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003716static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3717 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3718 // range, then 0 is placed into the resulting vector. So pretty much any mask
3719 // of 8 elements can work here.
3720 return VT == MVT::v8i8 && M.size() == 8;
3721}
3722
Bob Wilsonc692cb72009-08-21 20:54:19 +00003723static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3724 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003725 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3726 if (EltSz == 64)
3727 return false;
3728
Bob Wilsonc692cb72009-08-21 20:54:19 +00003729 unsigned NumElts = VT.getVectorNumElements();
3730 WhichResult = (M[0] == 0 ? 0 : 1);
3731 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003732 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3733 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003734 return false;
3735 }
3736 return true;
3737}
3738
Bob Wilson324f4f12009-12-03 06:40:55 +00003739/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3740/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3741/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3742static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3743 unsigned &WhichResult) {
3744 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3745 if (EltSz == 64)
3746 return false;
3747
3748 unsigned NumElts = VT.getVectorNumElements();
3749 WhichResult = (M[0] == 0 ? 0 : 1);
3750 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003751 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3752 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003753 return false;
3754 }
3755 return true;
3756}
3757
Bob Wilsonc692cb72009-08-21 20:54:19 +00003758static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3759 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003760 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3761 if (EltSz == 64)
3762 return false;
3763
Bob Wilsonc692cb72009-08-21 20:54:19 +00003764 unsigned NumElts = VT.getVectorNumElements();
3765 WhichResult = (M[0] == 0 ? 0 : 1);
3766 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003767 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003768 if ((unsigned) M[i] != 2 * i + WhichResult)
3769 return false;
3770 }
3771
3772 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003773 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003774 return false;
3775
3776 return true;
3777}
3778
Bob Wilson324f4f12009-12-03 06:40:55 +00003779/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3780/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3781/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3782static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3783 unsigned &WhichResult) {
3784 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3785 if (EltSz == 64)
3786 return false;
3787
3788 unsigned Half = VT.getVectorNumElements() / 2;
3789 WhichResult = (M[0] == 0 ? 0 : 1);
3790 for (unsigned j = 0; j != 2; ++j) {
3791 unsigned Idx = WhichResult;
3792 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003793 int MIdx = M[i + j * Half];
3794 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003795 return false;
3796 Idx += 2;
3797 }
3798 }
3799
3800 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3801 if (VT.is64BitVector() && EltSz == 32)
3802 return false;
3803
3804 return true;
3805}
3806
Bob Wilsonc692cb72009-08-21 20:54:19 +00003807static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3808 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003809 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3810 if (EltSz == 64)
3811 return false;
3812
Bob Wilsonc692cb72009-08-21 20:54:19 +00003813 unsigned NumElts = VT.getVectorNumElements();
3814 WhichResult = (M[0] == 0 ? 0 : 1);
3815 unsigned Idx = WhichResult * NumElts / 2;
3816 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003817 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3818 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003819 return false;
3820 Idx += 1;
3821 }
3822
3823 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003824 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003825 return false;
3826
3827 return true;
3828}
3829
Bob Wilson324f4f12009-12-03 06:40:55 +00003830/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3831/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3832/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3833static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3834 unsigned &WhichResult) {
3835 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3836 if (EltSz == 64)
3837 return false;
3838
3839 unsigned NumElts = VT.getVectorNumElements();
3840 WhichResult = (M[0] == 0 ? 0 : 1);
3841 unsigned Idx = WhichResult * NumElts / 2;
3842 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003843 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3844 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003845 return false;
3846 Idx += 1;
3847 }
3848
3849 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3850 if (VT.is64BitVector() && EltSz == 32)
3851 return false;
3852
3853 return true;
3854}
3855
Dale Johannesenf630c712010-07-29 20:10:08 +00003856// If N is an integer constant that can be moved into a register in one
3857// instruction, return an SDValue of such a constant (will become a MOV
3858// instruction). Otherwise return null.
3859static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3860 const ARMSubtarget *ST, DebugLoc dl) {
3861 uint64_t Val;
3862 if (!isa<ConstantSDNode>(N))
3863 return SDValue();
3864 Val = cast<ConstantSDNode>(N)->getZExtValue();
3865
3866 if (ST->isThumb1Only()) {
3867 if (Val <= 255 || ~Val <= 255)
3868 return DAG.getConstant(Val, MVT::i32);
3869 } else {
3870 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3871 return DAG.getConstant(Val, MVT::i32);
3872 }
3873 return SDValue();
3874}
3875
Bob Wilson5bafff32009-06-22 23:27:02 +00003876// If this is a case we can't handle, return null and let the default
3877// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003878SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3879 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003880 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003881 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003882 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003883
3884 APInt SplatBits, SplatUndef;
3885 unsigned SplatBitSize;
3886 bool HasAnyUndefs;
3887 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003888 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003889 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003890 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003891 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003892 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003893 DAG, VmovVT, VT.is128BitVector(),
3894 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003895 if (Val.getNode()) {
3896 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003897 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003898 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003899
3900 // Try an immediate VMVN.
3901 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3902 ((1LL << SplatBitSize) - 1));
3903 Val = isNEONModifiedImm(NegatedImm,
3904 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003905 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003906 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003907 if (Val.getNode()) {
3908 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003909 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003910 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003911 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003912 }
3913
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003914 // Scan through the operands to see if only one value is used.
3915 unsigned NumElts = VT.getVectorNumElements();
3916 bool isOnlyLowElement = true;
3917 bool usesOnlyOneValue = true;
3918 bool isConstant = true;
3919 SDValue Value;
3920 for (unsigned i = 0; i < NumElts; ++i) {
3921 SDValue V = Op.getOperand(i);
3922 if (V.getOpcode() == ISD::UNDEF)
3923 continue;
3924 if (i > 0)
3925 isOnlyLowElement = false;
3926 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3927 isConstant = false;
3928
3929 if (!Value.getNode())
3930 Value = V;
3931 else if (V != Value)
3932 usesOnlyOneValue = false;
3933 }
3934
3935 if (!Value.getNode())
3936 return DAG.getUNDEF(VT);
3937
3938 if (isOnlyLowElement)
3939 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3940
Dale Johannesenf630c712010-07-29 20:10:08 +00003941 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3942
Dale Johannesen575cd142010-10-19 20:00:17 +00003943 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3944 // i32 and try again.
3945 if (usesOnlyOneValue && EltSize <= 32) {
3946 if (!isConstant)
3947 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3948 if (VT.getVectorElementType().isFloatingPoint()) {
3949 SmallVector<SDValue, 8> Ops;
3950 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003951 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003952 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003953 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3954 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003955 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3956 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003957 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003958 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003959 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3960 if (Val.getNode())
3961 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003962 }
3963
3964 // If all elements are constants and the case above didn't get hit, fall back
3965 // to the default expansion, which will generate a load from the constant
3966 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003967 if (isConstant)
3968 return SDValue();
3969
Bob Wilson11a1dff2011-01-07 21:37:30 +00003970 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3971 if (NumElts >= 4) {
3972 SDValue shuffle = ReconstructShuffle(Op, DAG);
3973 if (shuffle != SDValue())
3974 return shuffle;
3975 }
3976
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003977 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003978 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3979 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003980 if (EltSize >= 32) {
3981 // Do the expansion with floating-point types, since that is what the VFP
3982 // registers are defined to use, and since i64 is not legal.
3983 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3984 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003985 SmallVector<SDValue, 8> Ops;
3986 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003987 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003988 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003989 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003990 }
3991
3992 return SDValue();
3993}
3994
Bob Wilson11a1dff2011-01-07 21:37:30 +00003995// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003996// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003997SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3998 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003999 DebugLoc dl = Op.getDebugLoc();
4000 EVT VT = Op.getValueType();
4001 unsigned NumElts = VT.getVectorNumElements();
4002
4003 SmallVector<SDValue, 2> SourceVecs;
4004 SmallVector<unsigned, 2> MinElts;
4005 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004006
Bob Wilson11a1dff2011-01-07 21:37:30 +00004007 for (unsigned i = 0; i < NumElts; ++i) {
4008 SDValue V = Op.getOperand(i);
4009 if (V.getOpcode() == ISD::UNDEF)
4010 continue;
4011 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4012 // A shuffle can only come from building a vector from various
4013 // elements of other vectors.
4014 return SDValue();
4015 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004016
Bob Wilson11a1dff2011-01-07 21:37:30 +00004017 // Record this extraction against the appropriate vector if possible...
4018 SDValue SourceVec = V.getOperand(0);
4019 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4020 bool FoundSource = false;
4021 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4022 if (SourceVecs[j] == SourceVec) {
4023 if (MinElts[j] > EltNo)
4024 MinElts[j] = EltNo;
4025 if (MaxElts[j] < EltNo)
4026 MaxElts[j] = EltNo;
4027 FoundSource = true;
4028 break;
4029 }
4030 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004031
Bob Wilson11a1dff2011-01-07 21:37:30 +00004032 // Or record a new source if not...
4033 if (!FoundSource) {
4034 SourceVecs.push_back(SourceVec);
4035 MinElts.push_back(EltNo);
4036 MaxElts.push_back(EltNo);
4037 }
4038 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004039
Bob Wilson11a1dff2011-01-07 21:37:30 +00004040 // Currently only do something sane when at most two source vectors
4041 // involved.
4042 if (SourceVecs.size() > 2)
4043 return SDValue();
4044
4045 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4046 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004047
Bob Wilson11a1dff2011-01-07 21:37:30 +00004048 // This loop extracts the usage patterns of the source vectors
4049 // and prepares appropriate SDValues for a shuffle if possible.
4050 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4051 if (SourceVecs[i].getValueType() == VT) {
4052 // No VEXT necessary
4053 ShuffleSrcs[i] = SourceVecs[i];
4054 VEXTOffsets[i] = 0;
4055 continue;
4056 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4057 // It probably isn't worth padding out a smaller vector just to
4058 // break it down again in a shuffle.
4059 return SDValue();
4060 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004061
Bob Wilson11a1dff2011-01-07 21:37:30 +00004062 // Since only 64-bit and 128-bit vectors are legal on ARM and
4063 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004064 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4065 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004066
Bob Wilson11a1dff2011-01-07 21:37:30 +00004067 if (MaxElts[i] - MinElts[i] >= NumElts) {
4068 // Span too large for a VEXT to cope
4069 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004070 }
4071
Bob Wilson11a1dff2011-01-07 21:37:30 +00004072 if (MinElts[i] >= NumElts) {
4073 // The extraction can just take the second half
4074 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004075 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4076 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004077 DAG.getIntPtrConstant(NumElts));
4078 } else if (MaxElts[i] < NumElts) {
4079 // The extraction can just take the first half
4080 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004081 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4082 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004083 DAG.getIntPtrConstant(0));
4084 } else {
4085 // An actual VEXT is needed
4086 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004087 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4088 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004089 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004090 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4091 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004092 DAG.getIntPtrConstant(NumElts));
4093 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4094 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4095 }
4096 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004097
Bob Wilson11a1dff2011-01-07 21:37:30 +00004098 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004099
Bob Wilson11a1dff2011-01-07 21:37:30 +00004100 for (unsigned i = 0; i < NumElts; ++i) {
4101 SDValue Entry = Op.getOperand(i);
4102 if (Entry.getOpcode() == ISD::UNDEF) {
4103 Mask.push_back(-1);
4104 continue;
4105 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004106
Bob Wilson11a1dff2011-01-07 21:37:30 +00004107 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004108 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4109 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004110 if (ExtractVec == SourceVecs[0]) {
4111 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4112 } else {
4113 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4114 }
4115 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004116
Bob Wilson11a1dff2011-01-07 21:37:30 +00004117 // Final check before we try to produce nonsense...
4118 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004119 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4120 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004121
Bob Wilson11a1dff2011-01-07 21:37:30 +00004122 return SDValue();
4123}
4124
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004125/// isShuffleMaskLegal - Targets can use this to indicate that they only
4126/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4127/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4128/// are assumed to be legal.
4129bool
4130ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4131 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004132 if (VT.getVectorNumElements() == 4 &&
4133 (VT.is128BitVector() || VT.is64BitVector())) {
4134 unsigned PFIndexes[4];
4135 for (unsigned i = 0; i != 4; ++i) {
4136 if (M[i] < 0)
4137 PFIndexes[i] = 8;
4138 else
4139 PFIndexes[i] = M[i];
4140 }
4141
4142 // Compute the index in the perfect shuffle table.
4143 unsigned PFTableIndex =
4144 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4145 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4146 unsigned Cost = (PFEntry >> 30);
4147
4148 if (Cost <= 4)
4149 return true;
4150 }
4151
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004152 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004153 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004154
Bob Wilson53dd2452010-06-07 23:53:38 +00004155 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4156 return (EltSize >= 32 ||
4157 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004158 isVREVMask(M, VT, 64) ||
4159 isVREVMask(M, VT, 32) ||
4160 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004161 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004162 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004163 isVTRNMask(M, VT, WhichResult) ||
4164 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004165 isVZIPMask(M, VT, WhichResult) ||
4166 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4167 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4168 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004169}
4170
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004171/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4172/// the specified operations to build the shuffle.
4173static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4174 SDValue RHS, SelectionDAG &DAG,
4175 DebugLoc dl) {
4176 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4177 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4178 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4179
4180 enum {
4181 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4182 OP_VREV,
4183 OP_VDUP0,
4184 OP_VDUP1,
4185 OP_VDUP2,
4186 OP_VDUP3,
4187 OP_VEXT1,
4188 OP_VEXT2,
4189 OP_VEXT3,
4190 OP_VUZPL, // VUZP, left result
4191 OP_VUZPR, // VUZP, right result
4192 OP_VZIPL, // VZIP, left result
4193 OP_VZIPR, // VZIP, right result
4194 OP_VTRNL, // VTRN, left result
4195 OP_VTRNR // VTRN, right result
4196 };
4197
4198 if (OpNum == OP_COPY) {
4199 if (LHSID == (1*9+2)*9+3) return LHS;
4200 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4201 return RHS;
4202 }
4203
4204 SDValue OpLHS, OpRHS;
4205 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4206 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4207 EVT VT = OpLHS.getValueType();
4208
4209 switch (OpNum) {
4210 default: llvm_unreachable("Unknown shuffle opcode!");
4211 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004212 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004213 if (VT.getVectorElementType() == MVT::i32 ||
4214 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004215 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4216 // vrev <4 x i16> -> VREV32
4217 if (VT.getVectorElementType() == MVT::i16)
4218 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4219 // vrev <4 x i8> -> VREV16
4220 assert(VT.getVectorElementType() == MVT::i8);
4221 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004222 case OP_VDUP0:
4223 case OP_VDUP1:
4224 case OP_VDUP2:
4225 case OP_VDUP3:
4226 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004227 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004228 case OP_VEXT1:
4229 case OP_VEXT2:
4230 case OP_VEXT3:
4231 return DAG.getNode(ARMISD::VEXT, dl, VT,
4232 OpLHS, OpRHS,
4233 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4234 case OP_VUZPL:
4235 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004236 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004237 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4238 case OP_VZIPL:
4239 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004240 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004241 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4242 case OP_VTRNL:
4243 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004244 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4245 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004246 }
4247}
4248
Bill Wendling69a05a72011-03-14 23:02:38 +00004249static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4250 SmallVectorImpl<int> &ShuffleMask,
4251 SelectionDAG &DAG) {
4252 // Check to see if we can use the VTBL instruction.
4253 SDValue V1 = Op.getOperand(0);
4254 SDValue V2 = Op.getOperand(1);
4255 DebugLoc DL = Op.getDebugLoc();
4256
4257 SmallVector<SDValue, 8> VTBLMask;
4258 for (SmallVectorImpl<int>::iterator
4259 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4260 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4261
4262 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4263 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4264 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4265 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004266
Owen Anderson76706012011-04-05 21:48:57 +00004267 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004268 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4269 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004270}
4271
Bob Wilson5bafff32009-06-22 23:27:02 +00004272static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004273 SDValue V1 = Op.getOperand(0);
4274 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004275 DebugLoc dl = Op.getDebugLoc();
4276 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004277 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004278 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004279
Bob Wilson28865062009-08-13 02:13:04 +00004280 // Convert shuffles that are directly supported on NEON to target-specific
4281 // DAG nodes, instead of keeping them as shuffles and matching them again
4282 // during code selection. This is more efficient and avoids the possibility
4283 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004284 // FIXME: floating-point vectors should be canonicalized to integer vectors
4285 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004286 SVN->getMask(ShuffleMask);
4287
Bob Wilson53dd2452010-06-07 23:53:38 +00004288 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4289 if (EltSize <= 32) {
4290 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4291 int Lane = SVN->getSplatIndex();
4292 // If this is undef splat, generate it via "just" vdup, if possible.
4293 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004294
Bob Wilson53dd2452010-06-07 23:53:38 +00004295 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4296 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4297 }
4298 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4299 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004300 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004301
4302 bool ReverseVEXT;
4303 unsigned Imm;
4304 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4305 if (ReverseVEXT)
4306 std::swap(V1, V2);
4307 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4308 DAG.getConstant(Imm, MVT::i32));
4309 }
4310
4311 if (isVREVMask(ShuffleMask, VT, 64))
4312 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4313 if (isVREVMask(ShuffleMask, VT, 32))
4314 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4315 if (isVREVMask(ShuffleMask, VT, 16))
4316 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4317
4318 // Check for Neon shuffles that modify both input vectors in place.
4319 // If both results are used, i.e., if there are two shuffles with the same
4320 // source operands and with masks corresponding to both results of one of
4321 // these operations, DAG memoization will ensure that a single node is
4322 // used for both shuffles.
4323 unsigned WhichResult;
4324 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4325 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4326 V1, V2).getValue(WhichResult);
4327 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4328 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4329 V1, V2).getValue(WhichResult);
4330 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4331 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4332 V1, V2).getValue(WhichResult);
4333
4334 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4335 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4336 V1, V1).getValue(WhichResult);
4337 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4338 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4339 V1, V1).getValue(WhichResult);
4340 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4341 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4342 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004343 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004344
Bob Wilsonc692cb72009-08-21 20:54:19 +00004345 // If the shuffle is not directly supported and it has 4 elements, use
4346 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004347 unsigned NumElts = VT.getVectorNumElements();
4348 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004349 unsigned PFIndexes[4];
4350 for (unsigned i = 0; i != 4; ++i) {
4351 if (ShuffleMask[i] < 0)
4352 PFIndexes[i] = 8;
4353 else
4354 PFIndexes[i] = ShuffleMask[i];
4355 }
4356
4357 // Compute the index in the perfect shuffle table.
4358 unsigned PFTableIndex =
4359 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004360 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4361 unsigned Cost = (PFEntry >> 30);
4362
4363 if (Cost <= 4)
4364 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4365 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004366
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004367 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004368 if (EltSize >= 32) {
4369 // Do the expansion with floating-point types, since that is what the VFP
4370 // registers are defined to use, and since i64 is not legal.
4371 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4372 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004373 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4374 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004375 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004376 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004377 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004378 Ops.push_back(DAG.getUNDEF(EltVT));
4379 else
4380 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4381 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4382 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4383 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004384 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004385 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004386 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004387 }
4388
Bill Wendling69a05a72011-03-14 23:02:38 +00004389 if (VT == MVT::v8i8) {
4390 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4391 if (NewOp.getNode())
4392 return NewOp;
4393 }
4394
Bob Wilson22cac0d2009-08-14 05:16:33 +00004395 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004396}
4397
Bob Wilson5bafff32009-06-22 23:27:02 +00004398static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004399 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004400 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004401 if (!isa<ConstantSDNode>(Lane))
4402 return SDValue();
4403
4404 SDValue Vec = Op.getOperand(0);
4405 if (Op.getValueType() == MVT::i32 &&
4406 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4407 DebugLoc dl = Op.getDebugLoc();
4408 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4409 }
4410
4411 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004412}
4413
Bob Wilsona6d65862009-08-03 20:36:38 +00004414static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4415 // The only time a CONCAT_VECTORS operation can have legal types is when
4416 // two 64-bit vectors are concatenated to a 128-bit vector.
4417 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4418 "unexpected CONCAT_VECTORS");
4419 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004420 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004421 SDValue Op0 = Op.getOperand(0);
4422 SDValue Op1 = Op.getOperand(1);
4423 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004425 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004426 DAG.getIntPtrConstant(0));
4427 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004428 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004429 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004430 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004431 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004432}
4433
Bob Wilson626613d2010-11-23 19:38:38 +00004434/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4435/// element has been zero/sign-extended, depending on the isSigned parameter,
4436/// from an integer type half its size.
4437static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4438 bool isSigned) {
4439 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4440 EVT VT = N->getValueType(0);
4441 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4442 SDNode *BVN = N->getOperand(0).getNode();
4443 if (BVN->getValueType(0) != MVT::v4i32 ||
4444 BVN->getOpcode() != ISD::BUILD_VECTOR)
4445 return false;
4446 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4447 unsigned HiElt = 1 - LoElt;
4448 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4449 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4450 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4451 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4452 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4453 return false;
4454 if (isSigned) {
4455 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4456 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4457 return true;
4458 } else {
4459 if (Hi0->isNullValue() && Hi1->isNullValue())
4460 return true;
4461 }
4462 return false;
4463 }
4464
4465 if (N->getOpcode() != ISD::BUILD_VECTOR)
4466 return false;
4467
4468 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4469 SDNode *Elt = N->getOperand(i).getNode();
4470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4471 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4472 unsigned HalfSize = EltSize / 2;
4473 if (isSigned) {
4474 int64_t SExtVal = C->getSExtValue();
4475 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4476 return false;
4477 } else {
4478 if ((C->getZExtValue() >> HalfSize) != 0)
4479 return false;
4480 }
4481 continue;
4482 }
4483 return false;
4484 }
4485
4486 return true;
4487}
4488
4489/// isSignExtended - Check if a node is a vector value that is sign-extended
4490/// or a constant BUILD_VECTOR with sign-extended elements.
4491static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4492 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4493 return true;
4494 if (isExtendedBUILD_VECTOR(N, DAG, true))
4495 return true;
4496 return false;
4497}
4498
4499/// isZeroExtended - Check if a node is a vector value that is zero-extended
4500/// or a constant BUILD_VECTOR with zero-extended elements.
4501static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4502 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4503 return true;
4504 if (isExtendedBUILD_VECTOR(N, DAG, false))
4505 return true;
4506 return false;
4507}
4508
4509/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4510/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004511static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4512 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4513 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004514 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4515 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4516 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4517 LD->isNonTemporal(), LD->getAlignment());
4518 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4519 // have been legalized as a BITCAST from v4i32.
4520 if (N->getOpcode() == ISD::BITCAST) {
4521 SDNode *BVN = N->getOperand(0).getNode();
4522 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4523 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4524 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4525 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4526 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4527 }
4528 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4529 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4530 EVT VT = N->getValueType(0);
4531 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4532 unsigned NumElts = VT.getVectorNumElements();
4533 MVT TruncVT = MVT::getIntegerVT(EltSize);
4534 SmallVector<SDValue, 8> Ops;
4535 for (unsigned i = 0; i != NumElts; ++i) {
4536 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4537 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004538 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004539 }
4540 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4541 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004542}
4543
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004544static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4545 unsigned Opcode = N->getOpcode();
4546 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4547 SDNode *N0 = N->getOperand(0).getNode();
4548 SDNode *N1 = N->getOperand(1).getNode();
4549 return N0->hasOneUse() && N1->hasOneUse() &&
4550 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4551 }
4552 return false;
4553}
4554
4555static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4556 unsigned Opcode = N->getOpcode();
4557 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4558 SDNode *N0 = N->getOperand(0).getNode();
4559 SDNode *N1 = N->getOperand(1).getNode();
4560 return N0->hasOneUse() && N1->hasOneUse() &&
4561 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4562 }
4563 return false;
4564}
4565
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004566static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4567 // Multiplications are only custom-lowered for 128-bit vectors so that
4568 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4569 EVT VT = Op.getValueType();
4570 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4571 SDNode *N0 = Op.getOperand(0).getNode();
4572 SDNode *N1 = Op.getOperand(1).getNode();
4573 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004574 bool isMLA = false;
4575 bool isN0SExt = isSignExtended(N0, DAG);
4576 bool isN1SExt = isSignExtended(N1, DAG);
4577 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004578 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004579 else {
4580 bool isN0ZExt = isZeroExtended(N0, DAG);
4581 bool isN1ZExt = isZeroExtended(N1, DAG);
4582 if (isN0ZExt && isN1ZExt)
4583 NewOpc = ARMISD::VMULLu;
4584 else if (isN1SExt || isN1ZExt) {
4585 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4586 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4587 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4588 NewOpc = ARMISD::VMULLs;
4589 isMLA = true;
4590 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4591 NewOpc = ARMISD::VMULLu;
4592 isMLA = true;
4593 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4594 std::swap(N0, N1);
4595 NewOpc = ARMISD::VMULLu;
4596 isMLA = true;
4597 }
4598 }
4599
4600 if (!NewOpc) {
4601 if (VT == MVT::v2i64)
4602 // Fall through to expand this. It is not legal.
4603 return SDValue();
4604 else
4605 // Other vector multiplications are legal.
4606 return Op;
4607 }
4608 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004609
4610 // Legalize to a VMULL instruction.
4611 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004612 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004613 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004614 if (!isMLA) {
4615 Op0 = SkipExtension(N0, DAG);
4616 assert(Op0.getValueType().is64BitVector() &&
4617 Op1.getValueType().is64BitVector() &&
4618 "unexpected types for extended operands to VMULL");
4619 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4620 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004621
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004622 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4623 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4624 // vmull q0, d4, d6
4625 // vmlal q0, d5, d6
4626 // is faster than
4627 // vaddl q0, d4, d5
4628 // vmovl q1, d6
4629 // vmul q0, q0, q1
4630 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4631 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4632 EVT Op1VT = Op1.getValueType();
4633 return DAG.getNode(N0->getOpcode(), DL, VT,
4634 DAG.getNode(NewOpc, DL, VT,
4635 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4636 DAG.getNode(NewOpc, DL, VT,
4637 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004638}
4639
Owen Anderson76706012011-04-05 21:48:57 +00004640static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004641LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4642 // Convert to float
4643 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4644 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4645 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4646 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4647 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4648 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4649 // Get reciprocal estimate.
4650 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004651 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004652 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4653 // Because char has a smaller range than uchar, we can actually get away
4654 // without any newton steps. This requires that we use a weird bias
4655 // of 0xb000, however (again, this has been exhaustively tested).
4656 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4657 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4658 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4659 Y = DAG.getConstant(0xb000, MVT::i32);
4660 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4661 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4662 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4663 // Convert back to short.
4664 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4665 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4666 return X;
4667}
4668
Owen Anderson76706012011-04-05 21:48:57 +00004669static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004670LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4671 SDValue N2;
4672 // Convert to float.
4673 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4674 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4675 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4676 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4677 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4678 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004679
Nate Begeman7973f352011-02-11 20:53:29 +00004680 // Use reciprocal estimate and one refinement step.
4681 // float4 recip = vrecpeq_f32(yf);
4682 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004683 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004684 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004685 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004686 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4687 N1, N2);
4688 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4689 // Because short has a smaller range than ushort, we can actually get away
4690 // with only a single newton step. This requires that we use a weird bias
4691 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004692 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004693 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4694 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004695 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004696 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4697 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4698 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4699 // Convert back to integer and return.
4700 // return vmovn_s32(vcvt_s32_f32(result));
4701 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4702 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4703 return N0;
4704}
4705
4706static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4707 EVT VT = Op.getValueType();
4708 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4709 "unexpected type for custom-lowering ISD::SDIV");
4710
4711 DebugLoc dl = Op.getDebugLoc();
4712 SDValue N0 = Op.getOperand(0);
4713 SDValue N1 = Op.getOperand(1);
4714 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004715
Nate Begeman7973f352011-02-11 20:53:29 +00004716 if (VT == MVT::v8i8) {
4717 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4718 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004719
Nate Begeman7973f352011-02-11 20:53:29 +00004720 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4721 DAG.getIntPtrConstant(4));
4722 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004723 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004724 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4725 DAG.getIntPtrConstant(0));
4726 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4727 DAG.getIntPtrConstant(0));
4728
4729 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4730 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4731
4732 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4733 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004734
Nate Begeman7973f352011-02-11 20:53:29 +00004735 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4736 return N0;
4737 }
4738 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4739}
4740
4741static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4742 EVT VT = Op.getValueType();
4743 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4744 "unexpected type for custom-lowering ISD::UDIV");
4745
4746 DebugLoc dl = Op.getDebugLoc();
4747 SDValue N0 = Op.getOperand(0);
4748 SDValue N1 = Op.getOperand(1);
4749 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004750
Nate Begeman7973f352011-02-11 20:53:29 +00004751 if (VT == MVT::v8i8) {
4752 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4753 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004754
Nate Begeman7973f352011-02-11 20:53:29 +00004755 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4756 DAG.getIntPtrConstant(4));
4757 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004758 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004759 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4760 DAG.getIntPtrConstant(0));
4761 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4762 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004763
Nate Begeman7973f352011-02-11 20:53:29 +00004764 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4765 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004766
Nate Begeman7973f352011-02-11 20:53:29 +00004767 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4768 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004769
4770 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004771 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4772 N0);
4773 return N0;
4774 }
Owen Anderson76706012011-04-05 21:48:57 +00004775
Nate Begeman7973f352011-02-11 20:53:29 +00004776 // v4i16 sdiv ... Convert to float.
4777 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4778 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4779 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4780 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4781 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004782 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004783
4784 // Use reciprocal estimate and two refinement steps.
4785 // float4 recip = vrecpeq_f32(yf);
4786 // recip *= vrecpsq_f32(yf, recip);
4787 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004788 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004789 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004790 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004791 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004792 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004793 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004794 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004795 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004796 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004797 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4798 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4799 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4800 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004801 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004802 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4803 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4804 N1 = DAG.getConstant(2, MVT::i32);
4805 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4806 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4807 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4808 // Convert back to integer and return.
4809 // return vmovn_u32(vcvt_s32_f32(result));
4810 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4811 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4812 return N0;
4813}
4814
Dan Gohmand858e902010-04-17 15:26:15 +00004815SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004816 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004817 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004818 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004819 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004820 case ISD::GlobalAddress:
4821 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4822 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004823 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004824 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004825 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4826 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004827 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004828 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004829 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00004830 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004831 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004832 case ISD::SINT_TO_FP:
4833 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4834 case ISD::FP_TO_SINT:
4835 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004836 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004837 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004838 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004839 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004840 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004841 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004842 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004843 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4844 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004845 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004846 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004847 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004848 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004849 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004850 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004851 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004852 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004853 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004854 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004855 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004856 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004857 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004858 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004859 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004860 case ISD::SDIV: return LowerSDIV(Op, DAG);
4861 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004862 }
Dan Gohman475871a2008-07-27 21:46:04 +00004863 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004864}
4865
Duncan Sands1607f052008-12-01 11:39:25 +00004866/// ReplaceNodeResults - Replace the results of node with an illegal result
4867/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004868void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4869 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004870 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004871 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004872 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004873 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004874 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004875 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004876 case ISD::BITCAST:
4877 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004878 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004879 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004880 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004881 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004882 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004883 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004884 if (Res.getNode())
4885 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004886}
Chris Lattner27a6c732007-11-24 07:07:01 +00004887
Evan Chenga8e29892007-01-19 07:51:42 +00004888//===----------------------------------------------------------------------===//
4889// ARM Scheduler Hooks
4890//===----------------------------------------------------------------------===//
4891
4892MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004893ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4894 MachineBasicBlock *BB,
4895 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004896 unsigned dest = MI->getOperand(0).getReg();
4897 unsigned ptr = MI->getOperand(1).getReg();
4898 unsigned oldval = MI->getOperand(2).getReg();
4899 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004900 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4901 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004902 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004903
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004904 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4905 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004906 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004907 : ARM::GPRRegisterClass);
4908
4909 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004910 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4911 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4912 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004913 }
4914
Jim Grosbach5278eb82009-12-11 01:42:04 +00004915 unsigned ldrOpc, strOpc;
4916 switch (Size) {
4917 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004918 case 1:
4919 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004920 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004921 break;
4922 case 2:
4923 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4924 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4925 break;
4926 case 4:
4927 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4928 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4929 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004930 }
4931
4932 MachineFunction *MF = BB->getParent();
4933 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4934 MachineFunction::iterator It = BB;
4935 ++It; // insert the new blocks after the current block
4936
4937 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4938 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4939 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4940 MF->insert(It, loop1MBB);
4941 MF->insert(It, loop2MBB);
4942 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004943
4944 // Transfer the remainder of BB and its successor edges to exitMBB.
4945 exitMBB->splice(exitMBB->begin(), BB,
4946 llvm::next(MachineBasicBlock::iterator(MI)),
4947 BB->end());
4948 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004949
4950 // thisMBB:
4951 // ...
4952 // fallthrough --> loop1MBB
4953 BB->addSuccessor(loop1MBB);
4954
4955 // loop1MBB:
4956 // ldrex dest, [ptr]
4957 // cmp dest, oldval
4958 // bne exitMBB
4959 BB = loop1MBB;
4960 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004961 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004962 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004963 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4964 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004965 BB->addSuccessor(loop2MBB);
4966 BB->addSuccessor(exitMBB);
4967
4968 // loop2MBB:
4969 // strex scratch, newval, [ptr]
4970 // cmp scratch, #0
4971 // bne loop1MBB
4972 BB = loop2MBB;
4973 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4974 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004975 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004976 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004977 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4978 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004979 BB->addSuccessor(loop1MBB);
4980 BB->addSuccessor(exitMBB);
4981
4982 // exitMBB:
4983 // ...
4984 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004985
Dan Gohman14152b42010-07-06 20:24:04 +00004986 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004987
Jim Grosbach5278eb82009-12-11 01:42:04 +00004988 return BB;
4989}
4990
4991MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004992ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4993 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004994 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4995 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4996
4997 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004998 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004999 MachineFunction::iterator It = BB;
5000 ++It;
5001
5002 unsigned dest = MI->getOperand(0).getReg();
5003 unsigned ptr = MI->getOperand(1).getReg();
5004 unsigned incr = MI->getOperand(2).getReg();
5005 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005006 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005007
5008 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5009 if (isThumb2) {
5010 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5011 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5012 }
5013
Jim Grosbachc3c23542009-12-14 04:22:04 +00005014 unsigned ldrOpc, strOpc;
5015 switch (Size) {
5016 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005017 case 1:
5018 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005019 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005020 break;
5021 case 2:
5022 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5023 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5024 break;
5025 case 4:
5026 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5027 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5028 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005029 }
5030
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005031 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5032 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5033 MF->insert(It, loopMBB);
5034 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005035
5036 // Transfer the remainder of BB and its successor edges to exitMBB.
5037 exitMBB->splice(exitMBB->begin(), BB,
5038 llvm::next(MachineBasicBlock::iterator(MI)),
5039 BB->end());
5040 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005041
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005042 TargetRegisterClass *TRC =
5043 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5044 unsigned scratch = MRI.createVirtualRegister(TRC);
5045 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005046
5047 // thisMBB:
5048 // ...
5049 // fallthrough --> loopMBB
5050 BB->addSuccessor(loopMBB);
5051
5052 // loopMBB:
5053 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005054 // <binop> scratch2, dest, incr
5055 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005056 // cmp scratch, #0
5057 // bne- loopMBB
5058 // fallthrough --> exitMBB
5059 BB = loopMBB;
5060 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005061 if (BinOpcode) {
5062 // operand order needs to go the other way for NAND
5063 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5064 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5065 addReg(incr).addReg(dest)).addReg(0);
5066 else
5067 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5068 addReg(dest).addReg(incr)).addReg(0);
5069 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005070
5071 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5072 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005073 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005074 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005075 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5076 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005077
5078 BB->addSuccessor(loopMBB);
5079 BB->addSuccessor(exitMBB);
5080
5081 // exitMBB:
5082 // ...
5083 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005084
Dan Gohman14152b42010-07-06 20:24:04 +00005085 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005086
Jim Grosbachc3c23542009-12-14 04:22:04 +00005087 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005088}
5089
Jim Grosbachf7da8822011-04-26 19:44:18 +00005090MachineBasicBlock *
5091ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5092 MachineBasicBlock *BB,
5093 unsigned Size,
5094 bool signExtend,
5095 ARMCC::CondCodes Cond) const {
5096 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5097
5098 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5099 MachineFunction *MF = BB->getParent();
5100 MachineFunction::iterator It = BB;
5101 ++It;
5102
5103 unsigned dest = MI->getOperand(0).getReg();
5104 unsigned ptr = MI->getOperand(1).getReg();
5105 unsigned incr = MI->getOperand(2).getReg();
5106 unsigned oldval = dest;
5107 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005108 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005109
5110 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5111 if (isThumb2) {
5112 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5113 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5114 }
5115
Jim Grosbachf7da8822011-04-26 19:44:18 +00005116 unsigned ldrOpc, strOpc, extendOpc;
5117 switch (Size) {
5118 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5119 case 1:
5120 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5121 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005122 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005123 break;
5124 case 2:
5125 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5126 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005127 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005128 break;
5129 case 4:
5130 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5131 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5132 extendOpc = 0;
5133 break;
5134 }
5135
5136 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5137 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5138 MF->insert(It, loopMBB);
5139 MF->insert(It, exitMBB);
5140
5141 // Transfer the remainder of BB and its successor edges to exitMBB.
5142 exitMBB->splice(exitMBB->begin(), BB,
5143 llvm::next(MachineBasicBlock::iterator(MI)),
5144 BB->end());
5145 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5146
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005147 TargetRegisterClass *TRC =
5148 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5149 unsigned scratch = MRI.createVirtualRegister(TRC);
5150 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005151
5152 // thisMBB:
5153 // ...
5154 // fallthrough --> loopMBB
5155 BB->addSuccessor(loopMBB);
5156
5157 // loopMBB:
5158 // ldrex dest, ptr
5159 // (sign extend dest, if required)
5160 // cmp dest, incr
5161 // cmov.cond scratch2, dest, incr
5162 // strex scratch, scratch2, ptr
5163 // cmp scratch, #0
5164 // bne- loopMBB
5165 // fallthrough --> exitMBB
5166 BB = loopMBB;
5167 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5168
5169 // Sign extend the value, if necessary.
5170 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005171 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005172 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5173 .addReg(dest)
5174 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005175 }
5176
5177 // Build compare and cmov instructions.
5178 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5179 .addReg(oldval).addReg(incr));
5180 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5181 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5182
5183 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5184 .addReg(ptr));
5185 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5186 .addReg(scratch).addImm(0));
5187 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5188 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5189
5190 BB->addSuccessor(loopMBB);
5191 BB->addSuccessor(exitMBB);
5192
5193 // exitMBB:
5194 // ...
5195 BB = exitMBB;
5196
5197 MI->eraseFromParent(); // The instruction is gone now.
5198
5199 return BB;
5200}
5201
Evan Cheng218977b2010-07-13 19:27:42 +00005202static
5203MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5204 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5205 E = MBB->succ_end(); I != E; ++I)
5206 if (*I != Succ)
5207 return *I;
5208 llvm_unreachable("Expecting a BB with two successors!");
5209}
5210
Andrew Trick1c3af772011-04-23 03:55:32 +00005211// FIXME: This opcode table should obviously be expressed in the target
5212// description. We probably just need a "machine opcode" value in the pseudo
5213// instruction. But the ideal solution maybe to simply remove the "S" version
5214// of the opcode altogether.
5215struct AddSubFlagsOpcodePair {
5216 unsigned PseudoOpc;
5217 unsigned MachineOpc;
5218};
5219
Benjamin Kramer2753ae32011-08-27 17:36:14 +00005220static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick1c3af772011-04-23 03:55:32 +00005221 {ARM::ADCSri, ARM::ADCri},
5222 {ARM::ADCSrr, ARM::ADCrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005223 {ARM::ADCSrsi, ARM::ADCrsi},
5224 {ARM::ADCSrsr, ARM::ADCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005225 {ARM::SBCSri, ARM::SBCri},
5226 {ARM::SBCSrr, ARM::SBCrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005227 {ARM::SBCSrsi, ARM::SBCrsi},
5228 {ARM::SBCSrsr, ARM::SBCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005229 {ARM::RSBSri, ARM::RSBri},
5230 {ARM::RSBSrr, ARM::RSBrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005231 {ARM::RSBSrsi, ARM::RSBrsi},
5232 {ARM::RSBSrsr, ARM::RSBrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005233 {ARM::RSCSri, ARM::RSCri},
Owen Anderson92a20222011-07-21 18:54:16 +00005234 {ARM::RSCSrsi, ARM::RSCrsi},
5235 {ARM::RSCSrsr, ARM::RSCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005236 {ARM::t2ADCSri, ARM::t2ADCri},
5237 {ARM::t2ADCSrr, ARM::t2ADCrr},
5238 {ARM::t2ADCSrs, ARM::t2ADCrs},
5239 {ARM::t2SBCSri, ARM::t2SBCri},
5240 {ARM::t2SBCSrr, ARM::t2SBCrr},
5241 {ARM::t2SBCSrs, ARM::t2SBCrs},
5242 {ARM::t2RSBSri, ARM::t2RSBri},
5243 {ARM::t2RSBSrs, ARM::t2RSBrs},
5244};
5245
5246// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5247// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5248//
5249// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5250// position to be recognized by the target descrition as the 'S' bit.
5251bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5252 MachineBasicBlock *BB) const {
5253 unsigned OldOpc = MI->getOpcode();
5254 unsigned NewOpc = 0;
5255
5256 // This is only called for instructions that need remapping, so iterating over
5257 // the tiny opcode table is not costly.
5258 static const int NPairs =
5259 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
Benjamin Kramer2753ae32011-08-27 17:36:14 +00005260 for (const AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
Andrew Trick1c3af772011-04-23 03:55:32 +00005261 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5262 if (OldOpc == Pair->PseudoOpc) {
5263 NewOpc = Pair->MachineOpc;
5264 break;
5265 }
5266 }
5267 if (!NewOpc)
5268 return false;
5269
5270 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5271 DebugLoc dl = MI->getDebugLoc();
5272 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5273 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5274 MIB.addOperand(MI->getOperand(i));
5275 AddDefaultPred(MIB);
5276 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5277 MI->eraseFromParent();
5278 return true;
5279}
5280
Jim Grosbache801dc42009-12-12 01:40:06 +00005281MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005282ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005283 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005284 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005285 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005286 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005287 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005288 default: {
5289 if (RemapAddSubWithFlags(MI, BB))
5290 return BB;
5291
Jim Grosbach5278eb82009-12-11 01:42:04 +00005292 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005293 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005294 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005295 case ARM::STRi_preidx:
5296 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00005297 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00005298 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
5299 // Decode the offset.
5300 unsigned Offset = MI->getOperand(4).getImm();
5301 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
5302 Offset = ARM_AM::getAM2Offset(Offset);
5303 if (isSub)
5304 Offset = -Offset;
5305
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005306 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00005307 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00005308 .addOperand(MI->getOperand(0)) // Rn_wb
5309 .addOperand(MI->getOperand(1)) // Rt
5310 .addOperand(MI->getOperand(2)) // Rn
5311 .addImm(Offset) // offset (skip GPR==zero_reg)
5312 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005313 .addOperand(MI->getOperand(6))
5314 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00005315 MI->eraseFromParent();
5316 return BB;
5317 }
5318 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00005319 case ARM::STRBr_preidx:
5320 case ARM::STRH_preidx: {
5321 unsigned NewOpc;
5322 switch (MI->getOpcode()) {
5323 default: llvm_unreachable("unexpected opcode!");
5324 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
5325 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
5326 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
5327 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005328 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5329 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5330 MIB.addOperand(MI->getOperand(i));
5331 MI->eraseFromParent();
5332 return BB;
5333 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005334 case ARM::ATOMIC_LOAD_ADD_I8:
5335 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5336 case ARM::ATOMIC_LOAD_ADD_I16:
5337 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5338 case ARM::ATOMIC_LOAD_ADD_I32:
5339 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005340
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005341 case ARM::ATOMIC_LOAD_AND_I8:
5342 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5343 case ARM::ATOMIC_LOAD_AND_I16:
5344 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5345 case ARM::ATOMIC_LOAD_AND_I32:
5346 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005347
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005348 case ARM::ATOMIC_LOAD_OR_I8:
5349 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5350 case ARM::ATOMIC_LOAD_OR_I16:
5351 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5352 case ARM::ATOMIC_LOAD_OR_I32:
5353 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005354
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005355 case ARM::ATOMIC_LOAD_XOR_I8:
5356 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5357 case ARM::ATOMIC_LOAD_XOR_I16:
5358 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5359 case ARM::ATOMIC_LOAD_XOR_I32:
5360 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005361
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005362 case ARM::ATOMIC_LOAD_NAND_I8:
5363 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5364 case ARM::ATOMIC_LOAD_NAND_I16:
5365 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5366 case ARM::ATOMIC_LOAD_NAND_I32:
5367 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005368
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005369 case ARM::ATOMIC_LOAD_SUB_I8:
5370 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5371 case ARM::ATOMIC_LOAD_SUB_I16:
5372 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5373 case ARM::ATOMIC_LOAD_SUB_I32:
5374 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005375
Jim Grosbachf7da8822011-04-26 19:44:18 +00005376 case ARM::ATOMIC_LOAD_MIN_I8:
5377 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5378 case ARM::ATOMIC_LOAD_MIN_I16:
5379 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5380 case ARM::ATOMIC_LOAD_MIN_I32:
5381 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5382
5383 case ARM::ATOMIC_LOAD_MAX_I8:
5384 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5385 case ARM::ATOMIC_LOAD_MAX_I16:
5386 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5387 case ARM::ATOMIC_LOAD_MAX_I32:
5388 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5389
5390 case ARM::ATOMIC_LOAD_UMIN_I8:
5391 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5392 case ARM::ATOMIC_LOAD_UMIN_I16:
5393 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5394 case ARM::ATOMIC_LOAD_UMIN_I32:
5395 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5396
5397 case ARM::ATOMIC_LOAD_UMAX_I8:
5398 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5399 case ARM::ATOMIC_LOAD_UMAX_I16:
5400 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5401 case ARM::ATOMIC_LOAD_UMAX_I32:
5402 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5403
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005404 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5405 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5406 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005407
5408 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5409 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5410 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005411
Evan Cheng007ea272009-08-12 05:17:19 +00005412 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005413 // To "insert" a SELECT_CC instruction, we actually have to insert the
5414 // diamond control-flow pattern. The incoming instruction knows the
5415 // destination vreg to set, the condition code register to branch on, the
5416 // true/false values to select between, and a branch opcode to use.
5417 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005418 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005419 ++It;
5420
5421 // thisMBB:
5422 // ...
5423 // TrueVal = ...
5424 // cmpTY ccX, r1, r2
5425 // bCC copy1MBB
5426 // fallthrough --> copy0MBB
5427 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005428 MachineFunction *F = BB->getParent();
5429 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5430 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005431 F->insert(It, copy0MBB);
5432 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005433
5434 // Transfer the remainder of BB and its successor edges to sinkMBB.
5435 sinkMBB->splice(sinkMBB->begin(), BB,
5436 llvm::next(MachineBasicBlock::iterator(MI)),
5437 BB->end());
5438 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5439
Dan Gohman258c58c2010-07-06 15:49:48 +00005440 BB->addSuccessor(copy0MBB);
5441 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005442
Dan Gohman14152b42010-07-06 20:24:04 +00005443 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5444 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5445
Evan Chenga8e29892007-01-19 07:51:42 +00005446 // copy0MBB:
5447 // %FalseValue = ...
5448 // # fallthrough to sinkMBB
5449 BB = copy0MBB;
5450
5451 // Update machine-CFG edges
5452 BB->addSuccessor(sinkMBB);
5453
5454 // sinkMBB:
5455 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5456 // ...
5457 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005458 BuildMI(*BB, BB->begin(), dl,
5459 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005460 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5461 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5462
Dan Gohman14152b42010-07-06 20:24:04 +00005463 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005464 return BB;
5465 }
Evan Cheng86198642009-08-07 00:34:42 +00005466
Evan Cheng218977b2010-07-13 19:27:42 +00005467 case ARM::BCCi64:
5468 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005469 // If there is an unconditional branch to the other successor, remove it.
5470 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005471
Evan Cheng218977b2010-07-13 19:27:42 +00005472 // Compare both parts that make up the double comparison separately for
5473 // equality.
5474 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5475
5476 unsigned LHS1 = MI->getOperand(1).getReg();
5477 unsigned LHS2 = MI->getOperand(2).getReg();
5478 if (RHSisZero) {
5479 AddDefaultPred(BuildMI(BB, dl,
5480 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5481 .addReg(LHS1).addImm(0));
5482 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5483 .addReg(LHS2).addImm(0)
5484 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5485 } else {
5486 unsigned RHS1 = MI->getOperand(3).getReg();
5487 unsigned RHS2 = MI->getOperand(4).getReg();
5488 AddDefaultPred(BuildMI(BB, dl,
5489 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5490 .addReg(LHS1).addReg(RHS1));
5491 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5492 .addReg(LHS2).addReg(RHS2)
5493 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5494 }
5495
5496 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5497 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5498 if (MI->getOperand(0).getImm() == ARMCC::NE)
5499 std::swap(destMBB, exitMBB);
5500
5501 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5502 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5503 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5504 .addMBB(exitMBB);
5505
5506 MI->eraseFromParent(); // The pseudo instruction is gone now.
5507 return BB;
5508 }
Evan Chenga8e29892007-01-19 07:51:42 +00005509 }
5510}
5511
5512//===----------------------------------------------------------------------===//
5513// ARM Optimization Hooks
5514//===----------------------------------------------------------------------===//
5515
Chris Lattnerd1980a52009-03-12 06:52:53 +00005516static
5517SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5518 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005519 SelectionDAG &DAG = DCI.DAG;
5520 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005521 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005522 unsigned Opc = N->getOpcode();
5523 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5524 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5525 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5526 ISD::CondCode CC = ISD::SETCC_INVALID;
5527
5528 if (isSlctCC) {
5529 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5530 } else {
5531 SDValue CCOp = Slct.getOperand(0);
5532 if (CCOp.getOpcode() == ISD::SETCC)
5533 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5534 }
5535
5536 bool DoXform = false;
5537 bool InvCC = false;
5538 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5539 "Bad input!");
5540
5541 if (LHS.getOpcode() == ISD::Constant &&
5542 cast<ConstantSDNode>(LHS)->isNullValue()) {
5543 DoXform = true;
5544 } else if (CC != ISD::SETCC_INVALID &&
5545 RHS.getOpcode() == ISD::Constant &&
5546 cast<ConstantSDNode>(RHS)->isNullValue()) {
5547 std::swap(LHS, RHS);
5548 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005549 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005550 Op0.getOperand(0).getValueType();
5551 bool isInt = OpVT.isInteger();
5552 CC = ISD::getSetCCInverse(CC, isInt);
5553
5554 if (!TLI.isCondCodeLegal(CC, OpVT))
5555 return SDValue(); // Inverse operator isn't legal.
5556
5557 DoXform = true;
5558 InvCC = true;
5559 }
5560
5561 if (DoXform) {
5562 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5563 if (isSlctCC)
5564 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5565 Slct.getOperand(0), Slct.getOperand(1), CC);
5566 SDValue CCOp = Slct.getOperand(0);
5567 if (InvCC)
5568 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5569 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5570 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5571 CCOp, OtherOp, Result);
5572 }
5573 return SDValue();
5574}
5575
Eric Christopherfa6f5912011-06-29 21:10:36 +00005576// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00005577// (only after legalization).
5578static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5579 TargetLowering::DAGCombinerInfo &DCI,
5580 const ARMSubtarget *Subtarget) {
5581
5582 // Only perform optimization if after legalize, and if NEON is available. We
5583 // also expected both operands to be BUILD_VECTORs.
5584 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5585 || N0.getOpcode() != ISD::BUILD_VECTOR
5586 || N1.getOpcode() != ISD::BUILD_VECTOR)
5587 return SDValue();
5588
5589 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5590 EVT VT = N->getValueType(0);
5591 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5592 return SDValue();
5593
5594 // Check that the vector operands are of the right form.
5595 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5596 // operands, where N is the size of the formed vector.
5597 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5598 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005599
5600 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005601 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005602 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005603 SDValue Vec = N0->getOperand(0)->getOperand(0);
5604 SDNode *V = Vec.getNode();
5605 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005606
Eric Christopherfa6f5912011-06-29 21:10:36 +00005607 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00005608 // check to see if each of their operands are an EXTRACT_VECTOR with
5609 // the same vector and appropriate index.
5610 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5611 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5612 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00005613
Tanya Lattner189531f2011-06-14 23:48:48 +00005614 SDValue ExtVec0 = N0->getOperand(i);
5615 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005616
Tanya Lattner189531f2011-06-14 23:48:48 +00005617 // First operand is the vector, verify its the same.
5618 if (V != ExtVec0->getOperand(0).getNode() ||
5619 V != ExtVec1->getOperand(0).getNode())
5620 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00005621
Tanya Lattner189531f2011-06-14 23:48:48 +00005622 // Second is the constant, verify its correct.
5623 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5624 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00005625
Tanya Lattner189531f2011-06-14 23:48:48 +00005626 // For the constant, we want to see all the even or all the odd.
5627 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5628 || C1->getZExtValue() != nextIndex+1)
5629 return SDValue();
5630
5631 // Increment index.
5632 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005633 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00005634 return SDValue();
5635 }
5636
5637 // Create VPADDL node.
5638 SelectionDAG &DAG = DCI.DAG;
5639 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00005640
5641 // Build operand list.
5642 SmallVector<SDValue, 8> Ops;
5643 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5644 TLI.getPointerTy()));
5645
5646 // Input is the vector.
5647 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005648
Tanya Lattner189531f2011-06-14 23:48:48 +00005649 // Get widened type and narrowed type.
5650 MVT widenType;
5651 unsigned numElem = VT.getVectorNumElements();
5652 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5653 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5654 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5655 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5656 default:
5657 assert(0 && "Invalid vector element type for padd optimization.");
5658 }
5659
5660 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5661 widenType, &Ops[0], Ops.size());
5662 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5663}
5664
Bob Wilson3d5792a2010-07-29 20:34:14 +00005665/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5666/// operands N0 and N1. This is a helper for PerformADDCombine that is
5667/// called with the default operands, and if that fails, with commuted
5668/// operands.
5669static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005670 TargetLowering::DAGCombinerInfo &DCI,
5671 const ARMSubtarget *Subtarget){
5672
5673 // Attempt to create vpaddl for this add.
5674 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5675 if (Result.getNode())
5676 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005677
Chris Lattnerd1980a52009-03-12 06:52:53 +00005678 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5679 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5680 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5681 if (Result.getNode()) return Result;
5682 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005683 return SDValue();
5684}
5685
Bob Wilson3d5792a2010-07-29 20:34:14 +00005686/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5687///
5688static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005689 TargetLowering::DAGCombinerInfo &DCI,
5690 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005691 SDValue N0 = N->getOperand(0);
5692 SDValue N1 = N->getOperand(1);
5693
5694 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005695 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005696 if (Result.getNode())
5697 return Result;
5698
5699 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005700 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005701}
5702
Chris Lattnerd1980a52009-03-12 06:52:53 +00005703/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005704///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005705static SDValue PerformSUBCombine(SDNode *N,
5706 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005707 SDValue N0 = N->getOperand(0);
5708 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005709
Chris Lattnerd1980a52009-03-12 06:52:53 +00005710 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5711 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5712 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5713 if (Result.getNode()) return Result;
5714 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005715
Chris Lattnerd1980a52009-03-12 06:52:53 +00005716 return SDValue();
5717}
5718
Evan Cheng463d3582011-03-31 19:38:48 +00005719/// PerformVMULCombine
5720/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5721/// special multiplier accumulator forwarding.
5722/// vmul d3, d0, d2
5723/// vmla d3, d1, d2
5724/// is faster than
5725/// vadd d3, d0, d1
5726/// vmul d3, d3, d2
5727static SDValue PerformVMULCombine(SDNode *N,
5728 TargetLowering::DAGCombinerInfo &DCI,
5729 const ARMSubtarget *Subtarget) {
5730 if (!Subtarget->hasVMLxForwarding())
5731 return SDValue();
5732
5733 SelectionDAG &DAG = DCI.DAG;
5734 SDValue N0 = N->getOperand(0);
5735 SDValue N1 = N->getOperand(1);
5736 unsigned Opcode = N0.getOpcode();
5737 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5738 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00005739 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00005740 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5741 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5742 return SDValue();
5743 std::swap(N0, N1);
5744 }
5745
5746 EVT VT = N->getValueType(0);
5747 DebugLoc DL = N->getDebugLoc();
5748 SDValue N00 = N0->getOperand(0);
5749 SDValue N01 = N0->getOperand(1);
5750 return DAG.getNode(Opcode, DL, VT,
5751 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5752 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5753}
5754
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005755static SDValue PerformMULCombine(SDNode *N,
5756 TargetLowering::DAGCombinerInfo &DCI,
5757 const ARMSubtarget *Subtarget) {
5758 SelectionDAG &DAG = DCI.DAG;
5759
5760 if (Subtarget->isThumb1Only())
5761 return SDValue();
5762
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005763 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5764 return SDValue();
5765
5766 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005767 if (VT.is64BitVector() || VT.is128BitVector())
5768 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005769 if (VT != MVT::i32)
5770 return SDValue();
5771
5772 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5773 if (!C)
5774 return SDValue();
5775
5776 uint64_t MulAmt = C->getZExtValue();
5777 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5778 ShiftAmt = ShiftAmt & (32 - 1);
5779 SDValue V = N->getOperand(0);
5780 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005781
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005782 SDValue Res;
5783 MulAmt >>= ShiftAmt;
5784 if (isPowerOf2_32(MulAmt - 1)) {
5785 // (mul x, 2^N + 1) => (add (shl x, N), x)
5786 Res = DAG.getNode(ISD::ADD, DL, VT,
5787 V, DAG.getNode(ISD::SHL, DL, VT,
5788 V, DAG.getConstant(Log2_32(MulAmt-1),
5789 MVT::i32)));
5790 } else if (isPowerOf2_32(MulAmt + 1)) {
5791 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5792 Res = DAG.getNode(ISD::SUB, DL, VT,
5793 DAG.getNode(ISD::SHL, DL, VT,
5794 V, DAG.getConstant(Log2_32(MulAmt+1),
5795 MVT::i32)),
5796 V);
5797 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005798 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005799
5800 if (ShiftAmt != 0)
5801 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5802 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005803
5804 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005805 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005806 return SDValue();
5807}
5808
Owen Anderson080c0922010-11-05 19:27:46 +00005809static SDValue PerformANDCombine(SDNode *N,
5810 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005811
Owen Anderson080c0922010-11-05 19:27:46 +00005812 // Attempt to use immediate-form VBIC
5813 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5814 DebugLoc dl = N->getDebugLoc();
5815 EVT VT = N->getValueType(0);
5816 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005817
Tanya Lattner0433b212011-04-07 15:24:20 +00005818 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5819 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005820
Owen Anderson080c0922010-11-05 19:27:46 +00005821 APInt SplatBits, SplatUndef;
5822 unsigned SplatBitSize;
5823 bool HasAnyUndefs;
5824 if (BVN &&
5825 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5826 if (SplatBitSize <= 64) {
5827 EVT VbicVT;
5828 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5829 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005830 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005831 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005832 if (Val.getNode()) {
5833 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005834 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005835 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005836 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005837 }
5838 }
5839 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005840
Owen Anderson080c0922010-11-05 19:27:46 +00005841 return SDValue();
5842}
5843
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005844/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5845static SDValue PerformORCombine(SDNode *N,
5846 TargetLowering::DAGCombinerInfo &DCI,
5847 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005848 // Attempt to use immediate-form VORR
5849 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5850 DebugLoc dl = N->getDebugLoc();
5851 EVT VT = N->getValueType(0);
5852 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005853
Tanya Lattner0433b212011-04-07 15:24:20 +00005854 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5855 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005856
Owen Anderson60f48702010-11-03 23:15:26 +00005857 APInt SplatBits, SplatUndef;
5858 unsigned SplatBitSize;
5859 bool HasAnyUndefs;
5860 if (BVN && Subtarget->hasNEON() &&
5861 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5862 if (SplatBitSize <= 64) {
5863 EVT VorrVT;
5864 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5865 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005866 DAG, VorrVT, VT.is128BitVector(),
5867 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005868 if (Val.getNode()) {
5869 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005870 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005871 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005872 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005873 }
5874 }
5875 }
5876
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005877 SDValue N0 = N->getOperand(0);
5878 if (N0.getOpcode() != ISD::AND)
5879 return SDValue();
5880 SDValue N1 = N->getOperand(1);
5881
5882 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5883 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5884 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5885 APInt SplatUndef;
5886 unsigned SplatBitSize;
5887 bool HasAnyUndefs;
5888
5889 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5890 APInt SplatBits0;
5891 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5892 HasAnyUndefs) && !HasAnyUndefs) {
5893 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5894 APInt SplatBits1;
5895 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5896 HasAnyUndefs) && !HasAnyUndefs &&
5897 SplatBits0 == ~SplatBits1) {
5898 // Canonicalize the vector type to make instruction selection simpler.
5899 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5900 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5901 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005902 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005903 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5904 }
5905 }
5906 }
5907
Jim Grosbach54238562010-07-17 03:30:54 +00005908 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5909 // reasonable.
5910
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005911 // BFI is only available on V6T2+
5912 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5913 return SDValue();
5914
Jim Grosbach54238562010-07-17 03:30:54 +00005915 DebugLoc DL = N->getDebugLoc();
5916 // 1) or (and A, mask), val => ARMbfi A, val, mask
5917 // iff (val & mask) == val
5918 //
5919 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5920 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005921 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005922 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005923 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005924 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005925
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005926 if (VT != MVT::i32)
5927 return SDValue();
5928
Evan Cheng30fb13f2010-12-13 20:32:54 +00005929 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005930
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005931 // The value and the mask need to be constants so we can verify this is
5932 // actually a bitfield set. If the mask is 0xffff, we can do better
5933 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005934 SDValue MaskOp = N0.getOperand(1);
5935 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5936 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005937 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005938 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005939 if (Mask == 0xffff)
5940 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005941 SDValue Res;
5942 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005943 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5944 if (N1C) {
5945 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005946 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005947 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005948
Evan Chenga9688c42010-12-11 04:11:38 +00005949 if (ARM::isBitFieldInvertedMask(Mask)) {
5950 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005951
Evan Cheng30fb13f2010-12-13 20:32:54 +00005952 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005953 DAG.getConstant(Val, MVT::i32),
5954 DAG.getConstant(Mask, MVT::i32));
5955
5956 // Do not add new nodes to DAG combiner worklist.
5957 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005958 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005959 }
Jim Grosbach54238562010-07-17 03:30:54 +00005960 } else if (N1.getOpcode() == ISD::AND) {
5961 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005962 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5963 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005964 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005965 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005966
Eric Christopher29aeed12011-03-26 01:21:03 +00005967 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5968 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005969 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005970 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005971 // The pack halfword instruction works better for masks that fit it,
5972 // so use that when it's available.
5973 if (Subtarget->hasT2ExtractPack() &&
5974 (Mask == 0xffff || Mask == 0xffff0000))
5975 return SDValue();
5976 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005977 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005978 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005979 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005980 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005981 DAG.getConstant(Mask, MVT::i32));
5982 // Do not add new nodes to DAG combiner worklist.
5983 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005984 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005985 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005986 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005987 // The pack halfword instruction works better for masks that fit it,
5988 // so use that when it's available.
5989 if (Subtarget->hasT2ExtractPack() &&
5990 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5991 return SDValue();
5992 // 2b
5993 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005994 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005995 DAG.getConstant(lsb, MVT::i32));
5996 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005997 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005998 // Do not add new nodes to DAG combiner worklist.
5999 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006000 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006001 }
6002 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006003
Evan Cheng30fb13f2010-12-13 20:32:54 +00006004 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
6005 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
6006 ARM::isBitFieldInvertedMask(~Mask)) {
6007 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
6008 // where lsb(mask) == #shamt and masked bits of B are known zero.
6009 SDValue ShAmt = N00.getOperand(1);
6010 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
6011 unsigned LSB = CountTrailingZeros_32(Mask);
6012 if (ShAmtC != LSB)
6013 return SDValue();
6014
6015 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
6016 DAG.getConstant(~Mask, MVT::i32));
6017
6018 // Do not add new nodes to DAG combiner worklist.
6019 DCI.CombineTo(N, Res, false);
6020 }
6021
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006022 return SDValue();
6023}
6024
Evan Chengbf188ae2011-06-15 01:12:31 +00006025/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
6026/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00006027static SDValue PerformBFICombine(SDNode *N,
6028 TargetLowering::DAGCombinerInfo &DCI) {
6029 SDValue N1 = N->getOperand(1);
6030 if (N1.getOpcode() == ISD::AND) {
6031 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6032 if (!N11C)
6033 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006034 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
6035 unsigned LSB = CountTrailingZeros_32(~InvMask);
6036 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
6037 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00006038 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006039 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00006040 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
6041 N->getOperand(0), N1.getOperand(0),
6042 N->getOperand(2));
6043 }
6044 return SDValue();
6045}
6046
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006047/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
6048/// ARMISD::VMOVRRD.
6049static SDValue PerformVMOVRRDCombine(SDNode *N,
6050 TargetLowering::DAGCombinerInfo &DCI) {
6051 // vmovrrd(vmovdrr x, y) -> x,y
6052 SDValue InDouble = N->getOperand(0);
6053 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6054 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006055
6056 // vmovrrd(load f64) -> (load i32), (load i32)
6057 SDNode *InNode = InDouble.getNode();
6058 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6059 InNode->getValueType(0) == MVT::f64 &&
6060 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6061 !cast<LoadSDNode>(InNode)->isVolatile()) {
6062 // TODO: Should this be done for non-FrameIndex operands?
6063 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6064
6065 SelectionDAG &DAG = DCI.DAG;
6066 DebugLoc DL = LD->getDebugLoc();
6067 SDValue BasePtr = LD->getBasePtr();
6068 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6069 LD->getPointerInfo(), LD->isVolatile(),
6070 LD->isNonTemporal(), LD->getAlignment());
6071
6072 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6073 DAG.getConstant(4, MVT::i32));
6074 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6075 LD->getPointerInfo(), LD->isVolatile(),
6076 LD->isNonTemporal(),
6077 std::min(4U, LD->getAlignment() / 2));
6078
6079 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6080 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6081 DCI.RemoveFromWorklist(LD);
6082 DAG.DeleteNode(LD);
6083 return Result;
6084 }
6085
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006086 return SDValue();
6087}
6088
6089/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6090/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6091static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6092 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6093 SDValue Op0 = N->getOperand(0);
6094 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006095 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006096 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006097 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006098 Op1 = Op1.getOperand(0);
6099 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6100 Op0.getNode() == Op1.getNode() &&
6101 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006102 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006103 N->getValueType(0), Op0.getOperand(0));
6104 return SDValue();
6105}
6106
Bob Wilson31600902010-12-21 06:43:19 +00006107/// PerformSTORECombine - Target-specific dag combine xforms for
6108/// ISD::STORE.
6109static SDValue PerformSTORECombine(SDNode *N,
6110 TargetLowering::DAGCombinerInfo &DCI) {
6111 // Bitcast an i64 store extracted from a vector to f64.
6112 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6113 StoreSDNode *St = cast<StoreSDNode>(N);
6114 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006115 if (!ISD::isNormalStore(St) || St->isVolatile())
6116 return SDValue();
6117
6118 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6119 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6120 SelectionDAG &DAG = DCI.DAG;
6121 DebugLoc DL = St->getDebugLoc();
6122 SDValue BasePtr = St->getBasePtr();
6123 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6124 StVal.getNode()->getOperand(0), BasePtr,
6125 St->getPointerInfo(), St->isVolatile(),
6126 St->isNonTemporal(), St->getAlignment());
6127
6128 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6129 DAG.getConstant(4, MVT::i32));
6130 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6131 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6132 St->isNonTemporal(),
6133 std::min(4U, St->getAlignment() / 2));
6134 }
6135
6136 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006137 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6138 return SDValue();
6139
6140 SelectionDAG &DAG = DCI.DAG;
6141 DebugLoc dl = StVal.getDebugLoc();
6142 SDValue IntVec = StVal.getOperand(0);
6143 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6144 IntVec.getValueType().getVectorNumElements());
6145 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6146 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6147 Vec, StVal.getOperand(1));
6148 dl = N->getDebugLoc();
6149 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6150 // Make the DAGCombiner fold the bitcasts.
6151 DCI.AddToWorklist(Vec.getNode());
6152 DCI.AddToWorklist(ExtElt.getNode());
6153 DCI.AddToWorklist(V.getNode());
6154 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6155 St->getPointerInfo(), St->isVolatile(),
6156 St->isNonTemporal(), St->getAlignment(),
6157 St->getTBAAInfo());
6158}
6159
6160/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6161/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6162/// i64 vector to have f64 elements, since the value can then be loaded
6163/// directly into a VFP register.
6164static bool hasNormalLoadOperand(SDNode *N) {
6165 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6166 for (unsigned i = 0; i < NumElts; ++i) {
6167 SDNode *Elt = N->getOperand(i).getNode();
6168 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6169 return true;
6170 }
6171 return false;
6172}
6173
Bob Wilson75f02882010-09-17 22:59:05 +00006174/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6175/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006176static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6177 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006178 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6179 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6180 // into a pair of GPRs, which is fine when the value is used as a scalar,
6181 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006182 SelectionDAG &DAG = DCI.DAG;
6183 if (N->getNumOperands() == 2) {
6184 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6185 if (RV.getNode())
6186 return RV;
6187 }
Bob Wilson75f02882010-09-17 22:59:05 +00006188
Bob Wilson31600902010-12-21 06:43:19 +00006189 // Load i64 elements as f64 values so that type legalization does not split
6190 // them up into i32 values.
6191 EVT VT = N->getValueType(0);
6192 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6193 return SDValue();
6194 DebugLoc dl = N->getDebugLoc();
6195 SmallVector<SDValue, 8> Ops;
6196 unsigned NumElts = VT.getVectorNumElements();
6197 for (unsigned i = 0; i < NumElts; ++i) {
6198 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6199 Ops.push_back(V);
6200 // Make the DAGCombiner fold the bitcast.
6201 DCI.AddToWorklist(V.getNode());
6202 }
6203 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6204 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6205 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6206}
6207
6208/// PerformInsertEltCombine - Target-specific dag combine xforms for
6209/// ISD::INSERT_VECTOR_ELT.
6210static SDValue PerformInsertEltCombine(SDNode *N,
6211 TargetLowering::DAGCombinerInfo &DCI) {
6212 // Bitcast an i64 load inserted into a vector to f64.
6213 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6214 EVT VT = N->getValueType(0);
6215 SDNode *Elt = N->getOperand(1).getNode();
6216 if (VT.getVectorElementType() != MVT::i64 ||
6217 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6218 return SDValue();
6219
6220 SelectionDAG &DAG = DCI.DAG;
6221 DebugLoc dl = N->getDebugLoc();
6222 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6223 VT.getVectorNumElements());
6224 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6225 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6226 // Make the DAGCombiner fold the bitcasts.
6227 DCI.AddToWorklist(Vec.getNode());
6228 DCI.AddToWorklist(V.getNode());
6229 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6230 Vec, V, N->getOperand(2));
6231 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006232}
6233
Bob Wilsonf20700c2010-10-27 20:38:28 +00006234/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6235/// ISD::VECTOR_SHUFFLE.
6236static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6237 // The LLVM shufflevector instruction does not require the shuffle mask
6238 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6239 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6240 // operands do not match the mask length, they are extended by concatenating
6241 // them with undef vectors. That is probably the right thing for other
6242 // targets, but for NEON it is better to concatenate two double-register
6243 // size vector operands into a single quad-register size vector. Do that
6244 // transformation here:
6245 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6246 // shuffle(concat(v1, v2), undef)
6247 SDValue Op0 = N->getOperand(0);
6248 SDValue Op1 = N->getOperand(1);
6249 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6250 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6251 Op0.getNumOperands() != 2 ||
6252 Op1.getNumOperands() != 2)
6253 return SDValue();
6254 SDValue Concat0Op1 = Op0.getOperand(1);
6255 SDValue Concat1Op1 = Op1.getOperand(1);
6256 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6257 Concat1Op1.getOpcode() != ISD::UNDEF)
6258 return SDValue();
6259 // Skip the transformation if any of the types are illegal.
6260 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6261 EVT VT = N->getValueType(0);
6262 if (!TLI.isTypeLegal(VT) ||
6263 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6264 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6265 return SDValue();
6266
6267 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6268 Op0.getOperand(0), Op1.getOperand(0));
6269 // Translate the shuffle mask.
6270 SmallVector<int, 16> NewMask;
6271 unsigned NumElts = VT.getVectorNumElements();
6272 unsigned HalfElts = NumElts/2;
6273 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6274 for (unsigned n = 0; n < NumElts; ++n) {
6275 int MaskElt = SVN->getMaskElt(n);
6276 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006277 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006278 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006279 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006280 NewElt = HalfElts + MaskElt - NumElts;
6281 NewMask.push_back(NewElt);
6282 }
6283 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6284 DAG.getUNDEF(VT), NewMask.data());
6285}
6286
Bob Wilson1c3ef902011-02-07 17:43:21 +00006287/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6288/// NEON load/store intrinsics to merge base address updates.
6289static SDValue CombineBaseUpdate(SDNode *N,
6290 TargetLowering::DAGCombinerInfo &DCI) {
6291 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6292 return SDValue();
6293
6294 SelectionDAG &DAG = DCI.DAG;
6295 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6296 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6297 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6298 SDValue Addr = N->getOperand(AddrOpIdx);
6299
6300 // Search for a use of the address operand that is an increment.
6301 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6302 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6303 SDNode *User = *UI;
6304 if (User->getOpcode() != ISD::ADD ||
6305 UI.getUse().getResNo() != Addr.getResNo())
6306 continue;
6307
6308 // Check that the add is independent of the load/store. Otherwise, folding
6309 // it would create a cycle.
6310 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6311 continue;
6312
6313 // Find the new opcode for the updating load/store.
6314 bool isLoad = true;
6315 bool isLaneOp = false;
6316 unsigned NewOpc = 0;
6317 unsigned NumVecs = 0;
6318 if (isIntrinsic) {
6319 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6320 switch (IntNo) {
6321 default: assert(0 && "unexpected intrinsic for Neon base update");
6322 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6323 NumVecs = 1; break;
6324 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6325 NumVecs = 2; break;
6326 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6327 NumVecs = 3; break;
6328 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6329 NumVecs = 4; break;
6330 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6331 NumVecs = 2; isLaneOp = true; break;
6332 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6333 NumVecs = 3; isLaneOp = true; break;
6334 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6335 NumVecs = 4; isLaneOp = true; break;
6336 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6337 NumVecs = 1; isLoad = false; break;
6338 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6339 NumVecs = 2; isLoad = false; break;
6340 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6341 NumVecs = 3; isLoad = false; break;
6342 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6343 NumVecs = 4; isLoad = false; break;
6344 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6345 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6346 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6347 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6348 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6349 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6350 }
6351 } else {
6352 isLaneOp = true;
6353 switch (N->getOpcode()) {
6354 default: assert(0 && "unexpected opcode for Neon base update");
6355 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6356 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6357 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6358 }
6359 }
6360
6361 // Find the size of memory referenced by the load/store.
6362 EVT VecTy;
6363 if (isLoad)
6364 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006365 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006366 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6367 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6368 if (isLaneOp)
6369 NumBytes /= VecTy.getVectorNumElements();
6370
6371 // If the increment is a constant, it must match the memory ref size.
6372 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6373 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6374 uint64_t IncVal = CInc->getZExtValue();
6375 if (IncVal != NumBytes)
6376 continue;
6377 } else if (NumBytes >= 3 * 16) {
6378 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6379 // separate instructions that make it harder to use a non-constant update.
6380 continue;
6381 }
6382
6383 // Create the new updating load/store node.
6384 EVT Tys[6];
6385 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6386 unsigned n;
6387 for (n = 0; n < NumResultVecs; ++n)
6388 Tys[n] = VecTy;
6389 Tys[n++] = MVT::i32;
6390 Tys[n] = MVT::Other;
6391 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6392 SmallVector<SDValue, 8> Ops;
6393 Ops.push_back(N->getOperand(0)); // incoming chain
6394 Ops.push_back(N->getOperand(AddrOpIdx));
6395 Ops.push_back(Inc);
6396 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6397 Ops.push_back(N->getOperand(i));
6398 }
6399 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6400 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6401 Ops.data(), Ops.size(),
6402 MemInt->getMemoryVT(),
6403 MemInt->getMemOperand());
6404
6405 // Update the uses.
6406 std::vector<SDValue> NewResults;
6407 for (unsigned i = 0; i < NumResultVecs; ++i) {
6408 NewResults.push_back(SDValue(UpdN.getNode(), i));
6409 }
6410 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6411 DCI.CombineTo(N, NewResults);
6412 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6413
6414 break;
Owen Anderson76706012011-04-05 21:48:57 +00006415 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006416 return SDValue();
6417}
6418
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006419/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6420/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6421/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6422/// return true.
6423static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6424 SelectionDAG &DAG = DCI.DAG;
6425 EVT VT = N->getValueType(0);
6426 // vldN-dup instructions only support 64-bit vectors for N > 1.
6427 if (!VT.is64BitVector())
6428 return false;
6429
6430 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6431 SDNode *VLD = N->getOperand(0).getNode();
6432 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6433 return false;
6434 unsigned NumVecs = 0;
6435 unsigned NewOpc = 0;
6436 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6437 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6438 NumVecs = 2;
6439 NewOpc = ARMISD::VLD2DUP;
6440 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6441 NumVecs = 3;
6442 NewOpc = ARMISD::VLD3DUP;
6443 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6444 NumVecs = 4;
6445 NewOpc = ARMISD::VLD4DUP;
6446 } else {
6447 return false;
6448 }
6449
6450 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6451 // numbers match the load.
6452 unsigned VLDLaneNo =
6453 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6454 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6455 UI != UE; ++UI) {
6456 // Ignore uses of the chain result.
6457 if (UI.getUse().getResNo() == NumVecs)
6458 continue;
6459 SDNode *User = *UI;
6460 if (User->getOpcode() != ARMISD::VDUPLANE ||
6461 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6462 return false;
6463 }
6464
6465 // Create the vldN-dup node.
6466 EVT Tys[5];
6467 unsigned n;
6468 for (n = 0; n < NumVecs; ++n)
6469 Tys[n] = VT;
6470 Tys[n] = MVT::Other;
6471 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6472 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6473 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6474 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6475 Ops, 2, VLDMemInt->getMemoryVT(),
6476 VLDMemInt->getMemOperand());
6477
6478 // Update the uses.
6479 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6480 UI != UE; ++UI) {
6481 unsigned ResNo = UI.getUse().getResNo();
6482 // Ignore uses of the chain result.
6483 if (ResNo == NumVecs)
6484 continue;
6485 SDNode *User = *UI;
6486 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6487 }
6488
6489 // Now the vldN-lane intrinsic is dead except for its chain result.
6490 // Update uses of the chain.
6491 std::vector<SDValue> VLDDupResults;
6492 for (unsigned n = 0; n < NumVecs; ++n)
6493 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6494 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6495 DCI.CombineTo(VLD, VLDDupResults);
6496
6497 return true;
6498}
6499
Bob Wilson9e82bf12010-07-14 01:22:12 +00006500/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6501/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006502static SDValue PerformVDUPLANECombine(SDNode *N,
6503 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006504 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006505
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006506 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6507 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6508 if (CombineVLDDUP(N, DCI))
6509 return SDValue(N, 0);
6510
6511 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6512 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006513 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006514 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006515 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006516 return SDValue();
6517
6518 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6519 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6520 // The canonical VMOV for a zero vector uses a 32-bit element size.
6521 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6522 unsigned EltBits;
6523 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6524 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006525 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006526 if (EltSize > VT.getVectorElementType().getSizeInBits())
6527 return SDValue();
6528
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006529 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006530}
6531
Eric Christopherfa6f5912011-06-29 21:10:36 +00006532// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00006533// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
6534static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
6535{
Chad Rosier118c9a02011-06-28 17:26:57 +00006536 integerPart cN;
6537 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00006538 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
6539 I != E; I++) {
6540 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
6541 if (!C)
6542 return false;
6543
Eric Christopherfa6f5912011-06-29 21:10:36 +00006544 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00006545 APFloat APF = C->getValueAPF();
6546 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
6547 != APFloat::opOK || !isExact)
6548 return false;
6549
6550 c0 = (I == 0) ? cN : c0;
6551 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
6552 return false;
6553 }
6554 C = c0;
6555 return true;
6556}
6557
6558/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
6559/// can replace combinations of VMUL and VCVT (floating-point to integer)
6560/// when the VMUL has a constant operand that is a power of 2.
6561///
6562/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6563/// vmul.f32 d16, d17, d16
6564/// vcvt.s32.f32 d16, d16
6565/// becomes:
6566/// vcvt.s32.f32 d16, d16, #3
6567static SDValue PerformVCVTCombine(SDNode *N,
6568 TargetLowering::DAGCombinerInfo &DCI,
6569 const ARMSubtarget *Subtarget) {
6570 SelectionDAG &DAG = DCI.DAG;
6571 SDValue Op = N->getOperand(0);
6572
6573 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
6574 Op.getOpcode() != ISD::FMUL)
6575 return SDValue();
6576
6577 uint64_t C;
6578 SDValue N0 = Op->getOperand(0);
6579 SDValue ConstVec = Op->getOperand(1);
6580 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
6581
Eric Christopherfa6f5912011-06-29 21:10:36 +00006582 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00006583 !isConstVecPow2(ConstVec, isSigned, C))
6584 return SDValue();
6585
6586 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
6587 Intrinsic::arm_neon_vcvtfp2fxu;
6588 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6589 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006590 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00006591 DAG.getConstant(Log2_64(C), MVT::i32));
6592}
6593
6594/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
6595/// can replace combinations of VCVT (integer to floating-point) and VDIV
6596/// when the VDIV has a constant operand that is a power of 2.
6597///
6598/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6599/// vcvt.f32.s32 d16, d16
6600/// vdiv.f32 d16, d17, d16
6601/// becomes:
6602/// vcvt.f32.s32 d16, d16, #3
6603static SDValue PerformVDIVCombine(SDNode *N,
6604 TargetLowering::DAGCombinerInfo &DCI,
6605 const ARMSubtarget *Subtarget) {
6606 SelectionDAG &DAG = DCI.DAG;
6607 SDValue Op = N->getOperand(0);
6608 unsigned OpOpcode = Op.getNode()->getOpcode();
6609
6610 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
6611 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
6612 return SDValue();
6613
6614 uint64_t C;
6615 SDValue ConstVec = N->getOperand(1);
6616 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
6617
6618 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
6619 !isConstVecPow2(ConstVec, isSigned, C))
6620 return SDValue();
6621
Eric Christopherfa6f5912011-06-29 21:10:36 +00006622 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00006623 Intrinsic::arm_neon_vcvtfxu2fp;
6624 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6625 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006626 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00006627 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
6628}
6629
6630/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00006631/// operand of a vector shift operation, where all the elements of the
6632/// build_vector must have the same constant integer value.
6633static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6634 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006635 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006636 Op = Op.getOperand(0);
6637 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6638 APInt SplatBits, SplatUndef;
6639 unsigned SplatBitSize;
6640 bool HasAnyUndefs;
6641 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6642 HasAnyUndefs, ElementBits) ||
6643 SplatBitSize > ElementBits)
6644 return false;
6645 Cnt = SplatBits.getSExtValue();
6646 return true;
6647}
6648
6649/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6650/// operand of a vector shift left operation. That value must be in the range:
6651/// 0 <= Value < ElementBits for a left shift; or
6652/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006653static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006654 assert(VT.isVector() && "vector shift count is not a vector type");
6655 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6656 if (! getVShiftImm(Op, ElementBits, Cnt))
6657 return false;
6658 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6659}
6660
6661/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6662/// operand of a vector shift right operation. For a shift opcode, the value
6663/// is positive, but for an intrinsic the value count must be negative. The
6664/// absolute value must be in the range:
6665/// 1 <= |Value| <= ElementBits for a right shift; or
6666/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006667static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006668 int64_t &Cnt) {
6669 assert(VT.isVector() && "vector shift count is not a vector type");
6670 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6671 if (! getVShiftImm(Op, ElementBits, Cnt))
6672 return false;
6673 if (isIntrinsic)
6674 Cnt = -Cnt;
6675 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6676}
6677
6678/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6679static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6680 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6681 switch (IntNo) {
6682 default:
6683 // Don't do anything for most intrinsics.
6684 break;
6685
6686 // Vector shifts: check for immediate versions and lower them.
6687 // Note: This is done during DAG combining instead of DAG legalizing because
6688 // the build_vectors for 64-bit vector element shift counts are generally
6689 // not legal, and it is hard to see their values after they get legalized to
6690 // loads from a constant pool.
6691 case Intrinsic::arm_neon_vshifts:
6692 case Intrinsic::arm_neon_vshiftu:
6693 case Intrinsic::arm_neon_vshiftls:
6694 case Intrinsic::arm_neon_vshiftlu:
6695 case Intrinsic::arm_neon_vshiftn:
6696 case Intrinsic::arm_neon_vrshifts:
6697 case Intrinsic::arm_neon_vrshiftu:
6698 case Intrinsic::arm_neon_vrshiftn:
6699 case Intrinsic::arm_neon_vqshifts:
6700 case Intrinsic::arm_neon_vqshiftu:
6701 case Intrinsic::arm_neon_vqshiftsu:
6702 case Intrinsic::arm_neon_vqshiftns:
6703 case Intrinsic::arm_neon_vqshiftnu:
6704 case Intrinsic::arm_neon_vqshiftnsu:
6705 case Intrinsic::arm_neon_vqrshiftns:
6706 case Intrinsic::arm_neon_vqrshiftnu:
6707 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006708 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006709 int64_t Cnt;
6710 unsigned VShiftOpc = 0;
6711
6712 switch (IntNo) {
6713 case Intrinsic::arm_neon_vshifts:
6714 case Intrinsic::arm_neon_vshiftu:
6715 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6716 VShiftOpc = ARMISD::VSHL;
6717 break;
6718 }
6719 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6720 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6721 ARMISD::VSHRs : ARMISD::VSHRu);
6722 break;
6723 }
6724 return SDValue();
6725
6726 case Intrinsic::arm_neon_vshiftls:
6727 case Intrinsic::arm_neon_vshiftlu:
6728 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6729 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006730 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006731
6732 case Intrinsic::arm_neon_vrshifts:
6733 case Intrinsic::arm_neon_vrshiftu:
6734 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6735 break;
6736 return SDValue();
6737
6738 case Intrinsic::arm_neon_vqshifts:
6739 case Intrinsic::arm_neon_vqshiftu:
6740 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6741 break;
6742 return SDValue();
6743
6744 case Intrinsic::arm_neon_vqshiftsu:
6745 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6746 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006747 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006748
6749 case Intrinsic::arm_neon_vshiftn:
6750 case Intrinsic::arm_neon_vrshiftn:
6751 case Intrinsic::arm_neon_vqshiftns:
6752 case Intrinsic::arm_neon_vqshiftnu:
6753 case Intrinsic::arm_neon_vqshiftnsu:
6754 case Intrinsic::arm_neon_vqrshiftns:
6755 case Intrinsic::arm_neon_vqrshiftnu:
6756 case Intrinsic::arm_neon_vqrshiftnsu:
6757 // Narrowing shifts require an immediate right shift.
6758 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6759 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006760 llvm_unreachable("invalid shift count for narrowing vector shift "
6761 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006762
6763 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006764 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006765 }
6766
6767 switch (IntNo) {
6768 case Intrinsic::arm_neon_vshifts:
6769 case Intrinsic::arm_neon_vshiftu:
6770 // Opcode already set above.
6771 break;
6772 case Intrinsic::arm_neon_vshiftls:
6773 case Intrinsic::arm_neon_vshiftlu:
6774 if (Cnt == VT.getVectorElementType().getSizeInBits())
6775 VShiftOpc = ARMISD::VSHLLi;
6776 else
6777 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6778 ARMISD::VSHLLs : ARMISD::VSHLLu);
6779 break;
6780 case Intrinsic::arm_neon_vshiftn:
6781 VShiftOpc = ARMISD::VSHRN; break;
6782 case Intrinsic::arm_neon_vrshifts:
6783 VShiftOpc = ARMISD::VRSHRs; break;
6784 case Intrinsic::arm_neon_vrshiftu:
6785 VShiftOpc = ARMISD::VRSHRu; break;
6786 case Intrinsic::arm_neon_vrshiftn:
6787 VShiftOpc = ARMISD::VRSHRN; break;
6788 case Intrinsic::arm_neon_vqshifts:
6789 VShiftOpc = ARMISD::VQSHLs; break;
6790 case Intrinsic::arm_neon_vqshiftu:
6791 VShiftOpc = ARMISD::VQSHLu; break;
6792 case Intrinsic::arm_neon_vqshiftsu:
6793 VShiftOpc = ARMISD::VQSHLsu; break;
6794 case Intrinsic::arm_neon_vqshiftns:
6795 VShiftOpc = ARMISD::VQSHRNs; break;
6796 case Intrinsic::arm_neon_vqshiftnu:
6797 VShiftOpc = ARMISD::VQSHRNu; break;
6798 case Intrinsic::arm_neon_vqshiftnsu:
6799 VShiftOpc = ARMISD::VQSHRNsu; break;
6800 case Intrinsic::arm_neon_vqrshiftns:
6801 VShiftOpc = ARMISD::VQRSHRNs; break;
6802 case Intrinsic::arm_neon_vqrshiftnu:
6803 VShiftOpc = ARMISD::VQRSHRNu; break;
6804 case Intrinsic::arm_neon_vqrshiftnsu:
6805 VShiftOpc = ARMISD::VQRSHRNsu; break;
6806 }
6807
6808 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006809 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006810 }
6811
6812 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006813 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006814 int64_t Cnt;
6815 unsigned VShiftOpc = 0;
6816
6817 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6818 VShiftOpc = ARMISD::VSLI;
6819 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6820 VShiftOpc = ARMISD::VSRI;
6821 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006822 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006823 }
6824
6825 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6826 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006827 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006828 }
6829
6830 case Intrinsic::arm_neon_vqrshifts:
6831 case Intrinsic::arm_neon_vqrshiftu:
6832 // No immediate versions of these to check for.
6833 break;
6834 }
6835
6836 return SDValue();
6837}
6838
6839/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6840/// lowers them. As with the vector shift intrinsics, this is done during DAG
6841/// combining instead of DAG legalizing because the build_vectors for 64-bit
6842/// vector element shift counts are generally not legal, and it is hard to see
6843/// their values after they get legalized to loads from a constant pool.
6844static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6845 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006846 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006847
6848 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006849 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6850 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006851 return SDValue();
6852
6853 assert(ST->hasNEON() && "unexpected vector shift");
6854 int64_t Cnt;
6855
6856 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006857 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006858
6859 case ISD::SHL:
6860 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6861 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006863 break;
6864
6865 case ISD::SRA:
6866 case ISD::SRL:
6867 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6868 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6869 ARMISD::VSHRs : ARMISD::VSHRu);
6870 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006871 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006872 }
6873 }
6874 return SDValue();
6875}
6876
6877/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6878/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6879static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6880 const ARMSubtarget *ST) {
6881 SDValue N0 = N->getOperand(0);
6882
6883 // Check for sign- and zero-extensions of vector extract operations of 8-
6884 // and 16-bit vector elements. NEON supports these directly. They are
6885 // handled during DAG combining because type legalization will promote them
6886 // to 32-bit types and it is messy to recognize the operations after that.
6887 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6888 SDValue Vec = N0.getOperand(0);
6889 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006890 EVT VT = N->getValueType(0);
6891 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006892 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6893
Owen Anderson825b72b2009-08-11 20:47:22 +00006894 if (VT == MVT::i32 &&
6895 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006896 TLI.isTypeLegal(Vec.getValueType()) &&
6897 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006898
6899 unsigned Opc = 0;
6900 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006901 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006902 case ISD::SIGN_EXTEND:
6903 Opc = ARMISD::VGETLANEs;
6904 break;
6905 case ISD::ZERO_EXTEND:
6906 case ISD::ANY_EXTEND:
6907 Opc = ARMISD::VGETLANEu;
6908 break;
6909 }
6910 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6911 }
6912 }
6913
6914 return SDValue();
6915}
6916
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006917/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6918/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6919static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6920 const ARMSubtarget *ST) {
6921 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006922 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006923 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6924 // a NaN; only do the transformation when it matches that behavior.
6925
6926 // For now only do this when using NEON for FP operations; if using VFP, it
6927 // is not obvious that the benefit outweighs the cost of switching to the
6928 // NEON pipeline.
6929 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6930 N->getValueType(0) != MVT::f32)
6931 return SDValue();
6932
6933 SDValue CondLHS = N->getOperand(0);
6934 SDValue CondRHS = N->getOperand(1);
6935 SDValue LHS = N->getOperand(2);
6936 SDValue RHS = N->getOperand(3);
6937 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6938
6939 unsigned Opcode = 0;
6940 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006941 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006942 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006943 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006944 IsReversed = true ; // x CC y ? y : x
6945 } else {
6946 return SDValue();
6947 }
6948
Bob Wilsone742bb52010-02-24 22:15:53 +00006949 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006950 switch (CC) {
6951 default: break;
6952 case ISD::SETOLT:
6953 case ISD::SETOLE:
6954 case ISD::SETLT:
6955 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006956 case ISD::SETULT:
6957 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006958 // If LHS is NaN, an ordered comparison will be false and the result will
6959 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6960 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6961 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6962 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6963 break;
6964 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6965 // will return -0, so vmin can only be used for unsafe math or if one of
6966 // the operands is known to be nonzero.
6967 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6968 !UnsafeFPMath &&
6969 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6970 break;
6971 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006972 break;
6973
6974 case ISD::SETOGT:
6975 case ISD::SETOGE:
6976 case ISD::SETGT:
6977 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006978 case ISD::SETUGT:
6979 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006980 // If LHS is NaN, an ordered comparison will be false and the result will
6981 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6982 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6983 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6984 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6985 break;
6986 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6987 // will return +0, so vmax can only be used for unsafe math or if one of
6988 // the operands is known to be nonzero.
6989 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6990 !UnsafeFPMath &&
6991 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6992 break;
6993 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006994 break;
6995 }
6996
6997 if (!Opcode)
6998 return SDValue();
6999 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
7000}
7001
Evan Chenge721f5c2011-07-13 00:42:17 +00007002/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
7003SDValue
7004ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
7005 SDValue Cmp = N->getOperand(4);
7006 if (Cmp.getOpcode() != ARMISD::CMPZ)
7007 // Only looking at EQ and NE cases.
7008 return SDValue();
7009
7010 EVT VT = N->getValueType(0);
7011 DebugLoc dl = N->getDebugLoc();
7012 SDValue LHS = Cmp.getOperand(0);
7013 SDValue RHS = Cmp.getOperand(1);
7014 SDValue FalseVal = N->getOperand(0);
7015 SDValue TrueVal = N->getOperand(1);
7016 SDValue ARMcc = N->getOperand(2);
7017 ARMCC::CondCodes CC = (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
7018
7019 // Simplify
7020 // mov r1, r0
7021 // cmp r1, x
7022 // mov r0, y
7023 // moveq r0, x
7024 // to
7025 // cmp r0, x
7026 // movne r0, y
7027 //
7028 // mov r1, r0
7029 // cmp r1, x
7030 // mov r0, x
7031 // movne r0, y
7032 // to
7033 // cmp r0, x
7034 // movne r0, y
7035 /// FIXME: Turn this into a target neutral optimization?
7036 SDValue Res;
7037 if (CC == ARMCC::NE && FalseVal == RHS) {
7038 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
7039 N->getOperand(3), Cmp);
7040 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
7041 SDValue ARMcc;
7042 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
7043 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
7044 N->getOperand(3), NewCmp);
7045 }
7046
7047 if (Res.getNode()) {
7048 APInt KnownZero, KnownOne;
7049 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
7050 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
7051 // Capture demanded bits information that would be otherwise lost.
7052 if (KnownZero == 0xfffffffe)
7053 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7054 DAG.getValueType(MVT::i1));
7055 else if (KnownZero == 0xffffff00)
7056 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7057 DAG.getValueType(MVT::i8));
7058 else if (KnownZero == 0xffff0000)
7059 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7060 DAG.getValueType(MVT::i16));
7061 }
7062
7063 return Res;
7064}
7065
Dan Gohman475871a2008-07-27 21:46:04 +00007066SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007067 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007068 switch (N->getOpcode()) {
7069 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00007070 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007071 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007072 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007073 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00007074 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00007075 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00007076 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007077 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00007078 case ISD::STORE: return PerformSTORECombine(N, DCI);
7079 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
7080 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00007081 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007082 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00007083 case ISD::FP_TO_SINT:
7084 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
7085 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007086 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00007087 case ISD::SHL:
7088 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007089 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00007090 case ISD::SIGN_EXTEND:
7091 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007092 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
7093 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00007094 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00007095 case ARMISD::VLD2DUP:
7096 case ARMISD::VLD3DUP:
7097 case ARMISD::VLD4DUP:
7098 return CombineBaseUpdate(N, DCI);
7099 case ISD::INTRINSIC_VOID:
7100 case ISD::INTRINSIC_W_CHAIN:
7101 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7102 case Intrinsic::arm_neon_vld1:
7103 case Intrinsic::arm_neon_vld2:
7104 case Intrinsic::arm_neon_vld3:
7105 case Intrinsic::arm_neon_vld4:
7106 case Intrinsic::arm_neon_vld2lane:
7107 case Intrinsic::arm_neon_vld3lane:
7108 case Intrinsic::arm_neon_vld4lane:
7109 case Intrinsic::arm_neon_vst1:
7110 case Intrinsic::arm_neon_vst2:
7111 case Intrinsic::arm_neon_vst3:
7112 case Intrinsic::arm_neon_vst4:
7113 case Intrinsic::arm_neon_vst2lane:
7114 case Intrinsic::arm_neon_vst3lane:
7115 case Intrinsic::arm_neon_vst4lane:
7116 return CombineBaseUpdate(N, DCI);
7117 default: break;
7118 }
7119 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007120 }
Dan Gohman475871a2008-07-27 21:46:04 +00007121 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007122}
7123
Evan Cheng31959b12011-02-02 01:06:55 +00007124bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
7125 EVT VT) const {
7126 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
7127}
7128
Bill Wendlingaf566342009-08-15 21:21:19 +00007129bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00007130 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00007131 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00007132
7133 switch (VT.getSimpleVT().SimpleTy) {
7134 default:
7135 return false;
7136 case MVT::i8:
7137 case MVT::i16:
7138 case MVT::i32:
7139 return true;
7140 // FIXME: VLD1 etc with standard alignment is legal.
7141 }
7142}
7143
Evan Chenge6c835f2009-08-14 20:09:37 +00007144static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
7145 if (V < 0)
7146 return false;
7147
7148 unsigned Scale = 1;
7149 switch (VT.getSimpleVT().SimpleTy) {
7150 default: return false;
7151 case MVT::i1:
7152 case MVT::i8:
7153 // Scale == 1;
7154 break;
7155 case MVT::i16:
7156 // Scale == 2;
7157 Scale = 2;
7158 break;
7159 case MVT::i32:
7160 // Scale == 4;
7161 Scale = 4;
7162 break;
7163 }
7164
7165 if ((V & (Scale - 1)) != 0)
7166 return false;
7167 V /= Scale;
7168 return V == (V & ((1LL << 5) - 1));
7169}
7170
7171static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
7172 const ARMSubtarget *Subtarget) {
7173 bool isNeg = false;
7174 if (V < 0) {
7175 isNeg = true;
7176 V = - V;
7177 }
7178
7179 switch (VT.getSimpleVT().SimpleTy) {
7180 default: return false;
7181 case MVT::i1:
7182 case MVT::i8:
7183 case MVT::i16:
7184 case MVT::i32:
7185 // + imm12 or - imm8
7186 if (isNeg)
7187 return V == (V & ((1LL << 8) - 1));
7188 return V == (V & ((1LL << 12) - 1));
7189 case MVT::f32:
7190 case MVT::f64:
7191 // Same as ARM mode. FIXME: NEON?
7192 if (!Subtarget->hasVFP2())
7193 return false;
7194 if ((V & 3) != 0)
7195 return false;
7196 V >>= 2;
7197 return V == (V & ((1LL << 8) - 1));
7198 }
7199}
7200
Evan Chengb01fad62007-03-12 23:30:29 +00007201/// isLegalAddressImmediate - Return true if the integer value can be used
7202/// as the offset of the target addressing mode for load / store of the
7203/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00007204static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007205 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00007206 if (V == 0)
7207 return true;
7208
Evan Cheng65011532009-03-09 19:15:00 +00007209 if (!VT.isSimple())
7210 return false;
7211
Evan Chenge6c835f2009-08-14 20:09:37 +00007212 if (Subtarget->isThumb1Only())
7213 return isLegalT1AddressImmediate(V, VT);
7214 else if (Subtarget->isThumb2())
7215 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007216
Evan Chenge6c835f2009-08-14 20:09:37 +00007217 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007218 if (V < 0)
7219 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007220 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007221 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007222 case MVT::i1:
7223 case MVT::i8:
7224 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007225 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007226 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007227 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007228 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007229 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007230 case MVT::f32:
7231 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007232 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007233 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007234 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007235 return false;
7236 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007237 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007238 }
Evan Chenga8e29892007-01-19 07:51:42 +00007239}
7240
Evan Chenge6c835f2009-08-14 20:09:37 +00007241bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7242 EVT VT) const {
7243 int Scale = AM.Scale;
7244 if (Scale < 0)
7245 return false;
7246
7247 switch (VT.getSimpleVT().SimpleTy) {
7248 default: return false;
7249 case MVT::i1:
7250 case MVT::i8:
7251 case MVT::i16:
7252 case MVT::i32:
7253 if (Scale == 1)
7254 return true;
7255 // r + r << imm
7256 Scale = Scale & ~1;
7257 return Scale == 2 || Scale == 4 || Scale == 8;
7258 case MVT::i64:
7259 // r + r
7260 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7261 return true;
7262 return false;
7263 case MVT::isVoid:
7264 // Note, we allow "void" uses (basically, uses that aren't loads or
7265 // stores), because arm allows folding a scale into many arithmetic
7266 // operations. This should be made more precise and revisited later.
7267
7268 // Allow r << imm, but the imm has to be a multiple of two.
7269 if (Scale & 1) return false;
7270 return isPowerOf2_32(Scale);
7271 }
7272}
7273
Chris Lattner37caf8c2007-04-09 23:33:39 +00007274/// isLegalAddressingMode - Return true if the addressing mode represented
7275/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007276bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007277 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007278 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007279 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007280 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007281
Chris Lattner37caf8c2007-04-09 23:33:39 +00007282 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007283 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007284 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007285
Chris Lattner37caf8c2007-04-09 23:33:39 +00007286 switch (AM.Scale) {
7287 case 0: // no scale reg, must be "r+i" or "r", or "i".
7288 break;
7289 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007290 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007291 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007292 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007293 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007294 // ARM doesn't support any R+R*scale+imm addr modes.
7295 if (AM.BaseOffs)
7296 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007297
Bob Wilson2c7dab12009-04-08 17:55:28 +00007298 if (!VT.isSimple())
7299 return false;
7300
Evan Chenge6c835f2009-08-14 20:09:37 +00007301 if (Subtarget->isThumb2())
7302 return isLegalT2ScaledAddressingMode(AM, VT);
7303
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007304 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007305 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007306 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007307 case MVT::i1:
7308 case MVT::i8:
7309 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007310 if (Scale < 0) Scale = -Scale;
7311 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007312 return true;
7313 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007314 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007315 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007316 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007317 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007318 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007319 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007320 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007321
Owen Anderson825b72b2009-08-11 20:47:22 +00007322 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007323 // Note, we allow "void" uses (basically, uses that aren't loads or
7324 // stores), because arm allows folding a scale into many arithmetic
7325 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007326
Chris Lattner37caf8c2007-04-09 23:33:39 +00007327 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007328 if (Scale & 1) return false;
7329 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007330 }
7331 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007332 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007333 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007334}
7335
Evan Cheng77e47512009-11-11 19:05:52 +00007336/// isLegalICmpImmediate - Return true if the specified immediate is legal
7337/// icmp immediate, that is the target has icmp instructions which can compare
7338/// a register against the immediate without having to materialize the
7339/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007340bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007341 if (!Subtarget->isThumb())
7342 return ARM_AM::getSOImmVal(Imm) != -1;
7343 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007344 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007345 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007346}
7347
Dan Gohmancca82142011-05-03 00:46:49 +00007348/// isLegalAddImmediate - Return true if the specified immediate is legal
7349/// add immediate, that is the target has add instructions which can add
7350/// a register with the immediate without having to materialize the
7351/// immediate into a register.
7352bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7353 return ARM_AM::getSOImmVal(Imm) != -1;
7354}
7355
Owen Andersone50ed302009-08-10 22:56:29 +00007356static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007357 bool isSEXTLoad, SDValue &Base,
7358 SDValue &Offset, bool &isInc,
7359 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007360 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7361 return false;
7362
Owen Anderson825b72b2009-08-11 20:47:22 +00007363 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007364 // AddressingMode 3
7365 Base = Ptr->getOperand(0);
7366 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007367 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007368 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007369 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007370 isInc = false;
7371 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7372 return true;
7373 }
7374 }
7375 isInc = (Ptr->getOpcode() == ISD::ADD);
7376 Offset = Ptr->getOperand(1);
7377 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007378 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007379 // AddressingMode 2
7380 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007381 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007382 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007383 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007384 isInc = false;
7385 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7386 Base = Ptr->getOperand(0);
7387 return true;
7388 }
7389 }
7390
7391 if (Ptr->getOpcode() == ISD::ADD) {
7392 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00007393 ARM_AM::ShiftOpc ShOpcVal=
7394 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00007395 if (ShOpcVal != ARM_AM::no_shift) {
7396 Base = Ptr->getOperand(1);
7397 Offset = Ptr->getOperand(0);
7398 } else {
7399 Base = Ptr->getOperand(0);
7400 Offset = Ptr->getOperand(1);
7401 }
7402 return true;
7403 }
7404
7405 isInc = (Ptr->getOpcode() == ISD::ADD);
7406 Base = Ptr->getOperand(0);
7407 Offset = Ptr->getOperand(1);
7408 return true;
7409 }
7410
Jim Grosbache5165492009-11-09 00:11:35 +00007411 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007412 return false;
7413}
7414
Owen Andersone50ed302009-08-10 22:56:29 +00007415static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007416 bool isSEXTLoad, SDValue &Base,
7417 SDValue &Offset, bool &isInc,
7418 SelectionDAG &DAG) {
7419 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7420 return false;
7421
7422 Base = Ptr->getOperand(0);
7423 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7424 int RHSC = (int)RHS->getZExtValue();
7425 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7426 assert(Ptr->getOpcode() == ISD::ADD);
7427 isInc = false;
7428 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7429 return true;
7430 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7431 isInc = Ptr->getOpcode() == ISD::ADD;
7432 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7433 return true;
7434 }
7435 }
7436
7437 return false;
7438}
7439
Evan Chenga8e29892007-01-19 07:51:42 +00007440/// getPreIndexedAddressParts - returns true by value, base pointer and
7441/// offset pointer and addressing mode by reference if the node's address
7442/// can be legally represented as pre-indexed load / store address.
7443bool
Dan Gohman475871a2008-07-27 21:46:04 +00007444ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7445 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007446 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007447 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007448 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007449 return false;
7450
Owen Andersone50ed302009-08-10 22:56:29 +00007451 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007452 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007453 bool isSEXTLoad = false;
7454 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7455 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007456 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007457 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7458 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7459 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007460 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007461 } else
7462 return false;
7463
7464 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007465 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007466 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007467 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7468 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007469 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007470 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007471 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007472 if (!isLegal)
7473 return false;
7474
7475 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7476 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007477}
7478
7479/// getPostIndexedAddressParts - returns true by value, base pointer and
7480/// offset pointer and addressing mode by reference if this node can be
7481/// combined with a load / store to form a post-indexed load / store.
7482bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007483 SDValue &Base,
7484 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007485 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007486 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007487 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007488 return false;
7489
Owen Andersone50ed302009-08-10 22:56:29 +00007490 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007491 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007492 bool isSEXTLoad = false;
7493 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007494 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007495 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007496 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7497 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007498 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007499 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007500 } else
7501 return false;
7502
7503 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007504 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007505 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007506 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007507 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007508 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007509 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7510 isInc, DAG);
7511 if (!isLegal)
7512 return false;
7513
Evan Cheng28dad2a2010-05-18 21:31:17 +00007514 if (Ptr != Base) {
7515 // Swap base ptr and offset to catch more post-index load / store when
7516 // it's legal. In Thumb2 mode, offset must be an immediate.
7517 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7518 !Subtarget->isThumb2())
7519 std::swap(Base, Offset);
7520
7521 // Post-indexed load / store update the base pointer.
7522 if (Ptr != Base)
7523 return false;
7524 }
7525
Evan Chenge88d5ce2009-07-02 07:28:31 +00007526 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7527 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007528}
7529
Dan Gohman475871a2008-07-27 21:46:04 +00007530void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007531 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007532 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007533 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007534 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007535 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007536 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007537 switch (Op.getOpcode()) {
7538 default: break;
7539 case ARMISD::CMOV: {
7540 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007541 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007542 if (KnownZero == 0 && KnownOne == 0) return;
7543
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007544 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007545 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7546 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007547 KnownZero &= KnownZeroRHS;
7548 KnownOne &= KnownOneRHS;
7549 return;
7550 }
7551 }
7552}
7553
7554//===----------------------------------------------------------------------===//
7555// ARM Inline Assembly Support
7556//===----------------------------------------------------------------------===//
7557
Evan Cheng55d42002011-01-08 01:24:27 +00007558bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7559 // Looking for "rev" which is V6+.
7560 if (!Subtarget->hasV6Ops())
7561 return false;
7562
7563 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7564 std::string AsmStr = IA->getAsmString();
7565 SmallVector<StringRef, 4> AsmPieces;
7566 SplitString(AsmStr, AsmPieces, ";\n");
7567
7568 switch (AsmPieces.size()) {
7569 default: return false;
7570 case 1:
7571 AsmStr = AsmPieces[0];
7572 AsmPieces.clear();
7573 SplitString(AsmStr, AsmPieces, " \t,");
7574
7575 // rev $0, $1
7576 if (AsmPieces.size() == 3 &&
7577 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7578 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007579 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00007580 if (Ty && Ty->getBitWidth() == 32)
7581 return IntrinsicLowering::LowerToByteSwap(CI);
7582 }
7583 break;
7584 }
7585
7586 return false;
7587}
7588
Evan Chenga8e29892007-01-19 07:51:42 +00007589/// getConstraintType - Given a constraint letter, return the type of
7590/// constraint it is for this target.
7591ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007592ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7593 if (Constraint.size() == 1) {
7594 switch (Constraint[0]) {
7595 default: break;
7596 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007597 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00007598 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007599 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007600 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00007601 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00007602 // An address with a single base register. Due to the way we
7603 // currently handle addresses it is the same as an 'r' memory constraint.
7604 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007605 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007606 } else if (Constraint.size() == 2) {
7607 switch (Constraint[0]) {
7608 default: break;
7609 // All 'U+' constraints are addresses.
7610 case 'U': return C_Memory;
7611 }
Evan Chenga8e29892007-01-19 07:51:42 +00007612 }
Chris Lattner4234f572007-03-25 02:14:49 +00007613 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007614}
7615
John Thompson44ab89e2010-10-29 17:29:13 +00007616/// Examine constraint type and operand type and determine a weight value.
7617/// This object must already have been set up with the operand type
7618/// and the current alternative constraint selected.
7619TargetLowering::ConstraintWeight
7620ARMTargetLowering::getSingleConstraintMatchWeight(
7621 AsmOperandInfo &info, const char *constraint) const {
7622 ConstraintWeight weight = CW_Invalid;
7623 Value *CallOperandVal = info.CallOperandVal;
7624 // If we don't have a value, we can't do a match,
7625 // but allow it at the lowest weight.
7626 if (CallOperandVal == NULL)
7627 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007628 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007629 // Look at the constraint type.
7630 switch (*constraint) {
7631 default:
7632 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7633 break;
7634 case 'l':
7635 if (type->isIntegerTy()) {
7636 if (Subtarget->isThumb())
7637 weight = CW_SpecificReg;
7638 else
7639 weight = CW_Register;
7640 }
7641 break;
7642 case 'w':
7643 if (type->isFloatingPointTy())
7644 weight = CW_Register;
7645 break;
7646 }
7647 return weight;
7648}
7649
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007650typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7651RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007652ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007653 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007654 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007655 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007656 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007657 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007658 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007659 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007660 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007661 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007662 case 'h': // High regs or no regs.
7663 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007664 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007665 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007666 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007667 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007668 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007669 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007670 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007671 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007672 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007673 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007674 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007675 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007676 case 'x':
7677 if (VT == MVT::f32)
7678 return RCPair(0U, ARM::SPR_8RegisterClass);
7679 if (VT.getSizeInBits() == 64)
7680 return RCPair(0U, ARM::DPR_8RegisterClass);
7681 if (VT.getSizeInBits() == 128)
7682 return RCPair(0U, ARM::QPR_8RegisterClass);
7683 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007684 case 't':
7685 if (VT == MVT::f32)
7686 return RCPair(0U, ARM::SPRRegisterClass);
7687 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007688 }
7689 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007690 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007691 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007692
Evan Chenga8e29892007-01-19 07:51:42 +00007693 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7694}
7695
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007696/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7697/// vector. If it is invalid, don't add anything to Ops.
7698void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007699 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007700 std::vector<SDValue>&Ops,
7701 SelectionDAG &DAG) const {
7702 SDValue Result(0, 0);
7703
Eric Christopher100c8332011-06-02 23:16:42 +00007704 // Currently only support length 1 constraints.
7705 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007706
Eric Christopher100c8332011-06-02 23:16:42 +00007707 char ConstraintLetter = Constraint[0];
7708 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007709 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007710 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007711 case 'I': case 'J': case 'K': case 'L':
7712 case 'M': case 'N': case 'O':
7713 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7714 if (!C)
7715 return;
7716
7717 int64_t CVal64 = C->getSExtValue();
7718 int CVal = (int) CVal64;
7719 // None of these constraints allow values larger than 32 bits. Check
7720 // that the value fits in an int.
7721 if (CVal != CVal64)
7722 return;
7723
Eric Christopher100c8332011-06-02 23:16:42 +00007724 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007725 case 'j':
7726 // Constant suitable for movw, must be between 0 and
7727 // 65535.
7728 if (Subtarget->hasV6T2Ops())
7729 if (CVal >= 0 && CVal <= 65535)
7730 break;
7731 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007732 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007733 if (Subtarget->isThumb1Only()) {
7734 // This must be a constant between 0 and 255, for ADD
7735 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007736 if (CVal >= 0 && CVal <= 255)
7737 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007738 } else if (Subtarget->isThumb2()) {
7739 // A constant that can be used as an immediate value in a
7740 // data-processing instruction.
7741 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7742 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007743 } else {
7744 // A constant that can be used as an immediate value in a
7745 // data-processing instruction.
7746 if (ARM_AM::getSOImmVal(CVal) != -1)
7747 break;
7748 }
7749 return;
7750
7751 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007752 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007753 // This must be a constant between -255 and -1, for negated ADD
7754 // immediates. This can be used in GCC with an "n" modifier that
7755 // prints the negated value, for use with SUB instructions. It is
7756 // not useful otherwise but is implemented for compatibility.
7757 if (CVal >= -255 && CVal <= -1)
7758 break;
7759 } else {
7760 // This must be a constant between -4095 and 4095. It is not clear
7761 // what this constraint is intended for. Implemented for
7762 // compatibility with GCC.
7763 if (CVal >= -4095 && CVal <= 4095)
7764 break;
7765 }
7766 return;
7767
7768 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007769 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007770 // A 32-bit value where only one byte has a nonzero value. Exclude
7771 // zero to match GCC. This constraint is used by GCC internally for
7772 // constants that can be loaded with a move/shift combination.
7773 // It is not useful otherwise but is implemented for compatibility.
7774 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7775 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007776 } else if (Subtarget->isThumb2()) {
7777 // A constant whose bitwise inverse can be used as an immediate
7778 // value in a data-processing instruction. This can be used in GCC
7779 // with a "B" modifier that prints the inverted value, for use with
7780 // BIC and MVN instructions. It is not useful otherwise but is
7781 // implemented for compatibility.
7782 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7783 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007784 } else {
7785 // A constant whose bitwise inverse can be used as an immediate
7786 // value in a data-processing instruction. This can be used in GCC
7787 // with a "B" modifier that prints the inverted value, for use with
7788 // BIC and MVN instructions. It is not useful otherwise but is
7789 // implemented for compatibility.
7790 if (ARM_AM::getSOImmVal(~CVal) != -1)
7791 break;
7792 }
7793 return;
7794
7795 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007796 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007797 // This must be a constant between -7 and 7,
7798 // for 3-operand ADD/SUB immediate instructions.
7799 if (CVal >= -7 && CVal < 7)
7800 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007801 } else if (Subtarget->isThumb2()) {
7802 // A constant whose negation can be used as an immediate value in a
7803 // data-processing instruction. This can be used in GCC with an "n"
7804 // modifier that prints the negated value, for use with SUB
7805 // instructions. It is not useful otherwise but is implemented for
7806 // compatibility.
7807 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7808 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007809 } else {
7810 // A constant whose negation can be used as an immediate value in a
7811 // data-processing instruction. This can be used in GCC with an "n"
7812 // modifier that prints the negated value, for use with SUB
7813 // instructions. It is not useful otherwise but is implemented for
7814 // compatibility.
7815 if (ARM_AM::getSOImmVal(-CVal) != -1)
7816 break;
7817 }
7818 return;
7819
7820 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007821 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007822 // This must be a multiple of 4 between 0 and 1020, for
7823 // ADD sp + immediate.
7824 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7825 break;
7826 } else {
7827 // A power of two or a constant between 0 and 32. This is used in
7828 // GCC for the shift amount on shifted register operands, but it is
7829 // useful in general for any shift amounts.
7830 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7831 break;
7832 }
7833 return;
7834
7835 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007836 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007837 // This must be a constant between 0 and 31, for shift amounts.
7838 if (CVal >= 0 && CVal <= 31)
7839 break;
7840 }
7841 return;
7842
7843 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007844 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007845 // This must be a multiple of 4 between -508 and 508, for
7846 // ADD/SUB sp = sp + immediate.
7847 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7848 break;
7849 }
7850 return;
7851 }
7852 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7853 break;
7854 }
7855
7856 if (Result.getNode()) {
7857 Ops.push_back(Result);
7858 return;
7859 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007860 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007861}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007862
7863bool
7864ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7865 // The ARM target isn't yet aware of offsets.
7866 return false;
7867}
Evan Cheng39382422009-10-28 01:44:26 +00007868
7869int ARM::getVFPf32Imm(const APFloat &FPImm) {
7870 APInt Imm = FPImm.bitcastToAPInt();
7871 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7872 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7873 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7874
7875 // We can handle 4 bits of mantissa.
7876 // mantissa = (16+UInt(e:f:g:h))/16.
7877 if (Mantissa & 0x7ffff)
7878 return -1;
7879 Mantissa >>= 19;
7880 if ((Mantissa & 0xf) != Mantissa)
7881 return -1;
7882
7883 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7884 if (Exp < -3 || Exp > 4)
7885 return -1;
7886 Exp = ((Exp+3) & 0x7) ^ 4;
7887
7888 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7889}
7890
7891int ARM::getVFPf64Imm(const APFloat &FPImm) {
7892 APInt Imm = FPImm.bitcastToAPInt();
7893 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7894 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7895 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7896
7897 // We can handle 4 bits of mantissa.
7898 // mantissa = (16+UInt(e:f:g:h))/16.
7899 if (Mantissa & 0xffffffffffffLL)
7900 return -1;
7901 Mantissa >>= 48;
7902 if ((Mantissa & 0xf) != Mantissa)
7903 return -1;
7904
7905 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7906 if (Exp < -3 || Exp > 4)
7907 return -1;
7908 Exp = ((Exp+3) & 0x7) ^ 4;
7909
7910 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7911}
7912
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007913bool ARM::isBitFieldInvertedMask(unsigned v) {
7914 if (v == 0xffffffff)
7915 return 0;
7916 // there can be 1's on either or both "outsides", all the "inside"
7917 // bits must be 0's
7918 unsigned int lsb = 0, msb = 31;
7919 while (v & (1 << msb)) --msb;
7920 while (v & (1 << lsb)) ++lsb;
7921 for (unsigned int i = lsb; i <= msb; ++i) {
7922 if (v & (1 << i))
7923 return 0;
7924 }
7925 return 1;
7926}
7927
Evan Cheng39382422009-10-28 01:44:26 +00007928/// isFPImmLegal - Returns true if the target can instruction select the
7929/// specified FP immediate natively. If false, the legalizer will
7930/// materialize the FP immediate as a load from a constant pool.
7931bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7932 if (!Subtarget->hasVFP3())
7933 return false;
7934 if (VT == MVT::f32)
7935 return ARM::getVFPf32Imm(Imm) != -1;
7936 if (VT == MVT::f64)
7937 return ARM::getVFPf64Imm(Imm) != -1;
7938 return false;
7939}
Bob Wilson65ffec42010-09-21 17:56:22 +00007940
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007941/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007942/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7943/// specified in the intrinsic calls.
7944bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7945 const CallInst &I,
7946 unsigned Intrinsic) const {
7947 switch (Intrinsic) {
7948 case Intrinsic::arm_neon_vld1:
7949 case Intrinsic::arm_neon_vld2:
7950 case Intrinsic::arm_neon_vld3:
7951 case Intrinsic::arm_neon_vld4:
7952 case Intrinsic::arm_neon_vld2lane:
7953 case Intrinsic::arm_neon_vld3lane:
7954 case Intrinsic::arm_neon_vld4lane: {
7955 Info.opc = ISD::INTRINSIC_W_CHAIN;
7956 // Conservatively set memVT to the entire set of vectors loaded.
7957 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7958 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7959 Info.ptrVal = I.getArgOperand(0);
7960 Info.offset = 0;
7961 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7962 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7963 Info.vol = false; // volatile loads with NEON intrinsics not supported
7964 Info.readMem = true;
7965 Info.writeMem = false;
7966 return true;
7967 }
7968 case Intrinsic::arm_neon_vst1:
7969 case Intrinsic::arm_neon_vst2:
7970 case Intrinsic::arm_neon_vst3:
7971 case Intrinsic::arm_neon_vst4:
7972 case Intrinsic::arm_neon_vst2lane:
7973 case Intrinsic::arm_neon_vst3lane:
7974 case Intrinsic::arm_neon_vst4lane: {
7975 Info.opc = ISD::INTRINSIC_VOID;
7976 // Conservatively set memVT to the entire set of vectors stored.
7977 unsigned NumElts = 0;
7978 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007979 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00007980 if (!ArgTy->isVectorTy())
7981 break;
7982 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7983 }
7984 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7985 Info.ptrVal = I.getArgOperand(0);
7986 Info.offset = 0;
7987 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7988 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7989 Info.vol = false; // volatile stores with NEON intrinsics not supported
7990 Info.readMem = false;
7991 Info.writeMem = true;
7992 return true;
7993 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007994 case Intrinsic::arm_strexd: {
7995 Info.opc = ISD::INTRINSIC_W_CHAIN;
7996 Info.memVT = MVT::i64;
7997 Info.ptrVal = I.getArgOperand(2);
7998 Info.offset = 0;
7999 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008000 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008001 Info.readMem = false;
8002 Info.writeMem = true;
8003 return true;
8004 }
8005 case Intrinsic::arm_ldrexd: {
8006 Info.opc = ISD::INTRINSIC_W_CHAIN;
8007 Info.memVT = MVT::i64;
8008 Info.ptrVal = I.getArgOperand(0);
8009 Info.offset = 0;
8010 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008011 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008012 Info.readMem = true;
8013 Info.writeMem = false;
8014 return true;
8015 }
Bob Wilson65ffec42010-09-21 17:56:22 +00008016 default:
8017 break;
8018 }
8019
8020 return false;
8021}