blob: f451b96dbbef1a0cae4e591a77199ac85a57be3e [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)
Duncan Sands28b77e92011-09-06 19:07:46 +0000109 setOperationAction(ISD::SETCC, 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
Duncan Sands28b77e92011-09-06 19:07:46 +0000181 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
182
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000184 // Uses VFP for Thumb libfuncs if available.
185 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
186 // Single-precision floating-point arithmetic.
187 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
188 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
189 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
190 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Double-precision floating-point arithmetic.
193 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
194 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
195 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
196 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000197
Evan Chengb1df8f22007-04-27 08:15:43 +0000198 // Single-precision comparisons.
199 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
200 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
201 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
202 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
203 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
204 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
205 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
206 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000207
Evan Chengb1df8f22007-04-27 08:15:43 +0000208 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000216
Evan Chengb1df8f22007-04-27 08:15:43 +0000217 // Double-precision comparisons.
218 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
219 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
220 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
221 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
222 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
223 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
224 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
225 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000226
Evan Chengb1df8f22007-04-27 08:15:43 +0000227 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
233 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
234 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000235
Evan Chengb1df8f22007-04-27 08:15:43 +0000236 // Floating-point to integer conversions.
237 // i64 conversions are done via library routines even when generating VFP
238 // instructions, so use the same ones.
239 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
241 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
242 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000243
Evan Chengb1df8f22007-04-27 08:15:43 +0000244 // Conversions between floating types.
245 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
246 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
247
248 // Integer to floating-point conversions.
249 // i64 conversions are done via library routines even when generating VFP
250 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000251 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
252 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000253 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
255 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
256 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
257 }
Evan Chenga8e29892007-01-19 07:51:42 +0000258 }
259
Bob Wilson2f954612009-05-22 17:38:41 +0000260 // These libcalls are not available in 32-bit.
261 setLibcallName(RTLIB::SHL_I128, 0);
262 setLibcallName(RTLIB::SRL_I128, 0);
263 setLibcallName(RTLIB::SRA_I128, 0);
264
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000265 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000266 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000267 // RTABI chapter 4.1.2, Table 2
268 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
269 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
270 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
271 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
272 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
276
277 // Double-precision floating-point comparison helper functions
278 // RTABI chapter 4.1.2, Table 3
279 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
281 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
282 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
283 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
284 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
286 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
288 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
289 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
290 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
291 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
293 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
294 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
295 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
302 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
303
304 // Single-precision floating-point arithmetic helper functions
305 // RTABI chapter 4.1.2, Table 4
306 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
307 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
308 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
309 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
310 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
314
315 // Single-precision floating-point comparison helper functions
316 // RTABI chapter 4.1.2, Table 5
317 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
319 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
320 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
321 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
322 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
324 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
326 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
327 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
328 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
329 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
331 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
332 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
333 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
340 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
341
342 // Floating-point to integer conversions.
343 // RTABI chapter 4.1.2, Table 6
344 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
346 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
347 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
350 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
351 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
360
361 // Conversions between floating types.
362 // RTABI chapter 4.1.2, Table 7
363 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
364 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
365 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000367
368 // Integer to floating-point conversions.
369 // RTABI chapter 4.1.2, Table 8
370 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
371 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
372 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
373 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
374 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
375 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
376 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
377 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
378 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
385 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
386
387 // Long long helper functions
388 // RTABI chapter 4.2, Table 9
389 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
390 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
391 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
392 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
393 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
394 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
395 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
399 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
400 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
401
402 // Integer division functions
403 // RTABI chapter 4.3.1
404 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
405 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
406 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
407 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
408 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
409 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
410 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
413 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
414 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000415 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000416
417 // Memory operations
418 // RTABI chapter 4.3.4
419 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
420 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
421 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000422 }
423
David Goodwinf1daf7d2009-07-08 23:10:31 +0000424 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000426 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000428 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000430 if (!Subtarget->isFPOnlySP())
431 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000432
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000434 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000435
436 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 addDRTypeForNEON(MVT::v2f32);
438 addDRTypeForNEON(MVT::v8i8);
439 addDRTypeForNEON(MVT::v4i16);
440 addDRTypeForNEON(MVT::v2i32);
441 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000442
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 addQRTypeForNEON(MVT::v4f32);
444 addQRTypeForNEON(MVT::v2f64);
445 addQRTypeForNEON(MVT::v16i8);
446 addQRTypeForNEON(MVT::v8i16);
447 addQRTypeForNEON(MVT::v4i32);
448 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000449
Bob Wilson74dc72e2009-09-15 23:55:57 +0000450 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
451 // neither Neon nor VFP support any arithmetic operations on it.
452 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
453 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
454 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
455 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
456 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
457 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000458 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000459 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
460 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
461 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
462 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
463 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
464 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
465 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
467 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
468 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
469 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
470 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
471 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
472 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
473 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
474 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
475 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
476
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000477 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
478
Bob Wilson642b3292009-09-16 00:32:15 +0000479 // Neon does not support some operations on v1i64 and v2i64 types.
480 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000481 // Custom handling for some quad-vector types to detect VMULL.
482 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
483 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
484 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000485 // Custom handling for some vector types to avoid expensive expansions
486 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
487 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
488 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
489 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000490 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
491 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000492 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
493 // a destination type that is wider than the source.
494 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
495 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000496
Bob Wilson1c3ef902011-02-07 17:43:21 +0000497 setTargetDAGCombine(ISD::INTRINSIC_VOID);
498 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000499 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
500 setTargetDAGCombine(ISD::SHL);
501 setTargetDAGCombine(ISD::SRL);
502 setTargetDAGCombine(ISD::SRA);
503 setTargetDAGCombine(ISD::SIGN_EXTEND);
504 setTargetDAGCombine(ISD::ZERO_EXTEND);
505 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000506 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000507 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000508 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000509 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
510 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000511 setTargetDAGCombine(ISD::FP_TO_SINT);
512 setTargetDAGCombine(ISD::FP_TO_UINT);
513 setTargetDAGCombine(ISD::FDIV);
Bob Wilson5bafff32009-06-22 23:27:02 +0000514 }
515
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000516 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000517
518 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000520
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000521 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000523
Evan Chenga8e29892007-01-19 07:51:42 +0000524 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000525 if (!Subtarget->isThumb1Only()) {
526 for (unsigned im = (unsigned)ISD::PRE_INC;
527 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setIndexedLoadAction(im, MVT::i1, Legal);
529 setIndexedLoadAction(im, MVT::i8, Legal);
530 setIndexedLoadAction(im, MVT::i16, Legal);
531 setIndexedLoadAction(im, MVT::i32, Legal);
532 setIndexedStoreAction(im, MVT::i1, Legal);
533 setIndexedStoreAction(im, MVT::i8, Legal);
534 setIndexedStoreAction(im, MVT::i16, Legal);
535 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000536 }
Evan Chenga8e29892007-01-19 07:51:42 +0000537 }
538
539 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000540 setOperationAction(ISD::MUL, MVT::i64, Expand);
541 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000542 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
544 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000545 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000546 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
547 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000548 setOperationAction(ISD::MULHS, MVT::i32, Expand);
549
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000550 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000551 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000552 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::SRL, MVT::i64, Custom);
554 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000555
Evan Cheng342e3162011-08-30 01:34:54 +0000556 if (!Subtarget->isThumb1Only()) {
557 // FIXME: We should do this for Thumb1 as well.
558 setOperationAction(ISD::ADDC, MVT::i32, Custom);
559 setOperationAction(ISD::ADDE, MVT::i32, Custom);
560 setOperationAction(ISD::SUBC, MVT::i32, Custom);
561 setOperationAction(ISD::SUBE, MVT::i32, Custom);
562 }
563
Evan Chenga8e29892007-01-19 07:51:42 +0000564 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000566 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000568 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000570
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000571 // Only ARMv6 has BSWAP.
572 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000574
Evan Chenga8e29892007-01-19 07:51:42 +0000575 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000576 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000577 // v7M has a hardware divider
578 setOperationAction(ISD::SDIV, MVT::i32, Expand);
579 setOperationAction(ISD::UDIV, MVT::i32, Expand);
580 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setOperationAction(ISD::SREM, MVT::i32, Expand);
582 setOperationAction(ISD::UREM, MVT::i32, Expand);
583 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
584 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000585
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
587 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
588 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
589 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000590 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000591
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000592 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000593
Evan Chenga8e29892007-01-19 07:51:42 +0000594 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::VASTART, MVT::Other, Custom);
596 setOperationAction(ISD::VAARG, MVT::Other, Expand);
597 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
598 setOperationAction(ISD::VAEND, MVT::Other, Expand);
599 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
600 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000601 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000602 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
603 setExceptionPointerRegister(ARM::R0);
604 setExceptionSelectorRegister(ARM::R1);
605
Evan Cheng3a1588a2010-04-15 22:20:34 +0000606 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000607 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
608 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000609 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000610 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000611 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000612 // membarrier needs custom lowering; the rest are legal and handled
613 // normally.
614 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000615 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000616 // Custom lowering for 64-bit ops
617 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
618 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
619 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
620 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
621 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
622 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000623 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000624 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
625 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000626 } else {
627 // Set them all for expansion, which will force libcalls.
628 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000629 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000630 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000631 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000632 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000633 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000634 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000635 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000636 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000637 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000638 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000639 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000640 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000641 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman069e2ed2011-08-26 02:59:24 +0000642 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
643 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000644 // Since the libcalls include locking, fold in the fences
645 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000646 }
Evan Chenga8e29892007-01-19 07:51:42 +0000647
Evan Cheng416941d2010-11-04 05:19:35 +0000648 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000649
Eli Friedmana2c6f452010-06-26 04:36:50 +0000650 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
651 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
653 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000654 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000656
Nate Begemand1fb5832010-08-03 21:31:55 +0000657 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000658 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
659 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000660 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000661 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
662 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000663
664 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000666 if (Subtarget->isTargetDarwin()) {
667 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
668 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000669 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000670 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000671 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000672
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::SETCC, MVT::i32, Expand);
674 setOperationAction(ISD::SETCC, MVT::f32, Expand);
675 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000676 setOperationAction(ISD::SELECT, MVT::i32, Custom);
677 setOperationAction(ISD::SELECT, MVT::f32, Custom);
678 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
680 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
681 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000682
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
684 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
685 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
686 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
687 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000688
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000689 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::FSIN, MVT::f64, Expand);
691 setOperationAction(ISD::FSIN, MVT::f32, Expand);
692 setOperationAction(ISD::FCOS, MVT::f32, Expand);
693 setOperationAction(ISD::FCOS, MVT::f64, Expand);
694 setOperationAction(ISD::FREM, MVT::f64, Expand);
695 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000696 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
698 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000699 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::FPOW, MVT::f64, Expand);
701 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000702
Cameron Zwarich33390842011-07-08 21:39:21 +0000703 setOperationAction(ISD::FMA, MVT::f64, Expand);
704 setOperationAction(ISD::FMA, MVT::f32, Expand);
705
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000706 // Various VFP goodness
707 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000708 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
709 if (Subtarget->hasVFP2()) {
710 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
711 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
712 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
713 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
714 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000715 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000716 if (!Subtarget->hasFP16()) {
717 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
718 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000719 }
Evan Cheng110cf482008-04-01 01:50:16 +0000720 }
Evan Chenga8e29892007-01-19 07:51:42 +0000721
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000722 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000723 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000724 setTargetDAGCombine(ISD::ADD);
725 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000726 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000727
Owen Anderson080c0922010-11-05 19:27:46 +0000728 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000729 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000730 if (Subtarget->hasNEON())
731 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000732
Evan Chenga8e29892007-01-19 07:51:42 +0000733 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000734
Evan Chengf7d87ee2010-05-21 00:43:17 +0000735 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
736 setSchedulingPreference(Sched::RegPressure);
737 else
738 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000739
Evan Cheng05219282011-01-06 06:52:41 +0000740 //// temporary - rewrite interface to use type
741 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000742
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000743 // On ARM arguments smaller than 4 bytes are extended, so all arguments
744 // are at least 4 bytes aligned.
745 setMinStackArgumentAlignment(4);
746
Evan Chengfff606d2010-09-24 19:07:23 +0000747 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000748
749 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000750}
751
Andrew Trick32cec0a2011-01-19 02:35:27 +0000752// FIXME: It might make sense to define the representative register class as the
753// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
754// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
755// SPR's representative would be DPR_VFP2. This should work well if register
756// pressure tracking were modified such that a register use would increment the
757// pressure of the register class's representative and all of it's super
758// classes' representatives transitively. We have not implemented this because
759// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000760// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000761// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000762std::pair<const TargetRegisterClass*, uint8_t>
763ARMTargetLowering::findRepresentativeClass(EVT VT) const{
764 const TargetRegisterClass *RRC = 0;
765 uint8_t Cost = 1;
766 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000767 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000768 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000769 // Use DPR as representative register class for all floating point
770 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
771 // the cost is 1 for both f32 and f64.
772 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000773 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000774 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000775 // When NEON is used for SP, only half of the register file is available
776 // because operations that define both SP and DP results will be constrained
777 // to the VFP2 class (D0-D15). We currently model this constraint prior to
778 // coalescing by double-counting the SP regs. See the FIXME above.
779 if (Subtarget->useNEONForSinglePrecisionFP())
780 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000781 break;
782 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
783 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000784 RRC = ARM::DPRRegisterClass;
785 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000786 break;
787 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000788 RRC = ARM::DPRRegisterClass;
789 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000790 break;
791 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000792 RRC = ARM::DPRRegisterClass;
793 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000794 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000795 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000796 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000797}
798
Evan Chenga8e29892007-01-19 07:51:42 +0000799const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
800 switch (Opcode) {
801 default: return 0;
802 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000803 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000804 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000805 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
806 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000807 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000808 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
809 case ARMISD::tCALL: return "ARMISD::tCALL";
810 case ARMISD::BRCOND: return "ARMISD::BRCOND";
811 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000812 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000813 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
814 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
815 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000816 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000817 case ARMISD::CMPFP: return "ARMISD::CMPFP";
818 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000819 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000820 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
821 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000822
Jim Grosbach3482c802010-01-18 19:58:49 +0000823 case ARMISD::RBIT: return "ARMISD::RBIT";
824
Bob Wilson76a312b2010-03-19 22:51:32 +0000825 case ARMISD::FTOSI: return "ARMISD::FTOSI";
826 case ARMISD::FTOUI: return "ARMISD::FTOUI";
827 case ARMISD::SITOF: return "ARMISD::SITOF";
828 case ARMISD::UITOF: return "ARMISD::UITOF";
829
Evan Chenga8e29892007-01-19 07:51:42 +0000830 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
831 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
832 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000833
Evan Cheng342e3162011-08-30 01:34:54 +0000834 case ARMISD::ADDC: return "ARMISD::ADDC";
835 case ARMISD::ADDE: return "ARMISD::ADDE";
836 case ARMISD::SUBC: return "ARMISD::SUBC";
837 case ARMISD::SUBE: return "ARMISD::SUBE";
838
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000839 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
840 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000841
Evan Chengc5942082009-10-28 06:55:03 +0000842 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
843 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000844 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000845
Dale Johannesen51e28e62010-06-03 21:09:53 +0000846 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000847
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000848 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000849
Evan Cheng86198642009-08-07 00:34:42 +0000850 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
851
Jim Grosbach3728e962009-12-10 00:11:09 +0000852 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000853 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000854
Evan Chengdfed19f2010-11-03 06:34:55 +0000855 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
856
Bob Wilson5bafff32009-06-22 23:27:02 +0000857 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000858 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000859 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000860 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
861 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 case ARMISD::VCGEU: return "ARMISD::VCGEU";
863 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000864 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
865 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000866 case ARMISD::VCGTU: return "ARMISD::VCGTU";
867 case ARMISD::VTST: return "ARMISD::VTST";
868
869 case ARMISD::VSHL: return "ARMISD::VSHL";
870 case ARMISD::VSHRs: return "ARMISD::VSHRs";
871 case ARMISD::VSHRu: return "ARMISD::VSHRu";
872 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
873 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
874 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
875 case ARMISD::VSHRN: return "ARMISD::VSHRN";
876 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
877 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
878 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
879 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
880 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
881 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
882 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
883 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
884 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
885 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
886 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
887 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
888 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
889 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000890 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000891 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000892 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000893 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000894 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000895 case ARMISD::VREV64: return "ARMISD::VREV64";
896 case ARMISD::VREV32: return "ARMISD::VREV32";
897 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000898 case ARMISD::VZIP: return "ARMISD::VZIP";
899 case ARMISD::VUZP: return "ARMISD::VUZP";
900 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000901 case ARMISD::VTBL1: return "ARMISD::VTBL1";
902 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000903 case ARMISD::VMULLs: return "ARMISD::VMULLs";
904 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000905 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000906 case ARMISD::FMAX: return "ARMISD::FMAX";
907 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000908 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000909 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
910 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000911 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000912 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
913 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
914 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000915 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
916 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
917 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
918 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
919 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
920 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
921 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
922 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
923 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
924 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
925 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
926 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
927 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
928 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
929 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
930 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
931 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000932 }
933}
934
Duncan Sands28b77e92011-09-06 19:07:46 +0000935EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
936 if (!VT.isVector()) return getPointerTy();
937 return VT.changeVectorElementTypeToInteger();
938}
939
Evan Cheng06b666c2010-05-15 02:18:07 +0000940/// getRegClassFor - Return the register class that should be used for the
941/// specified value type.
942TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
943 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
944 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
945 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000946 if (Subtarget->hasNEON()) {
947 if (VT == MVT::v4i64)
948 return ARM::QQPRRegisterClass;
949 else if (VT == MVT::v8i64)
950 return ARM::QQQQPRRegisterClass;
951 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000952 return TargetLowering::getRegClassFor(VT);
953}
954
Eric Christopherab695882010-07-21 22:26:11 +0000955// Create a fast isel object.
956FastISel *
957ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
958 return ARM::createFastISel(funcInfo);
959}
960
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000961/// getMaximalGlobalOffset - Returns the maximal possible offset which can
962/// be used for loads / stores from the global.
963unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
964 return (Subtarget->isThumb1Only() ? 127 : 4095);
965}
966
Evan Cheng1cc39842010-05-20 23:26:43 +0000967Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000968 unsigned NumVals = N->getNumValues();
969 if (!NumVals)
970 return Sched::RegPressure;
971
972 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000973 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000974 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000975 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000976 if (VT.isFloatingPoint() || VT.isVector())
977 return Sched::Latency;
978 }
Evan Chengc10f5432010-05-28 23:25:23 +0000979
980 if (!N->isMachineOpcode())
981 return Sched::RegPressure;
982
983 // Load are scheduled for latency even if there instruction itinerary
984 // is not available.
985 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000986 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000987
Evan Chenge837dea2011-06-28 19:10:37 +0000988 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +0000989 return Sched::RegPressure;
990 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +0000991 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000992 return Sched::Latency;
993
Evan Cheng1cc39842010-05-20 23:26:43 +0000994 return Sched::RegPressure;
995}
996
Evan Chenga8e29892007-01-19 07:51:42 +0000997//===----------------------------------------------------------------------===//
998// Lowering Code
999//===----------------------------------------------------------------------===//
1000
Evan Chenga8e29892007-01-19 07:51:42 +00001001/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1002static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1003 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001004 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001005 case ISD::SETNE: return ARMCC::NE;
1006 case ISD::SETEQ: return ARMCC::EQ;
1007 case ISD::SETGT: return ARMCC::GT;
1008 case ISD::SETGE: return ARMCC::GE;
1009 case ISD::SETLT: return ARMCC::LT;
1010 case ISD::SETLE: return ARMCC::LE;
1011 case ISD::SETUGT: return ARMCC::HI;
1012 case ISD::SETUGE: return ARMCC::HS;
1013 case ISD::SETULT: return ARMCC::LO;
1014 case ISD::SETULE: return ARMCC::LS;
1015 }
1016}
1017
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001018/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1019static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001020 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001021 CondCode2 = ARMCC::AL;
1022 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001023 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001024 case ISD::SETEQ:
1025 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1026 case ISD::SETGT:
1027 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1028 case ISD::SETGE:
1029 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1030 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001031 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001032 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1033 case ISD::SETO: CondCode = ARMCC::VC; break;
1034 case ISD::SETUO: CondCode = ARMCC::VS; break;
1035 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1036 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1037 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1038 case ISD::SETLT:
1039 case ISD::SETULT: CondCode = ARMCC::LT; break;
1040 case ISD::SETLE:
1041 case ISD::SETULE: CondCode = ARMCC::LE; break;
1042 case ISD::SETNE:
1043 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1044 }
Evan Chenga8e29892007-01-19 07:51:42 +00001045}
1046
Bob Wilson1f595bb2009-04-17 19:07:39 +00001047//===----------------------------------------------------------------------===//
1048// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001049//===----------------------------------------------------------------------===//
1050
1051#include "ARMGenCallingConv.inc"
1052
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001053/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1054/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001055CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001056 bool Return,
1057 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001058 switch (CC) {
1059 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001060 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001061 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001062 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001063 if (!Subtarget->isAAPCS_ABI())
1064 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1065 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1066 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1067 }
1068 // Fallthrough
1069 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001070 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001071 if (!Subtarget->isAAPCS_ABI())
1072 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1073 else if (Subtarget->hasVFP2() &&
1074 FloatABIType == FloatABI::Hard && !isVarArg)
1075 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1076 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1077 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001078 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001079 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001080 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001081 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001082 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001083 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001084 }
1085}
1086
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087/// LowerCallResult - Lower the result values of a call into the
1088/// appropriate copies out of appropriate physical registers.
1089SDValue
1090ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001091 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092 const SmallVectorImpl<ISD::InputArg> &Ins,
1093 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001094 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001095
Bob Wilson1f595bb2009-04-17 19:07:39 +00001096 // Assign locations to each value returned by this call.
1097 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001098 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1099 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001100 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001101 CCAssignFnForNode(CallConv, /* Return*/ true,
1102 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001103
1104 // Copy all of the result registers out of their specified physreg.
1105 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1106 CCValAssign VA = RVLocs[i];
1107
Bob Wilson80915242009-04-25 00:33:20 +00001108 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001109 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001110 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001111 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001113 Chain = Lo.getValue(1);
1114 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001116 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001117 InFlag);
1118 Chain = Hi.getValue(1);
1119 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001120 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001121
Owen Anderson825b72b2009-08-11 20:47:22 +00001122 if (VA.getLocVT() == MVT::v2f64) {
1123 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1124 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1125 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001126
1127 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001128 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001129 Chain = Lo.getValue(1);
1130 InFlag = Lo.getValue(2);
1131 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001133 Chain = Hi.getValue(1);
1134 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001135 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001136 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1137 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001138 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001139 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001140 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1141 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001142 Chain = Val.getValue(1);
1143 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001144 }
Bob Wilson80915242009-04-25 00:33:20 +00001145
1146 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001147 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001148 case CCValAssign::Full: break;
1149 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001150 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001151 break;
1152 }
1153
Dan Gohman98ca4f22009-08-05 01:29:28 +00001154 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001155 }
1156
Dan Gohman98ca4f22009-08-05 01:29:28 +00001157 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001158}
1159
Bob Wilsondee46d72009-04-17 20:35:10 +00001160/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001161SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001162ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1163 SDValue StackPtr, SDValue Arg,
1164 DebugLoc dl, SelectionDAG &DAG,
1165 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001166 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 unsigned LocMemOffset = VA.getLocMemOffset();
1168 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1169 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001170 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001171 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001172 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001173}
1174
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001176 SDValue Chain, SDValue &Arg,
1177 RegsToPassVector &RegsToPass,
1178 CCValAssign &VA, CCValAssign &NextVA,
1179 SDValue &StackPtr,
1180 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001181 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001182
Jim Grosbache5165492009-11-09 00:11:35 +00001183 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001184 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001185 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1186
1187 if (NextVA.isRegLoc())
1188 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1189 else {
1190 assert(NextVA.isMemLoc());
1191 if (StackPtr.getNode() == 0)
1192 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1193
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1195 dl, DAG, NextVA,
1196 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001197 }
1198}
1199
Dan Gohman98ca4f22009-08-05 01:29:28 +00001200/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001201/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1202/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001203SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001204ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001205 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001206 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001208 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001209 const SmallVectorImpl<ISD::InputArg> &Ins,
1210 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001211 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001212 MachineFunction &MF = DAG.getMachineFunction();
1213 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1214 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001215 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001216 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001217 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001218 if (isTailCall) {
1219 // Check if it's really possible to do a tail call.
1220 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1221 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001222 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001223 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1224 // detected sibcalls.
1225 if (isTailCall) {
1226 ++NumTailCalls;
1227 IsSibCall = true;
1228 }
1229 }
Evan Chenga8e29892007-01-19 07:51:42 +00001230
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 // Analyze operands of the call, assigning locations to each operand.
1232 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001233 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1234 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001236 CCAssignFnForNode(CallConv, /* Return*/ false,
1237 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001238
Bob Wilson1f595bb2009-04-17 19:07:39 +00001239 // Get a count of how many bytes are to be pushed on the stack.
1240 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001241
Dale Johannesen51e28e62010-06-03 21:09:53 +00001242 // For tail calls, memory operands are available in our caller's stack.
1243 if (IsSibCall)
1244 NumBytes = 0;
1245
Evan Chenga8e29892007-01-19 07:51:42 +00001246 // Adjust the stack pointer for the new arguments...
1247 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001248 if (!IsSibCall)
1249 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001250
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001251 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001252
Bob Wilson5bafff32009-06-22 23:27:02 +00001253 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001255
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001257 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1259 i != e;
1260 ++i, ++realArgIdx) {
1261 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001262 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001263 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001264 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001265
Bob Wilson1f595bb2009-04-17 19:07:39 +00001266 // Promote the value if needed.
1267 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001268 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001269 case CCValAssign::Full: break;
1270 case CCValAssign::SExt:
1271 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1272 break;
1273 case CCValAssign::ZExt:
1274 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1275 break;
1276 case CCValAssign::AExt:
1277 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1278 break;
1279 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001281 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001282 }
1283
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001284 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001285 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001286 if (VA.getLocVT() == MVT::v2f64) {
1287 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1288 DAG.getConstant(0, MVT::i32));
1289 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1290 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001291
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001293 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1294
1295 VA = ArgLocs[++i]; // skip ahead to next loc
1296 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001297 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001298 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1299 } else {
1300 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001301
Dan Gohman98ca4f22009-08-05 01:29:28 +00001302 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1303 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001304 }
1305 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001306 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001307 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001308 }
1309 } else if (VA.isRegLoc()) {
1310 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001311 } else if (isByVal) {
1312 assert(VA.isMemLoc());
1313 unsigned offset = 0;
1314
1315 // True if this byval aggregate will be split between registers
1316 // and memory.
1317 if (CCInfo.isFirstByValRegValid()) {
1318 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1319 unsigned int i, j;
1320 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1321 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1322 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1323 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1324 MachinePointerInfo(),
1325 false, false, 0);
1326 MemOpChains.push_back(Load.getValue(1));
1327 RegsToPass.push_back(std::make_pair(j, Load));
1328 }
1329 offset = ARM::R4 - CCInfo.getFirstByValReg();
1330 CCInfo.clearFirstByValReg();
1331 }
1332
1333 unsigned LocMemOffset = VA.getLocMemOffset();
1334 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1335 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1336 StkPtrOff);
1337 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1338 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1339 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1340 MVT::i32);
1341 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1342 Flags.getByValAlign(),
1343 /*isVolatile=*/false,
1344 /*AlwaysInline=*/false,
1345 MachinePointerInfo(0),
1346 MachinePointerInfo(0)));
1347
1348 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001349 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001350
Dan Gohman98ca4f22009-08-05 01:29:28 +00001351 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1352 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001353 }
Evan Chenga8e29892007-01-19 07:51:42 +00001354 }
1355
1356 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001357 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001358 &MemOpChains[0], MemOpChains.size());
1359
1360 // Build a sequence of copy-to-reg nodes chained together with token chain
1361 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001362 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001363 // Tail call byval lowering might overwrite argument registers so in case of
1364 // tail call optimization the copies to registers are lowered later.
1365 if (!isTailCall)
1366 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1367 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1368 RegsToPass[i].second, InFlag);
1369 InFlag = Chain.getValue(1);
1370 }
Evan Chenga8e29892007-01-19 07:51:42 +00001371
Dale Johannesen51e28e62010-06-03 21:09:53 +00001372 // For tail calls lower the arguments to the 'real' stack slot.
1373 if (isTailCall) {
1374 // Force all the incoming stack arguments to be loaded from the stack
1375 // before any new outgoing arguments are stored to the stack, because the
1376 // outgoing stack slots may alias the incoming argument stack slots, and
1377 // the alias isn't otherwise explicit. This is slightly more conservative
1378 // than necessary, because it means that each store effectively depends
1379 // on every argument instead of just those arguments it would clobber.
1380
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001381 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001382 InFlag = SDValue();
1383 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1384 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1385 RegsToPass[i].second, InFlag);
1386 InFlag = Chain.getValue(1);
1387 }
1388 InFlag =SDValue();
1389 }
1390
Bill Wendling056292f2008-09-16 21:48:12 +00001391 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1392 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1393 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001394 bool isDirect = false;
1395 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001396 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001397 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001398
1399 if (EnableARMLongCalls) {
1400 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1401 && "long-calls with non-static relocation model!");
1402 // Handle a global address or an external symbol. If it's not one of
1403 // those, the target's already in a register, so we don't need to do
1404 // anything extra.
1405 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001406 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001407 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001408 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001409 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1410 ARMPCLabelIndex,
1411 ARMCP::CPValue, 0);
1412 // Get the address of the callee into a register
1413 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1414 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1415 Callee = DAG.getLoad(getPointerTy(), dl,
1416 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001417 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001418 false, false, 0);
1419 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1420 const char *Sym = S->getSymbol();
1421
1422 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001423 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001424 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1425 Sym, ARMPCLabelIndex, 0);
1426 // Get the address of the callee into a register
1427 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1428 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1429 Callee = DAG.getLoad(getPointerTy(), dl,
1430 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001431 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001432 false, false, 0);
1433 }
1434 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001435 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001436 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001437 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001438 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001439 getTargetMachine().getRelocationModel() != Reloc::Static;
1440 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001441 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001442 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001443 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001444 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001445 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001446 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001447 ARMPCLabelIndex,
1448 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001449 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001450 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001451 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001452 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001453 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001454 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001455 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001456 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001457 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001458 } else {
1459 // On ELF targets for PIC code, direct calls should go through the PLT
1460 unsigned OpFlags = 0;
1461 if (Subtarget->isTargetELF() &&
1462 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1463 OpFlags = ARMII::MO_PLT;
1464 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1465 }
Bill Wendling056292f2008-09-16 21:48:12 +00001466 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001467 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001468 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001469 getTargetMachine().getRelocationModel() != Reloc::Static;
1470 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001471 // tBX takes a register source operand.
1472 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001473 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001474 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001475 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001476 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001477 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001479 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001480 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001481 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001482 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001483 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001484 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001485 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001486 } else {
1487 unsigned OpFlags = 0;
1488 // On ELF targets for PIC code, direct calls should go through the PLT
1489 if (Subtarget->isTargetELF() &&
1490 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1491 OpFlags = ARMII::MO_PLT;
1492 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1493 }
Evan Chenga8e29892007-01-19 07:51:42 +00001494 }
1495
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001496 // FIXME: handle tail calls differently.
1497 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001498 if (Subtarget->isThumb()) {
1499 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001500 CallOpc = ARMISD::CALL_NOLINK;
1501 else
1502 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1503 } else {
1504 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001505 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1506 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001507 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001508
Dan Gohman475871a2008-07-27 21:46:04 +00001509 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001510 Ops.push_back(Chain);
1511 Ops.push_back(Callee);
1512
1513 // Add argument registers to the end of the list so that they are known live
1514 // into the call.
1515 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1516 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1517 RegsToPass[i].second.getValueType()));
1518
Gabor Greifba36cb52008-08-28 21:40:38 +00001519 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001520 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001521
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001522 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001523 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001524 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001525
Duncan Sands4bdcb612008-07-02 17:40:58 +00001526 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001527 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001528 InFlag = Chain.getValue(1);
1529
Chris Lattnere563bbc2008-10-11 22:08:30 +00001530 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1531 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001532 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001533 InFlag = Chain.getValue(1);
1534
Bob Wilson1f595bb2009-04-17 19:07:39 +00001535 // Handle result values, copying them out of physregs into vregs that we
1536 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1538 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001539}
1540
Stuart Hastingsf222e592011-02-28 17:17:53 +00001541/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001542/// on the stack. Remember the next parameter register to allocate,
1543/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001544/// this.
1545void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001546llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1547 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1548 assert((State->getCallOrPrologue() == Prologue ||
1549 State->getCallOrPrologue() == Call) &&
1550 "unhandled ParmContext");
1551 if ((!State->isFirstByValRegValid()) &&
1552 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1553 State->setFirstByValReg(reg);
1554 // At a call site, a byval parameter that is split between
1555 // registers and memory needs its size truncated here. In a
1556 // function prologue, such byval parameters are reassembled in
1557 // memory, and are not truncated.
1558 if (State->getCallOrPrologue() == Call) {
1559 unsigned excess = 4 * (ARM::R4 - reg);
1560 assert(size >= excess && "expected larger existing stack allocation");
1561 size -= excess;
1562 }
1563 }
1564 // Confiscate any remaining parameter registers to preclude their
1565 // assignment to subsequent parameters.
1566 while (State->AllocateReg(GPRArgRegs, 4))
1567 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001568}
1569
Dale Johannesen51e28e62010-06-03 21:09:53 +00001570/// MatchingStackOffset - Return true if the given stack call argument is
1571/// already available in the same position (relatively) of the caller's
1572/// incoming argument stack.
1573static
1574bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1575 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1576 const ARMInstrInfo *TII) {
1577 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1578 int FI = INT_MAX;
1579 if (Arg.getOpcode() == ISD::CopyFromReg) {
1580 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001581 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001582 return false;
1583 MachineInstr *Def = MRI->getVRegDef(VR);
1584 if (!Def)
1585 return false;
1586 if (!Flags.isByVal()) {
1587 if (!TII->isLoadFromStackSlot(Def, FI))
1588 return false;
1589 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001590 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001591 }
1592 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1593 if (Flags.isByVal())
1594 // ByVal argument is passed in as a pointer but it's now being
1595 // dereferenced. e.g.
1596 // define @foo(%struct.X* %A) {
1597 // tail call @bar(%struct.X* byval %A)
1598 // }
1599 return false;
1600 SDValue Ptr = Ld->getBasePtr();
1601 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1602 if (!FINode)
1603 return false;
1604 FI = FINode->getIndex();
1605 } else
1606 return false;
1607
1608 assert(FI != INT_MAX);
1609 if (!MFI->isFixedObjectIndex(FI))
1610 return false;
1611 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1612}
1613
1614/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1615/// for tail call optimization. Targets which want to do tail call
1616/// optimization should implement this function.
1617bool
1618ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1619 CallingConv::ID CalleeCC,
1620 bool isVarArg,
1621 bool isCalleeStructRet,
1622 bool isCallerStructRet,
1623 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001624 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001625 const SmallVectorImpl<ISD::InputArg> &Ins,
1626 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001627 const Function *CallerF = DAG.getMachineFunction().getFunction();
1628 CallingConv::ID CallerCC = CallerF->getCallingConv();
1629 bool CCMatch = CallerCC == CalleeCC;
1630
1631 // Look for obvious safe cases to perform tail call optimization that do not
1632 // require ABI changes. This is what gcc calls sibcall.
1633
Jim Grosbach7616b642010-06-16 23:45:49 +00001634 // Do not sibcall optimize vararg calls unless the call site is not passing
1635 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636 if (isVarArg && !Outs.empty())
1637 return false;
1638
1639 // Also avoid sibcall optimization if either caller or callee uses struct
1640 // return semantics.
1641 if (isCalleeStructRet || isCallerStructRet)
1642 return false;
1643
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001644 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001645 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1646 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1647 // support in the assembler and linker to be used. This would need to be
1648 // fixed to fully support tail calls in Thumb1.
1649 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001650 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1651 // LR. This means if we need to reload LR, it takes an extra instructions,
1652 // which outweighs the value of the tail call; but here we don't know yet
1653 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001654 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001655 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001656
1657 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1658 // but we need to make sure there are enough registers; the only valid
1659 // registers are the 4 used for parameters. We don't currently do this
1660 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001661 if (Subtarget->isThumb1Only())
1662 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001663
Dale Johannesen51e28e62010-06-03 21:09:53 +00001664 // If the calling conventions do not match, then we'd better make sure the
1665 // results are returned in the same way as what the caller expects.
1666 if (!CCMatch) {
1667 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001668 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1669 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001670 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1671
1672 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001673 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1674 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001675 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1676
1677 if (RVLocs1.size() != RVLocs2.size())
1678 return false;
1679 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1680 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1681 return false;
1682 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1683 return false;
1684 if (RVLocs1[i].isRegLoc()) {
1685 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1686 return false;
1687 } else {
1688 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1689 return false;
1690 }
1691 }
1692 }
1693
1694 // If the callee takes no arguments then go on to check the results of the
1695 // call.
1696 if (!Outs.empty()) {
1697 // Check if stack adjustment is needed. For now, do not do this if any
1698 // argument is passed on the stack.
1699 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001700 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1701 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001702 CCInfo.AnalyzeCallOperands(Outs,
1703 CCAssignFnForNode(CalleeCC, false, isVarArg));
1704 if (CCInfo.getNextStackOffset()) {
1705 MachineFunction &MF = DAG.getMachineFunction();
1706
1707 // Check if the arguments are already laid out in the right way as
1708 // the caller's fixed stack objects.
1709 MachineFrameInfo *MFI = MF.getFrameInfo();
1710 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1711 const ARMInstrInfo *TII =
1712 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001713 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1714 i != e;
1715 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001716 CCValAssign &VA = ArgLocs[i];
1717 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001718 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001719 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001720 if (VA.getLocInfo() == CCValAssign::Indirect)
1721 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001722 if (VA.needsCustom()) {
1723 // f64 and vector types are split into multiple registers or
1724 // register/stack-slot combinations. The types will not match
1725 // the registers; give up on memory f64 refs until we figure
1726 // out what to do about this.
1727 if (!VA.isRegLoc())
1728 return false;
1729 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001730 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001731 if (RegVT == MVT::v2f64) {
1732 if (!ArgLocs[++i].isRegLoc())
1733 return false;
1734 if (!ArgLocs[++i].isRegLoc())
1735 return false;
1736 }
1737 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001738 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1739 MFI, MRI, TII))
1740 return false;
1741 }
1742 }
1743 }
1744 }
1745
1746 return true;
1747}
1748
Dan Gohman98ca4f22009-08-05 01:29:28 +00001749SDValue
1750ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001751 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001753 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001754 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001755
Bob Wilsondee46d72009-04-17 20:35:10 +00001756 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001758
Bob Wilsondee46d72009-04-17 20:35:10 +00001759 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001760 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1761 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001762
Dan Gohman98ca4f22009-08-05 01:29:28 +00001763 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001764 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1765 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001766
1767 // If this is the first return lowered for this function, add
1768 // the regs to the liveout set for the function.
1769 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1770 for (unsigned i = 0; i != RVLocs.size(); ++i)
1771 if (RVLocs[i].isRegLoc())
1772 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001773 }
1774
Bob Wilson1f595bb2009-04-17 19:07:39 +00001775 SDValue Flag;
1776
1777 // Copy the result values into the output registers.
1778 for (unsigned i = 0, realRVLocIdx = 0;
1779 i != RVLocs.size();
1780 ++i, ++realRVLocIdx) {
1781 CCValAssign &VA = RVLocs[i];
1782 assert(VA.isRegLoc() && "Can only return in registers!");
1783
Dan Gohmanc9403652010-07-07 15:54:55 +00001784 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001785
1786 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001787 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001788 case CCValAssign::Full: break;
1789 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001790 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001791 break;
1792 }
1793
Bob Wilson1f595bb2009-04-17 19:07:39 +00001794 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001796 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1798 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001799 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001800 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001801
1802 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1803 Flag = Chain.getValue(1);
1804 VA = RVLocs[++i]; // skip ahead to next loc
1805 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1806 HalfGPRs.getValue(1), Flag);
1807 Flag = Chain.getValue(1);
1808 VA = RVLocs[++i]; // skip ahead to next loc
1809
1810 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001811 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1812 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001813 }
1814 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1815 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001816 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001817 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001819 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001820 VA = RVLocs[++i]; // skip ahead to next loc
1821 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1822 Flag);
1823 } else
1824 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1825
Bob Wilsondee46d72009-04-17 20:35:10 +00001826 // Guarantee that all emitted copies are
1827 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001828 Flag = Chain.getValue(1);
1829 }
1830
1831 SDValue result;
1832 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001834 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001835 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001836
1837 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001838}
1839
Evan Cheng3d2125c2010-11-30 23:55:39 +00001840bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1841 if (N->getNumValues() != 1)
1842 return false;
1843 if (!N->hasNUsesOfValue(1, 0))
1844 return false;
1845
1846 unsigned NumCopies = 0;
1847 SDNode* Copies[2];
1848 SDNode *Use = *N->use_begin();
1849 if (Use->getOpcode() == ISD::CopyToReg) {
1850 Copies[NumCopies++] = Use;
1851 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1852 // f64 returned in a pair of GPRs.
1853 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1854 UI != UE; ++UI) {
1855 if (UI->getOpcode() != ISD::CopyToReg)
1856 return false;
1857 Copies[UI.getUse().getResNo()] = *UI;
1858 ++NumCopies;
1859 }
1860 } else if (Use->getOpcode() == ISD::BITCAST) {
1861 // f32 returned in a single GPR.
1862 if (!Use->hasNUsesOfValue(1, 0))
1863 return false;
1864 Use = *Use->use_begin();
1865 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1866 return false;
1867 Copies[NumCopies++] = Use;
1868 } else {
1869 return false;
1870 }
1871
1872 if (NumCopies != 1 && NumCopies != 2)
1873 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001874
1875 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001876 for (unsigned i = 0; i < NumCopies; ++i) {
1877 SDNode *Copy = Copies[i];
1878 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1879 UI != UE; ++UI) {
1880 if (UI->getOpcode() == ISD::CopyToReg) {
1881 SDNode *Use = *UI;
1882 if (Use == Copies[0] || Use == Copies[1])
1883 continue;
1884 return false;
1885 }
1886 if (UI->getOpcode() != ARMISD::RET_FLAG)
1887 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001888 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001889 }
1890 }
1891
Evan Cheng1bf891a2010-12-01 22:59:46 +00001892 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001893}
1894
Evan Cheng485fafc2011-03-21 01:19:09 +00001895bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1896 if (!EnableARMTailCalls)
1897 return false;
1898
1899 if (!CI->isTailCall())
1900 return false;
1901
1902 return !Subtarget->isThumb1Only();
1903}
1904
Bob Wilsonb62d2572009-11-03 00:02:05 +00001905// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1906// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1907// one of the above mentioned nodes. It has to be wrapped because otherwise
1908// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1909// be used to form addressing mode. These wrapped nodes will be selected
1910// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001911static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001912 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001913 // FIXME there is no actual debug info here
1914 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001915 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001916 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001917 if (CP->isMachineConstantPoolEntry())
1918 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1919 CP->getAlignment());
1920 else
1921 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1922 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001923 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001924}
1925
Jim Grosbache1102ca2010-07-19 17:20:38 +00001926unsigned ARMTargetLowering::getJumpTableEncoding() const {
1927 return MachineJumpTableInfo::EK_Inline;
1928}
1929
Dan Gohmand858e902010-04-17 15:26:15 +00001930SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1931 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001932 MachineFunction &MF = DAG.getMachineFunction();
1933 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1934 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001935 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001936 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001937 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001938 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1939 SDValue CPAddr;
1940 if (RelocM == Reloc::Static) {
1941 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1942 } else {
1943 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001944 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001945 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1946 ARMCP::CPBlockAddress,
1947 PCAdj);
1948 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1949 }
1950 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1951 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001952 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001953 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001954 if (RelocM == Reloc::Static)
1955 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001956 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001957 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001958}
1959
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001960// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001961SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001962ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001963 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001964 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001965 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001966 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001967 MachineFunction &MF = DAG.getMachineFunction();
1968 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001969 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001970 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001971 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001972 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001973 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001975 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001976 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001977 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001978 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001979
Evan Chenge7e0d622009-11-06 22:24:13 +00001980 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001981 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001982
1983 // call __tls_get_addr.
1984 ArgListTy Args;
1985 ArgListEntry Entry;
1986 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001987 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001988 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001989 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001990 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001991 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00001992 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001993 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001994 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001995 return CallResult.first;
1996}
1997
1998// Lower ISD::GlobalTLSAddress using the "initial exec" or
1999// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002000SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002001ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002002 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002003 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002004 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002005 SDValue Offset;
2006 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002007 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002008 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002009 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002010
Chris Lattner4fb63d02009-07-15 04:12:33 +00002011 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002012 MachineFunction &MF = DAG.getMachineFunction();
2013 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002014 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002015 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002016 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2017 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002018 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002019 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002020 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002022 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002023 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002024 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002025 Chain = Offset.getValue(1);
2026
Evan Chenge7e0d622009-11-06 22:24:13 +00002027 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002028 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002029
Evan Cheng9eda6892009-10-31 03:39:36 +00002030 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002031 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002032 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002033 } else {
2034 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002035 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002036 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002037 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002038 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002039 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002040 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002041 }
2042
2043 // The address of the thread local variable is the add of the thread
2044 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002045 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002046}
2047
Dan Gohman475871a2008-07-27 21:46:04 +00002048SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002049ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002050 // TODO: implement the "local dynamic" model
2051 assert(Subtarget->isTargetELF() &&
2052 "TLS not implemented for non-ELF targets");
2053 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2054 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2055 // otherwise use the "Local Exec" TLS Model
2056 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2057 return LowerToTLSGeneralDynamicModel(GA, DAG);
2058 else
2059 return LowerToTLSExecModels(GA, DAG);
2060}
2061
Dan Gohman475871a2008-07-27 21:46:04 +00002062SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002063 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002064 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002065 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002066 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002067 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2068 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002069 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002070 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002071 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002072 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002073 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002074 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002075 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002076 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002077 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002078 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002079 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002080 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002081 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002082 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002083 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002084 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002085 }
2086
2087 // If we have T2 ops, we can materialize the address directly via movt/movw
2088 // pair. This is always cheaper.
2089 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002090 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002091 // FIXME: Once remat is capable of dealing with instructions with register
2092 // operands, expand this into two nodes.
2093 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2094 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002095 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002096 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2097 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2098 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2099 MachinePointerInfo::getConstantPool(),
2100 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002101 }
2102}
2103
Dan Gohman475871a2008-07-27 21:46:04 +00002104SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002105 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002106 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002107 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002108 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002109 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002110 MachineFunction &MF = DAG.getMachineFunction();
2111 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2112
Evan Cheng4abce0c2011-05-27 20:11:27 +00002113 // FIXME: Enable this for static codegen when tool issues are fixed.
2114 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002115 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002116 // FIXME: Once remat is capable of dealing with instructions with register
2117 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002118 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002119 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2120 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2121
Evan Cheng53519f02011-01-21 18:55:51 +00002122 unsigned Wrapper = (RelocM == Reloc::PIC_)
2123 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2124 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002125 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002126 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2127 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2128 MachinePointerInfo::getGOT(), false, false, 0);
2129 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002130 }
2131
2132 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002133 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002134 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002135 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002136 } else {
2137 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002138 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2139 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002140 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002141 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002142 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002143 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002144
Evan Cheng9eda6892009-10-31 03:39:36 +00002145 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002146 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002147 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002148 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002149
2150 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002151 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002152 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002153 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002154
Evan Cheng63476a82009-09-03 07:04:02 +00002155 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002156 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002157 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002158
2159 return Result;
2160}
2161
Dan Gohman475871a2008-07-27 21:46:04 +00002162SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002163 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002164 assert(Subtarget->isTargetELF() &&
2165 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002166 MachineFunction &MF = DAG.getMachineFunction();
2167 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002168 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002169 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002170 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002171 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002172 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2173 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002174 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002175 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002177 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002178 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002179 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002180 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002181 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002182}
2183
Jim Grosbach0e0da732009-05-12 23:59:14 +00002184SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002185ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2186 const {
2187 DebugLoc dl = Op.getDebugLoc();
2188 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002189 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002190}
2191
2192SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002193ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2194 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002195 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002196 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2197 Op.getOperand(1), Val);
2198}
2199
2200SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002201ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2202 DebugLoc dl = Op.getDebugLoc();
2203 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2204 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2205}
2206
2207SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002208ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002209 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002210 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002211 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002212 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002213 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002214 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002215 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002216 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2217 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002218 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002219 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002220 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002221 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002222 EVT PtrVT = getPointerTy();
2223 DebugLoc dl = Op.getDebugLoc();
2224 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2225 SDValue CPAddr;
2226 unsigned PCAdj = (RelocM != Reloc::PIC_)
2227 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002228 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002229 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2230 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002231 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002232 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002233 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002234 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002235 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002236 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002237
2238 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002239 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002240 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2241 }
2242 return Result;
2243 }
Evan Cheng92e39162011-03-29 23:06:19 +00002244 case Intrinsic::arm_neon_vmulls:
2245 case Intrinsic::arm_neon_vmullu: {
2246 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2247 ? ARMISD::VMULLs : ARMISD::VMULLu;
2248 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2249 Op.getOperand(1), Op.getOperand(2));
2250 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002251 }
2252}
2253
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002254static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002255 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002256 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002257 if (!Subtarget->hasDataBarrier()) {
2258 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2259 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2260 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002261 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002262 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002263 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002264 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002265 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002266
2267 SDValue Op5 = Op.getOperand(5);
2268 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2269 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2270 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2271 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2272
2273 ARM_MB::MemBOpt DMBOpt;
2274 if (isDeviceBarrier)
2275 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2276 else
2277 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2278 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2279 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002280}
2281
Eli Friedman26689ac2011-08-03 21:06:02 +00002282
2283static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2284 const ARMSubtarget *Subtarget) {
2285 // FIXME: handle "fence singlethread" more efficiently.
2286 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002287 if (!Subtarget->hasDataBarrier()) {
2288 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2289 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2290 // here.
2291 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2292 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002293 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002294 DAG.getConstant(0, MVT::i32));
2295 }
2296
Eli Friedman26689ac2011-08-03 21:06:02 +00002297 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002298 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002299}
2300
Evan Chengdfed19f2010-11-03 06:34:55 +00002301static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2302 const ARMSubtarget *Subtarget) {
2303 // ARM pre v5TE and Thumb1 does not have preload instructions.
2304 if (!(Subtarget->isThumb2() ||
2305 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2306 // Just preserve the chain.
2307 return Op.getOperand(0);
2308
2309 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002310 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2311 if (!isRead &&
2312 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2313 // ARMv7 with MP extension has PLDW.
2314 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002315
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002316 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2317 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002318 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002319 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002320 isData = ~isData & 1;
2321 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002322
2323 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002324 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2325 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002326}
2327
Dan Gohman1e93df62010-04-17 14:41:14 +00002328static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2329 MachineFunction &MF = DAG.getMachineFunction();
2330 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2331
Evan Chenga8e29892007-01-19 07:51:42 +00002332 // vastart just stores the address of the VarArgsFrameIndex slot into the
2333 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002334 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002335 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002336 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002337 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002338 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2339 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002340}
2341
Dan Gohman475871a2008-07-27 21:46:04 +00002342SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002343ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2344 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002345 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002346 MachineFunction &MF = DAG.getMachineFunction();
2347 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2348
2349 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002350 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002351 RC = ARM::tGPRRegisterClass;
2352 else
2353 RC = ARM::GPRRegisterClass;
2354
2355 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002356 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002357 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002358
2359 SDValue ArgValue2;
2360 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002361 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002362 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002363
2364 // Create load node to retrieve arguments from the stack.
2365 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002366 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002367 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002368 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002369 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002370 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002371 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002372 }
2373
Jim Grosbache5165492009-11-09 00:11:35 +00002374 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002375}
2376
Stuart Hastingsc7315872011-04-20 16:47:52 +00002377void
2378ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2379 unsigned &VARegSize, unsigned &VARegSaveSize)
2380 const {
2381 unsigned NumGPRs;
2382 if (CCInfo.isFirstByValRegValid())
2383 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2384 else {
2385 unsigned int firstUnalloced;
2386 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2387 sizeof(GPRArgRegs) /
2388 sizeof(GPRArgRegs[0]));
2389 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2390 }
2391
2392 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2393 VARegSize = NumGPRs * 4;
2394 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2395}
2396
2397// The remaining GPRs hold either the beginning of variable-argument
2398// data, or the beginning of an aggregate passed by value (usuall
2399// byval). Either way, we allocate stack slots adjacent to the data
2400// provided by our caller, and store the unallocated registers there.
2401// If this is a variadic function, the va_list pointer will begin with
2402// these values; otherwise, this reassembles a (byval) structure that
2403// was split between registers and memory.
2404void
2405ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2406 DebugLoc dl, SDValue &Chain,
2407 unsigned ArgOffset) const {
2408 MachineFunction &MF = DAG.getMachineFunction();
2409 MachineFrameInfo *MFI = MF.getFrameInfo();
2410 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2411 unsigned firstRegToSaveIndex;
2412 if (CCInfo.isFirstByValRegValid())
2413 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2414 else {
2415 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2416 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2417 }
2418
2419 unsigned VARegSize, VARegSaveSize;
2420 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2421 if (VARegSaveSize) {
2422 // If this function is vararg, store any remaining integer argument regs
2423 // to their spots on the stack so that they may be loaded by deferencing
2424 // the result of va_next.
2425 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002426 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2427 ArgOffset + VARegSaveSize
2428 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002429 false));
2430 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2431 getPointerTy());
2432
2433 SmallVector<SDValue, 4> MemOps;
2434 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2435 TargetRegisterClass *RC;
2436 if (AFI->isThumb1OnlyFunction())
2437 RC = ARM::tGPRRegisterClass;
2438 else
2439 RC = ARM::GPRRegisterClass;
2440
2441 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2442 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2443 SDValue Store =
2444 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002445 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002446 false, false, 0);
2447 MemOps.push_back(Store);
2448 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2449 DAG.getConstant(4, getPointerTy()));
2450 }
2451 if (!MemOps.empty())
2452 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2453 &MemOps[0], MemOps.size());
2454 } else
2455 // This will point to the next argument passed via stack.
2456 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2457}
2458
Bob Wilson5bafff32009-06-22 23:27:02 +00002459SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002460ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002461 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002462 const SmallVectorImpl<ISD::InputArg>
2463 &Ins,
2464 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002465 SmallVectorImpl<SDValue> &InVals)
2466 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002467 MachineFunction &MF = DAG.getMachineFunction();
2468 MachineFrameInfo *MFI = MF.getFrameInfo();
2469
Bob Wilson1f595bb2009-04-17 19:07:39 +00002470 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2471
2472 // Assign locations to all of the incoming arguments.
2473 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002474 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2475 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002476 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002477 CCAssignFnForNode(CallConv, /* Return*/ false,
2478 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002479
2480 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002481 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002482
Stuart Hastingsf222e592011-02-28 17:17:53 +00002483 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002484 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2485 CCValAssign &VA = ArgLocs[i];
2486
Bob Wilsondee46d72009-04-17 20:35:10 +00002487 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002488 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002489 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002490
Bob Wilson1f595bb2009-04-17 19:07:39 +00002491 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002492 // f64 and vector types are split up into multiple registers or
2493 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002494 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002495 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002496 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002497 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002498 SDValue ArgValue2;
2499 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002500 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002501 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2502 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002503 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002504 false, false, 0);
2505 } else {
2506 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2507 Chain, DAG, dl);
2508 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002509 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2510 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002511 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002512 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002513 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2514 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002515 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002516
Bob Wilson5bafff32009-06-22 23:27:02 +00002517 } else {
2518 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002519
Owen Anderson825b72b2009-08-11 20:47:22 +00002520 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002521 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002522 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002523 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002524 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002525 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002526 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002527 RC = (AFI->isThumb1OnlyFunction() ?
2528 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002529 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002530 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002531
2532 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002533 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002534 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002535 }
2536
2537 // If this is an 8 or 16-bit value, it is really passed promoted
2538 // to 32 bits. Insert an assert[sz]ext to capture this, then
2539 // truncate to the right size.
2540 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002541 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002542 case CCValAssign::Full: break;
2543 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002544 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002545 break;
2546 case CCValAssign::SExt:
2547 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2548 DAG.getValueType(VA.getValVT()));
2549 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2550 break;
2551 case CCValAssign::ZExt:
2552 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2553 DAG.getValueType(VA.getValVT()));
2554 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2555 break;
2556 }
2557
Dan Gohman98ca4f22009-08-05 01:29:28 +00002558 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002559
2560 } else { // VA.isRegLoc()
2561
2562 // sanity check
2563 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002564 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002565
Stuart Hastingsf222e592011-02-28 17:17:53 +00002566 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002567
Stuart Hastingsf222e592011-02-28 17:17:53 +00002568 // Some Ins[] entries become multiple ArgLoc[] entries.
2569 // Process them only once.
2570 if (index != lastInsIndex)
2571 {
2572 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002573 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002574 // This can be changed with more analysis.
2575 // In case of tail call optimization mark all arguments mutable.
2576 // Since they could be overwritten by lowering of arguments in case of
2577 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002578 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002579 unsigned VARegSize, VARegSaveSize;
2580 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2581 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2582 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002583 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002584 int FI = MFI->CreateFixedObject(Bytes,
2585 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002586 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2587 } else {
2588 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2589 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002590
Stuart Hastingsf222e592011-02-28 17:17:53 +00002591 // Create load nodes to retrieve arguments from the stack.
2592 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2593 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2594 MachinePointerInfo::getFixedStack(FI),
2595 false, false, 0));
2596 }
2597 lastInsIndex = index;
2598 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002599 }
2600 }
2601
2602 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002603 if (isVarArg)
2604 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002605
Dan Gohman98ca4f22009-08-05 01:29:28 +00002606 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002607}
2608
2609/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002610static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002611 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002612 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002613 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002614 // Maybe this has already been legalized into the constant pool?
2615 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002616 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002617 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002618 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002619 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002620 }
2621 }
2622 return false;
2623}
2624
Evan Chenga8e29892007-01-19 07:51:42 +00002625/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2626/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002627SDValue
2628ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002629 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002630 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002631 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002632 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002633 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002634 // Constant does not fit, try adjusting it by one?
2635 switch (CC) {
2636 default: break;
2637 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002638 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002639 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002640 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002641 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002642 }
2643 break;
2644 case ISD::SETULT:
2645 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002646 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002647 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002648 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002649 }
2650 break;
2651 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002652 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002653 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002654 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002656 }
2657 break;
2658 case ISD::SETULE:
2659 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002660 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002661 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002662 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002663 }
2664 break;
2665 }
2666 }
2667 }
2668
2669 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002670 ARMISD::NodeType CompareType;
2671 switch (CondCode) {
2672 default:
2673 CompareType = ARMISD::CMP;
2674 break;
2675 case ARMCC::EQ:
2676 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002677 // Uses only Z Flag
2678 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002679 break;
2680 }
Evan Cheng218977b2010-07-13 19:27:42 +00002681 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002682 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002683}
2684
2685/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002686SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002687ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002688 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002689 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002690 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002691 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002692 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002693 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2694 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002695}
2696
Bob Wilson79f56c92011-03-08 01:17:20 +00002697/// duplicateCmp - Glue values can have only one use, so this function
2698/// duplicates a comparison node.
2699SDValue
2700ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2701 unsigned Opc = Cmp.getOpcode();
2702 DebugLoc DL = Cmp.getDebugLoc();
2703 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2704 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2705
2706 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2707 Cmp = Cmp.getOperand(0);
2708 Opc = Cmp.getOpcode();
2709 if (Opc == ARMISD::CMPFP)
2710 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2711 else {
2712 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2713 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2714 }
2715 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2716}
2717
Bill Wendlingde2b1512010-08-11 08:43:16 +00002718SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2719 SDValue Cond = Op.getOperand(0);
2720 SDValue SelectTrue = Op.getOperand(1);
2721 SDValue SelectFalse = Op.getOperand(2);
2722 DebugLoc dl = Op.getDebugLoc();
2723
2724 // Convert:
2725 //
2726 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2727 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2728 //
2729 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2730 const ConstantSDNode *CMOVTrue =
2731 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2732 const ConstantSDNode *CMOVFalse =
2733 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2734
2735 if (CMOVTrue && CMOVFalse) {
2736 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2737 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2738
2739 SDValue True;
2740 SDValue False;
2741 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2742 True = SelectTrue;
2743 False = SelectFalse;
2744 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2745 True = SelectFalse;
2746 False = SelectTrue;
2747 }
2748
2749 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002750 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002751 SDValue ARMcc = Cond.getOperand(2);
2752 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002753 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002754 assert(True.getValueType() == VT);
2755 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002756 }
2757 }
2758 }
2759
2760 return DAG.getSelectCC(dl, Cond,
2761 DAG.getConstant(0, Cond.getValueType()),
2762 SelectTrue, SelectFalse, ISD::SETNE);
2763}
2764
Dan Gohmand858e902010-04-17 15:26:15 +00002765SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002766 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002767 SDValue LHS = Op.getOperand(0);
2768 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002769 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002770 SDValue TrueVal = Op.getOperand(2);
2771 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002772 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002773
Owen Anderson825b72b2009-08-11 20:47:22 +00002774 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002775 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002776 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002777 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Evan Chenge721f5c2011-07-13 00:42:17 +00002778 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002779 }
2780
2781 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002782 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002783
Evan Cheng218977b2010-07-13 19:27:42 +00002784 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2785 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002786 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002787 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002788 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002789 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002790 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002791 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002792 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002793 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002794 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002795 }
2796 return Result;
2797}
2798
Evan Cheng218977b2010-07-13 19:27:42 +00002799/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2800/// to morph to an integer compare sequence.
2801static bool canChangeToInt(SDValue Op, bool &SeenZero,
2802 const ARMSubtarget *Subtarget) {
2803 SDNode *N = Op.getNode();
2804 if (!N->hasOneUse())
2805 // Otherwise it requires moving the value from fp to integer registers.
2806 return false;
2807 if (!N->getNumValues())
2808 return false;
2809 EVT VT = Op.getValueType();
2810 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2811 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2812 // vmrs are very slow, e.g. cortex-a8.
2813 return false;
2814
2815 if (isFloatingPointZero(Op)) {
2816 SeenZero = true;
2817 return true;
2818 }
2819 return ISD::isNormalLoad(N);
2820}
2821
2822static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2823 if (isFloatingPointZero(Op))
2824 return DAG.getConstant(0, MVT::i32);
2825
2826 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2827 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002828 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002829 Ld->isVolatile(), Ld->isNonTemporal(),
2830 Ld->getAlignment());
2831
2832 llvm_unreachable("Unknown VFP cmp argument!");
2833}
2834
2835static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2836 SDValue &RetVal1, SDValue &RetVal2) {
2837 if (isFloatingPointZero(Op)) {
2838 RetVal1 = DAG.getConstant(0, MVT::i32);
2839 RetVal2 = DAG.getConstant(0, MVT::i32);
2840 return;
2841 }
2842
2843 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2844 SDValue Ptr = Ld->getBasePtr();
2845 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2846 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002847 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002848 Ld->isVolatile(), Ld->isNonTemporal(),
2849 Ld->getAlignment());
2850
2851 EVT PtrType = Ptr.getValueType();
2852 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2853 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2854 PtrType, Ptr, DAG.getConstant(4, PtrType));
2855 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2856 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002857 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002858 Ld->isVolatile(), Ld->isNonTemporal(),
2859 NewAlign);
2860 return;
2861 }
2862
2863 llvm_unreachable("Unknown VFP cmp argument!");
2864}
2865
2866/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2867/// f32 and even f64 comparisons to integer ones.
2868SDValue
2869ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2870 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002871 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002872 SDValue LHS = Op.getOperand(2);
2873 SDValue RHS = Op.getOperand(3);
2874 SDValue Dest = Op.getOperand(4);
2875 DebugLoc dl = Op.getDebugLoc();
2876
2877 bool SeenZero = false;
2878 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2879 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002880 // If one of the operand is zero, it's safe to ignore the NaN case since
2881 // we only care about equality comparisons.
2882 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002883 // If unsafe fp math optimization is enabled and there are no other uses of
2884 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002885 // to an integer comparison.
2886 if (CC == ISD::SETOEQ)
2887 CC = ISD::SETEQ;
2888 else if (CC == ISD::SETUNE)
2889 CC = ISD::SETNE;
2890
2891 SDValue ARMcc;
2892 if (LHS.getValueType() == MVT::f32) {
2893 LHS = bitcastf32Toi32(LHS, DAG);
2894 RHS = bitcastf32Toi32(RHS, DAG);
2895 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2896 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2897 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2898 Chain, Dest, ARMcc, CCR, Cmp);
2899 }
2900
2901 SDValue LHS1, LHS2;
2902 SDValue RHS1, RHS2;
2903 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2904 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2905 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2906 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002907 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002908 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2909 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2910 }
2911
2912 return SDValue();
2913}
2914
2915SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2916 SDValue Chain = Op.getOperand(0);
2917 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2918 SDValue LHS = Op.getOperand(2);
2919 SDValue RHS = Op.getOperand(3);
2920 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002921 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002922
Owen Anderson825b72b2009-08-11 20:47:22 +00002923 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002924 SDValue ARMcc;
2925 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002926 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002927 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002928 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002929 }
2930
Owen Anderson825b72b2009-08-11 20:47:22 +00002931 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002932
2933 if (UnsafeFPMath &&
2934 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2935 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2936 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2937 if (Result.getNode())
2938 return Result;
2939 }
2940
Evan Chenga8e29892007-01-19 07:51:42 +00002941 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002942 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002943
Evan Cheng218977b2010-07-13 19:27:42 +00002944 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2945 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002946 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002947 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002948 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002949 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002950 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002951 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2952 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002953 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002954 }
2955 return Res;
2956}
2957
Dan Gohmand858e902010-04-17 15:26:15 +00002958SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002959 SDValue Chain = Op.getOperand(0);
2960 SDValue Table = Op.getOperand(1);
2961 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002962 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002963
Owen Andersone50ed302009-08-10 22:56:29 +00002964 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002965 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2966 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002967 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002968 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002969 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002970 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2971 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002972 if (Subtarget->isThumb2()) {
2973 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2974 // which does another jump to the destination. This also makes it easier
2975 // to translate it to TBB / TBH later.
2976 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002978 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002979 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002980 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002981 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002982 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002983 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002984 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002985 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002986 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002987 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002988 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002989 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002990 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002991 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002992 }
Evan Chenga8e29892007-01-19 07:51:42 +00002993}
2994
Bob Wilson76a312b2010-03-19 22:51:32 +00002995static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2996 DebugLoc dl = Op.getDebugLoc();
2997 unsigned Opc;
2998
2999 switch (Op.getOpcode()) {
3000 default:
3001 assert(0 && "Invalid opcode!");
3002 case ISD::FP_TO_SINT:
3003 Opc = ARMISD::FTOSI;
3004 break;
3005 case ISD::FP_TO_UINT:
3006 Opc = ARMISD::FTOUI;
3007 break;
3008 }
3009 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003010 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003011}
3012
Cameron Zwarich3007d332011-03-29 21:41:55 +00003013static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3014 EVT VT = Op.getValueType();
3015 DebugLoc dl = Op.getDebugLoc();
3016
Duncan Sands1f6a3292011-08-12 14:54:45 +00003017 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3018 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003019 if (VT != MVT::v4f32)
3020 return DAG.UnrollVectorOp(Op.getNode());
3021
3022 unsigned CastOpc;
3023 unsigned Opc;
3024 switch (Op.getOpcode()) {
3025 default:
3026 assert(0 && "Invalid opcode!");
3027 case ISD::SINT_TO_FP:
3028 CastOpc = ISD::SIGN_EXTEND;
3029 Opc = ISD::SINT_TO_FP;
3030 break;
3031 case ISD::UINT_TO_FP:
3032 CastOpc = ISD::ZERO_EXTEND;
3033 Opc = ISD::UINT_TO_FP;
3034 break;
3035 }
3036
3037 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3038 return DAG.getNode(Opc, dl, VT, Op);
3039}
3040
Bob Wilson76a312b2010-03-19 22:51:32 +00003041static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3042 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003043 if (VT.isVector())
3044 return LowerVectorINT_TO_FP(Op, DAG);
3045
Bob Wilson76a312b2010-03-19 22:51:32 +00003046 DebugLoc dl = Op.getDebugLoc();
3047 unsigned Opc;
3048
3049 switch (Op.getOpcode()) {
3050 default:
3051 assert(0 && "Invalid opcode!");
3052 case ISD::SINT_TO_FP:
3053 Opc = ARMISD::SITOF;
3054 break;
3055 case ISD::UINT_TO_FP:
3056 Opc = ARMISD::UITOF;
3057 break;
3058 }
3059
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003060 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003061 return DAG.getNode(Opc, dl, VT, Op);
3062}
3063
Evan Cheng515fe3a2010-07-08 02:08:50 +00003064SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003065 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003066 SDValue Tmp0 = Op.getOperand(0);
3067 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003068 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003069 EVT VT = Op.getValueType();
3070 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003071 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3072 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3073 bool UseNEON = !InGPR && Subtarget->hasNEON();
3074
3075 if (UseNEON) {
3076 // Use VBSL to copy the sign bit.
3077 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3078 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3079 DAG.getTargetConstant(EncodedVal, MVT::i32));
3080 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3081 if (VT == MVT::f64)
3082 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3083 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3084 DAG.getConstant(32, MVT::i32));
3085 else /*if (VT == MVT::f32)*/
3086 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3087 if (SrcVT == MVT::f32) {
3088 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3089 if (VT == MVT::f64)
3090 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3091 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3092 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003093 } else if (VT == MVT::f32)
3094 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3095 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3096 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003097 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3098 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3099
3100 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3101 MVT::i32);
3102 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3103 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3104 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003105
Evan Chenge573fb32011-02-23 02:24:55 +00003106 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3107 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3108 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003109 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003110 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3111 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3112 DAG.getConstant(0, MVT::i32));
3113 } else {
3114 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3115 }
3116
3117 return Res;
3118 }
Evan Chengc143dd42011-02-11 02:28:55 +00003119
3120 // Bitcast operand 1 to i32.
3121 if (SrcVT == MVT::f64)
3122 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3123 &Tmp1, 1).getValue(1);
3124 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3125
Evan Chenge573fb32011-02-23 02:24:55 +00003126 // Or in the signbit with integer operations.
3127 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3128 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3129 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3130 if (VT == MVT::f32) {
3131 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3132 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3133 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3134 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003135 }
3136
Evan Chenge573fb32011-02-23 02:24:55 +00003137 // f64: Or the high part with signbit and then combine two parts.
3138 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3139 &Tmp0, 1);
3140 SDValue Lo = Tmp0.getValue(0);
3141 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3142 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3143 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003144}
3145
Evan Cheng2457f2c2010-05-22 01:47:14 +00003146SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3147 MachineFunction &MF = DAG.getMachineFunction();
3148 MachineFrameInfo *MFI = MF.getFrameInfo();
3149 MFI->setReturnAddressIsTaken(true);
3150
3151 EVT VT = Op.getValueType();
3152 DebugLoc dl = Op.getDebugLoc();
3153 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3154 if (Depth) {
3155 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3156 SDValue Offset = DAG.getConstant(4, MVT::i32);
3157 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3158 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003159 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003160 }
3161
3162 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003163 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003164 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3165}
3166
Dan Gohmand858e902010-04-17 15:26:15 +00003167SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003168 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3169 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003170
Owen Andersone50ed302009-08-10 22:56:29 +00003171 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003172 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3173 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003174 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003175 ? ARM::R7 : ARM::R11;
3176 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3177 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003178 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3179 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003180 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003181 return FrameAddr;
3182}
3183
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003184/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003185/// expand a bit convert where either the source or destination type is i64 to
3186/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3187/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3188/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003189static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003190 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3191 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003192 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003193
Bob Wilson9f3f0612010-04-17 05:30:19 +00003194 // This function is only supposed to be called for i64 types, either as the
3195 // source or destination of the bit convert.
3196 EVT SrcVT = Op.getValueType();
3197 EVT DstVT = N->getValueType(0);
3198 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003199 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003200
Bob Wilson9f3f0612010-04-17 05:30:19 +00003201 // Turn i64->f64 into VMOVDRR.
3202 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3204 DAG.getConstant(0, MVT::i32));
3205 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3206 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003207 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003208 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003209 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003210
Jim Grosbache5165492009-11-09 00:11:35 +00003211 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003212 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3213 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3214 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3215 // Merge the pieces into a single i64 value.
3216 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3217 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003218
Bob Wilson9f3f0612010-04-17 05:30:19 +00003219 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003220}
3221
Bob Wilson5bafff32009-06-22 23:27:02 +00003222/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003223/// Zero vectors are used to represent vector negation and in those cases
3224/// will be implemented with the NEON VNEG instruction. However, VNEG does
3225/// not support i64 elements, so sometimes the zero vectors will need to be
3226/// explicitly constructed. Regardless, use a canonical VMOV to create the
3227/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003228static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003229 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003230 // The canonical modified immediate encoding of a zero vector is....0!
3231 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3232 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3233 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003234 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003235}
3236
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003237/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3238/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003239SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3240 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003241 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3242 EVT VT = Op.getValueType();
3243 unsigned VTBits = VT.getSizeInBits();
3244 DebugLoc dl = Op.getDebugLoc();
3245 SDValue ShOpLo = Op.getOperand(0);
3246 SDValue ShOpHi = Op.getOperand(1);
3247 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003248 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003249 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003250
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003251 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3252
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003253 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3254 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3255 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3256 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3257 DAG.getConstant(VTBits, MVT::i32));
3258 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3259 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003260 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003261
3262 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3263 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003264 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003265 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003266 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003267 CCR, Cmp);
3268
3269 SDValue Ops[2] = { Lo, Hi };
3270 return DAG.getMergeValues(Ops, 2, dl);
3271}
3272
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003273/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3274/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003275SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3276 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003277 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3278 EVT VT = Op.getValueType();
3279 unsigned VTBits = VT.getSizeInBits();
3280 DebugLoc dl = Op.getDebugLoc();
3281 SDValue ShOpLo = Op.getOperand(0);
3282 SDValue ShOpHi = Op.getOperand(1);
3283 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003284 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003285
3286 assert(Op.getOpcode() == ISD::SHL_PARTS);
3287 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3288 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3289 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3290 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3291 DAG.getConstant(VTBits, MVT::i32));
3292 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3293 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3294
3295 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3296 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3297 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003298 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003299 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003300 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003301 CCR, Cmp);
3302
3303 SDValue Ops[2] = { Lo, Hi };
3304 return DAG.getMergeValues(Ops, 2, dl);
3305}
3306
Jim Grosbach4725ca72010-09-08 03:54:02 +00003307SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003308 SelectionDAG &DAG) const {
3309 // The rounding mode is in bits 23:22 of the FPSCR.
3310 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3311 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3312 // so that the shift + and get folded into a bitfield extract.
3313 DebugLoc dl = Op.getDebugLoc();
3314 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3315 DAG.getConstant(Intrinsic::arm_get_fpscr,
3316 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003317 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003318 DAG.getConstant(1U << 22, MVT::i32));
3319 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3320 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003321 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003322 DAG.getConstant(3, MVT::i32));
3323}
3324
Jim Grosbach3482c802010-01-18 19:58:49 +00003325static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3326 const ARMSubtarget *ST) {
3327 EVT VT = N->getValueType(0);
3328 DebugLoc dl = N->getDebugLoc();
3329
3330 if (!ST->hasV6T2Ops())
3331 return SDValue();
3332
3333 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3334 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3335}
3336
Bob Wilson5bafff32009-06-22 23:27:02 +00003337static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3338 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003339 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003340 DebugLoc dl = N->getDebugLoc();
3341
Bob Wilsond5448bb2010-11-18 21:16:28 +00003342 if (!VT.isVector())
3343 return SDValue();
3344
Bob Wilson5bafff32009-06-22 23:27:02 +00003345 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003346 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003347
Bob Wilsond5448bb2010-11-18 21:16:28 +00003348 // Left shifts translate directly to the vshiftu intrinsic.
3349 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003350 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003351 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3352 N->getOperand(0), N->getOperand(1));
3353
3354 assert((N->getOpcode() == ISD::SRA ||
3355 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3356
3357 // NEON uses the same intrinsics for both left and right shifts. For
3358 // right shifts, the shift amounts are negative, so negate the vector of
3359 // shift amounts.
3360 EVT ShiftVT = N->getOperand(1).getValueType();
3361 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3362 getZeroVector(ShiftVT, DAG, dl),
3363 N->getOperand(1));
3364 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3365 Intrinsic::arm_neon_vshifts :
3366 Intrinsic::arm_neon_vshiftu);
3367 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3368 DAG.getConstant(vshiftInt, MVT::i32),
3369 N->getOperand(0), NegatedCount);
3370}
3371
3372static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3373 const ARMSubtarget *ST) {
3374 EVT VT = N->getValueType(0);
3375 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003376
Eli Friedmance392eb2009-08-22 03:13:10 +00003377 // We can get here for a node like i32 = ISD::SHL i32, i64
3378 if (VT != MVT::i64)
3379 return SDValue();
3380
3381 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003382 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003383
Chris Lattner27a6c732007-11-24 07:07:01 +00003384 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3385 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003386 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003387 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003388
Chris Lattner27a6c732007-11-24 07:07:01 +00003389 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003390 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003391
Chris Lattner27a6c732007-11-24 07:07:01 +00003392 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003393 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003394 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003395 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003396 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003397
Chris Lattner27a6c732007-11-24 07:07:01 +00003398 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3399 // captures the result into a carry flag.
3400 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003401 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003402
Chris Lattner27a6c732007-11-24 07:07:01 +00003403 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003404 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003405
Chris Lattner27a6c732007-11-24 07:07:01 +00003406 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003407 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003408}
3409
Bob Wilson5bafff32009-06-22 23:27:02 +00003410static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3411 SDValue TmpOp0, TmpOp1;
3412 bool Invert = false;
3413 bool Swap = false;
3414 unsigned Opc = 0;
3415
3416 SDValue Op0 = Op.getOperand(0);
3417 SDValue Op1 = Op.getOperand(1);
3418 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003419 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003420 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3421 DebugLoc dl = Op.getDebugLoc();
3422
3423 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3424 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003425 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003426 case ISD::SETUNE:
3427 case ISD::SETNE: Invert = true; // Fallthrough
3428 case ISD::SETOEQ:
3429 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3430 case ISD::SETOLT:
3431 case ISD::SETLT: Swap = true; // Fallthrough
3432 case ISD::SETOGT:
3433 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3434 case ISD::SETOLE:
3435 case ISD::SETLE: Swap = true; // Fallthrough
3436 case ISD::SETOGE:
3437 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3438 case ISD::SETUGE: Swap = true; // Fallthrough
3439 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3440 case ISD::SETUGT: Swap = true; // Fallthrough
3441 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3442 case ISD::SETUEQ: Invert = true; // Fallthrough
3443 case ISD::SETONE:
3444 // Expand this to (OLT | OGT).
3445 TmpOp0 = Op0;
3446 TmpOp1 = Op1;
3447 Opc = ISD::OR;
3448 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3449 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3450 break;
3451 case ISD::SETUO: Invert = true; // Fallthrough
3452 case ISD::SETO:
3453 // Expand this to (OLT | OGE).
3454 TmpOp0 = Op0;
3455 TmpOp1 = Op1;
3456 Opc = ISD::OR;
3457 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3458 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3459 break;
3460 }
3461 } else {
3462 // Integer comparisons.
3463 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003464 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003465 case ISD::SETNE: Invert = true;
3466 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3467 case ISD::SETLT: Swap = true;
3468 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3469 case ISD::SETLE: Swap = true;
3470 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3471 case ISD::SETULT: Swap = true;
3472 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3473 case ISD::SETULE: Swap = true;
3474 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3475 }
3476
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003477 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003478 if (Opc == ARMISD::VCEQ) {
3479
3480 SDValue AndOp;
3481 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3482 AndOp = Op0;
3483 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3484 AndOp = Op1;
3485
3486 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003487 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003488 AndOp = AndOp.getOperand(0);
3489
3490 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3491 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003492 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3493 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003494 Invert = !Invert;
3495 }
3496 }
3497 }
3498
3499 if (Swap)
3500 std::swap(Op0, Op1);
3501
Owen Andersonc24cb352010-11-08 23:21:22 +00003502 // If one of the operands is a constant vector zero, attempt to fold the
3503 // comparison to a specialized compare-against-zero form.
3504 SDValue SingleOp;
3505 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3506 SingleOp = Op0;
3507 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3508 if (Opc == ARMISD::VCGE)
3509 Opc = ARMISD::VCLEZ;
3510 else if (Opc == ARMISD::VCGT)
3511 Opc = ARMISD::VCLTZ;
3512 SingleOp = Op1;
3513 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003514
Owen Andersonc24cb352010-11-08 23:21:22 +00003515 SDValue Result;
3516 if (SingleOp.getNode()) {
3517 switch (Opc) {
3518 case ARMISD::VCEQ:
3519 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3520 case ARMISD::VCGE:
3521 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3522 case ARMISD::VCLEZ:
3523 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3524 case ARMISD::VCGT:
3525 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3526 case ARMISD::VCLTZ:
3527 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3528 default:
3529 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3530 }
3531 } else {
3532 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3533 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003534
3535 if (Invert)
3536 Result = DAG.getNOT(dl, Result, VT);
3537
3538 return Result;
3539}
3540
Bob Wilsond3c42842010-06-14 22:19:57 +00003541/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3542/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003543/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003544static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3545 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003546 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003547 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003548
Bob Wilson827b2102010-06-15 19:05:35 +00003549 // SplatBitSize is set to the smallest size that splats the vector, so a
3550 // zero vector will always have SplatBitSize == 8. However, NEON modified
3551 // immediate instructions others than VMOV do not support the 8-bit encoding
3552 // of a zero vector, and the default encoding of zero is supposed to be the
3553 // 32-bit version.
3554 if (SplatBits == 0)
3555 SplatBitSize = 32;
3556
Bob Wilson5bafff32009-06-22 23:27:02 +00003557 switch (SplatBitSize) {
3558 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003559 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003560 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003561 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003562 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003563 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003564 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003565 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003566 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003567
3568 case 16:
3569 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003570 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003571 if ((SplatBits & ~0xff) == 0) {
3572 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003573 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003574 Imm = SplatBits;
3575 break;
3576 }
3577 if ((SplatBits & ~0xff00) == 0) {
3578 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003579 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003580 Imm = SplatBits >> 8;
3581 break;
3582 }
3583 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003584
3585 case 32:
3586 // NEON's 32-bit VMOV supports splat values where:
3587 // * only one byte is nonzero, or
3588 // * the least significant byte is 0xff and the second byte is nonzero, or
3589 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003590 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003591 if ((SplatBits & ~0xff) == 0) {
3592 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003593 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003594 Imm = SplatBits;
3595 break;
3596 }
3597 if ((SplatBits & ~0xff00) == 0) {
3598 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003599 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003600 Imm = SplatBits >> 8;
3601 break;
3602 }
3603 if ((SplatBits & ~0xff0000) == 0) {
3604 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003605 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003606 Imm = SplatBits >> 16;
3607 break;
3608 }
3609 if ((SplatBits & ~0xff000000) == 0) {
3610 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003611 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003612 Imm = SplatBits >> 24;
3613 break;
3614 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003615
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003616 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3617 if (type == OtherModImm) return SDValue();
3618
Bob Wilson5bafff32009-06-22 23:27:02 +00003619 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003620 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3621 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003622 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003623 Imm = SplatBits >> 8;
3624 SplatBits |= 0xff;
3625 break;
3626 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003627
3628 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003629 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3630 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003631 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003632 Imm = SplatBits >> 16;
3633 SplatBits |= 0xffff;
3634 break;
3635 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003636
3637 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3638 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3639 // VMOV.I32. A (very) minor optimization would be to replicate the value
3640 // and fall through here to test for a valid 64-bit splat. But, then the
3641 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003642 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003643
3644 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003645 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003646 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003647 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003648 uint64_t BitMask = 0xff;
3649 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003650 unsigned ImmMask = 1;
3651 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003652 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003653 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003654 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003655 Imm |= ImmMask;
3656 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003657 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003658 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003659 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003660 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003661 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003662 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003663 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003664 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003665 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003666 break;
3667 }
3668
Bob Wilson1a913ed2010-06-11 21:34:50 +00003669 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003670 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003671 return SDValue();
3672 }
3673
Bob Wilsoncba270d2010-07-13 21:16:48 +00003674 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3675 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003676}
3677
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003678static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3679 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003680 unsigned NumElts = VT.getVectorNumElements();
3681 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003682
3683 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3684 if (M[0] < 0)
3685 return false;
3686
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003687 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003688
3689 // If this is a VEXT shuffle, the immediate value is the index of the first
3690 // element. The other shuffle indices must be the successive elements after
3691 // the first one.
3692 unsigned ExpectedElt = Imm;
3693 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003694 // Increment the expected index. If it wraps around, it may still be
3695 // a VEXT but the source vectors must be swapped.
3696 ExpectedElt += 1;
3697 if (ExpectedElt == NumElts * 2) {
3698 ExpectedElt = 0;
3699 ReverseVEXT = true;
3700 }
3701
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003702 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003703 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003704 return false;
3705 }
3706
3707 // Adjust the index value if the source operands will be swapped.
3708 if (ReverseVEXT)
3709 Imm -= NumElts;
3710
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003711 return true;
3712}
3713
Bob Wilson8bb9e482009-07-26 00:39:34 +00003714/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3715/// instruction with the specified blocksize. (The order of the elements
3716/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003717static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3718 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003719 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3720 "Only possible block sizes for VREV are: 16, 32, 64");
3721
Bob Wilson8bb9e482009-07-26 00:39:34 +00003722 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003723 if (EltSz == 64)
3724 return false;
3725
3726 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003727 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003728 // If the first shuffle index is UNDEF, be optimistic.
3729 if (M[0] < 0)
3730 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003731
3732 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3733 return false;
3734
3735 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003736 if (M[i] < 0) continue; // ignore UNDEF indices
3737 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003738 return false;
3739 }
3740
3741 return true;
3742}
3743
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003744static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3745 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3746 // range, then 0 is placed into the resulting vector. So pretty much any mask
3747 // of 8 elements can work here.
3748 return VT == MVT::v8i8 && M.size() == 8;
3749}
3750
Bob Wilsonc692cb72009-08-21 20:54:19 +00003751static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3752 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003753 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3754 if (EltSz == 64)
3755 return false;
3756
Bob Wilsonc692cb72009-08-21 20:54:19 +00003757 unsigned NumElts = VT.getVectorNumElements();
3758 WhichResult = (M[0] == 0 ? 0 : 1);
3759 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003760 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3761 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003762 return false;
3763 }
3764 return true;
3765}
3766
Bob Wilson324f4f12009-12-03 06:40:55 +00003767/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3768/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3769/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3770static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3771 unsigned &WhichResult) {
3772 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3773 if (EltSz == 64)
3774 return false;
3775
3776 unsigned NumElts = VT.getVectorNumElements();
3777 WhichResult = (M[0] == 0 ? 0 : 1);
3778 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003779 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3780 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003781 return false;
3782 }
3783 return true;
3784}
3785
Bob Wilsonc692cb72009-08-21 20:54:19 +00003786static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3787 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003788 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3789 if (EltSz == 64)
3790 return false;
3791
Bob Wilsonc692cb72009-08-21 20:54:19 +00003792 unsigned NumElts = VT.getVectorNumElements();
3793 WhichResult = (M[0] == 0 ? 0 : 1);
3794 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003795 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003796 if ((unsigned) M[i] != 2 * i + WhichResult)
3797 return false;
3798 }
3799
3800 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003801 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003802 return false;
3803
3804 return true;
3805}
3806
Bob Wilson324f4f12009-12-03 06:40:55 +00003807/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3808/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3809/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3810static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3811 unsigned &WhichResult) {
3812 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3813 if (EltSz == 64)
3814 return false;
3815
3816 unsigned Half = VT.getVectorNumElements() / 2;
3817 WhichResult = (M[0] == 0 ? 0 : 1);
3818 for (unsigned j = 0; j != 2; ++j) {
3819 unsigned Idx = WhichResult;
3820 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003821 int MIdx = M[i + j * Half];
3822 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003823 return false;
3824 Idx += 2;
3825 }
3826 }
3827
3828 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3829 if (VT.is64BitVector() && EltSz == 32)
3830 return false;
3831
3832 return true;
3833}
3834
Bob Wilsonc692cb72009-08-21 20:54:19 +00003835static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3836 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003837 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3838 if (EltSz == 64)
3839 return false;
3840
Bob Wilsonc692cb72009-08-21 20:54:19 +00003841 unsigned NumElts = VT.getVectorNumElements();
3842 WhichResult = (M[0] == 0 ? 0 : 1);
3843 unsigned Idx = WhichResult * NumElts / 2;
3844 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003845 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3846 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003847 return false;
3848 Idx += 1;
3849 }
3850
3851 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003852 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003853 return false;
3854
3855 return true;
3856}
3857
Bob Wilson324f4f12009-12-03 06:40:55 +00003858/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3859/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3860/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3861static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3862 unsigned &WhichResult) {
3863 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3864 if (EltSz == 64)
3865 return false;
3866
3867 unsigned NumElts = VT.getVectorNumElements();
3868 WhichResult = (M[0] == 0 ? 0 : 1);
3869 unsigned Idx = WhichResult * NumElts / 2;
3870 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003871 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3872 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003873 return false;
3874 Idx += 1;
3875 }
3876
3877 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3878 if (VT.is64BitVector() && EltSz == 32)
3879 return false;
3880
3881 return true;
3882}
3883
Dale Johannesenf630c712010-07-29 20:10:08 +00003884// If N is an integer constant that can be moved into a register in one
3885// instruction, return an SDValue of such a constant (will become a MOV
3886// instruction). Otherwise return null.
3887static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3888 const ARMSubtarget *ST, DebugLoc dl) {
3889 uint64_t Val;
3890 if (!isa<ConstantSDNode>(N))
3891 return SDValue();
3892 Val = cast<ConstantSDNode>(N)->getZExtValue();
3893
3894 if (ST->isThumb1Only()) {
3895 if (Val <= 255 || ~Val <= 255)
3896 return DAG.getConstant(Val, MVT::i32);
3897 } else {
3898 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3899 return DAG.getConstant(Val, MVT::i32);
3900 }
3901 return SDValue();
3902}
3903
Bob Wilson5bafff32009-06-22 23:27:02 +00003904// If this is a case we can't handle, return null and let the default
3905// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003906SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3907 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003908 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003909 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003910 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003911
3912 APInt SplatBits, SplatUndef;
3913 unsigned SplatBitSize;
3914 bool HasAnyUndefs;
3915 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003916 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003917 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003918 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003919 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003920 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003921 DAG, VmovVT, VT.is128BitVector(),
3922 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003923 if (Val.getNode()) {
3924 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003925 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003926 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003927
3928 // Try an immediate VMVN.
3929 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3930 ((1LL << SplatBitSize) - 1));
3931 Val = isNEONModifiedImm(NegatedImm,
3932 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003933 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003934 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003935 if (Val.getNode()) {
3936 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003937 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003938 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003939 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003940 }
3941
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003942 // Scan through the operands to see if only one value is used.
3943 unsigned NumElts = VT.getVectorNumElements();
3944 bool isOnlyLowElement = true;
3945 bool usesOnlyOneValue = true;
3946 bool isConstant = true;
3947 SDValue Value;
3948 for (unsigned i = 0; i < NumElts; ++i) {
3949 SDValue V = Op.getOperand(i);
3950 if (V.getOpcode() == ISD::UNDEF)
3951 continue;
3952 if (i > 0)
3953 isOnlyLowElement = false;
3954 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3955 isConstant = false;
3956
3957 if (!Value.getNode())
3958 Value = V;
3959 else if (V != Value)
3960 usesOnlyOneValue = false;
3961 }
3962
3963 if (!Value.getNode())
3964 return DAG.getUNDEF(VT);
3965
3966 if (isOnlyLowElement)
3967 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3968
Dale Johannesenf630c712010-07-29 20:10:08 +00003969 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3970
Dale Johannesen575cd142010-10-19 20:00:17 +00003971 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3972 // i32 and try again.
3973 if (usesOnlyOneValue && EltSize <= 32) {
3974 if (!isConstant)
3975 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3976 if (VT.getVectorElementType().isFloatingPoint()) {
3977 SmallVector<SDValue, 8> Ops;
3978 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003979 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003980 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003981 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3982 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003983 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3984 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003985 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003986 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003987 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3988 if (Val.getNode())
3989 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003990 }
3991
3992 // If all elements are constants and the case above didn't get hit, fall back
3993 // to the default expansion, which will generate a load from the constant
3994 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003995 if (isConstant)
3996 return SDValue();
3997
Bob Wilson11a1dff2011-01-07 21:37:30 +00003998 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3999 if (NumElts >= 4) {
4000 SDValue shuffle = ReconstructShuffle(Op, DAG);
4001 if (shuffle != SDValue())
4002 return shuffle;
4003 }
4004
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004005 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004006 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4007 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004008 if (EltSize >= 32) {
4009 // Do the expansion with floating-point types, since that is what the VFP
4010 // registers are defined to use, and since i64 is not legal.
4011 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4012 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004013 SmallVector<SDValue, 8> Ops;
4014 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004015 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004016 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004017 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004018 }
4019
4020 return SDValue();
4021}
4022
Bob Wilson11a1dff2011-01-07 21:37:30 +00004023// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004024// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004025SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4026 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004027 DebugLoc dl = Op.getDebugLoc();
4028 EVT VT = Op.getValueType();
4029 unsigned NumElts = VT.getVectorNumElements();
4030
4031 SmallVector<SDValue, 2> SourceVecs;
4032 SmallVector<unsigned, 2> MinElts;
4033 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004034
Bob Wilson11a1dff2011-01-07 21:37:30 +00004035 for (unsigned i = 0; i < NumElts; ++i) {
4036 SDValue V = Op.getOperand(i);
4037 if (V.getOpcode() == ISD::UNDEF)
4038 continue;
4039 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4040 // A shuffle can only come from building a vector from various
4041 // elements of other vectors.
4042 return SDValue();
4043 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004044
Bob Wilson11a1dff2011-01-07 21:37:30 +00004045 // Record this extraction against the appropriate vector if possible...
4046 SDValue SourceVec = V.getOperand(0);
4047 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4048 bool FoundSource = false;
4049 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4050 if (SourceVecs[j] == SourceVec) {
4051 if (MinElts[j] > EltNo)
4052 MinElts[j] = EltNo;
4053 if (MaxElts[j] < EltNo)
4054 MaxElts[j] = EltNo;
4055 FoundSource = true;
4056 break;
4057 }
4058 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004059
Bob Wilson11a1dff2011-01-07 21:37:30 +00004060 // Or record a new source if not...
4061 if (!FoundSource) {
4062 SourceVecs.push_back(SourceVec);
4063 MinElts.push_back(EltNo);
4064 MaxElts.push_back(EltNo);
4065 }
4066 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004067
Bob Wilson11a1dff2011-01-07 21:37:30 +00004068 // Currently only do something sane when at most two source vectors
4069 // involved.
4070 if (SourceVecs.size() > 2)
4071 return SDValue();
4072
4073 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4074 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004075
Bob Wilson11a1dff2011-01-07 21:37:30 +00004076 // This loop extracts the usage patterns of the source vectors
4077 // and prepares appropriate SDValues for a shuffle if possible.
4078 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4079 if (SourceVecs[i].getValueType() == VT) {
4080 // No VEXT necessary
4081 ShuffleSrcs[i] = SourceVecs[i];
4082 VEXTOffsets[i] = 0;
4083 continue;
4084 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4085 // It probably isn't worth padding out a smaller vector just to
4086 // break it down again in a shuffle.
4087 return SDValue();
4088 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004089
Bob Wilson11a1dff2011-01-07 21:37:30 +00004090 // Since only 64-bit and 128-bit vectors are legal on ARM and
4091 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004092 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4093 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004094
Bob Wilson11a1dff2011-01-07 21:37:30 +00004095 if (MaxElts[i] - MinElts[i] >= NumElts) {
4096 // Span too large for a VEXT to cope
4097 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004098 }
4099
Bob Wilson11a1dff2011-01-07 21:37:30 +00004100 if (MinElts[i] >= NumElts) {
4101 // The extraction can just take the second half
4102 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004103 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4104 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004105 DAG.getIntPtrConstant(NumElts));
4106 } else if (MaxElts[i] < NumElts) {
4107 // The extraction can just take the first half
4108 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004109 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4110 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004111 DAG.getIntPtrConstant(0));
4112 } else {
4113 // An actual VEXT is needed
4114 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004115 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4116 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004117 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004118 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4119 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004120 DAG.getIntPtrConstant(NumElts));
4121 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4122 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4123 }
4124 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004125
Bob Wilson11a1dff2011-01-07 21:37:30 +00004126 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004127
Bob Wilson11a1dff2011-01-07 21:37:30 +00004128 for (unsigned i = 0; i < NumElts; ++i) {
4129 SDValue Entry = Op.getOperand(i);
4130 if (Entry.getOpcode() == ISD::UNDEF) {
4131 Mask.push_back(-1);
4132 continue;
4133 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004134
Bob Wilson11a1dff2011-01-07 21:37:30 +00004135 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004136 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4137 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004138 if (ExtractVec == SourceVecs[0]) {
4139 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4140 } else {
4141 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4142 }
4143 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004144
Bob Wilson11a1dff2011-01-07 21:37:30 +00004145 // Final check before we try to produce nonsense...
4146 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004147 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4148 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004149
Bob Wilson11a1dff2011-01-07 21:37:30 +00004150 return SDValue();
4151}
4152
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004153/// isShuffleMaskLegal - Targets can use this to indicate that they only
4154/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4155/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4156/// are assumed to be legal.
4157bool
4158ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4159 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004160 if (VT.getVectorNumElements() == 4 &&
4161 (VT.is128BitVector() || VT.is64BitVector())) {
4162 unsigned PFIndexes[4];
4163 for (unsigned i = 0; i != 4; ++i) {
4164 if (M[i] < 0)
4165 PFIndexes[i] = 8;
4166 else
4167 PFIndexes[i] = M[i];
4168 }
4169
4170 // Compute the index in the perfect shuffle table.
4171 unsigned PFTableIndex =
4172 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4173 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4174 unsigned Cost = (PFEntry >> 30);
4175
4176 if (Cost <= 4)
4177 return true;
4178 }
4179
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004180 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004181 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004182
Bob Wilson53dd2452010-06-07 23:53:38 +00004183 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4184 return (EltSize >= 32 ||
4185 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004186 isVREVMask(M, VT, 64) ||
4187 isVREVMask(M, VT, 32) ||
4188 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004189 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004190 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004191 isVTRNMask(M, VT, WhichResult) ||
4192 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004193 isVZIPMask(M, VT, WhichResult) ||
4194 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4195 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4196 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004197}
4198
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004199/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4200/// the specified operations to build the shuffle.
4201static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4202 SDValue RHS, SelectionDAG &DAG,
4203 DebugLoc dl) {
4204 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4205 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4206 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4207
4208 enum {
4209 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4210 OP_VREV,
4211 OP_VDUP0,
4212 OP_VDUP1,
4213 OP_VDUP2,
4214 OP_VDUP3,
4215 OP_VEXT1,
4216 OP_VEXT2,
4217 OP_VEXT3,
4218 OP_VUZPL, // VUZP, left result
4219 OP_VUZPR, // VUZP, right result
4220 OP_VZIPL, // VZIP, left result
4221 OP_VZIPR, // VZIP, right result
4222 OP_VTRNL, // VTRN, left result
4223 OP_VTRNR // VTRN, right result
4224 };
4225
4226 if (OpNum == OP_COPY) {
4227 if (LHSID == (1*9+2)*9+3) return LHS;
4228 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4229 return RHS;
4230 }
4231
4232 SDValue OpLHS, OpRHS;
4233 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4234 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4235 EVT VT = OpLHS.getValueType();
4236
4237 switch (OpNum) {
4238 default: llvm_unreachable("Unknown shuffle opcode!");
4239 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004240 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004241 if (VT.getVectorElementType() == MVT::i32 ||
4242 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004243 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4244 // vrev <4 x i16> -> VREV32
4245 if (VT.getVectorElementType() == MVT::i16)
4246 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4247 // vrev <4 x i8> -> VREV16
4248 assert(VT.getVectorElementType() == MVT::i8);
4249 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004250 case OP_VDUP0:
4251 case OP_VDUP1:
4252 case OP_VDUP2:
4253 case OP_VDUP3:
4254 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004255 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004256 case OP_VEXT1:
4257 case OP_VEXT2:
4258 case OP_VEXT3:
4259 return DAG.getNode(ARMISD::VEXT, dl, VT,
4260 OpLHS, OpRHS,
4261 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4262 case OP_VUZPL:
4263 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004264 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004265 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4266 case OP_VZIPL:
4267 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004268 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004269 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4270 case OP_VTRNL:
4271 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004272 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4273 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004274 }
4275}
4276
Bill Wendling69a05a72011-03-14 23:02:38 +00004277static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4278 SmallVectorImpl<int> &ShuffleMask,
4279 SelectionDAG &DAG) {
4280 // Check to see if we can use the VTBL instruction.
4281 SDValue V1 = Op.getOperand(0);
4282 SDValue V2 = Op.getOperand(1);
4283 DebugLoc DL = Op.getDebugLoc();
4284
4285 SmallVector<SDValue, 8> VTBLMask;
4286 for (SmallVectorImpl<int>::iterator
4287 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4288 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4289
4290 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4291 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4292 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4293 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004294
Owen Anderson76706012011-04-05 21:48:57 +00004295 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004296 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4297 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004298}
4299
Bob Wilson5bafff32009-06-22 23:27:02 +00004300static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004301 SDValue V1 = Op.getOperand(0);
4302 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004303 DebugLoc dl = Op.getDebugLoc();
4304 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004305 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004306 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004307
Bob Wilson28865062009-08-13 02:13:04 +00004308 // Convert shuffles that are directly supported on NEON to target-specific
4309 // DAG nodes, instead of keeping them as shuffles and matching them again
4310 // during code selection. This is more efficient and avoids the possibility
4311 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004312 // FIXME: floating-point vectors should be canonicalized to integer vectors
4313 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004314 SVN->getMask(ShuffleMask);
4315
Bob Wilson53dd2452010-06-07 23:53:38 +00004316 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4317 if (EltSize <= 32) {
4318 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4319 int Lane = SVN->getSplatIndex();
4320 // If this is undef splat, generate it via "just" vdup, if possible.
4321 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004322
Bob Wilson53dd2452010-06-07 23:53:38 +00004323 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4324 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4325 }
4326 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4327 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004328 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004329
4330 bool ReverseVEXT;
4331 unsigned Imm;
4332 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4333 if (ReverseVEXT)
4334 std::swap(V1, V2);
4335 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4336 DAG.getConstant(Imm, MVT::i32));
4337 }
4338
4339 if (isVREVMask(ShuffleMask, VT, 64))
4340 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4341 if (isVREVMask(ShuffleMask, VT, 32))
4342 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4343 if (isVREVMask(ShuffleMask, VT, 16))
4344 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4345
4346 // Check for Neon shuffles that modify both input vectors in place.
4347 // If both results are used, i.e., if there are two shuffles with the same
4348 // source operands and with masks corresponding to both results of one of
4349 // these operations, DAG memoization will ensure that a single node is
4350 // used for both shuffles.
4351 unsigned WhichResult;
4352 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4353 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4354 V1, V2).getValue(WhichResult);
4355 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4356 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4357 V1, V2).getValue(WhichResult);
4358 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4359 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4360 V1, V2).getValue(WhichResult);
4361
4362 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4363 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4364 V1, V1).getValue(WhichResult);
4365 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4366 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4367 V1, V1).getValue(WhichResult);
4368 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4369 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4370 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004371 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004372
Bob Wilsonc692cb72009-08-21 20:54:19 +00004373 // If the shuffle is not directly supported and it has 4 elements, use
4374 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004375 unsigned NumElts = VT.getVectorNumElements();
4376 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004377 unsigned PFIndexes[4];
4378 for (unsigned i = 0; i != 4; ++i) {
4379 if (ShuffleMask[i] < 0)
4380 PFIndexes[i] = 8;
4381 else
4382 PFIndexes[i] = ShuffleMask[i];
4383 }
4384
4385 // Compute the index in the perfect shuffle table.
4386 unsigned PFTableIndex =
4387 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004388 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4389 unsigned Cost = (PFEntry >> 30);
4390
4391 if (Cost <= 4)
4392 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4393 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004394
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004395 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004396 if (EltSize >= 32) {
4397 // Do the expansion with floating-point types, since that is what the VFP
4398 // registers are defined to use, and since i64 is not legal.
4399 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4400 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004401 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4402 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004403 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004404 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004405 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004406 Ops.push_back(DAG.getUNDEF(EltVT));
4407 else
4408 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4409 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4410 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4411 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004412 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004413 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004414 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004415 }
4416
Bill Wendling69a05a72011-03-14 23:02:38 +00004417 if (VT == MVT::v8i8) {
4418 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4419 if (NewOp.getNode())
4420 return NewOp;
4421 }
4422
Bob Wilson22cac0d2009-08-14 05:16:33 +00004423 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004424}
4425
Bob Wilson5bafff32009-06-22 23:27:02 +00004426static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004427 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004428 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004429 if (!isa<ConstantSDNode>(Lane))
4430 return SDValue();
4431
4432 SDValue Vec = Op.getOperand(0);
4433 if (Op.getValueType() == MVT::i32 &&
4434 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4435 DebugLoc dl = Op.getDebugLoc();
4436 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4437 }
4438
4439 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004440}
4441
Bob Wilsona6d65862009-08-03 20:36:38 +00004442static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4443 // The only time a CONCAT_VECTORS operation can have legal types is when
4444 // two 64-bit vectors are concatenated to a 128-bit vector.
4445 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4446 "unexpected CONCAT_VECTORS");
4447 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004448 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004449 SDValue Op0 = Op.getOperand(0);
4450 SDValue Op1 = Op.getOperand(1);
4451 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004452 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004453 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004454 DAG.getIntPtrConstant(0));
4455 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004456 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004457 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004458 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004459 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004460}
4461
Bob Wilson626613d2010-11-23 19:38:38 +00004462/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4463/// element has been zero/sign-extended, depending on the isSigned parameter,
4464/// from an integer type half its size.
4465static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4466 bool isSigned) {
4467 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4468 EVT VT = N->getValueType(0);
4469 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4470 SDNode *BVN = N->getOperand(0).getNode();
4471 if (BVN->getValueType(0) != MVT::v4i32 ||
4472 BVN->getOpcode() != ISD::BUILD_VECTOR)
4473 return false;
4474 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4475 unsigned HiElt = 1 - LoElt;
4476 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4477 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4478 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4479 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4480 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4481 return false;
4482 if (isSigned) {
4483 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4484 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4485 return true;
4486 } else {
4487 if (Hi0->isNullValue() && Hi1->isNullValue())
4488 return true;
4489 }
4490 return false;
4491 }
4492
4493 if (N->getOpcode() != ISD::BUILD_VECTOR)
4494 return false;
4495
4496 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4497 SDNode *Elt = N->getOperand(i).getNode();
4498 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4499 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4500 unsigned HalfSize = EltSize / 2;
4501 if (isSigned) {
4502 int64_t SExtVal = C->getSExtValue();
4503 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4504 return false;
4505 } else {
4506 if ((C->getZExtValue() >> HalfSize) != 0)
4507 return false;
4508 }
4509 continue;
4510 }
4511 return false;
4512 }
4513
4514 return true;
4515}
4516
4517/// isSignExtended - Check if a node is a vector value that is sign-extended
4518/// or a constant BUILD_VECTOR with sign-extended elements.
4519static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4520 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4521 return true;
4522 if (isExtendedBUILD_VECTOR(N, DAG, true))
4523 return true;
4524 return false;
4525}
4526
4527/// isZeroExtended - Check if a node is a vector value that is zero-extended
4528/// or a constant BUILD_VECTOR with zero-extended elements.
4529static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4530 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4531 return true;
4532 if (isExtendedBUILD_VECTOR(N, DAG, false))
4533 return true;
4534 return false;
4535}
4536
4537/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4538/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004539static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4540 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4541 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004542 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4543 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4544 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4545 LD->isNonTemporal(), LD->getAlignment());
4546 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4547 // have been legalized as a BITCAST from v4i32.
4548 if (N->getOpcode() == ISD::BITCAST) {
4549 SDNode *BVN = N->getOperand(0).getNode();
4550 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4551 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4552 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4553 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4554 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4555 }
4556 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4557 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4558 EVT VT = N->getValueType(0);
4559 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4560 unsigned NumElts = VT.getVectorNumElements();
4561 MVT TruncVT = MVT::getIntegerVT(EltSize);
4562 SmallVector<SDValue, 8> Ops;
4563 for (unsigned i = 0; i != NumElts; ++i) {
4564 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4565 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004566 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004567 }
4568 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4569 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004570}
4571
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004572static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4573 unsigned Opcode = N->getOpcode();
4574 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4575 SDNode *N0 = N->getOperand(0).getNode();
4576 SDNode *N1 = N->getOperand(1).getNode();
4577 return N0->hasOneUse() && N1->hasOneUse() &&
4578 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4579 }
4580 return false;
4581}
4582
4583static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4584 unsigned Opcode = N->getOpcode();
4585 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4586 SDNode *N0 = N->getOperand(0).getNode();
4587 SDNode *N1 = N->getOperand(1).getNode();
4588 return N0->hasOneUse() && N1->hasOneUse() &&
4589 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4590 }
4591 return false;
4592}
4593
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004594static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4595 // Multiplications are only custom-lowered for 128-bit vectors so that
4596 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4597 EVT VT = Op.getValueType();
4598 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4599 SDNode *N0 = Op.getOperand(0).getNode();
4600 SDNode *N1 = Op.getOperand(1).getNode();
4601 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004602 bool isMLA = false;
4603 bool isN0SExt = isSignExtended(N0, DAG);
4604 bool isN1SExt = isSignExtended(N1, DAG);
4605 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004606 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004607 else {
4608 bool isN0ZExt = isZeroExtended(N0, DAG);
4609 bool isN1ZExt = isZeroExtended(N1, DAG);
4610 if (isN0ZExt && isN1ZExt)
4611 NewOpc = ARMISD::VMULLu;
4612 else if (isN1SExt || isN1ZExt) {
4613 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4614 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4615 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4616 NewOpc = ARMISD::VMULLs;
4617 isMLA = true;
4618 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4619 NewOpc = ARMISD::VMULLu;
4620 isMLA = true;
4621 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4622 std::swap(N0, N1);
4623 NewOpc = ARMISD::VMULLu;
4624 isMLA = true;
4625 }
4626 }
4627
4628 if (!NewOpc) {
4629 if (VT == MVT::v2i64)
4630 // Fall through to expand this. It is not legal.
4631 return SDValue();
4632 else
4633 // Other vector multiplications are legal.
4634 return Op;
4635 }
4636 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004637
4638 // Legalize to a VMULL instruction.
4639 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004640 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004641 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004642 if (!isMLA) {
4643 Op0 = SkipExtension(N0, DAG);
4644 assert(Op0.getValueType().is64BitVector() &&
4645 Op1.getValueType().is64BitVector() &&
4646 "unexpected types for extended operands to VMULL");
4647 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4648 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004649
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004650 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4651 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4652 // vmull q0, d4, d6
4653 // vmlal q0, d5, d6
4654 // is faster than
4655 // vaddl q0, d4, d5
4656 // vmovl q1, d6
4657 // vmul q0, q0, q1
4658 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4659 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4660 EVT Op1VT = Op1.getValueType();
4661 return DAG.getNode(N0->getOpcode(), DL, VT,
4662 DAG.getNode(NewOpc, DL, VT,
4663 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4664 DAG.getNode(NewOpc, DL, VT,
4665 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004666}
4667
Owen Anderson76706012011-04-05 21:48:57 +00004668static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004669LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4670 // Convert to float
4671 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4672 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4673 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4674 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4675 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4676 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4677 // Get reciprocal estimate.
4678 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004679 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004680 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4681 // Because char has a smaller range than uchar, we can actually get away
4682 // without any newton steps. This requires that we use a weird bias
4683 // of 0xb000, however (again, this has been exhaustively tested).
4684 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4685 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4686 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4687 Y = DAG.getConstant(0xb000, MVT::i32);
4688 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4689 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4690 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4691 // Convert back to short.
4692 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4693 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4694 return X;
4695}
4696
Owen Anderson76706012011-04-05 21:48:57 +00004697static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004698LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4699 SDValue N2;
4700 // Convert to float.
4701 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4702 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4703 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4704 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4705 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4706 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004707
Nate Begeman7973f352011-02-11 20:53:29 +00004708 // Use reciprocal estimate and one refinement step.
4709 // float4 recip = vrecpeq_f32(yf);
4710 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004711 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004712 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004713 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004714 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4715 N1, N2);
4716 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4717 // Because short has a smaller range than ushort, we can actually get away
4718 // with only a single newton step. This requires that we use a weird bias
4719 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004720 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004721 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4722 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004723 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004724 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4725 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4726 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4727 // Convert back to integer and return.
4728 // return vmovn_s32(vcvt_s32_f32(result));
4729 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4730 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4731 return N0;
4732}
4733
4734static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4735 EVT VT = Op.getValueType();
4736 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4737 "unexpected type for custom-lowering ISD::SDIV");
4738
4739 DebugLoc dl = Op.getDebugLoc();
4740 SDValue N0 = Op.getOperand(0);
4741 SDValue N1 = Op.getOperand(1);
4742 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004743
Nate Begeman7973f352011-02-11 20:53:29 +00004744 if (VT == MVT::v8i8) {
4745 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4746 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004747
Nate Begeman7973f352011-02-11 20:53:29 +00004748 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4749 DAG.getIntPtrConstant(4));
4750 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004751 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004752 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4753 DAG.getIntPtrConstant(0));
4754 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4755 DAG.getIntPtrConstant(0));
4756
4757 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4758 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4759
4760 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4761 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004762
Nate Begeman7973f352011-02-11 20:53:29 +00004763 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4764 return N0;
4765 }
4766 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4767}
4768
4769static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4770 EVT VT = Op.getValueType();
4771 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4772 "unexpected type for custom-lowering ISD::UDIV");
4773
4774 DebugLoc dl = Op.getDebugLoc();
4775 SDValue N0 = Op.getOperand(0);
4776 SDValue N1 = Op.getOperand(1);
4777 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004778
Nate Begeman7973f352011-02-11 20:53:29 +00004779 if (VT == MVT::v8i8) {
4780 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4781 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004782
Nate Begeman7973f352011-02-11 20:53:29 +00004783 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4784 DAG.getIntPtrConstant(4));
4785 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004786 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004787 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4788 DAG.getIntPtrConstant(0));
4789 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4790 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004791
Nate Begeman7973f352011-02-11 20:53:29 +00004792 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4793 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004794
Nate Begeman7973f352011-02-11 20:53:29 +00004795 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4796 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004797
4798 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004799 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4800 N0);
4801 return N0;
4802 }
Owen Anderson76706012011-04-05 21:48:57 +00004803
Nate Begeman7973f352011-02-11 20:53:29 +00004804 // v4i16 sdiv ... Convert to float.
4805 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4806 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4807 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4808 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4809 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004810 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004811
4812 // Use reciprocal estimate and two refinement steps.
4813 // float4 recip = vrecpeq_f32(yf);
4814 // recip *= vrecpsq_f32(yf, recip);
4815 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004816 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004817 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004818 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004819 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004820 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004821 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004822 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004823 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004824 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004825 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4826 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4827 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4828 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004829 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004830 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4831 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4832 N1 = DAG.getConstant(2, MVT::i32);
4833 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4834 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4835 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4836 // Convert back to integer and return.
4837 // return vmovn_u32(vcvt_s32_f32(result));
4838 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4839 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4840 return N0;
4841}
4842
Evan Cheng342e3162011-08-30 01:34:54 +00004843static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4844 EVT VT = Op.getNode()->getValueType(0);
4845 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4846
4847 unsigned Opc;
4848 bool ExtraOp = false;
4849 switch (Op.getOpcode()) {
4850 default: assert(0 && "Invalid code");
4851 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4852 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4853 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4854 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4855 }
4856
4857 if (!ExtraOp)
4858 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4859 Op.getOperand(1));
4860 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4861 Op.getOperand(1), Op.getOperand(2));
4862}
4863
Eli Friedman2bdffe42011-08-31 00:31:29 +00004864static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004865ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4866 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004867 EVT T = Node->getValueType(0);
4868 DebugLoc dl = Node->getDebugLoc();
4869 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
4870
Eli Friedman4d3f3292011-08-31 17:52:22 +00004871 SmallVector<SDValue, 6> Ops;
4872 Ops.push_back(Node->getOperand(0)); // Chain
4873 Ops.push_back(Node->getOperand(1)); // Ptr
4874 // Low part of Val1
4875 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4876 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4877 // High part of Val1
4878 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4879 Node->getOperand(2), DAG.getIntPtrConstant(1)));
4880 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
4881 // High part of Val1
4882 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4883 Node->getOperand(3), DAG.getIntPtrConstant(0)));
4884 // High part of Val2
4885 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4886 Node->getOperand(3), DAG.getIntPtrConstant(1)));
4887 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00004888 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4889 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00004890 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00004891 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00004892 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00004893 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
4894 Results.push_back(Result.getValue(2));
4895}
4896
Dan Gohmand858e902010-04-17 15:26:15 +00004897SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004898 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004899 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004900 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004901 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004902 case ISD::GlobalAddress:
4903 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4904 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004905 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004906 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004907 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4908 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004909 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004910 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004911 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00004912 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004913 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004914 case ISD::SINT_TO_FP:
4915 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4916 case ISD::FP_TO_SINT:
4917 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004918 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004919 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004920 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004921 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004922 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004923 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004924 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004925 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4926 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004927 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004928 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004929 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004930 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004931 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004932 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004933 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004934 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00004935 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004936 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004937 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004938 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004939 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004940 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004941 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004942 case ISD::SDIV: return LowerSDIV(Op, DAG);
4943 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00004944 case ISD::ADDC:
4945 case ISD::ADDE:
4946 case ISD::SUBC:
4947 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004948 }
Dan Gohman475871a2008-07-27 21:46:04 +00004949 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004950}
4951
Duncan Sands1607f052008-12-01 11:39:25 +00004952/// ReplaceNodeResults - Replace the results of node with an illegal result
4953/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004954void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4955 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004956 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004957 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004958 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004959 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004960 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004961 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004962 case ISD::BITCAST:
4963 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004964 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004965 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004966 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004967 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004968 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00004969 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004970 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004971 return;
4972 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004973 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004974 return;
4975 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004976 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004977 return;
4978 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004979 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004980 return;
4981 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004982 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004983 return;
4984 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004985 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004986 return;
4987 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004988 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004989 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00004990 case ISD::ATOMIC_CMP_SWAP:
4991 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
4992 return;
Duncan Sands1607f052008-12-01 11:39:25 +00004993 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004994 if (Res.getNode())
4995 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004996}
Chris Lattner27a6c732007-11-24 07:07:01 +00004997
Evan Chenga8e29892007-01-19 07:51:42 +00004998//===----------------------------------------------------------------------===//
4999// ARM Scheduler Hooks
5000//===----------------------------------------------------------------------===//
5001
5002MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005003ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5004 MachineBasicBlock *BB,
5005 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005006 unsigned dest = MI->getOperand(0).getReg();
5007 unsigned ptr = MI->getOperand(1).getReg();
5008 unsigned oldval = MI->getOperand(2).getReg();
5009 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005010 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5011 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005012 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005013
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005014 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5015 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005016 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005017 : ARM::GPRRegisterClass);
5018
5019 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005020 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5021 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5022 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005023 }
5024
Jim Grosbach5278eb82009-12-11 01:42:04 +00005025 unsigned ldrOpc, strOpc;
5026 switch (Size) {
5027 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005028 case 1:
5029 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005030 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005031 break;
5032 case 2:
5033 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5034 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5035 break;
5036 case 4:
5037 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5038 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5039 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005040 }
5041
5042 MachineFunction *MF = BB->getParent();
5043 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5044 MachineFunction::iterator It = BB;
5045 ++It; // insert the new blocks after the current block
5046
5047 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5048 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5049 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5050 MF->insert(It, loop1MBB);
5051 MF->insert(It, loop2MBB);
5052 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005053
5054 // Transfer the remainder of BB and its successor edges to exitMBB.
5055 exitMBB->splice(exitMBB->begin(), BB,
5056 llvm::next(MachineBasicBlock::iterator(MI)),
5057 BB->end());
5058 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005059
5060 // thisMBB:
5061 // ...
5062 // fallthrough --> loop1MBB
5063 BB->addSuccessor(loop1MBB);
5064
5065 // loop1MBB:
5066 // ldrex dest, [ptr]
5067 // cmp dest, oldval
5068 // bne exitMBB
5069 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005070 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5071 if (ldrOpc == ARM::t2LDREX)
5072 MIB.addImm(0);
5073 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005074 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005075 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005076 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5077 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005078 BB->addSuccessor(loop2MBB);
5079 BB->addSuccessor(exitMBB);
5080
5081 // loop2MBB:
5082 // strex scratch, newval, [ptr]
5083 // cmp scratch, #0
5084 // bne loop1MBB
5085 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005086 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5087 if (strOpc == ARM::t2STREX)
5088 MIB.addImm(0);
5089 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005090 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005091 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005092 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5093 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005094 BB->addSuccessor(loop1MBB);
5095 BB->addSuccessor(exitMBB);
5096
5097 // exitMBB:
5098 // ...
5099 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005100
Dan Gohman14152b42010-07-06 20:24:04 +00005101 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005102
Jim Grosbach5278eb82009-12-11 01:42:04 +00005103 return BB;
5104}
5105
5106MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005107ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5108 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005109 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5110 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5111
5112 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005113 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005114 MachineFunction::iterator It = BB;
5115 ++It;
5116
5117 unsigned dest = MI->getOperand(0).getReg();
5118 unsigned ptr = MI->getOperand(1).getReg();
5119 unsigned incr = MI->getOperand(2).getReg();
5120 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005121 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005122
5123 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5124 if (isThumb2) {
5125 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5126 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5127 }
5128
Jim Grosbachc3c23542009-12-14 04:22:04 +00005129 unsigned ldrOpc, strOpc;
5130 switch (Size) {
5131 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005132 case 1:
5133 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005134 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005135 break;
5136 case 2:
5137 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5138 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5139 break;
5140 case 4:
5141 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5142 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5143 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005144 }
5145
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005146 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5147 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5148 MF->insert(It, loopMBB);
5149 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005150
5151 // Transfer the remainder of BB and its successor edges to exitMBB.
5152 exitMBB->splice(exitMBB->begin(), BB,
5153 llvm::next(MachineBasicBlock::iterator(MI)),
5154 BB->end());
5155 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005156
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005157 TargetRegisterClass *TRC =
5158 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5159 unsigned scratch = MRI.createVirtualRegister(TRC);
5160 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005161
5162 // thisMBB:
5163 // ...
5164 // fallthrough --> loopMBB
5165 BB->addSuccessor(loopMBB);
5166
5167 // loopMBB:
5168 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005169 // <binop> scratch2, dest, incr
5170 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005171 // cmp scratch, #0
5172 // bne- loopMBB
5173 // fallthrough --> exitMBB
5174 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005175 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5176 if (ldrOpc == ARM::t2LDREX)
5177 MIB.addImm(0);
5178 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005179 if (BinOpcode) {
5180 // operand order needs to go the other way for NAND
5181 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5182 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5183 addReg(incr).addReg(dest)).addReg(0);
5184 else
5185 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5186 addReg(dest).addReg(incr)).addReg(0);
5187 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005188
Jim Grosbachb6aed502011-09-09 18:37:27 +00005189 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5190 if (strOpc == ARM::t2STREX)
5191 MIB.addImm(0);
5192 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005193 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005194 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005195 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5196 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005197
5198 BB->addSuccessor(loopMBB);
5199 BB->addSuccessor(exitMBB);
5200
5201 // exitMBB:
5202 // ...
5203 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005204
Dan Gohman14152b42010-07-06 20:24:04 +00005205 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005206
Jim Grosbachc3c23542009-12-14 04:22:04 +00005207 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005208}
5209
Jim Grosbachf7da8822011-04-26 19:44:18 +00005210MachineBasicBlock *
5211ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5212 MachineBasicBlock *BB,
5213 unsigned Size,
5214 bool signExtend,
5215 ARMCC::CondCodes Cond) const {
5216 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5217
5218 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5219 MachineFunction *MF = BB->getParent();
5220 MachineFunction::iterator It = BB;
5221 ++It;
5222
5223 unsigned dest = MI->getOperand(0).getReg();
5224 unsigned ptr = MI->getOperand(1).getReg();
5225 unsigned incr = MI->getOperand(2).getReg();
5226 unsigned oldval = dest;
5227 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005228 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005229
5230 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5231 if (isThumb2) {
5232 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5233 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5234 }
5235
Jim Grosbachf7da8822011-04-26 19:44:18 +00005236 unsigned ldrOpc, strOpc, extendOpc;
5237 switch (Size) {
5238 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5239 case 1:
5240 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5241 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005242 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005243 break;
5244 case 2:
5245 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5246 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005247 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005248 break;
5249 case 4:
5250 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5251 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5252 extendOpc = 0;
5253 break;
5254 }
5255
5256 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5257 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5258 MF->insert(It, loopMBB);
5259 MF->insert(It, exitMBB);
5260
5261 // Transfer the remainder of BB and its successor edges to exitMBB.
5262 exitMBB->splice(exitMBB->begin(), BB,
5263 llvm::next(MachineBasicBlock::iterator(MI)),
5264 BB->end());
5265 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5266
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005267 TargetRegisterClass *TRC =
5268 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5269 unsigned scratch = MRI.createVirtualRegister(TRC);
5270 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005271
5272 // thisMBB:
5273 // ...
5274 // fallthrough --> loopMBB
5275 BB->addSuccessor(loopMBB);
5276
5277 // loopMBB:
5278 // ldrex dest, ptr
5279 // (sign extend dest, if required)
5280 // cmp dest, incr
5281 // cmov.cond scratch2, dest, incr
5282 // strex scratch, scratch2, ptr
5283 // cmp scratch, #0
5284 // bne- loopMBB
5285 // fallthrough --> exitMBB
5286 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005287 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5288 if (ldrOpc == ARM::t2LDREX)
5289 MIB.addImm(0);
5290 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005291
5292 // Sign extend the value, if necessary.
5293 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005294 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005295 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5296 .addReg(dest)
5297 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005298 }
5299
5300 // Build compare and cmov instructions.
5301 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5302 .addReg(oldval).addReg(incr));
5303 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5304 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5305
Jim Grosbachb6aed502011-09-09 18:37:27 +00005306 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5307 if (strOpc == ARM::t2STREX)
5308 MIB.addImm(0);
5309 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005310 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5311 .addReg(scratch).addImm(0));
5312 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5313 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5314
5315 BB->addSuccessor(loopMBB);
5316 BB->addSuccessor(exitMBB);
5317
5318 // exitMBB:
5319 // ...
5320 BB = exitMBB;
5321
5322 MI->eraseFromParent(); // The instruction is gone now.
5323
5324 return BB;
5325}
5326
Eli Friedman2bdffe42011-08-31 00:31:29 +00005327MachineBasicBlock *
5328ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5329 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005330 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005331 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5332 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5333
5334 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5335 MachineFunction *MF = BB->getParent();
5336 MachineFunction::iterator It = BB;
5337 ++It;
5338
5339 unsigned destlo = MI->getOperand(0).getReg();
5340 unsigned desthi = MI->getOperand(1).getReg();
5341 unsigned ptr = MI->getOperand(2).getReg();
5342 unsigned vallo = MI->getOperand(3).getReg();
5343 unsigned valhi = MI->getOperand(4).getReg();
5344 DebugLoc dl = MI->getDebugLoc();
5345 bool isThumb2 = Subtarget->isThumb2();
5346
5347 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5348 if (isThumb2) {
5349 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5350 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5351 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5352 }
5353
5354 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5355 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5356
5357 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005358 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005359 if (IsCmpxchg) {
5360 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5361 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5362 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005363 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5364 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005365 if (IsCmpxchg) {
5366 MF->insert(It, contBB);
5367 MF->insert(It, cont2BB);
5368 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005369 MF->insert(It, exitMBB);
5370
5371 // Transfer the remainder of BB and its successor edges to exitMBB.
5372 exitMBB->splice(exitMBB->begin(), BB,
5373 llvm::next(MachineBasicBlock::iterator(MI)),
5374 BB->end());
5375 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5376
5377 TargetRegisterClass *TRC =
5378 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5379 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5380
5381 // thisMBB:
5382 // ...
5383 // fallthrough --> loopMBB
5384 BB->addSuccessor(loopMBB);
5385
5386 // loopMBB:
5387 // ldrexd r2, r3, ptr
5388 // <binopa> r0, r2, incr
5389 // <binopb> r1, r3, incr
5390 // strexd storesuccess, r0, r1, ptr
5391 // cmp storesuccess, #0
5392 // bne- loopMBB
5393 // fallthrough --> exitMBB
5394 //
5395 // Note that the registers are explicitly specified because there is not any
5396 // way to force the register allocator to allocate a register pair.
5397 //
5398 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
5399 // need to properly enforce the restriction that the two output registers
5400 // for ldrexd must be different.
5401 BB = loopMBB;
5402 // Load
5403 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5404 .addReg(ARM::R2, RegState::Define)
5405 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5406 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5407 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5408 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005409
5410 if (IsCmpxchg) {
5411 // Add early exit
5412 for (unsigned i = 0; i < 2; i++) {
5413 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5414 ARM::CMPrr))
5415 .addReg(i == 0 ? destlo : desthi)
5416 .addReg(i == 0 ? vallo : valhi));
5417 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5418 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5419 BB->addSuccessor(exitMBB);
5420 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5421 BB = (i == 0 ? contBB : cont2BB);
5422 }
5423
5424 // Copy to physregs for strexd
5425 unsigned setlo = MI->getOperand(5).getReg();
5426 unsigned sethi = MI->getOperand(6).getReg();
5427 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5428 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5429 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005430 // Perform binary operation
5431 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5432 .addReg(destlo).addReg(vallo))
5433 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5434 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5435 .addReg(desthi).addReg(valhi)).addReg(0);
5436 } else {
5437 // Copy to physregs for strexd
5438 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5439 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5440 }
5441
5442 // Store
5443 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5444 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5445 // Cmp+jump
5446 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5447 .addReg(storesuccess).addImm(0));
5448 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5449 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5450
5451 BB->addSuccessor(loopMBB);
5452 BB->addSuccessor(exitMBB);
5453
5454 // exitMBB:
5455 // ...
5456 BB = exitMBB;
5457
5458 MI->eraseFromParent(); // The instruction is gone now.
5459
5460 return BB;
5461}
5462
Evan Cheng218977b2010-07-13 19:27:42 +00005463static
5464MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5465 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5466 E = MBB->succ_end(); I != E; ++I)
5467 if (*I != Succ)
5468 return *I;
5469 llvm_unreachable("Expecting a BB with two successors!");
5470}
5471
Jim Grosbache801dc42009-12-12 01:40:06 +00005472MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005473ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005474 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005475 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005476 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005477 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005478 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005479 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005480 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005481 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005482 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005483 case ARM::STRi_preidx:
5484 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00005485 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00005486 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
5487 // Decode the offset.
5488 unsigned Offset = MI->getOperand(4).getImm();
5489 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
5490 Offset = ARM_AM::getAM2Offset(Offset);
5491 if (isSub)
5492 Offset = -Offset;
5493
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005494 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00005495 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00005496 .addOperand(MI->getOperand(0)) // Rn_wb
5497 .addOperand(MI->getOperand(1)) // Rt
5498 .addOperand(MI->getOperand(2)) // Rn
5499 .addImm(Offset) // offset (skip GPR==zero_reg)
5500 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005501 .addOperand(MI->getOperand(6))
5502 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00005503 MI->eraseFromParent();
5504 return BB;
5505 }
5506 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00005507 case ARM::STRBr_preidx:
5508 case ARM::STRH_preidx: {
5509 unsigned NewOpc;
5510 switch (MI->getOpcode()) {
5511 default: llvm_unreachable("unexpected opcode!");
5512 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
5513 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
5514 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
5515 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005516 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5517 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5518 MIB.addOperand(MI->getOperand(i));
5519 MI->eraseFromParent();
5520 return BB;
5521 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005522 case ARM::ATOMIC_LOAD_ADD_I8:
5523 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5524 case ARM::ATOMIC_LOAD_ADD_I16:
5525 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5526 case ARM::ATOMIC_LOAD_ADD_I32:
5527 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005528
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005529 case ARM::ATOMIC_LOAD_AND_I8:
5530 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5531 case ARM::ATOMIC_LOAD_AND_I16:
5532 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5533 case ARM::ATOMIC_LOAD_AND_I32:
5534 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005535
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005536 case ARM::ATOMIC_LOAD_OR_I8:
5537 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5538 case ARM::ATOMIC_LOAD_OR_I16:
5539 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5540 case ARM::ATOMIC_LOAD_OR_I32:
5541 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005542
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005543 case ARM::ATOMIC_LOAD_XOR_I8:
5544 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5545 case ARM::ATOMIC_LOAD_XOR_I16:
5546 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5547 case ARM::ATOMIC_LOAD_XOR_I32:
5548 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005549
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005550 case ARM::ATOMIC_LOAD_NAND_I8:
5551 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5552 case ARM::ATOMIC_LOAD_NAND_I16:
5553 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5554 case ARM::ATOMIC_LOAD_NAND_I32:
5555 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005556
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005557 case ARM::ATOMIC_LOAD_SUB_I8:
5558 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5559 case ARM::ATOMIC_LOAD_SUB_I16:
5560 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5561 case ARM::ATOMIC_LOAD_SUB_I32:
5562 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005563
Jim Grosbachf7da8822011-04-26 19:44:18 +00005564 case ARM::ATOMIC_LOAD_MIN_I8:
5565 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5566 case ARM::ATOMIC_LOAD_MIN_I16:
5567 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5568 case ARM::ATOMIC_LOAD_MIN_I32:
5569 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5570
5571 case ARM::ATOMIC_LOAD_MAX_I8:
5572 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5573 case ARM::ATOMIC_LOAD_MAX_I16:
5574 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5575 case ARM::ATOMIC_LOAD_MAX_I32:
5576 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5577
5578 case ARM::ATOMIC_LOAD_UMIN_I8:
5579 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5580 case ARM::ATOMIC_LOAD_UMIN_I16:
5581 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5582 case ARM::ATOMIC_LOAD_UMIN_I32:
5583 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5584
5585 case ARM::ATOMIC_LOAD_UMAX_I8:
5586 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5587 case ARM::ATOMIC_LOAD_UMAX_I16:
5588 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5589 case ARM::ATOMIC_LOAD_UMAX_I32:
5590 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5591
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005592 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5593 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5594 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005595
5596 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5597 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5598 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005599
Eli Friedman2bdffe42011-08-31 00:31:29 +00005600
5601 case ARM::ATOMADD6432:
5602 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005603 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
5604 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005605 case ARM::ATOMSUB6432:
5606 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005607 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
5608 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005609 case ARM::ATOMOR6432:
5610 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005611 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005612 case ARM::ATOMXOR6432:
5613 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005614 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005615 case ARM::ATOMAND6432:
5616 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005617 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005618 case ARM::ATOMSWAP6432:
5619 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005620 case ARM::ATOMCMPXCHG6432:
5621 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
5622 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
5623 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005624
Evan Cheng007ea272009-08-12 05:17:19 +00005625 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005626 // To "insert" a SELECT_CC instruction, we actually have to insert the
5627 // diamond control-flow pattern. The incoming instruction knows the
5628 // destination vreg to set, the condition code register to branch on, the
5629 // true/false values to select between, and a branch opcode to use.
5630 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005631 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005632 ++It;
5633
5634 // thisMBB:
5635 // ...
5636 // TrueVal = ...
5637 // cmpTY ccX, r1, r2
5638 // bCC copy1MBB
5639 // fallthrough --> copy0MBB
5640 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005641 MachineFunction *F = BB->getParent();
5642 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5643 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005644 F->insert(It, copy0MBB);
5645 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005646
5647 // Transfer the remainder of BB and its successor edges to sinkMBB.
5648 sinkMBB->splice(sinkMBB->begin(), BB,
5649 llvm::next(MachineBasicBlock::iterator(MI)),
5650 BB->end());
5651 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5652
Dan Gohman258c58c2010-07-06 15:49:48 +00005653 BB->addSuccessor(copy0MBB);
5654 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005655
Dan Gohman14152b42010-07-06 20:24:04 +00005656 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5657 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5658
Evan Chenga8e29892007-01-19 07:51:42 +00005659 // copy0MBB:
5660 // %FalseValue = ...
5661 // # fallthrough to sinkMBB
5662 BB = copy0MBB;
5663
5664 // Update machine-CFG edges
5665 BB->addSuccessor(sinkMBB);
5666
5667 // sinkMBB:
5668 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5669 // ...
5670 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005671 BuildMI(*BB, BB->begin(), dl,
5672 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005673 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5674 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5675
Dan Gohman14152b42010-07-06 20:24:04 +00005676 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005677 return BB;
5678 }
Evan Cheng86198642009-08-07 00:34:42 +00005679
Evan Cheng218977b2010-07-13 19:27:42 +00005680 case ARM::BCCi64:
5681 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005682 // If there is an unconditional branch to the other successor, remove it.
5683 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005684
Evan Cheng218977b2010-07-13 19:27:42 +00005685 // Compare both parts that make up the double comparison separately for
5686 // equality.
5687 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5688
5689 unsigned LHS1 = MI->getOperand(1).getReg();
5690 unsigned LHS2 = MI->getOperand(2).getReg();
5691 if (RHSisZero) {
5692 AddDefaultPred(BuildMI(BB, dl,
5693 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5694 .addReg(LHS1).addImm(0));
5695 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5696 .addReg(LHS2).addImm(0)
5697 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5698 } else {
5699 unsigned RHS1 = MI->getOperand(3).getReg();
5700 unsigned RHS2 = MI->getOperand(4).getReg();
5701 AddDefaultPred(BuildMI(BB, dl,
5702 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5703 .addReg(LHS1).addReg(RHS1));
5704 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5705 .addReg(LHS2).addReg(RHS2)
5706 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5707 }
5708
5709 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5710 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5711 if (MI->getOperand(0).getImm() == ARMCC::NE)
5712 std::swap(destMBB, exitMBB);
5713
5714 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5715 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5716 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5717 .addMBB(exitMBB);
5718
5719 MI->eraseFromParent(); // The pseudo instruction is gone now.
5720 return BB;
5721 }
Evan Chenga8e29892007-01-19 07:51:42 +00005722 }
5723}
5724
Evan Cheng37fefc22011-08-30 19:09:48 +00005725void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
5726 SDNode *Node) const {
5727 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC,
5728 // RSB, RSC. Coming out of isel, they have an implicit CPSR def, but the
5729 // optional operand is not filled in. If the carry bit is used, then change
5730 // the optional operand to CPSR. Otherwise, remove the CPSR implicit def.
5731 const MCInstrDesc &MCID = MI->getDesc();
5732 if (Node->hasAnyUseOfValue(1)) {
Evan Cheng4a517082011-09-06 18:52:20 +00005733 MachineOperand &MO = MI->getOperand(MCID.getNumOperands() - 1);
Evan Cheng37fefc22011-08-30 19:09:48 +00005734 MO.setReg(ARM::CPSR);
5735 MO.setIsDef(true);
5736 } else {
5737 for (unsigned i = MCID.getNumOperands(), e = MI->getNumOperands();
5738 i != e; ++i) {
5739 const MachineOperand &MO = MI->getOperand(i);
5740 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
5741 MI->RemoveOperand(i);
5742 break;
5743 }
5744 }
5745 }
5746}
5747
Evan Chenga8e29892007-01-19 07:51:42 +00005748//===----------------------------------------------------------------------===//
5749// ARM Optimization Hooks
5750//===----------------------------------------------------------------------===//
5751
Chris Lattnerd1980a52009-03-12 06:52:53 +00005752static
5753SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5754 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005755 SelectionDAG &DAG = DCI.DAG;
5756 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005757 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005758 unsigned Opc = N->getOpcode();
5759 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5760 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5761 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5762 ISD::CondCode CC = ISD::SETCC_INVALID;
5763
5764 if (isSlctCC) {
5765 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5766 } else {
5767 SDValue CCOp = Slct.getOperand(0);
5768 if (CCOp.getOpcode() == ISD::SETCC)
5769 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5770 }
5771
5772 bool DoXform = false;
5773 bool InvCC = false;
5774 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5775 "Bad input!");
5776
5777 if (LHS.getOpcode() == ISD::Constant &&
5778 cast<ConstantSDNode>(LHS)->isNullValue()) {
5779 DoXform = true;
5780 } else if (CC != ISD::SETCC_INVALID &&
5781 RHS.getOpcode() == ISD::Constant &&
5782 cast<ConstantSDNode>(RHS)->isNullValue()) {
5783 std::swap(LHS, RHS);
5784 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005785 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005786 Op0.getOperand(0).getValueType();
5787 bool isInt = OpVT.isInteger();
5788 CC = ISD::getSetCCInverse(CC, isInt);
5789
5790 if (!TLI.isCondCodeLegal(CC, OpVT))
5791 return SDValue(); // Inverse operator isn't legal.
5792
5793 DoXform = true;
5794 InvCC = true;
5795 }
5796
5797 if (DoXform) {
5798 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5799 if (isSlctCC)
5800 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5801 Slct.getOperand(0), Slct.getOperand(1), CC);
5802 SDValue CCOp = Slct.getOperand(0);
5803 if (InvCC)
5804 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5805 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5806 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5807 CCOp, OtherOp, Result);
5808 }
5809 return SDValue();
5810}
5811
Eric Christopherfa6f5912011-06-29 21:10:36 +00005812// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00005813// (only after legalization).
5814static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5815 TargetLowering::DAGCombinerInfo &DCI,
5816 const ARMSubtarget *Subtarget) {
5817
5818 // Only perform optimization if after legalize, and if NEON is available. We
5819 // also expected both operands to be BUILD_VECTORs.
5820 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5821 || N0.getOpcode() != ISD::BUILD_VECTOR
5822 || N1.getOpcode() != ISD::BUILD_VECTOR)
5823 return SDValue();
5824
5825 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5826 EVT VT = N->getValueType(0);
5827 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5828 return SDValue();
5829
5830 // Check that the vector operands are of the right form.
5831 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5832 // operands, where N is the size of the formed vector.
5833 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5834 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005835
5836 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005837 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005838 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005839 SDValue Vec = N0->getOperand(0)->getOperand(0);
5840 SDNode *V = Vec.getNode();
5841 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005842
Eric Christopherfa6f5912011-06-29 21:10:36 +00005843 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00005844 // check to see if each of their operands are an EXTRACT_VECTOR with
5845 // the same vector and appropriate index.
5846 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5847 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5848 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00005849
Tanya Lattner189531f2011-06-14 23:48:48 +00005850 SDValue ExtVec0 = N0->getOperand(i);
5851 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005852
Tanya Lattner189531f2011-06-14 23:48:48 +00005853 // First operand is the vector, verify its the same.
5854 if (V != ExtVec0->getOperand(0).getNode() ||
5855 V != ExtVec1->getOperand(0).getNode())
5856 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00005857
Tanya Lattner189531f2011-06-14 23:48:48 +00005858 // Second is the constant, verify its correct.
5859 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5860 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00005861
Tanya Lattner189531f2011-06-14 23:48:48 +00005862 // For the constant, we want to see all the even or all the odd.
5863 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5864 || C1->getZExtValue() != nextIndex+1)
5865 return SDValue();
5866
5867 // Increment index.
5868 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005869 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00005870 return SDValue();
5871 }
5872
5873 // Create VPADDL node.
5874 SelectionDAG &DAG = DCI.DAG;
5875 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00005876
5877 // Build operand list.
5878 SmallVector<SDValue, 8> Ops;
5879 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5880 TLI.getPointerTy()));
5881
5882 // Input is the vector.
5883 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005884
Tanya Lattner189531f2011-06-14 23:48:48 +00005885 // Get widened type and narrowed type.
5886 MVT widenType;
5887 unsigned numElem = VT.getVectorNumElements();
5888 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5889 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5890 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5891 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5892 default:
5893 assert(0 && "Invalid vector element type for padd optimization.");
5894 }
5895
5896 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5897 widenType, &Ops[0], Ops.size());
5898 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5899}
5900
Bob Wilson3d5792a2010-07-29 20:34:14 +00005901/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5902/// operands N0 and N1. This is a helper for PerformADDCombine that is
5903/// called with the default operands, and if that fails, with commuted
5904/// operands.
5905static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005906 TargetLowering::DAGCombinerInfo &DCI,
5907 const ARMSubtarget *Subtarget){
5908
5909 // Attempt to create vpaddl for this add.
5910 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5911 if (Result.getNode())
5912 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005913
Chris Lattnerd1980a52009-03-12 06:52:53 +00005914 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5915 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5916 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5917 if (Result.getNode()) return Result;
5918 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005919 return SDValue();
5920}
5921
Bob Wilson3d5792a2010-07-29 20:34:14 +00005922/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5923///
5924static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005925 TargetLowering::DAGCombinerInfo &DCI,
5926 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005927 SDValue N0 = N->getOperand(0);
5928 SDValue N1 = N->getOperand(1);
5929
5930 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005931 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005932 if (Result.getNode())
5933 return Result;
5934
5935 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005936 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005937}
5938
Chris Lattnerd1980a52009-03-12 06:52:53 +00005939/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005940///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005941static SDValue PerformSUBCombine(SDNode *N,
5942 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005943 SDValue N0 = N->getOperand(0);
5944 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005945
Chris Lattnerd1980a52009-03-12 06:52:53 +00005946 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5947 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5948 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5949 if (Result.getNode()) return Result;
5950 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005951
Chris Lattnerd1980a52009-03-12 06:52:53 +00005952 return SDValue();
5953}
5954
Evan Cheng463d3582011-03-31 19:38:48 +00005955/// PerformVMULCombine
5956/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5957/// special multiplier accumulator forwarding.
5958/// vmul d3, d0, d2
5959/// vmla d3, d1, d2
5960/// is faster than
5961/// vadd d3, d0, d1
5962/// vmul d3, d3, d2
5963static SDValue PerformVMULCombine(SDNode *N,
5964 TargetLowering::DAGCombinerInfo &DCI,
5965 const ARMSubtarget *Subtarget) {
5966 if (!Subtarget->hasVMLxForwarding())
5967 return SDValue();
5968
5969 SelectionDAG &DAG = DCI.DAG;
5970 SDValue N0 = N->getOperand(0);
5971 SDValue N1 = N->getOperand(1);
5972 unsigned Opcode = N0.getOpcode();
5973 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5974 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00005975 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00005976 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5977 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5978 return SDValue();
5979 std::swap(N0, N1);
5980 }
5981
5982 EVT VT = N->getValueType(0);
5983 DebugLoc DL = N->getDebugLoc();
5984 SDValue N00 = N0->getOperand(0);
5985 SDValue N01 = N0->getOperand(1);
5986 return DAG.getNode(Opcode, DL, VT,
5987 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5988 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5989}
5990
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005991static SDValue PerformMULCombine(SDNode *N,
5992 TargetLowering::DAGCombinerInfo &DCI,
5993 const ARMSubtarget *Subtarget) {
5994 SelectionDAG &DAG = DCI.DAG;
5995
5996 if (Subtarget->isThumb1Only())
5997 return SDValue();
5998
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005999 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6000 return SDValue();
6001
6002 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006003 if (VT.is64BitVector() || VT.is128BitVector())
6004 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006005 if (VT != MVT::i32)
6006 return SDValue();
6007
6008 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6009 if (!C)
6010 return SDValue();
6011
6012 uint64_t MulAmt = C->getZExtValue();
6013 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6014 ShiftAmt = ShiftAmt & (32 - 1);
6015 SDValue V = N->getOperand(0);
6016 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006017
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006018 SDValue Res;
6019 MulAmt >>= ShiftAmt;
6020 if (isPowerOf2_32(MulAmt - 1)) {
6021 // (mul x, 2^N + 1) => (add (shl x, N), x)
6022 Res = DAG.getNode(ISD::ADD, DL, VT,
6023 V, DAG.getNode(ISD::SHL, DL, VT,
6024 V, DAG.getConstant(Log2_32(MulAmt-1),
6025 MVT::i32)));
6026 } else if (isPowerOf2_32(MulAmt + 1)) {
6027 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6028 Res = DAG.getNode(ISD::SUB, DL, VT,
6029 DAG.getNode(ISD::SHL, DL, VT,
6030 V, DAG.getConstant(Log2_32(MulAmt+1),
6031 MVT::i32)),
6032 V);
6033 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006034 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006035
6036 if (ShiftAmt != 0)
6037 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6038 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006039
6040 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006041 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006042 return SDValue();
6043}
6044
Owen Anderson080c0922010-11-05 19:27:46 +00006045static SDValue PerformANDCombine(SDNode *N,
6046 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006047
Owen Anderson080c0922010-11-05 19:27:46 +00006048 // Attempt to use immediate-form VBIC
6049 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6050 DebugLoc dl = N->getDebugLoc();
6051 EVT VT = N->getValueType(0);
6052 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006053
Tanya Lattner0433b212011-04-07 15:24:20 +00006054 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6055 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006056
Owen Anderson080c0922010-11-05 19:27:46 +00006057 APInt SplatBits, SplatUndef;
6058 unsigned SplatBitSize;
6059 bool HasAnyUndefs;
6060 if (BVN &&
6061 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6062 if (SplatBitSize <= 64) {
6063 EVT VbicVT;
6064 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6065 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006066 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006067 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006068 if (Val.getNode()) {
6069 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006070 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006071 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006072 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006073 }
6074 }
6075 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006076
Owen Anderson080c0922010-11-05 19:27:46 +00006077 return SDValue();
6078}
6079
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006080/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6081static SDValue PerformORCombine(SDNode *N,
6082 TargetLowering::DAGCombinerInfo &DCI,
6083 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006084 // Attempt to use immediate-form VORR
6085 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6086 DebugLoc dl = N->getDebugLoc();
6087 EVT VT = N->getValueType(0);
6088 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006089
Tanya Lattner0433b212011-04-07 15:24:20 +00006090 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6091 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006092
Owen Anderson60f48702010-11-03 23:15:26 +00006093 APInt SplatBits, SplatUndef;
6094 unsigned SplatBitSize;
6095 bool HasAnyUndefs;
6096 if (BVN && Subtarget->hasNEON() &&
6097 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6098 if (SplatBitSize <= 64) {
6099 EVT VorrVT;
6100 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6101 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006102 DAG, VorrVT, VT.is128BitVector(),
6103 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006104 if (Val.getNode()) {
6105 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006106 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006107 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006108 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006109 }
6110 }
6111 }
6112
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006113 SDValue N0 = N->getOperand(0);
6114 if (N0.getOpcode() != ISD::AND)
6115 return SDValue();
6116 SDValue N1 = N->getOperand(1);
6117
6118 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6119 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6120 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6121 APInt SplatUndef;
6122 unsigned SplatBitSize;
6123 bool HasAnyUndefs;
6124
6125 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6126 APInt SplatBits0;
6127 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6128 HasAnyUndefs) && !HasAnyUndefs) {
6129 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6130 APInt SplatBits1;
6131 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6132 HasAnyUndefs) && !HasAnyUndefs &&
6133 SplatBits0 == ~SplatBits1) {
6134 // Canonicalize the vector type to make instruction selection simpler.
6135 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6136 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6137 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006138 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006139 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6140 }
6141 }
6142 }
6143
Jim Grosbach54238562010-07-17 03:30:54 +00006144 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6145 // reasonable.
6146
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006147 // BFI is only available on V6T2+
6148 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6149 return SDValue();
6150
Jim Grosbach54238562010-07-17 03:30:54 +00006151 DebugLoc DL = N->getDebugLoc();
6152 // 1) or (and A, mask), val => ARMbfi A, val, mask
6153 // iff (val & mask) == val
6154 //
6155 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6156 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006157 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006158 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006159 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006160 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006161
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006162 if (VT != MVT::i32)
6163 return SDValue();
6164
Evan Cheng30fb13f2010-12-13 20:32:54 +00006165 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006166
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006167 // The value and the mask need to be constants so we can verify this is
6168 // actually a bitfield set. If the mask is 0xffff, we can do better
6169 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006170 SDValue MaskOp = N0.getOperand(1);
6171 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6172 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006173 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006174 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006175 if (Mask == 0xffff)
6176 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006177 SDValue Res;
6178 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006179 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6180 if (N1C) {
6181 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006182 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006183 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006184
Evan Chenga9688c42010-12-11 04:11:38 +00006185 if (ARM::isBitFieldInvertedMask(Mask)) {
6186 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006187
Evan Cheng30fb13f2010-12-13 20:32:54 +00006188 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006189 DAG.getConstant(Val, MVT::i32),
6190 DAG.getConstant(Mask, MVT::i32));
6191
6192 // Do not add new nodes to DAG combiner worklist.
6193 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006194 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006195 }
Jim Grosbach54238562010-07-17 03:30:54 +00006196 } else if (N1.getOpcode() == ISD::AND) {
6197 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006198 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6199 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006200 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006201 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006202
Eric Christopher29aeed12011-03-26 01:21:03 +00006203 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6204 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006205 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006206 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006207 // The pack halfword instruction works better for masks that fit it,
6208 // so use that when it's available.
6209 if (Subtarget->hasT2ExtractPack() &&
6210 (Mask == 0xffff || Mask == 0xffff0000))
6211 return SDValue();
6212 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006213 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006214 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006215 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006216 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006217 DAG.getConstant(Mask, MVT::i32));
6218 // Do not add new nodes to DAG combiner worklist.
6219 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006220 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006221 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006222 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006223 // The pack halfword instruction works better for masks that fit it,
6224 // so use that when it's available.
6225 if (Subtarget->hasT2ExtractPack() &&
6226 (Mask2 == 0xffff || Mask2 == 0xffff0000))
6227 return SDValue();
6228 // 2b
6229 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006230 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00006231 DAG.getConstant(lsb, MVT::i32));
6232 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00006233 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00006234 // Do not add new nodes to DAG combiner worklist.
6235 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006236 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006237 }
6238 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006239
Evan Cheng30fb13f2010-12-13 20:32:54 +00006240 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
6241 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
6242 ARM::isBitFieldInvertedMask(~Mask)) {
6243 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
6244 // where lsb(mask) == #shamt and masked bits of B are known zero.
6245 SDValue ShAmt = N00.getOperand(1);
6246 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
6247 unsigned LSB = CountTrailingZeros_32(Mask);
6248 if (ShAmtC != LSB)
6249 return SDValue();
6250
6251 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
6252 DAG.getConstant(~Mask, MVT::i32));
6253
6254 // Do not add new nodes to DAG combiner worklist.
6255 DCI.CombineTo(N, Res, false);
6256 }
6257
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006258 return SDValue();
6259}
6260
Evan Chengbf188ae2011-06-15 01:12:31 +00006261/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
6262/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00006263static SDValue PerformBFICombine(SDNode *N,
6264 TargetLowering::DAGCombinerInfo &DCI) {
6265 SDValue N1 = N->getOperand(1);
6266 if (N1.getOpcode() == ISD::AND) {
6267 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6268 if (!N11C)
6269 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006270 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
6271 unsigned LSB = CountTrailingZeros_32(~InvMask);
6272 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
6273 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00006274 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006275 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00006276 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
6277 N->getOperand(0), N1.getOperand(0),
6278 N->getOperand(2));
6279 }
6280 return SDValue();
6281}
6282
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006283/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
6284/// ARMISD::VMOVRRD.
6285static SDValue PerformVMOVRRDCombine(SDNode *N,
6286 TargetLowering::DAGCombinerInfo &DCI) {
6287 // vmovrrd(vmovdrr x, y) -> x,y
6288 SDValue InDouble = N->getOperand(0);
6289 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6290 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006291
6292 // vmovrrd(load f64) -> (load i32), (load i32)
6293 SDNode *InNode = InDouble.getNode();
6294 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6295 InNode->getValueType(0) == MVT::f64 &&
6296 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6297 !cast<LoadSDNode>(InNode)->isVolatile()) {
6298 // TODO: Should this be done for non-FrameIndex operands?
6299 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6300
6301 SelectionDAG &DAG = DCI.DAG;
6302 DebugLoc DL = LD->getDebugLoc();
6303 SDValue BasePtr = LD->getBasePtr();
6304 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6305 LD->getPointerInfo(), LD->isVolatile(),
6306 LD->isNonTemporal(), LD->getAlignment());
6307
6308 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6309 DAG.getConstant(4, MVT::i32));
6310 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6311 LD->getPointerInfo(), LD->isVolatile(),
6312 LD->isNonTemporal(),
6313 std::min(4U, LD->getAlignment() / 2));
6314
6315 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6316 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6317 DCI.RemoveFromWorklist(LD);
6318 DAG.DeleteNode(LD);
6319 return Result;
6320 }
6321
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006322 return SDValue();
6323}
6324
6325/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6326/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6327static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6328 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6329 SDValue Op0 = N->getOperand(0);
6330 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006331 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006332 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006333 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006334 Op1 = Op1.getOperand(0);
6335 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6336 Op0.getNode() == Op1.getNode() &&
6337 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006338 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006339 N->getValueType(0), Op0.getOperand(0));
6340 return SDValue();
6341}
6342
Bob Wilson31600902010-12-21 06:43:19 +00006343/// PerformSTORECombine - Target-specific dag combine xforms for
6344/// ISD::STORE.
6345static SDValue PerformSTORECombine(SDNode *N,
6346 TargetLowering::DAGCombinerInfo &DCI) {
6347 // Bitcast an i64 store extracted from a vector to f64.
6348 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6349 StoreSDNode *St = cast<StoreSDNode>(N);
6350 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006351 if (!ISD::isNormalStore(St) || St->isVolatile())
6352 return SDValue();
6353
6354 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6355 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6356 SelectionDAG &DAG = DCI.DAG;
6357 DebugLoc DL = St->getDebugLoc();
6358 SDValue BasePtr = St->getBasePtr();
6359 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6360 StVal.getNode()->getOperand(0), BasePtr,
6361 St->getPointerInfo(), St->isVolatile(),
6362 St->isNonTemporal(), St->getAlignment());
6363
6364 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6365 DAG.getConstant(4, MVT::i32));
6366 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6367 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6368 St->isNonTemporal(),
6369 std::min(4U, St->getAlignment() / 2));
6370 }
6371
6372 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006373 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6374 return SDValue();
6375
6376 SelectionDAG &DAG = DCI.DAG;
6377 DebugLoc dl = StVal.getDebugLoc();
6378 SDValue IntVec = StVal.getOperand(0);
6379 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6380 IntVec.getValueType().getVectorNumElements());
6381 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6382 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6383 Vec, StVal.getOperand(1));
6384 dl = N->getDebugLoc();
6385 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6386 // Make the DAGCombiner fold the bitcasts.
6387 DCI.AddToWorklist(Vec.getNode());
6388 DCI.AddToWorklist(ExtElt.getNode());
6389 DCI.AddToWorklist(V.getNode());
6390 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6391 St->getPointerInfo(), St->isVolatile(),
6392 St->isNonTemporal(), St->getAlignment(),
6393 St->getTBAAInfo());
6394}
6395
6396/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6397/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6398/// i64 vector to have f64 elements, since the value can then be loaded
6399/// directly into a VFP register.
6400static bool hasNormalLoadOperand(SDNode *N) {
6401 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6402 for (unsigned i = 0; i < NumElts; ++i) {
6403 SDNode *Elt = N->getOperand(i).getNode();
6404 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6405 return true;
6406 }
6407 return false;
6408}
6409
Bob Wilson75f02882010-09-17 22:59:05 +00006410/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6411/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006412static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6413 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006414 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6415 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6416 // into a pair of GPRs, which is fine when the value is used as a scalar,
6417 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006418 SelectionDAG &DAG = DCI.DAG;
6419 if (N->getNumOperands() == 2) {
6420 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6421 if (RV.getNode())
6422 return RV;
6423 }
Bob Wilson75f02882010-09-17 22:59:05 +00006424
Bob Wilson31600902010-12-21 06:43:19 +00006425 // Load i64 elements as f64 values so that type legalization does not split
6426 // them up into i32 values.
6427 EVT VT = N->getValueType(0);
6428 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6429 return SDValue();
6430 DebugLoc dl = N->getDebugLoc();
6431 SmallVector<SDValue, 8> Ops;
6432 unsigned NumElts = VT.getVectorNumElements();
6433 for (unsigned i = 0; i < NumElts; ++i) {
6434 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6435 Ops.push_back(V);
6436 // Make the DAGCombiner fold the bitcast.
6437 DCI.AddToWorklist(V.getNode());
6438 }
6439 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6440 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6441 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6442}
6443
6444/// PerformInsertEltCombine - Target-specific dag combine xforms for
6445/// ISD::INSERT_VECTOR_ELT.
6446static SDValue PerformInsertEltCombine(SDNode *N,
6447 TargetLowering::DAGCombinerInfo &DCI) {
6448 // Bitcast an i64 load inserted into a vector to f64.
6449 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6450 EVT VT = N->getValueType(0);
6451 SDNode *Elt = N->getOperand(1).getNode();
6452 if (VT.getVectorElementType() != MVT::i64 ||
6453 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6454 return SDValue();
6455
6456 SelectionDAG &DAG = DCI.DAG;
6457 DebugLoc dl = N->getDebugLoc();
6458 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6459 VT.getVectorNumElements());
6460 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6461 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6462 // Make the DAGCombiner fold the bitcasts.
6463 DCI.AddToWorklist(Vec.getNode());
6464 DCI.AddToWorklist(V.getNode());
6465 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6466 Vec, V, N->getOperand(2));
6467 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006468}
6469
Bob Wilsonf20700c2010-10-27 20:38:28 +00006470/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6471/// ISD::VECTOR_SHUFFLE.
6472static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6473 // The LLVM shufflevector instruction does not require the shuffle mask
6474 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6475 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6476 // operands do not match the mask length, they are extended by concatenating
6477 // them with undef vectors. That is probably the right thing for other
6478 // targets, but for NEON it is better to concatenate two double-register
6479 // size vector operands into a single quad-register size vector. Do that
6480 // transformation here:
6481 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6482 // shuffle(concat(v1, v2), undef)
6483 SDValue Op0 = N->getOperand(0);
6484 SDValue Op1 = N->getOperand(1);
6485 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6486 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6487 Op0.getNumOperands() != 2 ||
6488 Op1.getNumOperands() != 2)
6489 return SDValue();
6490 SDValue Concat0Op1 = Op0.getOperand(1);
6491 SDValue Concat1Op1 = Op1.getOperand(1);
6492 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6493 Concat1Op1.getOpcode() != ISD::UNDEF)
6494 return SDValue();
6495 // Skip the transformation if any of the types are illegal.
6496 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6497 EVT VT = N->getValueType(0);
6498 if (!TLI.isTypeLegal(VT) ||
6499 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6500 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6501 return SDValue();
6502
6503 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6504 Op0.getOperand(0), Op1.getOperand(0));
6505 // Translate the shuffle mask.
6506 SmallVector<int, 16> NewMask;
6507 unsigned NumElts = VT.getVectorNumElements();
6508 unsigned HalfElts = NumElts/2;
6509 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6510 for (unsigned n = 0; n < NumElts; ++n) {
6511 int MaskElt = SVN->getMaskElt(n);
6512 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006513 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006514 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006515 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006516 NewElt = HalfElts + MaskElt - NumElts;
6517 NewMask.push_back(NewElt);
6518 }
6519 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6520 DAG.getUNDEF(VT), NewMask.data());
6521}
6522
Bob Wilson1c3ef902011-02-07 17:43:21 +00006523/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6524/// NEON load/store intrinsics to merge base address updates.
6525static SDValue CombineBaseUpdate(SDNode *N,
6526 TargetLowering::DAGCombinerInfo &DCI) {
6527 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6528 return SDValue();
6529
6530 SelectionDAG &DAG = DCI.DAG;
6531 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6532 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6533 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6534 SDValue Addr = N->getOperand(AddrOpIdx);
6535
6536 // Search for a use of the address operand that is an increment.
6537 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6538 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6539 SDNode *User = *UI;
6540 if (User->getOpcode() != ISD::ADD ||
6541 UI.getUse().getResNo() != Addr.getResNo())
6542 continue;
6543
6544 // Check that the add is independent of the load/store. Otherwise, folding
6545 // it would create a cycle.
6546 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6547 continue;
6548
6549 // Find the new opcode for the updating load/store.
6550 bool isLoad = true;
6551 bool isLaneOp = false;
6552 unsigned NewOpc = 0;
6553 unsigned NumVecs = 0;
6554 if (isIntrinsic) {
6555 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6556 switch (IntNo) {
6557 default: assert(0 && "unexpected intrinsic for Neon base update");
6558 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6559 NumVecs = 1; break;
6560 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6561 NumVecs = 2; break;
6562 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6563 NumVecs = 3; break;
6564 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6565 NumVecs = 4; break;
6566 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6567 NumVecs = 2; isLaneOp = true; break;
6568 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6569 NumVecs = 3; isLaneOp = true; break;
6570 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6571 NumVecs = 4; isLaneOp = true; break;
6572 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6573 NumVecs = 1; isLoad = false; break;
6574 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6575 NumVecs = 2; isLoad = false; break;
6576 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6577 NumVecs = 3; isLoad = false; break;
6578 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6579 NumVecs = 4; isLoad = false; break;
6580 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6581 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6582 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6583 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6584 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6585 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6586 }
6587 } else {
6588 isLaneOp = true;
6589 switch (N->getOpcode()) {
6590 default: assert(0 && "unexpected opcode for Neon base update");
6591 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6592 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6593 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6594 }
6595 }
6596
6597 // Find the size of memory referenced by the load/store.
6598 EVT VecTy;
6599 if (isLoad)
6600 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006601 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006602 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6603 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6604 if (isLaneOp)
6605 NumBytes /= VecTy.getVectorNumElements();
6606
6607 // If the increment is a constant, it must match the memory ref size.
6608 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6609 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6610 uint64_t IncVal = CInc->getZExtValue();
6611 if (IncVal != NumBytes)
6612 continue;
6613 } else if (NumBytes >= 3 * 16) {
6614 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6615 // separate instructions that make it harder to use a non-constant update.
6616 continue;
6617 }
6618
6619 // Create the new updating load/store node.
6620 EVT Tys[6];
6621 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6622 unsigned n;
6623 for (n = 0; n < NumResultVecs; ++n)
6624 Tys[n] = VecTy;
6625 Tys[n++] = MVT::i32;
6626 Tys[n] = MVT::Other;
6627 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6628 SmallVector<SDValue, 8> Ops;
6629 Ops.push_back(N->getOperand(0)); // incoming chain
6630 Ops.push_back(N->getOperand(AddrOpIdx));
6631 Ops.push_back(Inc);
6632 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6633 Ops.push_back(N->getOperand(i));
6634 }
6635 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6636 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6637 Ops.data(), Ops.size(),
6638 MemInt->getMemoryVT(),
6639 MemInt->getMemOperand());
6640
6641 // Update the uses.
6642 std::vector<SDValue> NewResults;
6643 for (unsigned i = 0; i < NumResultVecs; ++i) {
6644 NewResults.push_back(SDValue(UpdN.getNode(), i));
6645 }
6646 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6647 DCI.CombineTo(N, NewResults);
6648 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6649
6650 break;
Owen Anderson76706012011-04-05 21:48:57 +00006651 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006652 return SDValue();
6653}
6654
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006655/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6656/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6657/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6658/// return true.
6659static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6660 SelectionDAG &DAG = DCI.DAG;
6661 EVT VT = N->getValueType(0);
6662 // vldN-dup instructions only support 64-bit vectors for N > 1.
6663 if (!VT.is64BitVector())
6664 return false;
6665
6666 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6667 SDNode *VLD = N->getOperand(0).getNode();
6668 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6669 return false;
6670 unsigned NumVecs = 0;
6671 unsigned NewOpc = 0;
6672 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6673 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6674 NumVecs = 2;
6675 NewOpc = ARMISD::VLD2DUP;
6676 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6677 NumVecs = 3;
6678 NewOpc = ARMISD::VLD3DUP;
6679 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6680 NumVecs = 4;
6681 NewOpc = ARMISD::VLD4DUP;
6682 } else {
6683 return false;
6684 }
6685
6686 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6687 // numbers match the load.
6688 unsigned VLDLaneNo =
6689 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6690 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6691 UI != UE; ++UI) {
6692 // Ignore uses of the chain result.
6693 if (UI.getUse().getResNo() == NumVecs)
6694 continue;
6695 SDNode *User = *UI;
6696 if (User->getOpcode() != ARMISD::VDUPLANE ||
6697 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6698 return false;
6699 }
6700
6701 // Create the vldN-dup node.
6702 EVT Tys[5];
6703 unsigned n;
6704 for (n = 0; n < NumVecs; ++n)
6705 Tys[n] = VT;
6706 Tys[n] = MVT::Other;
6707 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6708 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6709 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6710 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6711 Ops, 2, VLDMemInt->getMemoryVT(),
6712 VLDMemInt->getMemOperand());
6713
6714 // Update the uses.
6715 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6716 UI != UE; ++UI) {
6717 unsigned ResNo = UI.getUse().getResNo();
6718 // Ignore uses of the chain result.
6719 if (ResNo == NumVecs)
6720 continue;
6721 SDNode *User = *UI;
6722 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6723 }
6724
6725 // Now the vldN-lane intrinsic is dead except for its chain result.
6726 // Update uses of the chain.
6727 std::vector<SDValue> VLDDupResults;
6728 for (unsigned n = 0; n < NumVecs; ++n)
6729 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6730 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6731 DCI.CombineTo(VLD, VLDDupResults);
6732
6733 return true;
6734}
6735
Bob Wilson9e82bf12010-07-14 01:22:12 +00006736/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6737/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006738static SDValue PerformVDUPLANECombine(SDNode *N,
6739 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006740 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006741
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006742 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6743 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6744 if (CombineVLDDUP(N, DCI))
6745 return SDValue(N, 0);
6746
6747 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6748 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006749 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006750 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006751 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006752 return SDValue();
6753
6754 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6755 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6756 // The canonical VMOV for a zero vector uses a 32-bit element size.
6757 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6758 unsigned EltBits;
6759 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6760 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006761 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006762 if (EltSize > VT.getVectorElementType().getSizeInBits())
6763 return SDValue();
6764
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006765 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006766}
6767
Eric Christopherfa6f5912011-06-29 21:10:36 +00006768// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00006769// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
6770static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
6771{
Chad Rosier118c9a02011-06-28 17:26:57 +00006772 integerPart cN;
6773 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00006774 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
6775 I != E; I++) {
6776 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
6777 if (!C)
6778 return false;
6779
Eric Christopherfa6f5912011-06-29 21:10:36 +00006780 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00006781 APFloat APF = C->getValueAPF();
6782 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
6783 != APFloat::opOK || !isExact)
6784 return false;
6785
6786 c0 = (I == 0) ? cN : c0;
6787 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
6788 return false;
6789 }
6790 C = c0;
6791 return true;
6792}
6793
6794/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
6795/// can replace combinations of VMUL and VCVT (floating-point to integer)
6796/// when the VMUL has a constant operand that is a power of 2.
6797///
6798/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6799/// vmul.f32 d16, d17, d16
6800/// vcvt.s32.f32 d16, d16
6801/// becomes:
6802/// vcvt.s32.f32 d16, d16, #3
6803static SDValue PerformVCVTCombine(SDNode *N,
6804 TargetLowering::DAGCombinerInfo &DCI,
6805 const ARMSubtarget *Subtarget) {
6806 SelectionDAG &DAG = DCI.DAG;
6807 SDValue Op = N->getOperand(0);
6808
6809 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
6810 Op.getOpcode() != ISD::FMUL)
6811 return SDValue();
6812
6813 uint64_t C;
6814 SDValue N0 = Op->getOperand(0);
6815 SDValue ConstVec = Op->getOperand(1);
6816 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
6817
Eric Christopherfa6f5912011-06-29 21:10:36 +00006818 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00006819 !isConstVecPow2(ConstVec, isSigned, C))
6820 return SDValue();
6821
6822 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
6823 Intrinsic::arm_neon_vcvtfp2fxu;
6824 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6825 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006826 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00006827 DAG.getConstant(Log2_64(C), MVT::i32));
6828}
6829
6830/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
6831/// can replace combinations of VCVT (integer to floating-point) and VDIV
6832/// when the VDIV has a constant operand that is a power of 2.
6833///
6834/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6835/// vcvt.f32.s32 d16, d16
6836/// vdiv.f32 d16, d17, d16
6837/// becomes:
6838/// vcvt.f32.s32 d16, d16, #3
6839static SDValue PerformVDIVCombine(SDNode *N,
6840 TargetLowering::DAGCombinerInfo &DCI,
6841 const ARMSubtarget *Subtarget) {
6842 SelectionDAG &DAG = DCI.DAG;
6843 SDValue Op = N->getOperand(0);
6844 unsigned OpOpcode = Op.getNode()->getOpcode();
6845
6846 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
6847 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
6848 return SDValue();
6849
6850 uint64_t C;
6851 SDValue ConstVec = N->getOperand(1);
6852 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
6853
6854 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
6855 !isConstVecPow2(ConstVec, isSigned, C))
6856 return SDValue();
6857
Eric Christopherfa6f5912011-06-29 21:10:36 +00006858 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00006859 Intrinsic::arm_neon_vcvtfxu2fp;
6860 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6861 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006862 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00006863 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
6864}
6865
6866/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00006867/// operand of a vector shift operation, where all the elements of the
6868/// build_vector must have the same constant integer value.
6869static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6870 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006871 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006872 Op = Op.getOperand(0);
6873 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6874 APInt SplatBits, SplatUndef;
6875 unsigned SplatBitSize;
6876 bool HasAnyUndefs;
6877 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6878 HasAnyUndefs, ElementBits) ||
6879 SplatBitSize > ElementBits)
6880 return false;
6881 Cnt = SplatBits.getSExtValue();
6882 return true;
6883}
6884
6885/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6886/// operand of a vector shift left operation. That value must be in the range:
6887/// 0 <= Value < ElementBits for a left shift; or
6888/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006889static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006890 assert(VT.isVector() && "vector shift count is not a vector type");
6891 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6892 if (! getVShiftImm(Op, ElementBits, Cnt))
6893 return false;
6894 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6895}
6896
6897/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6898/// operand of a vector shift right operation. For a shift opcode, the value
6899/// is positive, but for an intrinsic the value count must be negative. The
6900/// absolute value must be in the range:
6901/// 1 <= |Value| <= ElementBits for a right shift; or
6902/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006903static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006904 int64_t &Cnt) {
6905 assert(VT.isVector() && "vector shift count is not a vector type");
6906 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6907 if (! getVShiftImm(Op, ElementBits, Cnt))
6908 return false;
6909 if (isIntrinsic)
6910 Cnt = -Cnt;
6911 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6912}
6913
6914/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6915static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6916 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6917 switch (IntNo) {
6918 default:
6919 // Don't do anything for most intrinsics.
6920 break;
6921
6922 // Vector shifts: check for immediate versions and lower them.
6923 // Note: This is done during DAG combining instead of DAG legalizing because
6924 // the build_vectors for 64-bit vector element shift counts are generally
6925 // not legal, and it is hard to see their values after they get legalized to
6926 // loads from a constant pool.
6927 case Intrinsic::arm_neon_vshifts:
6928 case Intrinsic::arm_neon_vshiftu:
6929 case Intrinsic::arm_neon_vshiftls:
6930 case Intrinsic::arm_neon_vshiftlu:
6931 case Intrinsic::arm_neon_vshiftn:
6932 case Intrinsic::arm_neon_vrshifts:
6933 case Intrinsic::arm_neon_vrshiftu:
6934 case Intrinsic::arm_neon_vrshiftn:
6935 case Intrinsic::arm_neon_vqshifts:
6936 case Intrinsic::arm_neon_vqshiftu:
6937 case Intrinsic::arm_neon_vqshiftsu:
6938 case Intrinsic::arm_neon_vqshiftns:
6939 case Intrinsic::arm_neon_vqshiftnu:
6940 case Intrinsic::arm_neon_vqshiftnsu:
6941 case Intrinsic::arm_neon_vqrshiftns:
6942 case Intrinsic::arm_neon_vqrshiftnu:
6943 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006944 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006945 int64_t Cnt;
6946 unsigned VShiftOpc = 0;
6947
6948 switch (IntNo) {
6949 case Intrinsic::arm_neon_vshifts:
6950 case Intrinsic::arm_neon_vshiftu:
6951 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6952 VShiftOpc = ARMISD::VSHL;
6953 break;
6954 }
6955 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6956 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6957 ARMISD::VSHRs : ARMISD::VSHRu);
6958 break;
6959 }
6960 return SDValue();
6961
6962 case Intrinsic::arm_neon_vshiftls:
6963 case Intrinsic::arm_neon_vshiftlu:
6964 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6965 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006966 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006967
6968 case Intrinsic::arm_neon_vrshifts:
6969 case Intrinsic::arm_neon_vrshiftu:
6970 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6971 break;
6972 return SDValue();
6973
6974 case Intrinsic::arm_neon_vqshifts:
6975 case Intrinsic::arm_neon_vqshiftu:
6976 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6977 break;
6978 return SDValue();
6979
6980 case Intrinsic::arm_neon_vqshiftsu:
6981 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6982 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006983 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006984
6985 case Intrinsic::arm_neon_vshiftn:
6986 case Intrinsic::arm_neon_vrshiftn:
6987 case Intrinsic::arm_neon_vqshiftns:
6988 case Intrinsic::arm_neon_vqshiftnu:
6989 case Intrinsic::arm_neon_vqshiftnsu:
6990 case Intrinsic::arm_neon_vqrshiftns:
6991 case Intrinsic::arm_neon_vqrshiftnu:
6992 case Intrinsic::arm_neon_vqrshiftnsu:
6993 // Narrowing shifts require an immediate right shift.
6994 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6995 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006996 llvm_unreachable("invalid shift count for narrowing vector shift "
6997 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006998
6999 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007000 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007001 }
7002
7003 switch (IntNo) {
7004 case Intrinsic::arm_neon_vshifts:
7005 case Intrinsic::arm_neon_vshiftu:
7006 // Opcode already set above.
7007 break;
7008 case Intrinsic::arm_neon_vshiftls:
7009 case Intrinsic::arm_neon_vshiftlu:
7010 if (Cnt == VT.getVectorElementType().getSizeInBits())
7011 VShiftOpc = ARMISD::VSHLLi;
7012 else
7013 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7014 ARMISD::VSHLLs : ARMISD::VSHLLu);
7015 break;
7016 case Intrinsic::arm_neon_vshiftn:
7017 VShiftOpc = ARMISD::VSHRN; break;
7018 case Intrinsic::arm_neon_vrshifts:
7019 VShiftOpc = ARMISD::VRSHRs; break;
7020 case Intrinsic::arm_neon_vrshiftu:
7021 VShiftOpc = ARMISD::VRSHRu; break;
7022 case Intrinsic::arm_neon_vrshiftn:
7023 VShiftOpc = ARMISD::VRSHRN; break;
7024 case Intrinsic::arm_neon_vqshifts:
7025 VShiftOpc = ARMISD::VQSHLs; break;
7026 case Intrinsic::arm_neon_vqshiftu:
7027 VShiftOpc = ARMISD::VQSHLu; break;
7028 case Intrinsic::arm_neon_vqshiftsu:
7029 VShiftOpc = ARMISD::VQSHLsu; break;
7030 case Intrinsic::arm_neon_vqshiftns:
7031 VShiftOpc = ARMISD::VQSHRNs; break;
7032 case Intrinsic::arm_neon_vqshiftnu:
7033 VShiftOpc = ARMISD::VQSHRNu; break;
7034 case Intrinsic::arm_neon_vqshiftnsu:
7035 VShiftOpc = ARMISD::VQSHRNsu; break;
7036 case Intrinsic::arm_neon_vqrshiftns:
7037 VShiftOpc = ARMISD::VQRSHRNs; break;
7038 case Intrinsic::arm_neon_vqrshiftnu:
7039 VShiftOpc = ARMISD::VQRSHRNu; break;
7040 case Intrinsic::arm_neon_vqrshiftnsu:
7041 VShiftOpc = ARMISD::VQRSHRNsu; break;
7042 }
7043
7044 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007045 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007046 }
7047
7048 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007049 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007050 int64_t Cnt;
7051 unsigned VShiftOpc = 0;
7052
7053 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7054 VShiftOpc = ARMISD::VSLI;
7055 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7056 VShiftOpc = ARMISD::VSRI;
7057 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007058 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007059 }
7060
7061 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7062 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007063 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007064 }
7065
7066 case Intrinsic::arm_neon_vqrshifts:
7067 case Intrinsic::arm_neon_vqrshiftu:
7068 // No immediate versions of these to check for.
7069 break;
7070 }
7071
7072 return SDValue();
7073}
7074
7075/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7076/// lowers them. As with the vector shift intrinsics, this is done during DAG
7077/// combining instead of DAG legalizing because the build_vectors for 64-bit
7078/// vector element shift counts are generally not legal, and it is hard to see
7079/// their values after they get legalized to loads from a constant pool.
7080static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7081 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007082 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007083
7084 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007085 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7086 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007087 return SDValue();
7088
7089 assert(ST->hasNEON() && "unexpected vector shift");
7090 int64_t Cnt;
7091
7092 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007093 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007094
7095 case ISD::SHL:
7096 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7097 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007098 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007099 break;
7100
7101 case ISD::SRA:
7102 case ISD::SRL:
7103 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7104 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7105 ARMISD::VSHRs : ARMISD::VSHRu);
7106 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007107 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007108 }
7109 }
7110 return SDValue();
7111}
7112
7113/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7114/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7115static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7116 const ARMSubtarget *ST) {
7117 SDValue N0 = N->getOperand(0);
7118
7119 // Check for sign- and zero-extensions of vector extract operations of 8-
7120 // and 16-bit vector elements. NEON supports these directly. They are
7121 // handled during DAG combining because type legalization will promote them
7122 // to 32-bit types and it is messy to recognize the operations after that.
7123 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7124 SDValue Vec = N0.getOperand(0);
7125 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007126 EVT VT = N->getValueType(0);
7127 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007128 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7129
Owen Anderson825b72b2009-08-11 20:47:22 +00007130 if (VT == MVT::i32 &&
7131 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007132 TLI.isTypeLegal(Vec.getValueType()) &&
7133 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007134
7135 unsigned Opc = 0;
7136 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007137 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007138 case ISD::SIGN_EXTEND:
7139 Opc = ARMISD::VGETLANEs;
7140 break;
7141 case ISD::ZERO_EXTEND:
7142 case ISD::ANY_EXTEND:
7143 Opc = ARMISD::VGETLANEu;
7144 break;
7145 }
7146 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7147 }
7148 }
7149
7150 return SDValue();
7151}
7152
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007153/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7154/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7155static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7156 const ARMSubtarget *ST) {
7157 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007158 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007159 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7160 // a NaN; only do the transformation when it matches that behavior.
7161
7162 // For now only do this when using NEON for FP operations; if using VFP, it
7163 // is not obvious that the benefit outweighs the cost of switching to the
7164 // NEON pipeline.
7165 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7166 N->getValueType(0) != MVT::f32)
7167 return SDValue();
7168
7169 SDValue CondLHS = N->getOperand(0);
7170 SDValue CondRHS = N->getOperand(1);
7171 SDValue LHS = N->getOperand(2);
7172 SDValue RHS = N->getOperand(3);
7173 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7174
7175 unsigned Opcode = 0;
7176 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007177 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007178 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007179 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007180 IsReversed = true ; // x CC y ? y : x
7181 } else {
7182 return SDValue();
7183 }
7184
Bob Wilsone742bb52010-02-24 22:15:53 +00007185 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007186 switch (CC) {
7187 default: break;
7188 case ISD::SETOLT:
7189 case ISD::SETOLE:
7190 case ISD::SETLT:
7191 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007192 case ISD::SETULT:
7193 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007194 // If LHS is NaN, an ordered comparison will be false and the result will
7195 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7196 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7197 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7198 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7199 break;
7200 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7201 // will return -0, so vmin can only be used for unsafe math or if one of
7202 // the operands is known to be nonzero.
7203 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
7204 !UnsafeFPMath &&
7205 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7206 break;
7207 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007208 break;
7209
7210 case ISD::SETOGT:
7211 case ISD::SETOGE:
7212 case ISD::SETGT:
7213 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007214 case ISD::SETUGT:
7215 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007216 // If LHS is NaN, an ordered comparison will be false and the result will
7217 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7218 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7219 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
7220 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7221 break;
7222 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
7223 // will return +0, so vmax can only be used for unsafe math or if one of
7224 // the operands is known to be nonzero.
7225 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
7226 !UnsafeFPMath &&
7227 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7228 break;
7229 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007230 break;
7231 }
7232
7233 if (!Opcode)
7234 return SDValue();
7235 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
7236}
7237
Evan Chenge721f5c2011-07-13 00:42:17 +00007238/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
7239SDValue
7240ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
7241 SDValue Cmp = N->getOperand(4);
7242 if (Cmp.getOpcode() != ARMISD::CMPZ)
7243 // Only looking at EQ and NE cases.
7244 return SDValue();
7245
7246 EVT VT = N->getValueType(0);
7247 DebugLoc dl = N->getDebugLoc();
7248 SDValue LHS = Cmp.getOperand(0);
7249 SDValue RHS = Cmp.getOperand(1);
7250 SDValue FalseVal = N->getOperand(0);
7251 SDValue TrueVal = N->getOperand(1);
7252 SDValue ARMcc = N->getOperand(2);
7253 ARMCC::CondCodes CC = (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
7254
7255 // Simplify
7256 // mov r1, r0
7257 // cmp r1, x
7258 // mov r0, y
7259 // moveq r0, x
7260 // to
7261 // cmp r0, x
7262 // movne r0, y
7263 //
7264 // mov r1, r0
7265 // cmp r1, x
7266 // mov r0, x
7267 // movne r0, y
7268 // to
7269 // cmp r0, x
7270 // movne r0, y
7271 /// FIXME: Turn this into a target neutral optimization?
7272 SDValue Res;
7273 if (CC == ARMCC::NE && FalseVal == RHS) {
7274 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
7275 N->getOperand(3), Cmp);
7276 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
7277 SDValue ARMcc;
7278 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
7279 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
7280 N->getOperand(3), NewCmp);
7281 }
7282
7283 if (Res.getNode()) {
7284 APInt KnownZero, KnownOne;
7285 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
7286 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
7287 // Capture demanded bits information that would be otherwise lost.
7288 if (KnownZero == 0xfffffffe)
7289 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7290 DAG.getValueType(MVT::i1));
7291 else if (KnownZero == 0xffffff00)
7292 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7293 DAG.getValueType(MVT::i8));
7294 else if (KnownZero == 0xffff0000)
7295 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7296 DAG.getValueType(MVT::i16));
7297 }
7298
7299 return Res;
7300}
7301
Dan Gohman475871a2008-07-27 21:46:04 +00007302SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007303 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007304 switch (N->getOpcode()) {
7305 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00007306 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007307 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007308 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007309 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00007310 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00007311 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00007312 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007313 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00007314 case ISD::STORE: return PerformSTORECombine(N, DCI);
7315 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
7316 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00007317 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007318 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00007319 case ISD::FP_TO_SINT:
7320 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
7321 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007322 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00007323 case ISD::SHL:
7324 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007325 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00007326 case ISD::SIGN_EXTEND:
7327 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007328 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
7329 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00007330 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00007331 case ARMISD::VLD2DUP:
7332 case ARMISD::VLD3DUP:
7333 case ARMISD::VLD4DUP:
7334 return CombineBaseUpdate(N, DCI);
7335 case ISD::INTRINSIC_VOID:
7336 case ISD::INTRINSIC_W_CHAIN:
7337 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7338 case Intrinsic::arm_neon_vld1:
7339 case Intrinsic::arm_neon_vld2:
7340 case Intrinsic::arm_neon_vld3:
7341 case Intrinsic::arm_neon_vld4:
7342 case Intrinsic::arm_neon_vld2lane:
7343 case Intrinsic::arm_neon_vld3lane:
7344 case Intrinsic::arm_neon_vld4lane:
7345 case Intrinsic::arm_neon_vst1:
7346 case Intrinsic::arm_neon_vst2:
7347 case Intrinsic::arm_neon_vst3:
7348 case Intrinsic::arm_neon_vst4:
7349 case Intrinsic::arm_neon_vst2lane:
7350 case Intrinsic::arm_neon_vst3lane:
7351 case Intrinsic::arm_neon_vst4lane:
7352 return CombineBaseUpdate(N, DCI);
7353 default: break;
7354 }
7355 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007356 }
Dan Gohman475871a2008-07-27 21:46:04 +00007357 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007358}
7359
Evan Cheng31959b12011-02-02 01:06:55 +00007360bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
7361 EVT VT) const {
7362 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
7363}
7364
Bill Wendlingaf566342009-08-15 21:21:19 +00007365bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00007366 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00007367 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00007368
7369 switch (VT.getSimpleVT().SimpleTy) {
7370 default:
7371 return false;
7372 case MVT::i8:
7373 case MVT::i16:
7374 case MVT::i32:
7375 return true;
7376 // FIXME: VLD1 etc with standard alignment is legal.
7377 }
7378}
7379
Evan Chenge6c835f2009-08-14 20:09:37 +00007380static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
7381 if (V < 0)
7382 return false;
7383
7384 unsigned Scale = 1;
7385 switch (VT.getSimpleVT().SimpleTy) {
7386 default: return false;
7387 case MVT::i1:
7388 case MVT::i8:
7389 // Scale == 1;
7390 break;
7391 case MVT::i16:
7392 // Scale == 2;
7393 Scale = 2;
7394 break;
7395 case MVT::i32:
7396 // Scale == 4;
7397 Scale = 4;
7398 break;
7399 }
7400
7401 if ((V & (Scale - 1)) != 0)
7402 return false;
7403 V /= Scale;
7404 return V == (V & ((1LL << 5) - 1));
7405}
7406
7407static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
7408 const ARMSubtarget *Subtarget) {
7409 bool isNeg = false;
7410 if (V < 0) {
7411 isNeg = true;
7412 V = - V;
7413 }
7414
7415 switch (VT.getSimpleVT().SimpleTy) {
7416 default: return false;
7417 case MVT::i1:
7418 case MVT::i8:
7419 case MVT::i16:
7420 case MVT::i32:
7421 // + imm12 or - imm8
7422 if (isNeg)
7423 return V == (V & ((1LL << 8) - 1));
7424 return V == (V & ((1LL << 12) - 1));
7425 case MVT::f32:
7426 case MVT::f64:
7427 // Same as ARM mode. FIXME: NEON?
7428 if (!Subtarget->hasVFP2())
7429 return false;
7430 if ((V & 3) != 0)
7431 return false;
7432 V >>= 2;
7433 return V == (V & ((1LL << 8) - 1));
7434 }
7435}
7436
Evan Chengb01fad62007-03-12 23:30:29 +00007437/// isLegalAddressImmediate - Return true if the integer value can be used
7438/// as the offset of the target addressing mode for load / store of the
7439/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00007440static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007441 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00007442 if (V == 0)
7443 return true;
7444
Evan Cheng65011532009-03-09 19:15:00 +00007445 if (!VT.isSimple())
7446 return false;
7447
Evan Chenge6c835f2009-08-14 20:09:37 +00007448 if (Subtarget->isThumb1Only())
7449 return isLegalT1AddressImmediate(V, VT);
7450 else if (Subtarget->isThumb2())
7451 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007452
Evan Chenge6c835f2009-08-14 20:09:37 +00007453 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007454 if (V < 0)
7455 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007456 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007457 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 case MVT::i1:
7459 case MVT::i8:
7460 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007461 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007462 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007463 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007464 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007465 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007466 case MVT::f32:
7467 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007468 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007469 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007470 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007471 return false;
7472 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007473 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007474 }
Evan Chenga8e29892007-01-19 07:51:42 +00007475}
7476
Evan Chenge6c835f2009-08-14 20:09:37 +00007477bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7478 EVT VT) const {
7479 int Scale = AM.Scale;
7480 if (Scale < 0)
7481 return false;
7482
7483 switch (VT.getSimpleVT().SimpleTy) {
7484 default: return false;
7485 case MVT::i1:
7486 case MVT::i8:
7487 case MVT::i16:
7488 case MVT::i32:
7489 if (Scale == 1)
7490 return true;
7491 // r + r << imm
7492 Scale = Scale & ~1;
7493 return Scale == 2 || Scale == 4 || Scale == 8;
7494 case MVT::i64:
7495 // r + r
7496 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7497 return true;
7498 return false;
7499 case MVT::isVoid:
7500 // Note, we allow "void" uses (basically, uses that aren't loads or
7501 // stores), because arm allows folding a scale into many arithmetic
7502 // operations. This should be made more precise and revisited later.
7503
7504 // Allow r << imm, but the imm has to be a multiple of two.
7505 if (Scale & 1) return false;
7506 return isPowerOf2_32(Scale);
7507 }
7508}
7509
Chris Lattner37caf8c2007-04-09 23:33:39 +00007510/// isLegalAddressingMode - Return true if the addressing mode represented
7511/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007512bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007513 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007514 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007515 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007516 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007517
Chris Lattner37caf8c2007-04-09 23:33:39 +00007518 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007519 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007520 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007521
Chris Lattner37caf8c2007-04-09 23:33:39 +00007522 switch (AM.Scale) {
7523 case 0: // no scale reg, must be "r+i" or "r", or "i".
7524 break;
7525 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007526 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007527 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007528 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007529 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007530 // ARM doesn't support any R+R*scale+imm addr modes.
7531 if (AM.BaseOffs)
7532 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007533
Bob Wilson2c7dab12009-04-08 17:55:28 +00007534 if (!VT.isSimple())
7535 return false;
7536
Evan Chenge6c835f2009-08-14 20:09:37 +00007537 if (Subtarget->isThumb2())
7538 return isLegalT2ScaledAddressingMode(AM, VT);
7539
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007540 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007541 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007542 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007543 case MVT::i1:
7544 case MVT::i8:
7545 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007546 if (Scale < 0) Scale = -Scale;
7547 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007548 return true;
7549 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007550 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007551 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007552 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007553 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007554 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007555 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007556 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007557
Owen Anderson825b72b2009-08-11 20:47:22 +00007558 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007559 // Note, we allow "void" uses (basically, uses that aren't loads or
7560 // stores), because arm allows folding a scale into many arithmetic
7561 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007562
Chris Lattner37caf8c2007-04-09 23:33:39 +00007563 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007564 if (Scale & 1) return false;
7565 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007566 }
7567 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007568 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007569 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007570}
7571
Evan Cheng77e47512009-11-11 19:05:52 +00007572/// isLegalICmpImmediate - Return true if the specified immediate is legal
7573/// icmp immediate, that is the target has icmp instructions which can compare
7574/// a register against the immediate without having to materialize the
7575/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007576bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007577 if (!Subtarget->isThumb())
7578 return ARM_AM::getSOImmVal(Imm) != -1;
7579 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007580 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007581 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007582}
7583
Dan Gohmancca82142011-05-03 00:46:49 +00007584/// isLegalAddImmediate - Return true if the specified immediate is legal
7585/// add immediate, that is the target has add instructions which can add
7586/// a register with the immediate without having to materialize the
7587/// immediate into a register.
7588bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7589 return ARM_AM::getSOImmVal(Imm) != -1;
7590}
7591
Owen Andersone50ed302009-08-10 22:56:29 +00007592static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007593 bool isSEXTLoad, SDValue &Base,
7594 SDValue &Offset, bool &isInc,
7595 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007596 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7597 return false;
7598
Owen Anderson825b72b2009-08-11 20:47:22 +00007599 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007600 // AddressingMode 3
7601 Base = Ptr->getOperand(0);
7602 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007603 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007604 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007605 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007606 isInc = false;
7607 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7608 return true;
7609 }
7610 }
7611 isInc = (Ptr->getOpcode() == ISD::ADD);
7612 Offset = Ptr->getOperand(1);
7613 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007614 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007615 // AddressingMode 2
7616 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007617 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007618 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007619 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007620 isInc = false;
7621 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7622 Base = Ptr->getOperand(0);
7623 return true;
7624 }
7625 }
7626
7627 if (Ptr->getOpcode() == ISD::ADD) {
7628 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00007629 ARM_AM::ShiftOpc ShOpcVal=
7630 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00007631 if (ShOpcVal != ARM_AM::no_shift) {
7632 Base = Ptr->getOperand(1);
7633 Offset = Ptr->getOperand(0);
7634 } else {
7635 Base = Ptr->getOperand(0);
7636 Offset = Ptr->getOperand(1);
7637 }
7638 return true;
7639 }
7640
7641 isInc = (Ptr->getOpcode() == ISD::ADD);
7642 Base = Ptr->getOperand(0);
7643 Offset = Ptr->getOperand(1);
7644 return true;
7645 }
7646
Jim Grosbache5165492009-11-09 00:11:35 +00007647 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007648 return false;
7649}
7650
Owen Andersone50ed302009-08-10 22:56:29 +00007651static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007652 bool isSEXTLoad, SDValue &Base,
7653 SDValue &Offset, bool &isInc,
7654 SelectionDAG &DAG) {
7655 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7656 return false;
7657
7658 Base = Ptr->getOperand(0);
7659 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7660 int RHSC = (int)RHS->getZExtValue();
7661 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7662 assert(Ptr->getOpcode() == ISD::ADD);
7663 isInc = false;
7664 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7665 return true;
7666 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7667 isInc = Ptr->getOpcode() == ISD::ADD;
7668 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7669 return true;
7670 }
7671 }
7672
7673 return false;
7674}
7675
Evan Chenga8e29892007-01-19 07:51:42 +00007676/// getPreIndexedAddressParts - returns true by value, base pointer and
7677/// offset pointer and addressing mode by reference if the node's address
7678/// can be legally represented as pre-indexed load / store address.
7679bool
Dan Gohman475871a2008-07-27 21:46:04 +00007680ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7681 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007682 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007683 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007684 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007685 return false;
7686
Owen Andersone50ed302009-08-10 22:56:29 +00007687 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007688 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007689 bool isSEXTLoad = false;
7690 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7691 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007692 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007693 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7694 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7695 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007696 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007697 } else
7698 return false;
7699
7700 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007701 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007702 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007703 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7704 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007705 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007706 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007707 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007708 if (!isLegal)
7709 return false;
7710
7711 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7712 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007713}
7714
7715/// getPostIndexedAddressParts - returns true by value, base pointer and
7716/// offset pointer and addressing mode by reference if this node can be
7717/// combined with a load / store to form a post-indexed load / store.
7718bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007719 SDValue &Base,
7720 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007721 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007722 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007723 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007724 return false;
7725
Owen Andersone50ed302009-08-10 22:56:29 +00007726 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007727 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007728 bool isSEXTLoad = false;
7729 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007730 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007731 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007732 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7733 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007734 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007735 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007736 } else
7737 return false;
7738
7739 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007740 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007741 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007742 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007743 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007744 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007745 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7746 isInc, DAG);
7747 if (!isLegal)
7748 return false;
7749
Evan Cheng28dad2a2010-05-18 21:31:17 +00007750 if (Ptr != Base) {
7751 // Swap base ptr and offset to catch more post-index load / store when
7752 // it's legal. In Thumb2 mode, offset must be an immediate.
7753 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7754 !Subtarget->isThumb2())
7755 std::swap(Base, Offset);
7756
7757 // Post-indexed load / store update the base pointer.
7758 if (Ptr != Base)
7759 return false;
7760 }
7761
Evan Chenge88d5ce2009-07-02 07:28:31 +00007762 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7763 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007764}
7765
Dan Gohman475871a2008-07-27 21:46:04 +00007766void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007767 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007768 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007769 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007770 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007771 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007772 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007773 switch (Op.getOpcode()) {
7774 default: break;
7775 case ARMISD::CMOV: {
7776 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007777 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007778 if (KnownZero == 0 && KnownOne == 0) return;
7779
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007780 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007781 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7782 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007783 KnownZero &= KnownZeroRHS;
7784 KnownOne &= KnownOneRHS;
7785 return;
7786 }
7787 }
7788}
7789
7790//===----------------------------------------------------------------------===//
7791// ARM Inline Assembly Support
7792//===----------------------------------------------------------------------===//
7793
Evan Cheng55d42002011-01-08 01:24:27 +00007794bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7795 // Looking for "rev" which is V6+.
7796 if (!Subtarget->hasV6Ops())
7797 return false;
7798
7799 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7800 std::string AsmStr = IA->getAsmString();
7801 SmallVector<StringRef, 4> AsmPieces;
7802 SplitString(AsmStr, AsmPieces, ";\n");
7803
7804 switch (AsmPieces.size()) {
7805 default: return false;
7806 case 1:
7807 AsmStr = AsmPieces[0];
7808 AsmPieces.clear();
7809 SplitString(AsmStr, AsmPieces, " \t,");
7810
7811 // rev $0, $1
7812 if (AsmPieces.size() == 3 &&
7813 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7814 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007815 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00007816 if (Ty && Ty->getBitWidth() == 32)
7817 return IntrinsicLowering::LowerToByteSwap(CI);
7818 }
7819 break;
7820 }
7821
7822 return false;
7823}
7824
Evan Chenga8e29892007-01-19 07:51:42 +00007825/// getConstraintType - Given a constraint letter, return the type of
7826/// constraint it is for this target.
7827ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007828ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7829 if (Constraint.size() == 1) {
7830 switch (Constraint[0]) {
7831 default: break;
7832 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007833 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00007834 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007835 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007836 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00007837 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00007838 // An address with a single base register. Due to the way we
7839 // currently handle addresses it is the same as an 'r' memory constraint.
7840 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007841 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007842 } else if (Constraint.size() == 2) {
7843 switch (Constraint[0]) {
7844 default: break;
7845 // All 'U+' constraints are addresses.
7846 case 'U': return C_Memory;
7847 }
Evan Chenga8e29892007-01-19 07:51:42 +00007848 }
Chris Lattner4234f572007-03-25 02:14:49 +00007849 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007850}
7851
John Thompson44ab89e2010-10-29 17:29:13 +00007852/// Examine constraint type and operand type and determine a weight value.
7853/// This object must already have been set up with the operand type
7854/// and the current alternative constraint selected.
7855TargetLowering::ConstraintWeight
7856ARMTargetLowering::getSingleConstraintMatchWeight(
7857 AsmOperandInfo &info, const char *constraint) const {
7858 ConstraintWeight weight = CW_Invalid;
7859 Value *CallOperandVal = info.CallOperandVal;
7860 // If we don't have a value, we can't do a match,
7861 // but allow it at the lowest weight.
7862 if (CallOperandVal == NULL)
7863 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007864 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007865 // Look at the constraint type.
7866 switch (*constraint) {
7867 default:
7868 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7869 break;
7870 case 'l':
7871 if (type->isIntegerTy()) {
7872 if (Subtarget->isThumb())
7873 weight = CW_SpecificReg;
7874 else
7875 weight = CW_Register;
7876 }
7877 break;
7878 case 'w':
7879 if (type->isFloatingPointTy())
7880 weight = CW_Register;
7881 break;
7882 }
7883 return weight;
7884}
7885
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007886typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7887RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007888ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007889 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007890 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007891 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007892 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007893 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007894 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007895 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007896 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007897 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007898 case 'h': // High regs or no regs.
7899 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007900 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007901 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007902 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007903 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007904 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007906 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007907 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007908 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007909 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007910 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007911 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007912 case 'x':
7913 if (VT == MVT::f32)
7914 return RCPair(0U, ARM::SPR_8RegisterClass);
7915 if (VT.getSizeInBits() == 64)
7916 return RCPair(0U, ARM::DPR_8RegisterClass);
7917 if (VT.getSizeInBits() == 128)
7918 return RCPair(0U, ARM::QPR_8RegisterClass);
7919 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007920 case 't':
7921 if (VT == MVT::f32)
7922 return RCPair(0U, ARM::SPRRegisterClass);
7923 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007924 }
7925 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007926 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007927 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007928
Evan Chenga8e29892007-01-19 07:51:42 +00007929 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7930}
7931
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007932/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7933/// vector. If it is invalid, don't add anything to Ops.
7934void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007935 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007936 std::vector<SDValue>&Ops,
7937 SelectionDAG &DAG) const {
7938 SDValue Result(0, 0);
7939
Eric Christopher100c8332011-06-02 23:16:42 +00007940 // Currently only support length 1 constraints.
7941 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007942
Eric Christopher100c8332011-06-02 23:16:42 +00007943 char ConstraintLetter = Constraint[0];
7944 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007945 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007946 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007947 case 'I': case 'J': case 'K': case 'L':
7948 case 'M': case 'N': case 'O':
7949 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7950 if (!C)
7951 return;
7952
7953 int64_t CVal64 = C->getSExtValue();
7954 int CVal = (int) CVal64;
7955 // None of these constraints allow values larger than 32 bits. Check
7956 // that the value fits in an int.
7957 if (CVal != CVal64)
7958 return;
7959
Eric Christopher100c8332011-06-02 23:16:42 +00007960 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007961 case 'j':
7962 // Constant suitable for movw, must be between 0 and
7963 // 65535.
7964 if (Subtarget->hasV6T2Ops())
7965 if (CVal >= 0 && CVal <= 65535)
7966 break;
7967 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007968 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007969 if (Subtarget->isThumb1Only()) {
7970 // This must be a constant between 0 and 255, for ADD
7971 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007972 if (CVal >= 0 && CVal <= 255)
7973 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007974 } else if (Subtarget->isThumb2()) {
7975 // A constant that can be used as an immediate value in a
7976 // data-processing instruction.
7977 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7978 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007979 } else {
7980 // A constant that can be used as an immediate value in a
7981 // data-processing instruction.
7982 if (ARM_AM::getSOImmVal(CVal) != -1)
7983 break;
7984 }
7985 return;
7986
7987 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007988 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007989 // This must be a constant between -255 and -1, for negated ADD
7990 // immediates. This can be used in GCC with an "n" modifier that
7991 // prints the negated value, for use with SUB instructions. It is
7992 // not useful otherwise but is implemented for compatibility.
7993 if (CVal >= -255 && CVal <= -1)
7994 break;
7995 } else {
7996 // This must be a constant between -4095 and 4095. It is not clear
7997 // what this constraint is intended for. Implemented for
7998 // compatibility with GCC.
7999 if (CVal >= -4095 && CVal <= 4095)
8000 break;
8001 }
8002 return;
8003
8004 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008005 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008006 // A 32-bit value where only one byte has a nonzero value. Exclude
8007 // zero to match GCC. This constraint is used by GCC internally for
8008 // constants that can be loaded with a move/shift combination.
8009 // It is not useful otherwise but is implemented for compatibility.
8010 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8011 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008012 } else if (Subtarget->isThumb2()) {
8013 // A constant whose bitwise inverse can be used as an immediate
8014 // value in a data-processing instruction. This can be used in GCC
8015 // with a "B" modifier that prints the inverted value, for use with
8016 // BIC and MVN instructions. It is not useful otherwise but is
8017 // implemented for compatibility.
8018 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8019 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008020 } else {
8021 // A constant whose bitwise inverse can be used as an immediate
8022 // value in a data-processing instruction. This can be used in GCC
8023 // with a "B" modifier that prints the inverted value, for use with
8024 // BIC and MVN instructions. It is not useful otherwise but is
8025 // implemented for compatibility.
8026 if (ARM_AM::getSOImmVal(~CVal) != -1)
8027 break;
8028 }
8029 return;
8030
8031 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008032 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008033 // This must be a constant between -7 and 7,
8034 // for 3-operand ADD/SUB immediate instructions.
8035 if (CVal >= -7 && CVal < 7)
8036 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008037 } else if (Subtarget->isThumb2()) {
8038 // A constant whose negation can be used as an immediate value in a
8039 // data-processing instruction. This can be used in GCC with an "n"
8040 // modifier that prints the negated value, for use with SUB
8041 // instructions. It is not useful otherwise but is implemented for
8042 // compatibility.
8043 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8044 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008045 } else {
8046 // A constant whose negation can be used as an immediate value in a
8047 // data-processing instruction. This can be used in GCC with an "n"
8048 // modifier that prints the negated value, for use with SUB
8049 // instructions. It is not useful otherwise but is implemented for
8050 // compatibility.
8051 if (ARM_AM::getSOImmVal(-CVal) != -1)
8052 break;
8053 }
8054 return;
8055
8056 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008057 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008058 // This must be a multiple of 4 between 0 and 1020, for
8059 // ADD sp + immediate.
8060 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8061 break;
8062 } else {
8063 // A power of two or a constant between 0 and 32. This is used in
8064 // GCC for the shift amount on shifted register operands, but it is
8065 // useful in general for any shift amounts.
8066 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8067 break;
8068 }
8069 return;
8070
8071 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008072 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008073 // This must be a constant between 0 and 31, for shift amounts.
8074 if (CVal >= 0 && CVal <= 31)
8075 break;
8076 }
8077 return;
8078
8079 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008080 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008081 // This must be a multiple of 4 between -508 and 508, for
8082 // ADD/SUB sp = sp + immediate.
8083 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8084 break;
8085 }
8086 return;
8087 }
8088 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8089 break;
8090 }
8091
8092 if (Result.getNode()) {
8093 Ops.push_back(Result);
8094 return;
8095 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008096 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008097}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008098
8099bool
8100ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8101 // The ARM target isn't yet aware of offsets.
8102 return false;
8103}
Evan Cheng39382422009-10-28 01:44:26 +00008104
8105int ARM::getVFPf32Imm(const APFloat &FPImm) {
8106 APInt Imm = FPImm.bitcastToAPInt();
8107 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
8108 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
8109 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
8110
8111 // We can handle 4 bits of mantissa.
8112 // mantissa = (16+UInt(e:f:g:h))/16.
8113 if (Mantissa & 0x7ffff)
8114 return -1;
8115 Mantissa >>= 19;
8116 if ((Mantissa & 0xf) != Mantissa)
8117 return -1;
8118
8119 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
8120 if (Exp < -3 || Exp > 4)
8121 return -1;
8122 Exp = ((Exp+3) & 0x7) ^ 4;
8123
8124 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
8125}
8126
8127int ARM::getVFPf64Imm(const APFloat &FPImm) {
8128 APInt Imm = FPImm.bitcastToAPInt();
8129 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
8130 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
8131 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
8132
8133 // We can handle 4 bits of mantissa.
8134 // mantissa = (16+UInt(e:f:g:h))/16.
8135 if (Mantissa & 0xffffffffffffLL)
8136 return -1;
8137 Mantissa >>= 48;
8138 if ((Mantissa & 0xf) != Mantissa)
8139 return -1;
8140
8141 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
8142 if (Exp < -3 || Exp > 4)
8143 return -1;
8144 Exp = ((Exp+3) & 0x7) ^ 4;
8145
8146 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
8147}
8148
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008149bool ARM::isBitFieldInvertedMask(unsigned v) {
8150 if (v == 0xffffffff)
8151 return 0;
8152 // there can be 1's on either or both "outsides", all the "inside"
8153 // bits must be 0's
8154 unsigned int lsb = 0, msb = 31;
8155 while (v & (1 << msb)) --msb;
8156 while (v & (1 << lsb)) ++lsb;
8157 for (unsigned int i = lsb; i <= msb; ++i) {
8158 if (v & (1 << i))
8159 return 0;
8160 }
8161 return 1;
8162}
8163
Evan Cheng39382422009-10-28 01:44:26 +00008164/// isFPImmLegal - Returns true if the target can instruction select the
8165/// specified FP immediate natively. If false, the legalizer will
8166/// materialize the FP immediate as a load from a constant pool.
8167bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8168 if (!Subtarget->hasVFP3())
8169 return false;
8170 if (VT == MVT::f32)
8171 return ARM::getVFPf32Imm(Imm) != -1;
8172 if (VT == MVT::f64)
8173 return ARM::getVFPf64Imm(Imm) != -1;
8174 return false;
8175}
Bob Wilson65ffec42010-09-21 17:56:22 +00008176
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008177/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008178/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8179/// specified in the intrinsic calls.
8180bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8181 const CallInst &I,
8182 unsigned Intrinsic) const {
8183 switch (Intrinsic) {
8184 case Intrinsic::arm_neon_vld1:
8185 case Intrinsic::arm_neon_vld2:
8186 case Intrinsic::arm_neon_vld3:
8187 case Intrinsic::arm_neon_vld4:
8188 case Intrinsic::arm_neon_vld2lane:
8189 case Intrinsic::arm_neon_vld3lane:
8190 case Intrinsic::arm_neon_vld4lane: {
8191 Info.opc = ISD::INTRINSIC_W_CHAIN;
8192 // Conservatively set memVT to the entire set of vectors loaded.
8193 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8194 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8195 Info.ptrVal = I.getArgOperand(0);
8196 Info.offset = 0;
8197 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8198 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8199 Info.vol = false; // volatile loads with NEON intrinsics not supported
8200 Info.readMem = true;
8201 Info.writeMem = false;
8202 return true;
8203 }
8204 case Intrinsic::arm_neon_vst1:
8205 case Intrinsic::arm_neon_vst2:
8206 case Intrinsic::arm_neon_vst3:
8207 case Intrinsic::arm_neon_vst4:
8208 case Intrinsic::arm_neon_vst2lane:
8209 case Intrinsic::arm_neon_vst3lane:
8210 case Intrinsic::arm_neon_vst4lane: {
8211 Info.opc = ISD::INTRINSIC_VOID;
8212 // Conservatively set memVT to the entire set of vectors stored.
8213 unsigned NumElts = 0;
8214 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008215 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008216 if (!ArgTy->isVectorTy())
8217 break;
8218 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8219 }
8220 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8221 Info.ptrVal = I.getArgOperand(0);
8222 Info.offset = 0;
8223 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8224 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8225 Info.vol = false; // volatile stores with NEON intrinsics not supported
8226 Info.readMem = false;
8227 Info.writeMem = true;
8228 return true;
8229 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008230 case Intrinsic::arm_strexd: {
8231 Info.opc = ISD::INTRINSIC_W_CHAIN;
8232 Info.memVT = MVT::i64;
8233 Info.ptrVal = I.getArgOperand(2);
8234 Info.offset = 0;
8235 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008236 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008237 Info.readMem = false;
8238 Info.writeMem = true;
8239 return true;
8240 }
8241 case Intrinsic::arm_ldrexd: {
8242 Info.opc = ISD::INTRINSIC_W_CHAIN;
8243 Info.memVT = MVT::i64;
8244 Info.ptrVal = I.getArgOperand(0);
8245 Info.offset = 0;
8246 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008247 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008248 Info.readMem = true;
8249 Info.writeMem = false;
8250 return true;
8251 }
Bob Wilson65ffec42010-09-21 17:56:22 +00008252 default:
8253 break;
8254 }
8255
8256 return false;
8257}