blob: 2a7b058715f7e48e49a36e99de0e63b2065bbfe7 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000017#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000026#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Cameron Zwaricha86686e2011-06-10 20:59:24 +000075namespace llvm {
76 class ARMCCState : public CCState {
77 public:
78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
80 LLVMContext &C, ParmContext PC)
81 : CCState(CC, isVarArg, MF, TM, locs, C) {
82 assert(((PC == Call) || (PC == Prologue)) &&
83 "ARMCCState users must specify whether their context is call"
84 "or prologue generation.");
85 CallOrPrologue = PC;
86 }
87 };
88}
89
Stuart Hastingsc7315872011-04-20 16:47:52 +000090// The APCS parameter registers.
91static const unsigned GPRArgRegs[] = {
92 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93};
94
Owen Andersone50ed302009-08-10 22:56:29 +000095void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
96 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000097 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
100 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 }
106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +0000109 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000110 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000111 if (ElemTy != MVT::i32) {
112 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
116 }
Owen Anderson70671842009-08-10 20:18:46 +0000117 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
118 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000119 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000120 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000121 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
122 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
125 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
126 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000127 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
128 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000129 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
130 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
131 setTruncStoreAction(VT.getSimpleVT(),
132 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000134 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135
136 // Promote all bit-wise operations.
137 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000139 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000146 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000147 }
Bob Wilson16330762009-09-16 00:17:28 +0000148
149 // Neon does not support vector divide/remainder operations.
150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Owen Andersone50ed302009-08-10 22:56:29 +0000158void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000159 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000161}
162
Owen Andersone50ed302009-08-10 22:56:29 +0000163void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000164 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000166}
167
Chris Lattnerf0144122009-07-28 03:13:23 +0000168static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000170 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000171
Chris Lattner80ec2792009-08-02 00:34:36 +0000172 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173}
174
Evan Chenga8e29892007-01-19 07:51:42 +0000175ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000176 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000178 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000179 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 // Uses VFP for Thumb libfuncs if available.
183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
184 // Single-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 // Double-precision floating-point arithmetic.
191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 // Single-precision comparisons.
197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 // Double-precision comparisons.
216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000224
Evan Chengb1df8f22007-04-27 08:15:43 +0000225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000233
Evan Chengb1df8f22007-04-27 08:15:43 +0000234 // Floating-point to integer conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000241
Evan Chengb1df8f22007-04-27 08:15:43 +0000242 // Conversions between floating types.
243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
245
246 // Integer to floating-point conversions.
247 // i64 conversions are done via library routines even when generating VFP
248 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000249 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
250 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256 }
257
Bob Wilson2f954612009-05-22 17:38:41 +0000258 // These libcalls are not available in 32-bit.
259 setLibcallName(RTLIB::SHL_I128, 0);
260 setLibcallName(RTLIB::SRL_I128, 0);
261 setLibcallName(RTLIB::SRA_I128, 0);
262
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000263 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000265 // RTABI chapter 4.1.2, Table 2
266 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
267 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
268 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
269 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
270 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
274
275 // Double-precision floating-point comparison helper functions
276 // RTABI chapter 4.1.2, Table 3
277 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
278 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
279 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
281 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
282 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
284 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
286 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
288 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
289 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
290 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
291 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
293 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
301
302 // Single-precision floating-point arithmetic helper functions
303 // RTABI chapter 4.1.2, Table 4
304 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
305 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
306 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
307 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
308 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
312
313 // Single-precision floating-point comparison helper functions
314 // RTABI chapter 4.1.2, Table 5
315 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
316 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
317 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
319 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
320 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
322 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
324 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
326 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
327 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
328 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
329 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
331 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
339
340 // Floating-point to integer conversions.
341 // RTABI chapter 4.1.2, Table 6
342 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
343 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
346 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
347 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
350 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
358
359 // Conversions between floating types.
360 // RTABI chapter 4.1.2, Table 7
361 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
362 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
363 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000365
366 // Integer to floating-point conversions.
367 // RTABI chapter 4.1.2, Table 8
368 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
369 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
370 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
371 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
372 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
373 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
374 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
375 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384
385 // Long long helper functions
386 // RTABI chapter 4.2, Table 9
387 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
388 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
389 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
390 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
391 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
392 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
393 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
399
400 // Integer division functions
401 // RTABI chapter 4.3.1
402 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
403 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
404 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
405 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
407 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
408 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000413 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000414
415 // Memory operations
416 // RTABI chapter 4.3.4
417 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
418 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
419 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000420 }
421
David Goodwinf1daf7d2009-07-08 23:10:31 +0000422 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000424 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000426 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000428 if (!Subtarget->isFPOnlySP())
429 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000430
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000432 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000433
434 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 addDRTypeForNEON(MVT::v2f32);
436 addDRTypeForNEON(MVT::v8i8);
437 addDRTypeForNEON(MVT::v4i16);
438 addDRTypeForNEON(MVT::v2i32);
439 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addQRTypeForNEON(MVT::v4f32);
442 addQRTypeForNEON(MVT::v2f64);
443 addQRTypeForNEON(MVT::v16i8);
444 addQRTypeForNEON(MVT::v8i16);
445 addQRTypeForNEON(MVT::v4i32);
446 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000447
Bob Wilson74dc72e2009-09-15 23:55:57 +0000448 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
449 // neither Neon nor VFP support any arithmetic operations on it.
450 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
451 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
452 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
453 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
454 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
455 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
456 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
457 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
458 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
459 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
460 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
461 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
462 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
463 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
464 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
465 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
467 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
468 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
469 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
470 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
471 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
472 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
473 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
474
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000475 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
476
Bob Wilson642b3292009-09-16 00:32:15 +0000477 // Neon does not support some operations on v1i64 and v2i64 types.
478 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000479 // Custom handling for some quad-vector types to detect VMULL.
480 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
481 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
482 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000483 // Custom handling for some vector types to avoid expensive expansions
484 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
485 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
486 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
487 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000488 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
489 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000490 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
491 // a destination type that is wider than the source.
492 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
493 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000494
Bob Wilson1c3ef902011-02-07 17:43:21 +0000495 setTargetDAGCombine(ISD::INTRINSIC_VOID);
496 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000497 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
498 setTargetDAGCombine(ISD::SHL);
499 setTargetDAGCombine(ISD::SRL);
500 setTargetDAGCombine(ISD::SRA);
501 setTargetDAGCombine(ISD::SIGN_EXTEND);
502 setTargetDAGCombine(ISD::ZERO_EXTEND);
503 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000504 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000505 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000506 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000507 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
508 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000509 setTargetDAGCombine(ISD::FP_TO_SINT);
510 setTargetDAGCombine(ISD::FP_TO_UINT);
511 setTargetDAGCombine(ISD::FDIV);
Bob Wilson5bafff32009-06-22 23:27:02 +0000512 }
513
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000514 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000515
516 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000518
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000519 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000521
Evan Chenga8e29892007-01-19 07:51:42 +0000522 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000523 if (!Subtarget->isThumb1Only()) {
524 for (unsigned im = (unsigned)ISD::PRE_INC;
525 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 setIndexedLoadAction(im, MVT::i1, Legal);
527 setIndexedLoadAction(im, MVT::i8, Legal);
528 setIndexedLoadAction(im, MVT::i16, Legal);
529 setIndexedLoadAction(im, MVT::i32, Legal);
530 setIndexedStoreAction(im, MVT::i1, Legal);
531 setIndexedStoreAction(im, MVT::i8, Legal);
532 setIndexedStoreAction(im, MVT::i16, Legal);
533 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000534 }
Evan Chenga8e29892007-01-19 07:51:42 +0000535 }
536
537 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000538 setOperationAction(ISD::MUL, MVT::i64, Expand);
539 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000540 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
542 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000543 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000544 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
545 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000546 setOperationAction(ISD::MULHS, MVT::i32, Expand);
547
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000548 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000549 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000550 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::SRL, MVT::i64, Custom);
552 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000553
554 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000556 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000558 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000560
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000561 // Only ARMv6 has BSWAP.
562 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000564
Evan Chenga8e29892007-01-19 07:51:42 +0000565 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000566 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000567 // v7M has a hardware divider
568 setOperationAction(ISD::SDIV, MVT::i32, Expand);
569 setOperationAction(ISD::UDIV, MVT::i32, Expand);
570 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::SREM, MVT::i32, Expand);
572 setOperationAction(ISD::UREM, MVT::i32, Expand);
573 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
574 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000575
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
577 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
578 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
579 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000580 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000581
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000582 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000583
Evan Chenga8e29892007-01-19 07:51:42 +0000584 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::VASTART, MVT::Other, Custom);
586 setOperationAction(ISD::VAARG, MVT::Other, Expand);
587 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
588 setOperationAction(ISD::VAEND, MVT::Other, Expand);
589 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
590 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000591 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000592 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
593 setExceptionPointerRegister(ARM::R0);
594 setExceptionSelectorRegister(ARM::R1);
595
Evan Cheng3a1588a2010-04-15 22:20:34 +0000596 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000597 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
598 // the default expansion.
599 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000600 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000601 // membarrier needs custom lowering; the rest are legal and handled
602 // normally.
603 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
604 } else {
605 // Set them all for expansion, which will force libcalls.
606 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
607 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
608 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
609 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000610 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
611 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
612 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000613 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
616 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
617 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
618 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
624 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
625 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
626 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
627 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
628 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
629 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
630 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000631 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i8, Expand);
632 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i16, Expand);
633 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
634 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i8, Expand);
635 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i16, Expand);
636 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
637 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i8, Expand);
638 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i16, Expand);
639 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
640 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i8, Expand);
641 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i16, Expand);
642 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000643 // Since the libcalls include locking, fold in the fences
644 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000645 }
646 // 64-bit versions are always libcalls (for now)
647 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000648 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000649 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
650 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
651 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
652 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
653 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
654 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000655
Evan Cheng416941d2010-11-04 05:19:35 +0000656 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000657
Eli Friedmana2c6f452010-06-26 04:36:50 +0000658 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
659 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
661 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000662 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000664
Nate Begemand1fb5832010-08-03 21:31:55 +0000665 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000666 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
667 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000668 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000669 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
670 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000671
672 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000674 if (Subtarget->isTargetDarwin()) {
675 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
676 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000677 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000678 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000679 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::SETCC, MVT::i32, Expand);
682 setOperationAction(ISD::SETCC, MVT::f32, Expand);
683 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000684 setOperationAction(ISD::SELECT, MVT::i32, Custom);
685 setOperationAction(ISD::SELECT, MVT::f32, Custom);
686 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
688 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
689 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000690
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
692 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
693 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
694 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
695 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000696
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000697 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::FSIN, MVT::f64, Expand);
699 setOperationAction(ISD::FSIN, MVT::f32, Expand);
700 setOperationAction(ISD::FCOS, MVT::f32, Expand);
701 setOperationAction(ISD::FCOS, MVT::f64, Expand);
702 setOperationAction(ISD::FREM, MVT::f64, Expand);
703 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000704 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
706 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000707 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 setOperationAction(ISD::FPOW, MVT::f64, Expand);
709 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000710
Cameron Zwarich33390842011-07-08 21:39:21 +0000711 setOperationAction(ISD::FMA, MVT::f64, Expand);
712 setOperationAction(ISD::FMA, MVT::f32, Expand);
713
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000714 // Various VFP goodness
715 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000716 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
717 if (Subtarget->hasVFP2()) {
718 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
719 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
720 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
721 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
722 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000723 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000724 if (!Subtarget->hasFP16()) {
725 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
726 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000727 }
Evan Cheng110cf482008-04-01 01:50:16 +0000728 }
Evan Chenga8e29892007-01-19 07:51:42 +0000729
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000730 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000731 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000732 setTargetDAGCombine(ISD::ADD);
733 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000734 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000735
Owen Anderson080c0922010-11-05 19:27:46 +0000736 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000737 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000738 if (Subtarget->hasNEON())
739 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000740
Evan Chenga8e29892007-01-19 07:51:42 +0000741 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000742
Evan Chengf7d87ee2010-05-21 00:43:17 +0000743 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
744 setSchedulingPreference(Sched::RegPressure);
745 else
746 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000747
Evan Cheng05219282011-01-06 06:52:41 +0000748 //// temporary - rewrite interface to use type
749 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000750
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000751 // On ARM arguments smaller than 4 bytes are extended, so all arguments
752 // are at least 4 bytes aligned.
753 setMinStackArgumentAlignment(4);
754
Evan Chengfff606d2010-09-24 19:07:23 +0000755 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000756
757 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000758}
759
Andrew Trick32cec0a2011-01-19 02:35:27 +0000760// FIXME: It might make sense to define the representative register class as the
761// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
762// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
763// SPR's representative would be DPR_VFP2. This should work well if register
764// pressure tracking were modified such that a register use would increment the
765// pressure of the register class's representative and all of it's super
766// classes' representatives transitively. We have not implemented this because
767// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000768// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000769// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000770std::pair<const TargetRegisterClass*, uint8_t>
771ARMTargetLowering::findRepresentativeClass(EVT VT) const{
772 const TargetRegisterClass *RRC = 0;
773 uint8_t Cost = 1;
774 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000775 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000776 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000777 // Use DPR as representative register class for all floating point
778 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
779 // the cost is 1 for both f32 and f64.
780 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000781 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000782 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000783 // When NEON is used for SP, only half of the register file is available
784 // because operations that define both SP and DP results will be constrained
785 // to the VFP2 class (D0-D15). We currently model this constraint prior to
786 // coalescing by double-counting the SP regs. See the FIXME above.
787 if (Subtarget->useNEONForSinglePrecisionFP())
788 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000789 break;
790 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
791 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000792 RRC = ARM::DPRRegisterClass;
793 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000794 break;
795 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000796 RRC = ARM::DPRRegisterClass;
797 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000798 break;
799 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000800 RRC = ARM::DPRRegisterClass;
801 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000802 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000803 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000804 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000805}
806
Evan Chenga8e29892007-01-19 07:51:42 +0000807const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
808 switch (Opcode) {
809 default: return 0;
810 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000811 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000812 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000813 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
814 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000815 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000816 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
817 case ARMISD::tCALL: return "ARMISD::tCALL";
818 case ARMISD::BRCOND: return "ARMISD::BRCOND";
819 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000820 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000821 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
822 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
823 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000824 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000825 case ARMISD::CMPFP: return "ARMISD::CMPFP";
826 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000827 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000828 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
829 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000830
Jim Grosbach3482c802010-01-18 19:58:49 +0000831 case ARMISD::RBIT: return "ARMISD::RBIT";
832
Bob Wilson76a312b2010-03-19 22:51:32 +0000833 case ARMISD::FTOSI: return "ARMISD::FTOSI";
834 case ARMISD::FTOUI: return "ARMISD::FTOUI";
835 case ARMISD::SITOF: return "ARMISD::SITOF";
836 case ARMISD::UITOF: return "ARMISD::UITOF";
837
Evan Chenga8e29892007-01-19 07:51:42 +0000838 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
839 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
840 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000841
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000842 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
843 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000844
Evan Chengc5942082009-10-28 06:55:03 +0000845 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
846 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000847 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000848
Dale Johannesen51e28e62010-06-03 21:09:53 +0000849 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000850
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000851 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000852
Evan Cheng86198642009-08-07 00:34:42 +0000853 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
854
Jim Grosbach3728e962009-12-10 00:11:09 +0000855 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000856 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000857
Evan Chengdfed19f2010-11-03 06:34:55 +0000858 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
859
Bob Wilson5bafff32009-06-22 23:27:02 +0000860 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000861 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000862 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000863 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
864 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000865 case ARMISD::VCGEU: return "ARMISD::VCGEU";
866 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000867 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
868 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000869 case ARMISD::VCGTU: return "ARMISD::VCGTU";
870 case ARMISD::VTST: return "ARMISD::VTST";
871
872 case ARMISD::VSHL: return "ARMISD::VSHL";
873 case ARMISD::VSHRs: return "ARMISD::VSHRs";
874 case ARMISD::VSHRu: return "ARMISD::VSHRu";
875 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
876 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
877 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
878 case ARMISD::VSHRN: return "ARMISD::VSHRN";
879 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
880 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
881 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
882 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
883 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
884 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
885 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
886 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
887 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
888 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
889 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
890 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
891 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
892 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000893 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000894 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000895 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000896 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000897 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000898 case ARMISD::VREV64: return "ARMISD::VREV64";
899 case ARMISD::VREV32: return "ARMISD::VREV32";
900 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000901 case ARMISD::VZIP: return "ARMISD::VZIP";
902 case ARMISD::VUZP: return "ARMISD::VUZP";
903 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000904 case ARMISD::VTBL1: return "ARMISD::VTBL1";
905 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000906 case ARMISD::VMULLs: return "ARMISD::VMULLs";
907 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000908 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000909 case ARMISD::FMAX: return "ARMISD::FMAX";
910 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000911 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000912 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
913 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000914 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000915 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
916 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
917 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000918 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
919 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
920 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
921 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
922 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
923 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
924 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
925 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
926 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
927 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
928 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
929 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
930 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
931 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
932 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
933 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
934 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000935 }
936}
937
Evan Cheng06b666c2010-05-15 02:18:07 +0000938/// getRegClassFor - Return the register class that should be used for the
939/// specified value type.
940TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
941 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
942 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
943 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000944 if (Subtarget->hasNEON()) {
945 if (VT == MVT::v4i64)
946 return ARM::QQPRRegisterClass;
947 else if (VT == MVT::v8i64)
948 return ARM::QQQQPRRegisterClass;
949 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000950 return TargetLowering::getRegClassFor(VT);
951}
952
Eric Christopherab695882010-07-21 22:26:11 +0000953// Create a fast isel object.
954FastISel *
955ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
956 return ARM::createFastISel(funcInfo);
957}
958
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000959/// getMaximalGlobalOffset - Returns the maximal possible offset which can
960/// be used for loads / stores from the global.
961unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
962 return (Subtarget->isThumb1Only() ? 127 : 4095);
963}
964
Evan Cheng1cc39842010-05-20 23:26:43 +0000965Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000966 unsigned NumVals = N->getNumValues();
967 if (!NumVals)
968 return Sched::RegPressure;
969
970 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000971 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000972 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000973 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000974 if (VT.isFloatingPoint() || VT.isVector())
975 return Sched::Latency;
976 }
Evan Chengc10f5432010-05-28 23:25:23 +0000977
978 if (!N->isMachineOpcode())
979 return Sched::RegPressure;
980
981 // Load are scheduled for latency even if there instruction itinerary
982 // is not available.
983 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000984 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000985
Evan Chenge837dea2011-06-28 19:10:37 +0000986 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +0000987 return Sched::RegPressure;
988 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +0000989 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000990 return Sched::Latency;
991
Evan Cheng1cc39842010-05-20 23:26:43 +0000992 return Sched::RegPressure;
993}
994
Evan Chenga8e29892007-01-19 07:51:42 +0000995//===----------------------------------------------------------------------===//
996// Lowering Code
997//===----------------------------------------------------------------------===//
998
Evan Chenga8e29892007-01-19 07:51:42 +0000999/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1000static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1001 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001002 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001003 case ISD::SETNE: return ARMCC::NE;
1004 case ISD::SETEQ: return ARMCC::EQ;
1005 case ISD::SETGT: return ARMCC::GT;
1006 case ISD::SETGE: return ARMCC::GE;
1007 case ISD::SETLT: return ARMCC::LT;
1008 case ISD::SETLE: return ARMCC::LE;
1009 case ISD::SETUGT: return ARMCC::HI;
1010 case ISD::SETUGE: return ARMCC::HS;
1011 case ISD::SETULT: return ARMCC::LO;
1012 case ISD::SETULE: return ARMCC::LS;
1013 }
1014}
1015
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001016/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1017static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001018 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001019 CondCode2 = ARMCC::AL;
1020 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001021 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001022 case ISD::SETEQ:
1023 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1024 case ISD::SETGT:
1025 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1026 case ISD::SETGE:
1027 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1028 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001029 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001030 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1031 case ISD::SETO: CondCode = ARMCC::VC; break;
1032 case ISD::SETUO: CondCode = ARMCC::VS; break;
1033 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1034 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1035 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1036 case ISD::SETLT:
1037 case ISD::SETULT: CondCode = ARMCC::LT; break;
1038 case ISD::SETLE:
1039 case ISD::SETULE: CondCode = ARMCC::LE; break;
1040 case ISD::SETNE:
1041 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1042 }
Evan Chenga8e29892007-01-19 07:51:42 +00001043}
1044
Bob Wilson1f595bb2009-04-17 19:07:39 +00001045//===----------------------------------------------------------------------===//
1046// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001047//===----------------------------------------------------------------------===//
1048
1049#include "ARMGenCallingConv.inc"
1050
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001051/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1052/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001053CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001054 bool Return,
1055 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001056 switch (CC) {
1057 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001058 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001059 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001060 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001061 if (!Subtarget->isAAPCS_ABI())
1062 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1063 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1064 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1065 }
1066 // Fallthrough
1067 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001068 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001069 if (!Subtarget->isAAPCS_ABI())
1070 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1071 else if (Subtarget->hasVFP2() &&
1072 FloatABIType == FloatABI::Hard && !isVarArg)
1073 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1074 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1075 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001076 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001077 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001078 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001079 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001080 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001081 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001082 }
1083}
1084
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085/// LowerCallResult - Lower the result values of a call into the
1086/// appropriate copies out of appropriate physical registers.
1087SDValue
1088ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001089 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001090 const SmallVectorImpl<ISD::InputArg> &Ins,
1091 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001092 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001093
Bob Wilson1f595bb2009-04-17 19:07:39 +00001094 // Assign locations to each value returned by this call.
1095 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001096 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1097 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001098 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001099 CCAssignFnForNode(CallConv, /* Return*/ true,
1100 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001101
1102 // Copy all of the result registers out of their specified physreg.
1103 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1104 CCValAssign VA = RVLocs[i];
1105
Bob Wilson80915242009-04-25 00:33:20 +00001106 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001108 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001109 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001110 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001111 Chain = Lo.getValue(1);
1112 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001113 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001114 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001115 InFlag);
1116 Chain = Hi.getValue(1);
1117 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001118 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001119
Owen Anderson825b72b2009-08-11 20:47:22 +00001120 if (VA.getLocVT() == MVT::v2f64) {
1121 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1122 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1123 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001124
1125 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001127 Chain = Lo.getValue(1);
1128 InFlag = Lo.getValue(2);
1129 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001130 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001131 Chain = Hi.getValue(1);
1132 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001133 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1135 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001136 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001138 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1139 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001140 Chain = Val.getValue(1);
1141 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142 }
Bob Wilson80915242009-04-25 00:33:20 +00001143
1144 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001145 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001146 case CCValAssign::Full: break;
1147 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001148 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001149 break;
1150 }
1151
Dan Gohman98ca4f22009-08-05 01:29:28 +00001152 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001153 }
1154
Dan Gohman98ca4f22009-08-05 01:29:28 +00001155 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001156}
1157
Bob Wilsondee46d72009-04-17 20:35:10 +00001158/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001159SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001160ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1161 SDValue StackPtr, SDValue Arg,
1162 DebugLoc dl, SelectionDAG &DAG,
1163 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001164 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165 unsigned LocMemOffset = VA.getLocMemOffset();
1166 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1167 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001168 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001169 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001170 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001171}
1172
Dan Gohman98ca4f22009-08-05 01:29:28 +00001173void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001174 SDValue Chain, SDValue &Arg,
1175 RegsToPassVector &RegsToPass,
1176 CCValAssign &VA, CCValAssign &NextVA,
1177 SDValue &StackPtr,
1178 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001179 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001180
Jim Grosbache5165492009-11-09 00:11:35 +00001181 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001182 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001183 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1184
1185 if (NextVA.isRegLoc())
1186 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1187 else {
1188 assert(NextVA.isMemLoc());
1189 if (StackPtr.getNode() == 0)
1190 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1191
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1193 dl, DAG, NextVA,
1194 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001195 }
1196}
1197
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001199/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1200/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001201SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001202ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001203 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001204 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001205 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001206 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207 const SmallVectorImpl<ISD::InputArg> &Ins,
1208 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001209 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001210 MachineFunction &MF = DAG.getMachineFunction();
1211 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1212 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001213 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001214 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001215 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001216 if (isTailCall) {
1217 // Check if it's really possible to do a tail call.
1218 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1219 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001220 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001221 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1222 // detected sibcalls.
1223 if (isTailCall) {
1224 ++NumTailCalls;
1225 IsSibCall = true;
1226 }
1227 }
Evan Chenga8e29892007-01-19 07:51:42 +00001228
Bob Wilson1f595bb2009-04-17 19:07:39 +00001229 // Analyze operands of the call, assigning locations to each operand.
1230 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001231 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1232 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001234 CCAssignFnForNode(CallConv, /* Return*/ false,
1235 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001236
Bob Wilson1f595bb2009-04-17 19:07:39 +00001237 // Get a count of how many bytes are to be pushed on the stack.
1238 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001239
Dale Johannesen51e28e62010-06-03 21:09:53 +00001240 // For tail calls, memory operands are available in our caller's stack.
1241 if (IsSibCall)
1242 NumBytes = 0;
1243
Evan Chenga8e29892007-01-19 07:51:42 +00001244 // Adjust the stack pointer for the new arguments...
1245 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001246 if (!IsSibCall)
1247 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001248
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001249 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001250
Bob Wilson5bafff32009-06-22 23:27:02 +00001251 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001253
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001255 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1257 i != e;
1258 ++i, ++realArgIdx) {
1259 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001260 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001261 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001262 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001263
Bob Wilson1f595bb2009-04-17 19:07:39 +00001264 // Promote the value if needed.
1265 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001266 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001267 case CCValAssign::Full: break;
1268 case CCValAssign::SExt:
1269 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1270 break;
1271 case CCValAssign::ZExt:
1272 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1273 break;
1274 case CCValAssign::AExt:
1275 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1276 break;
1277 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001278 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001280 }
1281
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001282 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001283 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001284 if (VA.getLocVT() == MVT::v2f64) {
1285 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1286 DAG.getConstant(0, MVT::i32));
1287 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1288 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001289
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001291 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1292
1293 VA = ArgLocs[++i]; // skip ahead to next loc
1294 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001295 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001296 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1297 } else {
1298 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001299
Dan Gohman98ca4f22009-08-05 01:29:28 +00001300 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1301 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001302 }
1303 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001304 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001305 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001306 }
1307 } else if (VA.isRegLoc()) {
1308 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001309 } else if (isByVal) {
1310 assert(VA.isMemLoc());
1311 unsigned offset = 0;
1312
1313 // True if this byval aggregate will be split between registers
1314 // and memory.
1315 if (CCInfo.isFirstByValRegValid()) {
1316 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1317 unsigned int i, j;
1318 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1319 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1320 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1321 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1322 MachinePointerInfo(),
1323 false, false, 0);
1324 MemOpChains.push_back(Load.getValue(1));
1325 RegsToPass.push_back(std::make_pair(j, Load));
1326 }
1327 offset = ARM::R4 - CCInfo.getFirstByValReg();
1328 CCInfo.clearFirstByValReg();
1329 }
1330
1331 unsigned LocMemOffset = VA.getLocMemOffset();
1332 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1333 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1334 StkPtrOff);
1335 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1336 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1337 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1338 MVT::i32);
1339 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1340 Flags.getByValAlign(),
1341 /*isVolatile=*/false,
1342 /*AlwaysInline=*/false,
1343 MachinePointerInfo(0),
1344 MachinePointerInfo(0)));
1345
1346 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001347 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001348
Dan Gohman98ca4f22009-08-05 01:29:28 +00001349 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1350 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001351 }
Evan Chenga8e29892007-01-19 07:51:42 +00001352 }
1353
1354 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001355 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001356 &MemOpChains[0], MemOpChains.size());
1357
1358 // Build a sequence of copy-to-reg nodes chained together with token chain
1359 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001360 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001361 // Tail call byval lowering might overwrite argument registers so in case of
1362 // tail call optimization the copies to registers are lowered later.
1363 if (!isTailCall)
1364 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1365 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1366 RegsToPass[i].second, InFlag);
1367 InFlag = Chain.getValue(1);
1368 }
Evan Chenga8e29892007-01-19 07:51:42 +00001369
Dale Johannesen51e28e62010-06-03 21:09:53 +00001370 // For tail calls lower the arguments to the 'real' stack slot.
1371 if (isTailCall) {
1372 // Force all the incoming stack arguments to be loaded from the stack
1373 // before any new outgoing arguments are stored to the stack, because the
1374 // outgoing stack slots may alias the incoming argument stack slots, and
1375 // the alias isn't otherwise explicit. This is slightly more conservative
1376 // than necessary, because it means that each store effectively depends
1377 // on every argument instead of just those arguments it would clobber.
1378
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001379 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001380 InFlag = SDValue();
1381 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1382 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1383 RegsToPass[i].second, InFlag);
1384 InFlag = Chain.getValue(1);
1385 }
1386 InFlag =SDValue();
1387 }
1388
Bill Wendling056292f2008-09-16 21:48:12 +00001389 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1390 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1391 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001392 bool isDirect = false;
1393 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001394 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001395 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001396
1397 if (EnableARMLongCalls) {
1398 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1399 && "long-calls with non-static relocation model!");
1400 // Handle a global address or an external symbol. If it's not one of
1401 // those, the target's already in a register, so we don't need to do
1402 // anything extra.
1403 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001404 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001405 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001406 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001407 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1408 ARMPCLabelIndex,
1409 ARMCP::CPValue, 0);
1410 // Get the address of the callee into a register
1411 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1412 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1413 Callee = DAG.getLoad(getPointerTy(), dl,
1414 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001415 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001416 false, false, 0);
1417 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1418 const char *Sym = S->getSymbol();
1419
1420 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001421 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001422 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1423 Sym, ARMPCLabelIndex, 0);
1424 // Get the address of the callee into a register
1425 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1426 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1427 Callee = DAG.getLoad(getPointerTy(), dl,
1428 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001429 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001430 false, false, 0);
1431 }
1432 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001433 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001434 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001435 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001436 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001437 getTargetMachine().getRelocationModel() != Reloc::Static;
1438 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001439 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001440 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001441 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001442 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001443 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001444 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001445 ARMPCLabelIndex,
1446 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001447 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001448 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001449 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001450 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001451 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001452 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001453 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001454 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001455 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001456 } else {
1457 // On ELF targets for PIC code, direct calls should go through the PLT
1458 unsigned OpFlags = 0;
1459 if (Subtarget->isTargetELF() &&
1460 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1461 OpFlags = ARMII::MO_PLT;
1462 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1463 }
Bill Wendling056292f2008-09-16 21:48:12 +00001464 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001465 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001466 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001467 getTargetMachine().getRelocationModel() != Reloc::Static;
1468 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001469 // tBX takes a register source operand.
1470 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001471 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001472 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001473 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001474 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001475 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001476 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001477 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001478 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001479 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001480 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001481 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001482 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001483 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001484 } else {
1485 unsigned OpFlags = 0;
1486 // On ELF targets for PIC code, direct calls should go through the PLT
1487 if (Subtarget->isTargetELF() &&
1488 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1489 OpFlags = ARMII::MO_PLT;
1490 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1491 }
Evan Chenga8e29892007-01-19 07:51:42 +00001492 }
1493
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001494 // FIXME: handle tail calls differently.
1495 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001496 if (Subtarget->isThumb()) {
1497 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001498 CallOpc = ARMISD::CALL_NOLINK;
1499 else
1500 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1501 } else {
1502 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001503 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1504 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001505 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001506
Dan Gohman475871a2008-07-27 21:46:04 +00001507 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001508 Ops.push_back(Chain);
1509 Ops.push_back(Callee);
1510
1511 // Add argument registers to the end of the list so that they are known live
1512 // into the call.
1513 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1514 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1515 RegsToPass[i].second.getValueType()));
1516
Gabor Greifba36cb52008-08-28 21:40:38 +00001517 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001518 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001519
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001520 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001521 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001522 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001523
Duncan Sands4bdcb612008-07-02 17:40:58 +00001524 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001525 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001526 InFlag = Chain.getValue(1);
1527
Chris Lattnere563bbc2008-10-11 22:08:30 +00001528 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1529 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001530 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001531 InFlag = Chain.getValue(1);
1532
Bob Wilson1f595bb2009-04-17 19:07:39 +00001533 // Handle result values, copying them out of physregs into vregs that we
1534 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001535 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1536 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001537}
1538
Stuart Hastingsf222e592011-02-28 17:17:53 +00001539/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001540/// on the stack. Remember the next parameter register to allocate,
1541/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001542/// this.
1543void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001544llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1545 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1546 assert((State->getCallOrPrologue() == Prologue ||
1547 State->getCallOrPrologue() == Call) &&
1548 "unhandled ParmContext");
1549 if ((!State->isFirstByValRegValid()) &&
1550 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1551 State->setFirstByValReg(reg);
1552 // At a call site, a byval parameter that is split between
1553 // registers and memory needs its size truncated here. In a
1554 // function prologue, such byval parameters are reassembled in
1555 // memory, and are not truncated.
1556 if (State->getCallOrPrologue() == Call) {
1557 unsigned excess = 4 * (ARM::R4 - reg);
1558 assert(size >= excess && "expected larger existing stack allocation");
1559 size -= excess;
1560 }
1561 }
1562 // Confiscate any remaining parameter registers to preclude their
1563 // assignment to subsequent parameters.
1564 while (State->AllocateReg(GPRArgRegs, 4))
1565 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001566}
1567
Dale Johannesen51e28e62010-06-03 21:09:53 +00001568/// MatchingStackOffset - Return true if the given stack call argument is
1569/// already available in the same position (relatively) of the caller's
1570/// incoming argument stack.
1571static
1572bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1573 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1574 const ARMInstrInfo *TII) {
1575 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1576 int FI = INT_MAX;
1577 if (Arg.getOpcode() == ISD::CopyFromReg) {
1578 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001579 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001580 return false;
1581 MachineInstr *Def = MRI->getVRegDef(VR);
1582 if (!Def)
1583 return false;
1584 if (!Flags.isByVal()) {
1585 if (!TII->isLoadFromStackSlot(Def, FI))
1586 return false;
1587 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001588 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001589 }
1590 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1591 if (Flags.isByVal())
1592 // ByVal argument is passed in as a pointer but it's now being
1593 // dereferenced. e.g.
1594 // define @foo(%struct.X* %A) {
1595 // tail call @bar(%struct.X* byval %A)
1596 // }
1597 return false;
1598 SDValue Ptr = Ld->getBasePtr();
1599 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1600 if (!FINode)
1601 return false;
1602 FI = FINode->getIndex();
1603 } else
1604 return false;
1605
1606 assert(FI != INT_MAX);
1607 if (!MFI->isFixedObjectIndex(FI))
1608 return false;
1609 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1610}
1611
1612/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1613/// for tail call optimization. Targets which want to do tail call
1614/// optimization should implement this function.
1615bool
1616ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1617 CallingConv::ID CalleeCC,
1618 bool isVarArg,
1619 bool isCalleeStructRet,
1620 bool isCallerStructRet,
1621 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001622 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001623 const SmallVectorImpl<ISD::InputArg> &Ins,
1624 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001625 const Function *CallerF = DAG.getMachineFunction().getFunction();
1626 CallingConv::ID CallerCC = CallerF->getCallingConv();
1627 bool CCMatch = CallerCC == CalleeCC;
1628
1629 // Look for obvious safe cases to perform tail call optimization that do not
1630 // require ABI changes. This is what gcc calls sibcall.
1631
Jim Grosbach7616b642010-06-16 23:45:49 +00001632 // Do not sibcall optimize vararg calls unless the call site is not passing
1633 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001634 if (isVarArg && !Outs.empty())
1635 return false;
1636
1637 // Also avoid sibcall optimization if either caller or callee uses struct
1638 // return semantics.
1639 if (isCalleeStructRet || isCallerStructRet)
1640 return false;
1641
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001642 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001643 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1644 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1645 // support in the assembler and linker to be used. This would need to be
1646 // fixed to fully support tail calls in Thumb1.
1647 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001648 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1649 // LR. This means if we need to reload LR, it takes an extra instructions,
1650 // which outweighs the value of the tail call; but here we don't know yet
1651 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001652 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001653 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001654
1655 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1656 // but we need to make sure there are enough registers; the only valid
1657 // registers are the 4 used for parameters. We don't currently do this
1658 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001659 if (Subtarget->isThumb1Only())
1660 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001661
Dale Johannesen51e28e62010-06-03 21:09:53 +00001662 // If the calling conventions do not match, then we'd better make sure the
1663 // results are returned in the same way as what the caller expects.
1664 if (!CCMatch) {
1665 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001666 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1667 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001668 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1669
1670 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001671 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1672 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001673 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1674
1675 if (RVLocs1.size() != RVLocs2.size())
1676 return false;
1677 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1678 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1679 return false;
1680 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1681 return false;
1682 if (RVLocs1[i].isRegLoc()) {
1683 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1684 return false;
1685 } else {
1686 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1687 return false;
1688 }
1689 }
1690 }
1691
1692 // If the callee takes no arguments then go on to check the results of the
1693 // call.
1694 if (!Outs.empty()) {
1695 // Check if stack adjustment is needed. For now, do not do this if any
1696 // argument is passed on the stack.
1697 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001698 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1699 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001700 CCInfo.AnalyzeCallOperands(Outs,
1701 CCAssignFnForNode(CalleeCC, false, isVarArg));
1702 if (CCInfo.getNextStackOffset()) {
1703 MachineFunction &MF = DAG.getMachineFunction();
1704
1705 // Check if the arguments are already laid out in the right way as
1706 // the caller's fixed stack objects.
1707 MachineFrameInfo *MFI = MF.getFrameInfo();
1708 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1709 const ARMInstrInfo *TII =
1710 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001711 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1712 i != e;
1713 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001714 CCValAssign &VA = ArgLocs[i];
1715 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001716 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001717 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001718 if (VA.getLocInfo() == CCValAssign::Indirect)
1719 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001720 if (VA.needsCustom()) {
1721 // f64 and vector types are split into multiple registers or
1722 // register/stack-slot combinations. The types will not match
1723 // the registers; give up on memory f64 refs until we figure
1724 // out what to do about this.
1725 if (!VA.isRegLoc())
1726 return false;
1727 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001728 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001729 if (RegVT == MVT::v2f64) {
1730 if (!ArgLocs[++i].isRegLoc())
1731 return false;
1732 if (!ArgLocs[++i].isRegLoc())
1733 return false;
1734 }
1735 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001736 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1737 MFI, MRI, TII))
1738 return false;
1739 }
1740 }
1741 }
1742 }
1743
1744 return true;
1745}
1746
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747SDValue
1748ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001749 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001751 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001752 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001753
Bob Wilsondee46d72009-04-17 20:35:10 +00001754 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001755 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001756
Bob Wilsondee46d72009-04-17 20:35:10 +00001757 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001758 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1759 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001760
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001762 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1763 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001764
1765 // If this is the first return lowered for this function, add
1766 // the regs to the liveout set for the function.
1767 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1768 for (unsigned i = 0; i != RVLocs.size(); ++i)
1769 if (RVLocs[i].isRegLoc())
1770 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001771 }
1772
Bob Wilson1f595bb2009-04-17 19:07:39 +00001773 SDValue Flag;
1774
1775 // Copy the result values into the output registers.
1776 for (unsigned i = 0, realRVLocIdx = 0;
1777 i != RVLocs.size();
1778 ++i, ++realRVLocIdx) {
1779 CCValAssign &VA = RVLocs[i];
1780 assert(VA.isRegLoc() && "Can only return in registers!");
1781
Dan Gohmanc9403652010-07-07 15:54:55 +00001782 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001783
1784 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001785 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001786 case CCValAssign::Full: break;
1787 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001788 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001789 break;
1790 }
1791
Bob Wilson1f595bb2009-04-17 19:07:39 +00001792 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001793 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001794 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1796 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001797 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001799
1800 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1801 Flag = Chain.getValue(1);
1802 VA = RVLocs[++i]; // skip ahead to next loc
1803 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1804 HalfGPRs.getValue(1), Flag);
1805 Flag = Chain.getValue(1);
1806 VA = RVLocs[++i]; // skip ahead to next loc
1807
1808 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1810 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001811 }
1812 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1813 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001814 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001815 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001816 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001817 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818 VA = RVLocs[++i]; // skip ahead to next loc
1819 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1820 Flag);
1821 } else
1822 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1823
Bob Wilsondee46d72009-04-17 20:35:10 +00001824 // Guarantee that all emitted copies are
1825 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001826 Flag = Chain.getValue(1);
1827 }
1828
1829 SDValue result;
1830 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001831 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001832 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001834
1835 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001836}
1837
Evan Cheng3d2125c2010-11-30 23:55:39 +00001838bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1839 if (N->getNumValues() != 1)
1840 return false;
1841 if (!N->hasNUsesOfValue(1, 0))
1842 return false;
1843
1844 unsigned NumCopies = 0;
1845 SDNode* Copies[2];
1846 SDNode *Use = *N->use_begin();
1847 if (Use->getOpcode() == ISD::CopyToReg) {
1848 Copies[NumCopies++] = Use;
1849 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1850 // f64 returned in a pair of GPRs.
1851 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1852 UI != UE; ++UI) {
1853 if (UI->getOpcode() != ISD::CopyToReg)
1854 return false;
1855 Copies[UI.getUse().getResNo()] = *UI;
1856 ++NumCopies;
1857 }
1858 } else if (Use->getOpcode() == ISD::BITCAST) {
1859 // f32 returned in a single GPR.
1860 if (!Use->hasNUsesOfValue(1, 0))
1861 return false;
1862 Use = *Use->use_begin();
1863 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1864 return false;
1865 Copies[NumCopies++] = Use;
1866 } else {
1867 return false;
1868 }
1869
1870 if (NumCopies != 1 && NumCopies != 2)
1871 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001872
1873 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001874 for (unsigned i = 0; i < NumCopies; ++i) {
1875 SDNode *Copy = Copies[i];
1876 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1877 UI != UE; ++UI) {
1878 if (UI->getOpcode() == ISD::CopyToReg) {
1879 SDNode *Use = *UI;
1880 if (Use == Copies[0] || Use == Copies[1])
1881 continue;
1882 return false;
1883 }
1884 if (UI->getOpcode() != ARMISD::RET_FLAG)
1885 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001886 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001887 }
1888 }
1889
Evan Cheng1bf891a2010-12-01 22:59:46 +00001890 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001891}
1892
Evan Cheng485fafc2011-03-21 01:19:09 +00001893bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1894 if (!EnableARMTailCalls)
1895 return false;
1896
1897 if (!CI->isTailCall())
1898 return false;
1899
1900 return !Subtarget->isThumb1Only();
1901}
1902
Bob Wilsonb62d2572009-11-03 00:02:05 +00001903// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1904// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1905// one of the above mentioned nodes. It has to be wrapped because otherwise
1906// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1907// be used to form addressing mode. These wrapped nodes will be selected
1908// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001909static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001910 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001911 // FIXME there is no actual debug info here
1912 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001913 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001914 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001915 if (CP->isMachineConstantPoolEntry())
1916 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1917 CP->getAlignment());
1918 else
1919 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1920 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001921 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001922}
1923
Jim Grosbache1102ca2010-07-19 17:20:38 +00001924unsigned ARMTargetLowering::getJumpTableEncoding() const {
1925 return MachineJumpTableInfo::EK_Inline;
1926}
1927
Dan Gohmand858e902010-04-17 15:26:15 +00001928SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1929 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001930 MachineFunction &MF = DAG.getMachineFunction();
1931 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1932 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001933 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001934 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001935 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001936 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1937 SDValue CPAddr;
1938 if (RelocM == Reloc::Static) {
1939 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1940 } else {
1941 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001942 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001943 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1944 ARMCP::CPBlockAddress,
1945 PCAdj);
1946 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1947 }
1948 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1949 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001950 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001951 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001952 if (RelocM == Reloc::Static)
1953 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001954 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001955 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001956}
1957
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001958// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001959SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001960ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001961 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001962 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001963 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001964 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001965 MachineFunction &MF = DAG.getMachineFunction();
1966 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001967 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001968 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001969 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001970 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001971 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001972 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001973 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001974 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001975 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001977
Evan Chenge7e0d622009-11-06 22:24:13 +00001978 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001979 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001980
1981 // call __tls_get_addr.
1982 ArgListTy Args;
1983 ArgListEntry Entry;
1984 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001985 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001986 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001987 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001988 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001989 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00001990 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001992 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001993 return CallResult.first;
1994}
1995
1996// Lower ISD::GlobalTLSAddress using the "initial exec" or
1997// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001998SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001999ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002000 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002001 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002002 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002003 SDValue Offset;
2004 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002005 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002006 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002007 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002008
Chris Lattner4fb63d02009-07-15 04:12:33 +00002009 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002010 MachineFunction &MF = DAG.getMachineFunction();
2011 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002012 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002013 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002014 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2015 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002016 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002017 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002018 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002019 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002020 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002021 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002022 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002023 Chain = Offset.getValue(1);
2024
Evan Chenge7e0d622009-11-06 22:24:13 +00002025 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002026 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002027
Evan Cheng9eda6892009-10-31 03:39:36 +00002028 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002029 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002030 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002031 } else {
2032 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002033 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002034 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002036 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002037 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002038 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002039 }
2040
2041 // The address of the thread local variable is the add of the thread
2042 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002043 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002044}
2045
Dan Gohman475871a2008-07-27 21:46:04 +00002046SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002047ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002048 // TODO: implement the "local dynamic" model
2049 assert(Subtarget->isTargetELF() &&
2050 "TLS not implemented for non-ELF targets");
2051 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2052 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2053 // otherwise use the "Local Exec" TLS Model
2054 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2055 return LowerToTLSGeneralDynamicModel(GA, DAG);
2056 else
2057 return LowerToTLSExecModels(GA, DAG);
2058}
2059
Dan Gohman475871a2008-07-27 21:46:04 +00002060SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002061 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002062 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002063 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002064 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002065 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2066 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002067 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002068 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002069 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002070 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002071 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002072 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002073 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002074 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002075 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002076 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002077 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002078 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002079 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002080 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002081 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002082 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002083 }
2084
2085 // If we have T2 ops, we can materialize the address directly via movt/movw
2086 // pair. This is always cheaper.
2087 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002088 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002089 // FIXME: Once remat is capable of dealing with instructions with register
2090 // operands, expand this into two nodes.
2091 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2092 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002093 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002094 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2095 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2096 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2097 MachinePointerInfo::getConstantPool(),
2098 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002099 }
2100}
2101
Dan Gohman475871a2008-07-27 21:46:04 +00002102SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002103 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002104 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002105 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002106 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002107 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002108 MachineFunction &MF = DAG.getMachineFunction();
2109 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2110
Evan Cheng4abce0c2011-05-27 20:11:27 +00002111 // FIXME: Enable this for static codegen when tool issues are fixed.
2112 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002113 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002114 // FIXME: Once remat is capable of dealing with instructions with register
2115 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002116 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002117 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2118 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2119
Evan Cheng53519f02011-01-21 18:55:51 +00002120 unsigned Wrapper = (RelocM == Reloc::PIC_)
2121 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2122 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002123 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002124 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2125 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2126 MachinePointerInfo::getGOT(), false, false, 0);
2127 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002128 }
2129
2130 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002131 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002132 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002133 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002134 } else {
2135 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002136 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2137 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002138 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002139 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002140 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002141 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002142
Evan Cheng9eda6892009-10-31 03:39:36 +00002143 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002144 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002145 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002147
2148 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002149 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002150 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002151 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002152
Evan Cheng63476a82009-09-03 07:04:02 +00002153 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002154 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002155 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002156
2157 return Result;
2158}
2159
Dan Gohman475871a2008-07-27 21:46:04 +00002160SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002161 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002162 assert(Subtarget->isTargetELF() &&
2163 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002164 MachineFunction &MF = DAG.getMachineFunction();
2165 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002166 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002167 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002168 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002169 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002170 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2171 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002172 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002173 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002174 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002175 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002176 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002177 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002178 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002179 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002180}
2181
Jim Grosbach0e0da732009-05-12 23:59:14 +00002182SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002183ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2184 const {
2185 DebugLoc dl = Op.getDebugLoc();
2186 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002187 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002188}
2189
2190SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002191ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2192 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002193 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002194 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2195 Op.getOperand(1), Val);
2196}
2197
2198SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002199ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2200 DebugLoc dl = Op.getDebugLoc();
2201 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2202 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2203}
2204
2205SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002206ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002207 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002208 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002209 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002210 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002211 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002212 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002213 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002214 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2215 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002216 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002217 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002218 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002219 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002220 EVT PtrVT = getPointerTy();
2221 DebugLoc dl = Op.getDebugLoc();
2222 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2223 SDValue CPAddr;
2224 unsigned PCAdj = (RelocM != Reloc::PIC_)
2225 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002226 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002227 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2228 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002229 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002230 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002231 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002232 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002233 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002234 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002235
2236 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002237 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002238 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2239 }
2240 return Result;
2241 }
Evan Cheng92e39162011-03-29 23:06:19 +00002242 case Intrinsic::arm_neon_vmulls:
2243 case Intrinsic::arm_neon_vmullu: {
2244 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2245 ? ARMISD::VMULLs : ARMISD::VMULLu;
2246 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2247 Op.getOperand(1), Op.getOperand(2));
2248 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002249 }
2250}
2251
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002252static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002253 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002254 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002255 if (!Subtarget->hasDataBarrier()) {
2256 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2257 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2258 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002259 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002260 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002261 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002262 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002263 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002264
2265 SDValue Op5 = Op.getOperand(5);
2266 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2267 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2268 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2269 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2270
2271 ARM_MB::MemBOpt DMBOpt;
2272 if (isDeviceBarrier)
2273 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2274 else
2275 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2276 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2277 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002278}
2279
Evan Chengdfed19f2010-11-03 06:34:55 +00002280static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2281 const ARMSubtarget *Subtarget) {
2282 // ARM pre v5TE and Thumb1 does not have preload instructions.
2283 if (!(Subtarget->isThumb2() ||
2284 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2285 // Just preserve the chain.
2286 return Op.getOperand(0);
2287
2288 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002289 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2290 if (!isRead &&
2291 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2292 // ARMv7 with MP extension has PLDW.
2293 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002294
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002295 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2296 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002297 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002298 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002299 isData = ~isData & 1;
2300 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002301
2302 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002303 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2304 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002305}
2306
Dan Gohman1e93df62010-04-17 14:41:14 +00002307static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2308 MachineFunction &MF = DAG.getMachineFunction();
2309 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2310
Evan Chenga8e29892007-01-19 07:51:42 +00002311 // vastart just stores the address of the VarArgsFrameIndex slot into the
2312 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002313 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002314 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002315 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002316 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002317 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2318 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002319}
2320
Dan Gohman475871a2008-07-27 21:46:04 +00002321SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002322ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2323 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002324 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002325 MachineFunction &MF = DAG.getMachineFunction();
2326 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2327
2328 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002329 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002330 RC = ARM::tGPRRegisterClass;
2331 else
2332 RC = ARM::GPRRegisterClass;
2333
2334 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002335 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002336 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002337
2338 SDValue ArgValue2;
2339 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002340 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002341 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002342
2343 // Create load node to retrieve arguments from the stack.
2344 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002345 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002346 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002347 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002348 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002349 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002350 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002351 }
2352
Jim Grosbache5165492009-11-09 00:11:35 +00002353 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002354}
2355
Stuart Hastingsc7315872011-04-20 16:47:52 +00002356void
2357ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2358 unsigned &VARegSize, unsigned &VARegSaveSize)
2359 const {
2360 unsigned NumGPRs;
2361 if (CCInfo.isFirstByValRegValid())
2362 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2363 else {
2364 unsigned int firstUnalloced;
2365 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2366 sizeof(GPRArgRegs) /
2367 sizeof(GPRArgRegs[0]));
2368 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2369 }
2370
2371 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2372 VARegSize = NumGPRs * 4;
2373 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2374}
2375
2376// The remaining GPRs hold either the beginning of variable-argument
2377// data, or the beginning of an aggregate passed by value (usuall
2378// byval). Either way, we allocate stack slots adjacent to the data
2379// provided by our caller, and store the unallocated registers there.
2380// If this is a variadic function, the va_list pointer will begin with
2381// these values; otherwise, this reassembles a (byval) structure that
2382// was split between registers and memory.
2383void
2384ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2385 DebugLoc dl, SDValue &Chain,
2386 unsigned ArgOffset) const {
2387 MachineFunction &MF = DAG.getMachineFunction();
2388 MachineFrameInfo *MFI = MF.getFrameInfo();
2389 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2390 unsigned firstRegToSaveIndex;
2391 if (CCInfo.isFirstByValRegValid())
2392 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2393 else {
2394 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2395 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2396 }
2397
2398 unsigned VARegSize, VARegSaveSize;
2399 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2400 if (VARegSaveSize) {
2401 // If this function is vararg, store any remaining integer argument regs
2402 // to their spots on the stack so that they may be loaded by deferencing
2403 // the result of va_next.
2404 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002405 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2406 ArgOffset + VARegSaveSize
2407 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002408 false));
2409 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2410 getPointerTy());
2411
2412 SmallVector<SDValue, 4> MemOps;
2413 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2414 TargetRegisterClass *RC;
2415 if (AFI->isThumb1OnlyFunction())
2416 RC = ARM::tGPRRegisterClass;
2417 else
2418 RC = ARM::GPRRegisterClass;
2419
2420 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2421 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2422 SDValue Store =
2423 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002424 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002425 false, false, 0);
2426 MemOps.push_back(Store);
2427 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2428 DAG.getConstant(4, getPointerTy()));
2429 }
2430 if (!MemOps.empty())
2431 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2432 &MemOps[0], MemOps.size());
2433 } else
2434 // This will point to the next argument passed via stack.
2435 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2436}
2437
Bob Wilson5bafff32009-06-22 23:27:02 +00002438SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002439ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002440 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002441 const SmallVectorImpl<ISD::InputArg>
2442 &Ins,
2443 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002444 SmallVectorImpl<SDValue> &InVals)
2445 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002446 MachineFunction &MF = DAG.getMachineFunction();
2447 MachineFrameInfo *MFI = MF.getFrameInfo();
2448
Bob Wilson1f595bb2009-04-17 19:07:39 +00002449 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2450
2451 // Assign locations to all of the incoming arguments.
2452 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002453 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2454 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002455 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002456 CCAssignFnForNode(CallConv, /* Return*/ false,
2457 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002458
2459 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002460 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002461
Stuart Hastingsf222e592011-02-28 17:17:53 +00002462 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002463 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2464 CCValAssign &VA = ArgLocs[i];
2465
Bob Wilsondee46d72009-04-17 20:35:10 +00002466 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002467 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002468 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002469
Bob Wilson1f595bb2009-04-17 19:07:39 +00002470 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002471 // f64 and vector types are split up into multiple registers or
2472 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002473 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002474 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002475 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002476 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002477 SDValue ArgValue2;
2478 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002479 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002480 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2481 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002482 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002483 false, false, 0);
2484 } else {
2485 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2486 Chain, DAG, dl);
2487 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002488 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2489 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002490 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002491 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002492 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2493 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002494 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002495
Bob Wilson5bafff32009-06-22 23:27:02 +00002496 } else {
2497 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002498
Owen Anderson825b72b2009-08-11 20:47:22 +00002499 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002500 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002501 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002502 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002503 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002504 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002505 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002506 RC = (AFI->isThumb1OnlyFunction() ?
2507 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002508 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002509 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002510
2511 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002512 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002513 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002514 }
2515
2516 // If this is an 8 or 16-bit value, it is really passed promoted
2517 // to 32 bits. Insert an assert[sz]ext to capture this, then
2518 // truncate to the right size.
2519 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002520 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002521 case CCValAssign::Full: break;
2522 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002523 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002524 break;
2525 case CCValAssign::SExt:
2526 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2527 DAG.getValueType(VA.getValVT()));
2528 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2529 break;
2530 case CCValAssign::ZExt:
2531 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2532 DAG.getValueType(VA.getValVT()));
2533 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2534 break;
2535 }
2536
Dan Gohman98ca4f22009-08-05 01:29:28 +00002537 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002538
2539 } else { // VA.isRegLoc()
2540
2541 // sanity check
2542 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002543 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002544
Stuart Hastingsf222e592011-02-28 17:17:53 +00002545 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002546
Stuart Hastingsf222e592011-02-28 17:17:53 +00002547 // Some Ins[] entries become multiple ArgLoc[] entries.
2548 // Process them only once.
2549 if (index != lastInsIndex)
2550 {
2551 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002552 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002553 // This can be changed with more analysis.
2554 // In case of tail call optimization mark all arguments mutable.
2555 // Since they could be overwritten by lowering of arguments in case of
2556 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002557 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002558 unsigned VARegSize, VARegSaveSize;
2559 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2560 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2561 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002562 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002563 int FI = MFI->CreateFixedObject(Bytes,
2564 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002565 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2566 } else {
2567 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2568 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002569
Stuart Hastingsf222e592011-02-28 17:17:53 +00002570 // Create load nodes to retrieve arguments from the stack.
2571 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2572 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2573 MachinePointerInfo::getFixedStack(FI),
2574 false, false, 0));
2575 }
2576 lastInsIndex = index;
2577 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002578 }
2579 }
2580
2581 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002582 if (isVarArg)
2583 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002584
Dan Gohman98ca4f22009-08-05 01:29:28 +00002585 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002586}
2587
2588/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002589static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002590 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002591 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002592 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002593 // Maybe this has already been legalized into the constant pool?
2594 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002595 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002596 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002597 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002598 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002599 }
2600 }
2601 return false;
2602}
2603
Evan Chenga8e29892007-01-19 07:51:42 +00002604/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2605/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002606SDValue
2607ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002608 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002609 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002610 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002611 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002612 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002613 // Constant does not fit, try adjusting it by one?
2614 switch (CC) {
2615 default: break;
2616 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002617 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002618 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002619 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002620 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002621 }
2622 break;
2623 case ISD::SETULT:
2624 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002625 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002626 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002627 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002628 }
2629 break;
2630 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002631 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002632 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002633 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002634 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002635 }
2636 break;
2637 case ISD::SETULE:
2638 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002639 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002640 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002641 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002642 }
2643 break;
2644 }
2645 }
2646 }
2647
2648 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002649 ARMISD::NodeType CompareType;
2650 switch (CondCode) {
2651 default:
2652 CompareType = ARMISD::CMP;
2653 break;
2654 case ARMCC::EQ:
2655 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002656 // Uses only Z Flag
2657 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002658 break;
2659 }
Evan Cheng218977b2010-07-13 19:27:42 +00002660 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002661 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002662}
2663
2664/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002665SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002666ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002667 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002668 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002669 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002670 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002671 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002672 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2673 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002674}
2675
Bob Wilson79f56c92011-03-08 01:17:20 +00002676/// duplicateCmp - Glue values can have only one use, so this function
2677/// duplicates a comparison node.
2678SDValue
2679ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2680 unsigned Opc = Cmp.getOpcode();
2681 DebugLoc DL = Cmp.getDebugLoc();
2682 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2683 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2684
2685 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2686 Cmp = Cmp.getOperand(0);
2687 Opc = Cmp.getOpcode();
2688 if (Opc == ARMISD::CMPFP)
2689 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2690 else {
2691 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2692 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2693 }
2694 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2695}
2696
Bill Wendlingde2b1512010-08-11 08:43:16 +00002697SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2698 SDValue Cond = Op.getOperand(0);
2699 SDValue SelectTrue = Op.getOperand(1);
2700 SDValue SelectFalse = Op.getOperand(2);
2701 DebugLoc dl = Op.getDebugLoc();
2702
2703 // Convert:
2704 //
2705 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2706 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2707 //
2708 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2709 const ConstantSDNode *CMOVTrue =
2710 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2711 const ConstantSDNode *CMOVFalse =
2712 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2713
2714 if (CMOVTrue && CMOVFalse) {
2715 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2716 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2717
2718 SDValue True;
2719 SDValue False;
2720 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2721 True = SelectTrue;
2722 False = SelectFalse;
2723 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2724 True = SelectFalse;
2725 False = SelectTrue;
2726 }
2727
2728 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002729 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002730 SDValue ARMcc = Cond.getOperand(2);
2731 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002732 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002733 assert(True.getValueType() == VT);
2734 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002735 }
2736 }
2737 }
2738
2739 return DAG.getSelectCC(dl, Cond,
2740 DAG.getConstant(0, Cond.getValueType()),
2741 SelectTrue, SelectFalse, ISD::SETNE);
2742}
2743
Dan Gohmand858e902010-04-17 15:26:15 +00002744SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002745 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002746 SDValue LHS = Op.getOperand(0);
2747 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002748 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002749 SDValue TrueVal = Op.getOperand(2);
2750 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002751 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002752
Owen Anderson825b72b2009-08-11 20:47:22 +00002753 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002754 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002755 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002756 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Evan Chenge721f5c2011-07-13 00:42:17 +00002757 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002758 }
2759
2760 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002761 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002762
Evan Cheng218977b2010-07-13 19:27:42 +00002763 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2764 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002765 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002766 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002767 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002768 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002769 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002770 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002771 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002772 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002773 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002774 }
2775 return Result;
2776}
2777
Evan Cheng218977b2010-07-13 19:27:42 +00002778/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2779/// to morph to an integer compare sequence.
2780static bool canChangeToInt(SDValue Op, bool &SeenZero,
2781 const ARMSubtarget *Subtarget) {
2782 SDNode *N = Op.getNode();
2783 if (!N->hasOneUse())
2784 // Otherwise it requires moving the value from fp to integer registers.
2785 return false;
2786 if (!N->getNumValues())
2787 return false;
2788 EVT VT = Op.getValueType();
2789 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2790 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2791 // vmrs are very slow, e.g. cortex-a8.
2792 return false;
2793
2794 if (isFloatingPointZero(Op)) {
2795 SeenZero = true;
2796 return true;
2797 }
2798 return ISD::isNormalLoad(N);
2799}
2800
2801static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2802 if (isFloatingPointZero(Op))
2803 return DAG.getConstant(0, MVT::i32);
2804
2805 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2806 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002807 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002808 Ld->isVolatile(), Ld->isNonTemporal(),
2809 Ld->getAlignment());
2810
2811 llvm_unreachable("Unknown VFP cmp argument!");
2812}
2813
2814static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2815 SDValue &RetVal1, SDValue &RetVal2) {
2816 if (isFloatingPointZero(Op)) {
2817 RetVal1 = DAG.getConstant(0, MVT::i32);
2818 RetVal2 = DAG.getConstant(0, MVT::i32);
2819 return;
2820 }
2821
2822 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2823 SDValue Ptr = Ld->getBasePtr();
2824 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2825 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002826 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002827 Ld->isVolatile(), Ld->isNonTemporal(),
2828 Ld->getAlignment());
2829
2830 EVT PtrType = Ptr.getValueType();
2831 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2832 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2833 PtrType, Ptr, DAG.getConstant(4, PtrType));
2834 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2835 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002836 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002837 Ld->isVolatile(), Ld->isNonTemporal(),
2838 NewAlign);
2839 return;
2840 }
2841
2842 llvm_unreachable("Unknown VFP cmp argument!");
2843}
2844
2845/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2846/// f32 and even f64 comparisons to integer ones.
2847SDValue
2848ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2849 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002850 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002851 SDValue LHS = Op.getOperand(2);
2852 SDValue RHS = Op.getOperand(3);
2853 SDValue Dest = Op.getOperand(4);
2854 DebugLoc dl = Op.getDebugLoc();
2855
2856 bool SeenZero = false;
2857 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2858 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002859 // If one of the operand is zero, it's safe to ignore the NaN case since
2860 // we only care about equality comparisons.
2861 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002862 // If unsafe fp math optimization is enabled and there are no other uses of
2863 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002864 // to an integer comparison.
2865 if (CC == ISD::SETOEQ)
2866 CC = ISD::SETEQ;
2867 else if (CC == ISD::SETUNE)
2868 CC = ISD::SETNE;
2869
2870 SDValue ARMcc;
2871 if (LHS.getValueType() == MVT::f32) {
2872 LHS = bitcastf32Toi32(LHS, DAG);
2873 RHS = bitcastf32Toi32(RHS, DAG);
2874 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2875 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2876 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2877 Chain, Dest, ARMcc, CCR, Cmp);
2878 }
2879
2880 SDValue LHS1, LHS2;
2881 SDValue RHS1, RHS2;
2882 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2883 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2884 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2885 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002886 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002887 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2888 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2889 }
2890
2891 return SDValue();
2892}
2893
2894SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2895 SDValue Chain = Op.getOperand(0);
2896 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2897 SDValue LHS = Op.getOperand(2);
2898 SDValue RHS = Op.getOperand(3);
2899 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002900 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002901
Owen Anderson825b72b2009-08-11 20:47:22 +00002902 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002903 SDValue ARMcc;
2904 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002905 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002906 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002907 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002908 }
2909
Owen Anderson825b72b2009-08-11 20:47:22 +00002910 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002911
2912 if (UnsafeFPMath &&
2913 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2914 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2915 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2916 if (Result.getNode())
2917 return Result;
2918 }
2919
Evan Chenga8e29892007-01-19 07:51:42 +00002920 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002921 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002922
Evan Cheng218977b2010-07-13 19:27:42 +00002923 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2924 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002925 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002926 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002927 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002928 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002929 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002930 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2931 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002932 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002933 }
2934 return Res;
2935}
2936
Dan Gohmand858e902010-04-17 15:26:15 +00002937SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002938 SDValue Chain = Op.getOperand(0);
2939 SDValue Table = Op.getOperand(1);
2940 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002941 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002942
Owen Andersone50ed302009-08-10 22:56:29 +00002943 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002944 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2945 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002946 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002947 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002948 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002949 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2950 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002951 if (Subtarget->isThumb2()) {
2952 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2953 // which does another jump to the destination. This also makes it easier
2954 // to translate it to TBB / TBH later.
2955 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002956 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002957 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002958 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002959 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002960 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002961 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002962 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002963 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002964 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002965 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002966 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002967 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002968 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002969 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002970 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002971 }
Evan Chenga8e29892007-01-19 07:51:42 +00002972}
2973
Bob Wilson76a312b2010-03-19 22:51:32 +00002974static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2975 DebugLoc dl = Op.getDebugLoc();
2976 unsigned Opc;
2977
2978 switch (Op.getOpcode()) {
2979 default:
2980 assert(0 && "Invalid opcode!");
2981 case ISD::FP_TO_SINT:
2982 Opc = ARMISD::FTOSI;
2983 break;
2984 case ISD::FP_TO_UINT:
2985 Opc = ARMISD::FTOUI;
2986 break;
2987 }
2988 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002989 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002990}
2991
Cameron Zwarich3007d332011-03-29 21:41:55 +00002992static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2993 EVT VT = Op.getValueType();
2994 DebugLoc dl = Op.getDebugLoc();
2995
2996 EVT OperandVT = Op.getOperand(0).getValueType();
2997 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2998 if (VT != MVT::v4f32)
2999 return DAG.UnrollVectorOp(Op.getNode());
3000
3001 unsigned CastOpc;
3002 unsigned Opc;
3003 switch (Op.getOpcode()) {
3004 default:
3005 assert(0 && "Invalid opcode!");
3006 case ISD::SINT_TO_FP:
3007 CastOpc = ISD::SIGN_EXTEND;
3008 Opc = ISD::SINT_TO_FP;
3009 break;
3010 case ISD::UINT_TO_FP:
3011 CastOpc = ISD::ZERO_EXTEND;
3012 Opc = ISD::UINT_TO_FP;
3013 break;
3014 }
3015
3016 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3017 return DAG.getNode(Opc, dl, VT, Op);
3018}
3019
Bob Wilson76a312b2010-03-19 22:51:32 +00003020static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3021 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003022 if (VT.isVector())
3023 return LowerVectorINT_TO_FP(Op, DAG);
3024
Bob Wilson76a312b2010-03-19 22:51:32 +00003025 DebugLoc dl = Op.getDebugLoc();
3026 unsigned Opc;
3027
3028 switch (Op.getOpcode()) {
3029 default:
3030 assert(0 && "Invalid opcode!");
3031 case ISD::SINT_TO_FP:
3032 Opc = ARMISD::SITOF;
3033 break;
3034 case ISD::UINT_TO_FP:
3035 Opc = ARMISD::UITOF;
3036 break;
3037 }
3038
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003039 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003040 return DAG.getNode(Opc, dl, VT, Op);
3041}
3042
Evan Cheng515fe3a2010-07-08 02:08:50 +00003043SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003044 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003045 SDValue Tmp0 = Op.getOperand(0);
3046 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003047 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003048 EVT VT = Op.getValueType();
3049 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003050 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3051 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3052 bool UseNEON = !InGPR && Subtarget->hasNEON();
3053
3054 if (UseNEON) {
3055 // Use VBSL to copy the sign bit.
3056 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3057 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3058 DAG.getTargetConstant(EncodedVal, MVT::i32));
3059 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3060 if (VT == MVT::f64)
3061 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3062 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3063 DAG.getConstant(32, MVT::i32));
3064 else /*if (VT == MVT::f32)*/
3065 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3066 if (SrcVT == MVT::f32) {
3067 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3068 if (VT == MVT::f64)
3069 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3070 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3071 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003072 } else if (VT == MVT::f32)
3073 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3074 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3075 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003076 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3077 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3078
3079 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3080 MVT::i32);
3081 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3082 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3083 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003084
Evan Chenge573fb32011-02-23 02:24:55 +00003085 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3086 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3087 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003088 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003089 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3090 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3091 DAG.getConstant(0, MVT::i32));
3092 } else {
3093 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3094 }
3095
3096 return Res;
3097 }
Evan Chengc143dd42011-02-11 02:28:55 +00003098
3099 // Bitcast operand 1 to i32.
3100 if (SrcVT == MVT::f64)
3101 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3102 &Tmp1, 1).getValue(1);
3103 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3104
Evan Chenge573fb32011-02-23 02:24:55 +00003105 // Or in the signbit with integer operations.
3106 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3107 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3108 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3109 if (VT == MVT::f32) {
3110 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3111 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3112 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3113 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003114 }
3115
Evan Chenge573fb32011-02-23 02:24:55 +00003116 // f64: Or the high part with signbit and then combine two parts.
3117 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3118 &Tmp0, 1);
3119 SDValue Lo = Tmp0.getValue(0);
3120 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3121 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3122 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003123}
3124
Evan Cheng2457f2c2010-05-22 01:47:14 +00003125SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3126 MachineFunction &MF = DAG.getMachineFunction();
3127 MachineFrameInfo *MFI = MF.getFrameInfo();
3128 MFI->setReturnAddressIsTaken(true);
3129
3130 EVT VT = Op.getValueType();
3131 DebugLoc dl = Op.getDebugLoc();
3132 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3133 if (Depth) {
3134 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3135 SDValue Offset = DAG.getConstant(4, MVT::i32);
3136 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3137 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003138 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003139 }
3140
3141 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003142 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003143 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3144}
3145
Dan Gohmand858e902010-04-17 15:26:15 +00003146SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003147 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3148 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003149
Owen Andersone50ed302009-08-10 22:56:29 +00003150 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003151 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3152 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003153 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003154 ? ARM::R7 : ARM::R11;
3155 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3156 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003157 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3158 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003159 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003160 return FrameAddr;
3161}
3162
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003163/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003164/// expand a bit convert where either the source or destination type is i64 to
3165/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3166/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3167/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003168static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003169 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3170 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003171 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003172
Bob Wilson9f3f0612010-04-17 05:30:19 +00003173 // This function is only supposed to be called for i64 types, either as the
3174 // source or destination of the bit convert.
3175 EVT SrcVT = Op.getValueType();
3176 EVT DstVT = N->getValueType(0);
3177 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003178 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003179
Bob Wilson9f3f0612010-04-17 05:30:19 +00003180 // Turn i64->f64 into VMOVDRR.
3181 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003182 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3183 DAG.getConstant(0, MVT::i32));
3184 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3185 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003186 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003187 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003188 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003189
Jim Grosbache5165492009-11-09 00:11:35 +00003190 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003191 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3192 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3193 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3194 // Merge the pieces into a single i64 value.
3195 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3196 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003197
Bob Wilson9f3f0612010-04-17 05:30:19 +00003198 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003199}
3200
Bob Wilson5bafff32009-06-22 23:27:02 +00003201/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003202/// Zero vectors are used to represent vector negation and in those cases
3203/// will be implemented with the NEON VNEG instruction. However, VNEG does
3204/// not support i64 elements, so sometimes the zero vectors will need to be
3205/// explicitly constructed. Regardless, use a canonical VMOV to create the
3206/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003207static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003208 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003209 // The canonical modified immediate encoding of a zero vector is....0!
3210 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3211 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3212 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003213 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003214}
3215
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003216/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3217/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003218SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3219 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003220 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3221 EVT VT = Op.getValueType();
3222 unsigned VTBits = VT.getSizeInBits();
3223 DebugLoc dl = Op.getDebugLoc();
3224 SDValue ShOpLo = Op.getOperand(0);
3225 SDValue ShOpHi = Op.getOperand(1);
3226 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003227 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003228 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003229
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003230 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3231
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003232 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3233 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3234 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3235 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3236 DAG.getConstant(VTBits, MVT::i32));
3237 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3238 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003239 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003240
3241 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3242 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003243 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003244 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003245 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003246 CCR, Cmp);
3247
3248 SDValue Ops[2] = { Lo, Hi };
3249 return DAG.getMergeValues(Ops, 2, dl);
3250}
3251
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003252/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3253/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003254SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3255 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003256 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3257 EVT VT = Op.getValueType();
3258 unsigned VTBits = VT.getSizeInBits();
3259 DebugLoc dl = Op.getDebugLoc();
3260 SDValue ShOpLo = Op.getOperand(0);
3261 SDValue ShOpHi = Op.getOperand(1);
3262 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003263 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003264
3265 assert(Op.getOpcode() == ISD::SHL_PARTS);
3266 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3267 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3268 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3269 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3270 DAG.getConstant(VTBits, MVT::i32));
3271 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3272 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3273
3274 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3275 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3276 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003277 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003278 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003279 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003280 CCR, Cmp);
3281
3282 SDValue Ops[2] = { Lo, Hi };
3283 return DAG.getMergeValues(Ops, 2, dl);
3284}
3285
Jim Grosbach4725ca72010-09-08 03:54:02 +00003286SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003287 SelectionDAG &DAG) const {
3288 // The rounding mode is in bits 23:22 of the FPSCR.
3289 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3290 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3291 // so that the shift + and get folded into a bitfield extract.
3292 DebugLoc dl = Op.getDebugLoc();
3293 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3294 DAG.getConstant(Intrinsic::arm_get_fpscr,
3295 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003296 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003297 DAG.getConstant(1U << 22, MVT::i32));
3298 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3299 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003300 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003301 DAG.getConstant(3, MVT::i32));
3302}
3303
Jim Grosbach3482c802010-01-18 19:58:49 +00003304static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3305 const ARMSubtarget *ST) {
3306 EVT VT = N->getValueType(0);
3307 DebugLoc dl = N->getDebugLoc();
3308
3309 if (!ST->hasV6T2Ops())
3310 return SDValue();
3311
3312 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3313 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3314}
3315
Bob Wilson5bafff32009-06-22 23:27:02 +00003316static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3317 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003318 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003319 DebugLoc dl = N->getDebugLoc();
3320
Bob Wilsond5448bb2010-11-18 21:16:28 +00003321 if (!VT.isVector())
3322 return SDValue();
3323
Bob Wilson5bafff32009-06-22 23:27:02 +00003324 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003325 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003326
Bob Wilsond5448bb2010-11-18 21:16:28 +00003327 // Left shifts translate directly to the vshiftu intrinsic.
3328 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003329 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003330 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3331 N->getOperand(0), N->getOperand(1));
3332
3333 assert((N->getOpcode() == ISD::SRA ||
3334 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3335
3336 // NEON uses the same intrinsics for both left and right shifts. For
3337 // right shifts, the shift amounts are negative, so negate the vector of
3338 // shift amounts.
3339 EVT ShiftVT = N->getOperand(1).getValueType();
3340 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3341 getZeroVector(ShiftVT, DAG, dl),
3342 N->getOperand(1));
3343 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3344 Intrinsic::arm_neon_vshifts :
3345 Intrinsic::arm_neon_vshiftu);
3346 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3347 DAG.getConstant(vshiftInt, MVT::i32),
3348 N->getOperand(0), NegatedCount);
3349}
3350
3351static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3352 const ARMSubtarget *ST) {
3353 EVT VT = N->getValueType(0);
3354 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003355
Eli Friedmance392eb2009-08-22 03:13:10 +00003356 // We can get here for a node like i32 = ISD::SHL i32, i64
3357 if (VT != MVT::i64)
3358 return SDValue();
3359
3360 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003361 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003362
Chris Lattner27a6c732007-11-24 07:07:01 +00003363 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3364 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003365 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003366 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003367
Chris Lattner27a6c732007-11-24 07:07:01 +00003368 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003369 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003370
Chris Lattner27a6c732007-11-24 07:07:01 +00003371 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003373 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003374 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003375 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003376
Chris Lattner27a6c732007-11-24 07:07:01 +00003377 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3378 // captures the result into a carry flag.
3379 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003380 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003381
Chris Lattner27a6c732007-11-24 07:07:01 +00003382 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003383 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003384
Chris Lattner27a6c732007-11-24 07:07:01 +00003385 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003386 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003387}
3388
Bob Wilson5bafff32009-06-22 23:27:02 +00003389static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3390 SDValue TmpOp0, TmpOp1;
3391 bool Invert = false;
3392 bool Swap = false;
3393 unsigned Opc = 0;
3394
3395 SDValue Op0 = Op.getOperand(0);
3396 SDValue Op1 = Op.getOperand(1);
3397 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003398 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003399 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3400 DebugLoc dl = Op.getDebugLoc();
3401
3402 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3403 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003404 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003405 case ISD::SETUNE:
3406 case ISD::SETNE: Invert = true; // Fallthrough
3407 case ISD::SETOEQ:
3408 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3409 case ISD::SETOLT:
3410 case ISD::SETLT: Swap = true; // Fallthrough
3411 case ISD::SETOGT:
3412 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3413 case ISD::SETOLE:
3414 case ISD::SETLE: Swap = true; // Fallthrough
3415 case ISD::SETOGE:
3416 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3417 case ISD::SETUGE: Swap = true; // Fallthrough
3418 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3419 case ISD::SETUGT: Swap = true; // Fallthrough
3420 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3421 case ISD::SETUEQ: Invert = true; // Fallthrough
3422 case ISD::SETONE:
3423 // Expand this to (OLT | OGT).
3424 TmpOp0 = Op0;
3425 TmpOp1 = Op1;
3426 Opc = ISD::OR;
3427 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3428 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3429 break;
3430 case ISD::SETUO: Invert = true; // Fallthrough
3431 case ISD::SETO:
3432 // Expand this to (OLT | OGE).
3433 TmpOp0 = Op0;
3434 TmpOp1 = Op1;
3435 Opc = ISD::OR;
3436 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3437 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3438 break;
3439 }
3440 } else {
3441 // Integer comparisons.
3442 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003443 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003444 case ISD::SETNE: Invert = true;
3445 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3446 case ISD::SETLT: Swap = true;
3447 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3448 case ISD::SETLE: Swap = true;
3449 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3450 case ISD::SETULT: Swap = true;
3451 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3452 case ISD::SETULE: Swap = true;
3453 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3454 }
3455
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003456 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003457 if (Opc == ARMISD::VCEQ) {
3458
3459 SDValue AndOp;
3460 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3461 AndOp = Op0;
3462 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3463 AndOp = Op1;
3464
3465 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003466 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003467 AndOp = AndOp.getOperand(0);
3468
3469 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3470 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003471 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3472 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003473 Invert = !Invert;
3474 }
3475 }
3476 }
3477
3478 if (Swap)
3479 std::swap(Op0, Op1);
3480
Owen Andersonc24cb352010-11-08 23:21:22 +00003481 // If one of the operands is a constant vector zero, attempt to fold the
3482 // comparison to a specialized compare-against-zero form.
3483 SDValue SingleOp;
3484 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3485 SingleOp = Op0;
3486 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3487 if (Opc == ARMISD::VCGE)
3488 Opc = ARMISD::VCLEZ;
3489 else if (Opc == ARMISD::VCGT)
3490 Opc = ARMISD::VCLTZ;
3491 SingleOp = Op1;
3492 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003493
Owen Andersonc24cb352010-11-08 23:21:22 +00003494 SDValue Result;
3495 if (SingleOp.getNode()) {
3496 switch (Opc) {
3497 case ARMISD::VCEQ:
3498 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3499 case ARMISD::VCGE:
3500 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3501 case ARMISD::VCLEZ:
3502 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3503 case ARMISD::VCGT:
3504 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3505 case ARMISD::VCLTZ:
3506 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3507 default:
3508 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3509 }
3510 } else {
3511 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3512 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003513
3514 if (Invert)
3515 Result = DAG.getNOT(dl, Result, VT);
3516
3517 return Result;
3518}
3519
Bob Wilsond3c42842010-06-14 22:19:57 +00003520/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3521/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003522/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003523static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3524 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003525 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003526 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003527
Bob Wilson827b2102010-06-15 19:05:35 +00003528 // SplatBitSize is set to the smallest size that splats the vector, so a
3529 // zero vector will always have SplatBitSize == 8. However, NEON modified
3530 // immediate instructions others than VMOV do not support the 8-bit encoding
3531 // of a zero vector, and the default encoding of zero is supposed to be the
3532 // 32-bit version.
3533 if (SplatBits == 0)
3534 SplatBitSize = 32;
3535
Bob Wilson5bafff32009-06-22 23:27:02 +00003536 switch (SplatBitSize) {
3537 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003538 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003539 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003540 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003541 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003542 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003543 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003544 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003545 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003546
3547 case 16:
3548 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003549 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003550 if ((SplatBits & ~0xff) == 0) {
3551 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003552 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003553 Imm = SplatBits;
3554 break;
3555 }
3556 if ((SplatBits & ~0xff00) == 0) {
3557 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003558 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003559 Imm = SplatBits >> 8;
3560 break;
3561 }
3562 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003563
3564 case 32:
3565 // NEON's 32-bit VMOV supports splat values where:
3566 // * only one byte is nonzero, or
3567 // * the least significant byte is 0xff and the second byte is nonzero, or
3568 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003569 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003570 if ((SplatBits & ~0xff) == 0) {
3571 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003572 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003573 Imm = SplatBits;
3574 break;
3575 }
3576 if ((SplatBits & ~0xff00) == 0) {
3577 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003578 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003579 Imm = SplatBits >> 8;
3580 break;
3581 }
3582 if ((SplatBits & ~0xff0000) == 0) {
3583 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003584 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003585 Imm = SplatBits >> 16;
3586 break;
3587 }
3588 if ((SplatBits & ~0xff000000) == 0) {
3589 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003590 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003591 Imm = SplatBits >> 24;
3592 break;
3593 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003594
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003595 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3596 if (type == OtherModImm) return SDValue();
3597
Bob Wilson5bafff32009-06-22 23:27:02 +00003598 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003599 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3600 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003601 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003602 Imm = SplatBits >> 8;
3603 SplatBits |= 0xff;
3604 break;
3605 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003606
3607 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003608 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3609 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003610 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003611 Imm = SplatBits >> 16;
3612 SplatBits |= 0xffff;
3613 break;
3614 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003615
3616 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3617 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3618 // VMOV.I32. A (very) minor optimization would be to replicate the value
3619 // and fall through here to test for a valid 64-bit splat. But, then the
3620 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003621 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003622
3623 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003624 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003625 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003626 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003627 uint64_t BitMask = 0xff;
3628 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003629 unsigned ImmMask = 1;
3630 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003631 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003632 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003633 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003634 Imm |= ImmMask;
3635 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003636 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003637 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003638 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003639 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003640 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003641 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003642 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003643 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003644 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003645 break;
3646 }
3647
Bob Wilson1a913ed2010-06-11 21:34:50 +00003648 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003649 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003650 return SDValue();
3651 }
3652
Bob Wilsoncba270d2010-07-13 21:16:48 +00003653 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3654 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003655}
3656
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003657static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3658 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003659 unsigned NumElts = VT.getVectorNumElements();
3660 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003661
3662 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3663 if (M[0] < 0)
3664 return false;
3665
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003666 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003667
3668 // If this is a VEXT shuffle, the immediate value is the index of the first
3669 // element. The other shuffle indices must be the successive elements after
3670 // the first one.
3671 unsigned ExpectedElt = Imm;
3672 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003673 // Increment the expected index. If it wraps around, it may still be
3674 // a VEXT but the source vectors must be swapped.
3675 ExpectedElt += 1;
3676 if (ExpectedElt == NumElts * 2) {
3677 ExpectedElt = 0;
3678 ReverseVEXT = true;
3679 }
3680
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003681 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003682 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003683 return false;
3684 }
3685
3686 // Adjust the index value if the source operands will be swapped.
3687 if (ReverseVEXT)
3688 Imm -= NumElts;
3689
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003690 return true;
3691}
3692
Bob Wilson8bb9e482009-07-26 00:39:34 +00003693/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3694/// instruction with the specified blocksize. (The order of the elements
3695/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003696static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3697 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003698 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3699 "Only possible block sizes for VREV are: 16, 32, 64");
3700
Bob Wilson8bb9e482009-07-26 00:39:34 +00003701 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003702 if (EltSz == 64)
3703 return false;
3704
3705 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003706 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003707 // If the first shuffle index is UNDEF, be optimistic.
3708 if (M[0] < 0)
3709 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003710
3711 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3712 return false;
3713
3714 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003715 if (M[i] < 0) continue; // ignore UNDEF indices
3716 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003717 return false;
3718 }
3719
3720 return true;
3721}
3722
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003723static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3724 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3725 // range, then 0 is placed into the resulting vector. So pretty much any mask
3726 // of 8 elements can work here.
3727 return VT == MVT::v8i8 && M.size() == 8;
3728}
3729
Bob Wilsonc692cb72009-08-21 20:54:19 +00003730static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3731 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003732 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3733 if (EltSz == 64)
3734 return false;
3735
Bob Wilsonc692cb72009-08-21 20:54:19 +00003736 unsigned NumElts = VT.getVectorNumElements();
3737 WhichResult = (M[0] == 0 ? 0 : 1);
3738 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003739 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3740 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003741 return false;
3742 }
3743 return true;
3744}
3745
Bob Wilson324f4f12009-12-03 06:40:55 +00003746/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3747/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3748/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3749static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3750 unsigned &WhichResult) {
3751 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3752 if (EltSz == 64)
3753 return false;
3754
3755 unsigned NumElts = VT.getVectorNumElements();
3756 WhichResult = (M[0] == 0 ? 0 : 1);
3757 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003758 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3759 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003760 return false;
3761 }
3762 return true;
3763}
3764
Bob Wilsonc692cb72009-08-21 20:54:19 +00003765static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3766 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003767 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3768 if (EltSz == 64)
3769 return false;
3770
Bob Wilsonc692cb72009-08-21 20:54:19 +00003771 unsigned NumElts = VT.getVectorNumElements();
3772 WhichResult = (M[0] == 0 ? 0 : 1);
3773 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003774 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003775 if ((unsigned) M[i] != 2 * i + WhichResult)
3776 return false;
3777 }
3778
3779 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003780 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003781 return false;
3782
3783 return true;
3784}
3785
Bob Wilson324f4f12009-12-03 06:40:55 +00003786/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3787/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3788/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3789static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3790 unsigned &WhichResult) {
3791 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3792 if (EltSz == 64)
3793 return false;
3794
3795 unsigned Half = VT.getVectorNumElements() / 2;
3796 WhichResult = (M[0] == 0 ? 0 : 1);
3797 for (unsigned j = 0; j != 2; ++j) {
3798 unsigned Idx = WhichResult;
3799 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003800 int MIdx = M[i + j * Half];
3801 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003802 return false;
3803 Idx += 2;
3804 }
3805 }
3806
3807 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3808 if (VT.is64BitVector() && EltSz == 32)
3809 return false;
3810
3811 return true;
3812}
3813
Bob Wilsonc692cb72009-08-21 20:54:19 +00003814static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3815 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003816 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3817 if (EltSz == 64)
3818 return false;
3819
Bob Wilsonc692cb72009-08-21 20:54:19 +00003820 unsigned NumElts = VT.getVectorNumElements();
3821 WhichResult = (M[0] == 0 ? 0 : 1);
3822 unsigned Idx = WhichResult * NumElts / 2;
3823 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003824 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3825 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003826 return false;
3827 Idx += 1;
3828 }
3829
3830 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003831 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003832 return false;
3833
3834 return true;
3835}
3836
Bob Wilson324f4f12009-12-03 06:40:55 +00003837/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3838/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3839/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3840static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3841 unsigned &WhichResult) {
3842 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3843 if (EltSz == 64)
3844 return false;
3845
3846 unsigned NumElts = VT.getVectorNumElements();
3847 WhichResult = (M[0] == 0 ? 0 : 1);
3848 unsigned Idx = WhichResult * NumElts / 2;
3849 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003850 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3851 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003852 return false;
3853 Idx += 1;
3854 }
3855
3856 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3857 if (VT.is64BitVector() && EltSz == 32)
3858 return false;
3859
3860 return true;
3861}
3862
Dale Johannesenf630c712010-07-29 20:10:08 +00003863// If N is an integer constant that can be moved into a register in one
3864// instruction, return an SDValue of such a constant (will become a MOV
3865// instruction). Otherwise return null.
3866static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3867 const ARMSubtarget *ST, DebugLoc dl) {
3868 uint64_t Val;
3869 if (!isa<ConstantSDNode>(N))
3870 return SDValue();
3871 Val = cast<ConstantSDNode>(N)->getZExtValue();
3872
3873 if (ST->isThumb1Only()) {
3874 if (Val <= 255 || ~Val <= 255)
3875 return DAG.getConstant(Val, MVT::i32);
3876 } else {
3877 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3878 return DAG.getConstant(Val, MVT::i32);
3879 }
3880 return SDValue();
3881}
3882
Bob Wilson5bafff32009-06-22 23:27:02 +00003883// If this is a case we can't handle, return null and let the default
3884// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003885SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3886 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003887 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003888 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003889 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003890
3891 APInt SplatBits, SplatUndef;
3892 unsigned SplatBitSize;
3893 bool HasAnyUndefs;
3894 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003895 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003896 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003897 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003898 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003899 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003900 DAG, VmovVT, VT.is128BitVector(),
3901 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003902 if (Val.getNode()) {
3903 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003904 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003905 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003906
3907 // Try an immediate VMVN.
3908 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3909 ((1LL << SplatBitSize) - 1));
3910 Val = isNEONModifiedImm(NegatedImm,
3911 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003912 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003913 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003914 if (Val.getNode()) {
3915 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003916 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003917 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003918 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003919 }
3920
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003921 // Scan through the operands to see if only one value is used.
3922 unsigned NumElts = VT.getVectorNumElements();
3923 bool isOnlyLowElement = true;
3924 bool usesOnlyOneValue = true;
3925 bool isConstant = true;
3926 SDValue Value;
3927 for (unsigned i = 0; i < NumElts; ++i) {
3928 SDValue V = Op.getOperand(i);
3929 if (V.getOpcode() == ISD::UNDEF)
3930 continue;
3931 if (i > 0)
3932 isOnlyLowElement = false;
3933 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3934 isConstant = false;
3935
3936 if (!Value.getNode())
3937 Value = V;
3938 else if (V != Value)
3939 usesOnlyOneValue = false;
3940 }
3941
3942 if (!Value.getNode())
3943 return DAG.getUNDEF(VT);
3944
3945 if (isOnlyLowElement)
3946 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3947
Dale Johannesenf630c712010-07-29 20:10:08 +00003948 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3949
Dale Johannesen575cd142010-10-19 20:00:17 +00003950 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3951 // i32 and try again.
3952 if (usesOnlyOneValue && EltSize <= 32) {
3953 if (!isConstant)
3954 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3955 if (VT.getVectorElementType().isFloatingPoint()) {
3956 SmallVector<SDValue, 8> Ops;
3957 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003958 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003959 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003960 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3961 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003962 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3963 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003964 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003965 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003966 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3967 if (Val.getNode())
3968 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003969 }
3970
3971 // If all elements are constants and the case above didn't get hit, fall back
3972 // to the default expansion, which will generate a load from the constant
3973 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003974 if (isConstant)
3975 return SDValue();
3976
Bob Wilson11a1dff2011-01-07 21:37:30 +00003977 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3978 if (NumElts >= 4) {
3979 SDValue shuffle = ReconstructShuffle(Op, DAG);
3980 if (shuffle != SDValue())
3981 return shuffle;
3982 }
3983
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003984 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003985 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3986 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003987 if (EltSize >= 32) {
3988 // Do the expansion with floating-point types, since that is what the VFP
3989 // registers are defined to use, and since i64 is not legal.
3990 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3991 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003992 SmallVector<SDValue, 8> Ops;
3993 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003994 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003995 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003996 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003997 }
3998
3999 return SDValue();
4000}
4001
Bob Wilson11a1dff2011-01-07 21:37:30 +00004002// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004003// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004004SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4005 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004006 DebugLoc dl = Op.getDebugLoc();
4007 EVT VT = Op.getValueType();
4008 unsigned NumElts = VT.getVectorNumElements();
4009
4010 SmallVector<SDValue, 2> SourceVecs;
4011 SmallVector<unsigned, 2> MinElts;
4012 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004013
Bob Wilson11a1dff2011-01-07 21:37:30 +00004014 for (unsigned i = 0; i < NumElts; ++i) {
4015 SDValue V = Op.getOperand(i);
4016 if (V.getOpcode() == ISD::UNDEF)
4017 continue;
4018 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4019 // A shuffle can only come from building a vector from various
4020 // elements of other vectors.
4021 return SDValue();
4022 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004023
Bob Wilson11a1dff2011-01-07 21:37:30 +00004024 // Record this extraction against the appropriate vector if possible...
4025 SDValue SourceVec = V.getOperand(0);
4026 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4027 bool FoundSource = false;
4028 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4029 if (SourceVecs[j] == SourceVec) {
4030 if (MinElts[j] > EltNo)
4031 MinElts[j] = EltNo;
4032 if (MaxElts[j] < EltNo)
4033 MaxElts[j] = EltNo;
4034 FoundSource = true;
4035 break;
4036 }
4037 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004038
Bob Wilson11a1dff2011-01-07 21:37:30 +00004039 // Or record a new source if not...
4040 if (!FoundSource) {
4041 SourceVecs.push_back(SourceVec);
4042 MinElts.push_back(EltNo);
4043 MaxElts.push_back(EltNo);
4044 }
4045 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004046
Bob Wilson11a1dff2011-01-07 21:37:30 +00004047 // Currently only do something sane when at most two source vectors
4048 // involved.
4049 if (SourceVecs.size() > 2)
4050 return SDValue();
4051
4052 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4053 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004054
Bob Wilson11a1dff2011-01-07 21:37:30 +00004055 // This loop extracts the usage patterns of the source vectors
4056 // and prepares appropriate SDValues for a shuffle if possible.
4057 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4058 if (SourceVecs[i].getValueType() == VT) {
4059 // No VEXT necessary
4060 ShuffleSrcs[i] = SourceVecs[i];
4061 VEXTOffsets[i] = 0;
4062 continue;
4063 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4064 // It probably isn't worth padding out a smaller vector just to
4065 // break it down again in a shuffle.
4066 return SDValue();
4067 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004068
Bob Wilson11a1dff2011-01-07 21:37:30 +00004069 // Since only 64-bit and 128-bit vectors are legal on ARM and
4070 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004071 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4072 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004073
Bob Wilson11a1dff2011-01-07 21:37:30 +00004074 if (MaxElts[i] - MinElts[i] >= NumElts) {
4075 // Span too large for a VEXT to cope
4076 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004077 }
4078
Bob Wilson11a1dff2011-01-07 21:37:30 +00004079 if (MinElts[i] >= NumElts) {
4080 // The extraction can just take the second half
4081 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004082 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4083 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004084 DAG.getIntPtrConstant(NumElts));
4085 } else if (MaxElts[i] < NumElts) {
4086 // The extraction can just take the first half
4087 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004088 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4089 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004090 DAG.getIntPtrConstant(0));
4091 } else {
4092 // An actual VEXT is needed
4093 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004094 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4095 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004096 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004097 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4098 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004099 DAG.getIntPtrConstant(NumElts));
4100 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4101 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4102 }
4103 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004104
Bob Wilson11a1dff2011-01-07 21:37:30 +00004105 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004106
Bob Wilson11a1dff2011-01-07 21:37:30 +00004107 for (unsigned i = 0; i < NumElts; ++i) {
4108 SDValue Entry = Op.getOperand(i);
4109 if (Entry.getOpcode() == ISD::UNDEF) {
4110 Mask.push_back(-1);
4111 continue;
4112 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004113
Bob Wilson11a1dff2011-01-07 21:37:30 +00004114 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004115 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4116 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004117 if (ExtractVec == SourceVecs[0]) {
4118 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4119 } else {
4120 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4121 }
4122 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004123
Bob Wilson11a1dff2011-01-07 21:37:30 +00004124 // Final check before we try to produce nonsense...
4125 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004126 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4127 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004128
Bob Wilson11a1dff2011-01-07 21:37:30 +00004129 return SDValue();
4130}
4131
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004132/// isShuffleMaskLegal - Targets can use this to indicate that they only
4133/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4134/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4135/// are assumed to be legal.
4136bool
4137ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4138 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004139 if (VT.getVectorNumElements() == 4 &&
4140 (VT.is128BitVector() || VT.is64BitVector())) {
4141 unsigned PFIndexes[4];
4142 for (unsigned i = 0; i != 4; ++i) {
4143 if (M[i] < 0)
4144 PFIndexes[i] = 8;
4145 else
4146 PFIndexes[i] = M[i];
4147 }
4148
4149 // Compute the index in the perfect shuffle table.
4150 unsigned PFTableIndex =
4151 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4152 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4153 unsigned Cost = (PFEntry >> 30);
4154
4155 if (Cost <= 4)
4156 return true;
4157 }
4158
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004159 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004160 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004161
Bob Wilson53dd2452010-06-07 23:53:38 +00004162 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4163 return (EltSize >= 32 ||
4164 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004165 isVREVMask(M, VT, 64) ||
4166 isVREVMask(M, VT, 32) ||
4167 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004168 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004169 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004170 isVTRNMask(M, VT, WhichResult) ||
4171 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004172 isVZIPMask(M, VT, WhichResult) ||
4173 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4174 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4175 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004176}
4177
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004178/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4179/// the specified operations to build the shuffle.
4180static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4181 SDValue RHS, SelectionDAG &DAG,
4182 DebugLoc dl) {
4183 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4184 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4185 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4186
4187 enum {
4188 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4189 OP_VREV,
4190 OP_VDUP0,
4191 OP_VDUP1,
4192 OP_VDUP2,
4193 OP_VDUP3,
4194 OP_VEXT1,
4195 OP_VEXT2,
4196 OP_VEXT3,
4197 OP_VUZPL, // VUZP, left result
4198 OP_VUZPR, // VUZP, right result
4199 OP_VZIPL, // VZIP, left result
4200 OP_VZIPR, // VZIP, right result
4201 OP_VTRNL, // VTRN, left result
4202 OP_VTRNR // VTRN, right result
4203 };
4204
4205 if (OpNum == OP_COPY) {
4206 if (LHSID == (1*9+2)*9+3) return LHS;
4207 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4208 return RHS;
4209 }
4210
4211 SDValue OpLHS, OpRHS;
4212 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4213 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4214 EVT VT = OpLHS.getValueType();
4215
4216 switch (OpNum) {
4217 default: llvm_unreachable("Unknown shuffle opcode!");
4218 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004219 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004220 if (VT.getVectorElementType() == MVT::i32 ||
4221 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004222 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4223 // vrev <4 x i16> -> VREV32
4224 if (VT.getVectorElementType() == MVT::i16)
4225 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4226 // vrev <4 x i8> -> VREV16
4227 assert(VT.getVectorElementType() == MVT::i8);
4228 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004229 case OP_VDUP0:
4230 case OP_VDUP1:
4231 case OP_VDUP2:
4232 case OP_VDUP3:
4233 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004234 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004235 case OP_VEXT1:
4236 case OP_VEXT2:
4237 case OP_VEXT3:
4238 return DAG.getNode(ARMISD::VEXT, dl, VT,
4239 OpLHS, OpRHS,
4240 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4241 case OP_VUZPL:
4242 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004243 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004244 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4245 case OP_VZIPL:
4246 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004247 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004248 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4249 case OP_VTRNL:
4250 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004251 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4252 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004253 }
4254}
4255
Bill Wendling69a05a72011-03-14 23:02:38 +00004256static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4257 SmallVectorImpl<int> &ShuffleMask,
4258 SelectionDAG &DAG) {
4259 // Check to see if we can use the VTBL instruction.
4260 SDValue V1 = Op.getOperand(0);
4261 SDValue V2 = Op.getOperand(1);
4262 DebugLoc DL = Op.getDebugLoc();
4263
4264 SmallVector<SDValue, 8> VTBLMask;
4265 for (SmallVectorImpl<int>::iterator
4266 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4267 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4268
4269 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4270 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4271 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4272 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004273
Owen Anderson76706012011-04-05 21:48:57 +00004274 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004275 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4276 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004277}
4278
Bob Wilson5bafff32009-06-22 23:27:02 +00004279static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004280 SDValue V1 = Op.getOperand(0);
4281 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004282 DebugLoc dl = Op.getDebugLoc();
4283 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004284 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004285 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004286
Bob Wilson28865062009-08-13 02:13:04 +00004287 // Convert shuffles that are directly supported on NEON to target-specific
4288 // DAG nodes, instead of keeping them as shuffles and matching them again
4289 // during code selection. This is more efficient and avoids the possibility
4290 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004291 // FIXME: floating-point vectors should be canonicalized to integer vectors
4292 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004293 SVN->getMask(ShuffleMask);
4294
Bob Wilson53dd2452010-06-07 23:53:38 +00004295 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4296 if (EltSize <= 32) {
4297 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4298 int Lane = SVN->getSplatIndex();
4299 // If this is undef splat, generate it via "just" vdup, if possible.
4300 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004301
Bob Wilson53dd2452010-06-07 23:53:38 +00004302 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4303 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4304 }
4305 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4306 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004307 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004308
4309 bool ReverseVEXT;
4310 unsigned Imm;
4311 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4312 if (ReverseVEXT)
4313 std::swap(V1, V2);
4314 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4315 DAG.getConstant(Imm, MVT::i32));
4316 }
4317
4318 if (isVREVMask(ShuffleMask, VT, 64))
4319 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4320 if (isVREVMask(ShuffleMask, VT, 32))
4321 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4322 if (isVREVMask(ShuffleMask, VT, 16))
4323 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4324
4325 // Check for Neon shuffles that modify both input vectors in place.
4326 // If both results are used, i.e., if there are two shuffles with the same
4327 // source operands and with masks corresponding to both results of one of
4328 // these operations, DAG memoization will ensure that a single node is
4329 // used for both shuffles.
4330 unsigned WhichResult;
4331 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4332 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4333 V1, V2).getValue(WhichResult);
4334 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4335 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4336 V1, V2).getValue(WhichResult);
4337 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4338 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4339 V1, V2).getValue(WhichResult);
4340
4341 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4342 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4343 V1, V1).getValue(WhichResult);
4344 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4345 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4346 V1, V1).getValue(WhichResult);
4347 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4348 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4349 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004350 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004351
Bob Wilsonc692cb72009-08-21 20:54:19 +00004352 // If the shuffle is not directly supported and it has 4 elements, use
4353 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004354 unsigned NumElts = VT.getVectorNumElements();
4355 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004356 unsigned PFIndexes[4];
4357 for (unsigned i = 0; i != 4; ++i) {
4358 if (ShuffleMask[i] < 0)
4359 PFIndexes[i] = 8;
4360 else
4361 PFIndexes[i] = ShuffleMask[i];
4362 }
4363
4364 // Compute the index in the perfect shuffle table.
4365 unsigned PFTableIndex =
4366 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004367 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4368 unsigned Cost = (PFEntry >> 30);
4369
4370 if (Cost <= 4)
4371 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4372 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004373
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004374 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004375 if (EltSize >= 32) {
4376 // Do the expansion with floating-point types, since that is what the VFP
4377 // registers are defined to use, and since i64 is not legal.
4378 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4379 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004380 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4381 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004382 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004383 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004384 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004385 Ops.push_back(DAG.getUNDEF(EltVT));
4386 else
4387 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4388 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4389 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4390 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004391 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004392 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004393 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004394 }
4395
Bill Wendling69a05a72011-03-14 23:02:38 +00004396 if (VT == MVT::v8i8) {
4397 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4398 if (NewOp.getNode())
4399 return NewOp;
4400 }
4401
Bob Wilson22cac0d2009-08-14 05:16:33 +00004402 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004403}
4404
Bob Wilson5bafff32009-06-22 23:27:02 +00004405static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004406 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004407 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004408 if (!isa<ConstantSDNode>(Lane))
4409 return SDValue();
4410
4411 SDValue Vec = Op.getOperand(0);
4412 if (Op.getValueType() == MVT::i32 &&
4413 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4414 DebugLoc dl = Op.getDebugLoc();
4415 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4416 }
4417
4418 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004419}
4420
Bob Wilsona6d65862009-08-03 20:36:38 +00004421static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4422 // The only time a CONCAT_VECTORS operation can have legal types is when
4423 // two 64-bit vectors are concatenated to a 128-bit vector.
4424 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4425 "unexpected CONCAT_VECTORS");
4426 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004427 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004428 SDValue Op0 = Op.getOperand(0);
4429 SDValue Op1 = Op.getOperand(1);
4430 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004431 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004432 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004433 DAG.getIntPtrConstant(0));
4434 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004435 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004436 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004437 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004438 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004439}
4440
Bob Wilson626613d2010-11-23 19:38:38 +00004441/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4442/// element has been zero/sign-extended, depending on the isSigned parameter,
4443/// from an integer type half its size.
4444static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4445 bool isSigned) {
4446 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4447 EVT VT = N->getValueType(0);
4448 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4449 SDNode *BVN = N->getOperand(0).getNode();
4450 if (BVN->getValueType(0) != MVT::v4i32 ||
4451 BVN->getOpcode() != ISD::BUILD_VECTOR)
4452 return false;
4453 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4454 unsigned HiElt = 1 - LoElt;
4455 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4456 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4457 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4458 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4459 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4460 return false;
4461 if (isSigned) {
4462 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4463 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4464 return true;
4465 } else {
4466 if (Hi0->isNullValue() && Hi1->isNullValue())
4467 return true;
4468 }
4469 return false;
4470 }
4471
4472 if (N->getOpcode() != ISD::BUILD_VECTOR)
4473 return false;
4474
4475 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4476 SDNode *Elt = N->getOperand(i).getNode();
4477 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4478 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4479 unsigned HalfSize = EltSize / 2;
4480 if (isSigned) {
4481 int64_t SExtVal = C->getSExtValue();
4482 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4483 return false;
4484 } else {
4485 if ((C->getZExtValue() >> HalfSize) != 0)
4486 return false;
4487 }
4488 continue;
4489 }
4490 return false;
4491 }
4492
4493 return true;
4494}
4495
4496/// isSignExtended - Check if a node is a vector value that is sign-extended
4497/// or a constant BUILD_VECTOR with sign-extended elements.
4498static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4499 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4500 return true;
4501 if (isExtendedBUILD_VECTOR(N, DAG, true))
4502 return true;
4503 return false;
4504}
4505
4506/// isZeroExtended - Check if a node is a vector value that is zero-extended
4507/// or a constant BUILD_VECTOR with zero-extended elements.
4508static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4509 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4510 return true;
4511 if (isExtendedBUILD_VECTOR(N, DAG, false))
4512 return true;
4513 return false;
4514}
4515
4516/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4517/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004518static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4519 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4520 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004521 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4522 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4523 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4524 LD->isNonTemporal(), LD->getAlignment());
4525 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4526 // have been legalized as a BITCAST from v4i32.
4527 if (N->getOpcode() == ISD::BITCAST) {
4528 SDNode *BVN = N->getOperand(0).getNode();
4529 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4530 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4531 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4532 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4533 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4534 }
4535 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4536 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4537 EVT VT = N->getValueType(0);
4538 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4539 unsigned NumElts = VT.getVectorNumElements();
4540 MVT TruncVT = MVT::getIntegerVT(EltSize);
4541 SmallVector<SDValue, 8> Ops;
4542 for (unsigned i = 0; i != NumElts; ++i) {
4543 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4544 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004545 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004546 }
4547 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4548 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004549}
4550
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004551static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4552 unsigned Opcode = N->getOpcode();
4553 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4554 SDNode *N0 = N->getOperand(0).getNode();
4555 SDNode *N1 = N->getOperand(1).getNode();
4556 return N0->hasOneUse() && N1->hasOneUse() &&
4557 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4558 }
4559 return false;
4560}
4561
4562static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4563 unsigned Opcode = N->getOpcode();
4564 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4565 SDNode *N0 = N->getOperand(0).getNode();
4566 SDNode *N1 = N->getOperand(1).getNode();
4567 return N0->hasOneUse() && N1->hasOneUse() &&
4568 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4569 }
4570 return false;
4571}
4572
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004573static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4574 // Multiplications are only custom-lowered for 128-bit vectors so that
4575 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4576 EVT VT = Op.getValueType();
4577 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4578 SDNode *N0 = Op.getOperand(0).getNode();
4579 SDNode *N1 = Op.getOperand(1).getNode();
4580 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004581 bool isMLA = false;
4582 bool isN0SExt = isSignExtended(N0, DAG);
4583 bool isN1SExt = isSignExtended(N1, DAG);
4584 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004585 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004586 else {
4587 bool isN0ZExt = isZeroExtended(N0, DAG);
4588 bool isN1ZExt = isZeroExtended(N1, DAG);
4589 if (isN0ZExt && isN1ZExt)
4590 NewOpc = ARMISD::VMULLu;
4591 else if (isN1SExt || isN1ZExt) {
4592 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4593 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4594 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4595 NewOpc = ARMISD::VMULLs;
4596 isMLA = true;
4597 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4598 NewOpc = ARMISD::VMULLu;
4599 isMLA = true;
4600 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4601 std::swap(N0, N1);
4602 NewOpc = ARMISD::VMULLu;
4603 isMLA = true;
4604 }
4605 }
4606
4607 if (!NewOpc) {
4608 if (VT == MVT::v2i64)
4609 // Fall through to expand this. It is not legal.
4610 return SDValue();
4611 else
4612 // Other vector multiplications are legal.
4613 return Op;
4614 }
4615 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004616
4617 // Legalize to a VMULL instruction.
4618 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004619 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004620 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004621 if (!isMLA) {
4622 Op0 = SkipExtension(N0, DAG);
4623 assert(Op0.getValueType().is64BitVector() &&
4624 Op1.getValueType().is64BitVector() &&
4625 "unexpected types for extended operands to VMULL");
4626 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4627 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004628
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004629 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4630 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4631 // vmull q0, d4, d6
4632 // vmlal q0, d5, d6
4633 // is faster than
4634 // vaddl q0, d4, d5
4635 // vmovl q1, d6
4636 // vmul q0, q0, q1
4637 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4638 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4639 EVT Op1VT = Op1.getValueType();
4640 return DAG.getNode(N0->getOpcode(), DL, VT,
4641 DAG.getNode(NewOpc, DL, VT,
4642 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4643 DAG.getNode(NewOpc, DL, VT,
4644 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004645}
4646
Owen Anderson76706012011-04-05 21:48:57 +00004647static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004648LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4649 // Convert to float
4650 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4651 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4652 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4653 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4654 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4655 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4656 // Get reciprocal estimate.
4657 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004658 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004659 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4660 // Because char has a smaller range than uchar, we can actually get away
4661 // without any newton steps. This requires that we use a weird bias
4662 // of 0xb000, however (again, this has been exhaustively tested).
4663 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4664 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4665 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4666 Y = DAG.getConstant(0xb000, MVT::i32);
4667 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4668 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4669 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4670 // Convert back to short.
4671 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4672 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4673 return X;
4674}
4675
Owen Anderson76706012011-04-05 21:48:57 +00004676static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004677LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4678 SDValue N2;
4679 // Convert to float.
4680 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4681 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4682 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4683 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4684 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4685 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004686
Nate Begeman7973f352011-02-11 20:53:29 +00004687 // Use reciprocal estimate and one refinement step.
4688 // float4 recip = vrecpeq_f32(yf);
4689 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004690 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004691 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004692 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004693 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4694 N1, N2);
4695 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4696 // Because short has a smaller range than ushort, we can actually get away
4697 // with only a single newton step. This requires that we use a weird bias
4698 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004699 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004700 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4701 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004702 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004703 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4704 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4705 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4706 // Convert back to integer and return.
4707 // return vmovn_s32(vcvt_s32_f32(result));
4708 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4709 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4710 return N0;
4711}
4712
4713static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4714 EVT VT = Op.getValueType();
4715 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4716 "unexpected type for custom-lowering ISD::SDIV");
4717
4718 DebugLoc dl = Op.getDebugLoc();
4719 SDValue N0 = Op.getOperand(0);
4720 SDValue N1 = Op.getOperand(1);
4721 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004722
Nate Begeman7973f352011-02-11 20:53:29 +00004723 if (VT == MVT::v8i8) {
4724 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4725 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004726
Nate Begeman7973f352011-02-11 20:53:29 +00004727 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4728 DAG.getIntPtrConstant(4));
4729 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004730 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004731 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4732 DAG.getIntPtrConstant(0));
4733 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4734 DAG.getIntPtrConstant(0));
4735
4736 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4737 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4738
4739 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4740 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004741
Nate Begeman7973f352011-02-11 20:53:29 +00004742 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4743 return N0;
4744 }
4745 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4746}
4747
4748static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4749 EVT VT = Op.getValueType();
4750 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4751 "unexpected type for custom-lowering ISD::UDIV");
4752
4753 DebugLoc dl = Op.getDebugLoc();
4754 SDValue N0 = Op.getOperand(0);
4755 SDValue N1 = Op.getOperand(1);
4756 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004757
Nate Begeman7973f352011-02-11 20:53:29 +00004758 if (VT == MVT::v8i8) {
4759 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4760 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004761
Nate Begeman7973f352011-02-11 20:53:29 +00004762 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4763 DAG.getIntPtrConstant(4));
4764 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004765 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004766 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4767 DAG.getIntPtrConstant(0));
4768 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4769 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004770
Nate Begeman7973f352011-02-11 20:53:29 +00004771 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4772 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004773
Nate Begeman7973f352011-02-11 20:53:29 +00004774 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4775 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004776
4777 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004778 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4779 N0);
4780 return N0;
4781 }
Owen Anderson76706012011-04-05 21:48:57 +00004782
Nate Begeman7973f352011-02-11 20:53:29 +00004783 // v4i16 sdiv ... Convert to float.
4784 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4785 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4786 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4787 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4788 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004789 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004790
4791 // Use reciprocal estimate and two refinement steps.
4792 // float4 recip = vrecpeq_f32(yf);
4793 // recip *= vrecpsq_f32(yf, recip);
4794 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004795 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004796 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004797 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004798 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004799 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004800 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004801 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004802 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004803 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004804 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4805 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4806 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4807 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004808 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004809 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4810 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4811 N1 = DAG.getConstant(2, MVT::i32);
4812 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4813 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4814 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4815 // Convert back to integer and return.
4816 // return vmovn_u32(vcvt_s32_f32(result));
4817 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4818 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4819 return N0;
4820}
4821
Dan Gohmand858e902010-04-17 15:26:15 +00004822SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004823 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004824 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004825 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004826 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004827 case ISD::GlobalAddress:
4828 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4829 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004830 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004831 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004832 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4833 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004834 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004835 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004836 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004837 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004838 case ISD::SINT_TO_FP:
4839 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4840 case ISD::FP_TO_SINT:
4841 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004842 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004843 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004844 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004845 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004846 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004847 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004848 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004849 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4850 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004851 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004852 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004853 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004854 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004855 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004856 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004857 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004858 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004859 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004860 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004861 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004862 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004863 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004864 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004865 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004866 case ISD::SDIV: return LowerSDIV(Op, DAG);
4867 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004868 }
Dan Gohman475871a2008-07-27 21:46:04 +00004869 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004870}
4871
Duncan Sands1607f052008-12-01 11:39:25 +00004872/// ReplaceNodeResults - Replace the results of node with an illegal result
4873/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004874void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4875 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004876 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004877 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004878 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004879 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004880 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004881 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004882 case ISD::BITCAST:
4883 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004884 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004885 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004886 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004887 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004888 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004889 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004890 if (Res.getNode())
4891 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004892}
Chris Lattner27a6c732007-11-24 07:07:01 +00004893
Evan Chenga8e29892007-01-19 07:51:42 +00004894//===----------------------------------------------------------------------===//
4895// ARM Scheduler Hooks
4896//===----------------------------------------------------------------------===//
4897
4898MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004899ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4900 MachineBasicBlock *BB,
4901 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004902 unsigned dest = MI->getOperand(0).getReg();
4903 unsigned ptr = MI->getOperand(1).getReg();
4904 unsigned oldval = MI->getOperand(2).getReg();
4905 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004906 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4907 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004908 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004909
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004910 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4911 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004912 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004913 : ARM::GPRRegisterClass);
4914
4915 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004916 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4917 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4918 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004919 }
4920
Jim Grosbach5278eb82009-12-11 01:42:04 +00004921 unsigned ldrOpc, strOpc;
4922 switch (Size) {
4923 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004924 case 1:
4925 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004926 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004927 break;
4928 case 2:
4929 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4930 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4931 break;
4932 case 4:
4933 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4934 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4935 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004936 }
4937
4938 MachineFunction *MF = BB->getParent();
4939 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4940 MachineFunction::iterator It = BB;
4941 ++It; // insert the new blocks after the current block
4942
4943 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4944 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4945 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4946 MF->insert(It, loop1MBB);
4947 MF->insert(It, loop2MBB);
4948 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004949
4950 // Transfer the remainder of BB and its successor edges to exitMBB.
4951 exitMBB->splice(exitMBB->begin(), BB,
4952 llvm::next(MachineBasicBlock::iterator(MI)),
4953 BB->end());
4954 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004955
4956 // thisMBB:
4957 // ...
4958 // fallthrough --> loop1MBB
4959 BB->addSuccessor(loop1MBB);
4960
4961 // loop1MBB:
4962 // ldrex dest, [ptr]
4963 // cmp dest, oldval
4964 // bne exitMBB
4965 BB = loop1MBB;
4966 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004967 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004968 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004969 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4970 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004971 BB->addSuccessor(loop2MBB);
4972 BB->addSuccessor(exitMBB);
4973
4974 // loop2MBB:
4975 // strex scratch, newval, [ptr]
4976 // cmp scratch, #0
4977 // bne loop1MBB
4978 BB = loop2MBB;
4979 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4980 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004981 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004982 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004983 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4984 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004985 BB->addSuccessor(loop1MBB);
4986 BB->addSuccessor(exitMBB);
4987
4988 // exitMBB:
4989 // ...
4990 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004991
Dan Gohman14152b42010-07-06 20:24:04 +00004992 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004993
Jim Grosbach5278eb82009-12-11 01:42:04 +00004994 return BB;
4995}
4996
4997MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004998ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4999 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005000 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5001 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5002
5003 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005004 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005005 MachineFunction::iterator It = BB;
5006 ++It;
5007
5008 unsigned dest = MI->getOperand(0).getReg();
5009 unsigned ptr = MI->getOperand(1).getReg();
5010 unsigned incr = MI->getOperand(2).getReg();
5011 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005012 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005013
5014 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5015 if (isThumb2) {
5016 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5017 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5018 }
5019
Jim Grosbachc3c23542009-12-14 04:22:04 +00005020 unsigned ldrOpc, strOpc;
5021 switch (Size) {
5022 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005023 case 1:
5024 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005025 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005026 break;
5027 case 2:
5028 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5029 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5030 break;
5031 case 4:
5032 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5033 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5034 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005035 }
5036
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005037 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5038 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5039 MF->insert(It, loopMBB);
5040 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005041
5042 // Transfer the remainder of BB and its successor edges to exitMBB.
5043 exitMBB->splice(exitMBB->begin(), BB,
5044 llvm::next(MachineBasicBlock::iterator(MI)),
5045 BB->end());
5046 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005047
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005048 TargetRegisterClass *TRC =
5049 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5050 unsigned scratch = MRI.createVirtualRegister(TRC);
5051 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005052
5053 // thisMBB:
5054 // ...
5055 // fallthrough --> loopMBB
5056 BB->addSuccessor(loopMBB);
5057
5058 // loopMBB:
5059 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005060 // <binop> scratch2, dest, incr
5061 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005062 // cmp scratch, #0
5063 // bne- loopMBB
5064 // fallthrough --> exitMBB
5065 BB = loopMBB;
5066 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005067 if (BinOpcode) {
5068 // operand order needs to go the other way for NAND
5069 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5070 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5071 addReg(incr).addReg(dest)).addReg(0);
5072 else
5073 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5074 addReg(dest).addReg(incr)).addReg(0);
5075 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005076
5077 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5078 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005079 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005080 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005081 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5082 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005083
5084 BB->addSuccessor(loopMBB);
5085 BB->addSuccessor(exitMBB);
5086
5087 // exitMBB:
5088 // ...
5089 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005090
Dan Gohman14152b42010-07-06 20:24:04 +00005091 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005092
Jim Grosbachc3c23542009-12-14 04:22:04 +00005093 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005094}
5095
Jim Grosbachf7da8822011-04-26 19:44:18 +00005096MachineBasicBlock *
5097ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5098 MachineBasicBlock *BB,
5099 unsigned Size,
5100 bool signExtend,
5101 ARMCC::CondCodes Cond) const {
5102 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5103
5104 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5105 MachineFunction *MF = BB->getParent();
5106 MachineFunction::iterator It = BB;
5107 ++It;
5108
5109 unsigned dest = MI->getOperand(0).getReg();
5110 unsigned ptr = MI->getOperand(1).getReg();
5111 unsigned incr = MI->getOperand(2).getReg();
5112 unsigned oldval = dest;
5113 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005114 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005115
5116 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5117 if (isThumb2) {
5118 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5119 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5120 }
5121
Jim Grosbachf7da8822011-04-26 19:44:18 +00005122 unsigned ldrOpc, strOpc, extendOpc;
5123 switch (Size) {
5124 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5125 case 1:
5126 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5127 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
5128 extendOpc = isThumb2 ? ARM::t2SXTBr : ARM::SXTBr;
5129 break;
5130 case 2:
5131 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5132 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5133 extendOpc = isThumb2 ? ARM::t2SXTHr : ARM::SXTHr;
5134 break;
5135 case 4:
5136 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5137 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5138 extendOpc = 0;
5139 break;
5140 }
5141
5142 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5143 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5144 MF->insert(It, loopMBB);
5145 MF->insert(It, exitMBB);
5146
5147 // Transfer the remainder of BB and its successor edges to exitMBB.
5148 exitMBB->splice(exitMBB->begin(), BB,
5149 llvm::next(MachineBasicBlock::iterator(MI)),
5150 BB->end());
5151 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5152
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005153 TargetRegisterClass *TRC =
5154 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5155 unsigned scratch = MRI.createVirtualRegister(TRC);
5156 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005157
5158 // thisMBB:
5159 // ...
5160 // fallthrough --> loopMBB
5161 BB->addSuccessor(loopMBB);
5162
5163 // loopMBB:
5164 // ldrex dest, ptr
5165 // (sign extend dest, if required)
5166 // cmp dest, incr
5167 // cmov.cond scratch2, dest, incr
5168 // strex scratch, scratch2, ptr
5169 // cmp scratch, #0
5170 // bne- loopMBB
5171 // fallthrough --> exitMBB
5172 BB = loopMBB;
5173 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5174
5175 // Sign extend the value, if necessary.
5176 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005177 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005178 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval).addReg(dest));
5179 }
5180
5181 // Build compare and cmov instructions.
5182 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5183 .addReg(oldval).addReg(incr));
5184 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5185 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5186
5187 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5188 .addReg(ptr));
5189 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5190 .addReg(scratch).addImm(0));
5191 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5192 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5193
5194 BB->addSuccessor(loopMBB);
5195 BB->addSuccessor(exitMBB);
5196
5197 // exitMBB:
5198 // ...
5199 BB = exitMBB;
5200
5201 MI->eraseFromParent(); // The instruction is gone now.
5202
5203 return BB;
5204}
5205
Evan Cheng218977b2010-07-13 19:27:42 +00005206static
5207MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5208 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5209 E = MBB->succ_end(); I != E; ++I)
5210 if (*I != Succ)
5211 return *I;
5212 llvm_unreachable("Expecting a BB with two successors!");
5213}
5214
Andrew Trick1c3af772011-04-23 03:55:32 +00005215// FIXME: This opcode table should obviously be expressed in the target
5216// description. We probably just need a "machine opcode" value in the pseudo
5217// instruction. But the ideal solution maybe to simply remove the "S" version
5218// of the opcode altogether.
5219struct AddSubFlagsOpcodePair {
5220 unsigned PseudoOpc;
5221 unsigned MachineOpc;
5222};
5223
5224static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5225 {ARM::ADCSri, ARM::ADCri},
5226 {ARM::ADCSrr, ARM::ADCrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005227 {ARM::ADCSrsi, ARM::ADCrsi},
5228 {ARM::ADCSrsr, ARM::ADCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005229 {ARM::SBCSri, ARM::SBCri},
5230 {ARM::SBCSrr, ARM::SBCrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005231 {ARM::SBCSrsi, ARM::SBCrsi},
5232 {ARM::SBCSrsr, ARM::SBCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005233 {ARM::RSBSri, ARM::RSBri},
5234 {ARM::RSBSrr, ARM::RSBrr},
Owen Anderson92a20222011-07-21 18:54:16 +00005235 {ARM::RSBSrsi, ARM::RSBrsi},
5236 {ARM::RSBSrsr, ARM::RSBrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005237 {ARM::RSCSri, ARM::RSCri},
Owen Anderson92a20222011-07-21 18:54:16 +00005238 {ARM::RSCSrsi, ARM::RSCrsi},
5239 {ARM::RSCSrsr, ARM::RSCrsr},
Andrew Trick1c3af772011-04-23 03:55:32 +00005240 {ARM::t2ADCSri, ARM::t2ADCri},
5241 {ARM::t2ADCSrr, ARM::t2ADCrr},
5242 {ARM::t2ADCSrs, ARM::t2ADCrs},
5243 {ARM::t2SBCSri, ARM::t2SBCri},
5244 {ARM::t2SBCSrr, ARM::t2SBCrr},
5245 {ARM::t2SBCSrs, ARM::t2SBCrs},
5246 {ARM::t2RSBSri, ARM::t2RSBri},
5247 {ARM::t2RSBSrs, ARM::t2RSBrs},
5248};
5249
5250// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5251// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5252//
5253// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5254// position to be recognized by the target descrition as the 'S' bit.
5255bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5256 MachineBasicBlock *BB) const {
5257 unsigned OldOpc = MI->getOpcode();
5258 unsigned NewOpc = 0;
5259
5260 // This is only called for instructions that need remapping, so iterating over
5261 // the tiny opcode table is not costly.
5262 static const int NPairs =
5263 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5264 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5265 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5266 if (OldOpc == Pair->PseudoOpc) {
5267 NewOpc = Pair->MachineOpc;
5268 break;
5269 }
5270 }
5271 if (!NewOpc)
5272 return false;
5273
5274 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5275 DebugLoc dl = MI->getDebugLoc();
5276 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5277 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5278 MIB.addOperand(MI->getOperand(i));
5279 AddDefaultPred(MIB);
5280 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5281 MI->eraseFromParent();
5282 return true;
5283}
5284
Jim Grosbache801dc42009-12-12 01:40:06 +00005285MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005286ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005287 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005288 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005289 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005290 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005291 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005292 default: {
5293 if (RemapAddSubWithFlags(MI, BB))
5294 return BB;
5295
Jim Grosbach5278eb82009-12-11 01:42:04 +00005296 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005297 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005298 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005299 case ARM::ATOMIC_LOAD_ADD_I8:
5300 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5301 case ARM::ATOMIC_LOAD_ADD_I16:
5302 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5303 case ARM::ATOMIC_LOAD_ADD_I32:
5304 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005305
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005306 case ARM::ATOMIC_LOAD_AND_I8:
5307 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5308 case ARM::ATOMIC_LOAD_AND_I16:
5309 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5310 case ARM::ATOMIC_LOAD_AND_I32:
5311 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005312
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005313 case ARM::ATOMIC_LOAD_OR_I8:
5314 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5315 case ARM::ATOMIC_LOAD_OR_I16:
5316 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5317 case ARM::ATOMIC_LOAD_OR_I32:
5318 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005319
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005320 case ARM::ATOMIC_LOAD_XOR_I8:
5321 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5322 case ARM::ATOMIC_LOAD_XOR_I16:
5323 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5324 case ARM::ATOMIC_LOAD_XOR_I32:
5325 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005326
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005327 case ARM::ATOMIC_LOAD_NAND_I8:
5328 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5329 case ARM::ATOMIC_LOAD_NAND_I16:
5330 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5331 case ARM::ATOMIC_LOAD_NAND_I32:
5332 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005333
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005334 case ARM::ATOMIC_LOAD_SUB_I8:
5335 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5336 case ARM::ATOMIC_LOAD_SUB_I16:
5337 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5338 case ARM::ATOMIC_LOAD_SUB_I32:
5339 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005340
Jim Grosbachf7da8822011-04-26 19:44:18 +00005341 case ARM::ATOMIC_LOAD_MIN_I8:
5342 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5343 case ARM::ATOMIC_LOAD_MIN_I16:
5344 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5345 case ARM::ATOMIC_LOAD_MIN_I32:
5346 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5347
5348 case ARM::ATOMIC_LOAD_MAX_I8:
5349 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5350 case ARM::ATOMIC_LOAD_MAX_I16:
5351 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5352 case ARM::ATOMIC_LOAD_MAX_I32:
5353 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5354
5355 case ARM::ATOMIC_LOAD_UMIN_I8:
5356 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5357 case ARM::ATOMIC_LOAD_UMIN_I16:
5358 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5359 case ARM::ATOMIC_LOAD_UMIN_I32:
5360 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5361
5362 case ARM::ATOMIC_LOAD_UMAX_I8:
5363 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5364 case ARM::ATOMIC_LOAD_UMAX_I16:
5365 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5366 case ARM::ATOMIC_LOAD_UMAX_I32:
5367 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5368
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005369 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5370 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5371 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005372
5373 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5374 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5375 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005376
Evan Cheng007ea272009-08-12 05:17:19 +00005377 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005378 // To "insert" a SELECT_CC instruction, we actually have to insert the
5379 // diamond control-flow pattern. The incoming instruction knows the
5380 // destination vreg to set, the condition code register to branch on, the
5381 // true/false values to select between, and a branch opcode to use.
5382 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005383 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005384 ++It;
5385
5386 // thisMBB:
5387 // ...
5388 // TrueVal = ...
5389 // cmpTY ccX, r1, r2
5390 // bCC copy1MBB
5391 // fallthrough --> copy0MBB
5392 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005393 MachineFunction *F = BB->getParent();
5394 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5395 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005396 F->insert(It, copy0MBB);
5397 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005398
5399 // Transfer the remainder of BB and its successor edges to sinkMBB.
5400 sinkMBB->splice(sinkMBB->begin(), BB,
5401 llvm::next(MachineBasicBlock::iterator(MI)),
5402 BB->end());
5403 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5404
Dan Gohman258c58c2010-07-06 15:49:48 +00005405 BB->addSuccessor(copy0MBB);
5406 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005407
Dan Gohman14152b42010-07-06 20:24:04 +00005408 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5409 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5410
Evan Chenga8e29892007-01-19 07:51:42 +00005411 // copy0MBB:
5412 // %FalseValue = ...
5413 // # fallthrough to sinkMBB
5414 BB = copy0MBB;
5415
5416 // Update machine-CFG edges
5417 BB->addSuccessor(sinkMBB);
5418
5419 // sinkMBB:
5420 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5421 // ...
5422 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005423 BuildMI(*BB, BB->begin(), dl,
5424 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005425 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5426 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5427
Dan Gohman14152b42010-07-06 20:24:04 +00005428 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005429 return BB;
5430 }
Evan Cheng86198642009-08-07 00:34:42 +00005431
Evan Cheng218977b2010-07-13 19:27:42 +00005432 case ARM::BCCi64:
5433 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005434 // If there is an unconditional branch to the other successor, remove it.
5435 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005436
Evan Cheng218977b2010-07-13 19:27:42 +00005437 // Compare both parts that make up the double comparison separately for
5438 // equality.
5439 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5440
5441 unsigned LHS1 = MI->getOperand(1).getReg();
5442 unsigned LHS2 = MI->getOperand(2).getReg();
5443 if (RHSisZero) {
5444 AddDefaultPred(BuildMI(BB, dl,
5445 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5446 .addReg(LHS1).addImm(0));
5447 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5448 .addReg(LHS2).addImm(0)
5449 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5450 } else {
5451 unsigned RHS1 = MI->getOperand(3).getReg();
5452 unsigned RHS2 = MI->getOperand(4).getReg();
5453 AddDefaultPred(BuildMI(BB, dl,
5454 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5455 .addReg(LHS1).addReg(RHS1));
5456 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5457 .addReg(LHS2).addReg(RHS2)
5458 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5459 }
5460
5461 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5462 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5463 if (MI->getOperand(0).getImm() == ARMCC::NE)
5464 std::swap(destMBB, exitMBB);
5465
5466 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5467 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5468 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5469 .addMBB(exitMBB);
5470
5471 MI->eraseFromParent(); // The pseudo instruction is gone now.
5472 return BB;
5473 }
Evan Chenga8e29892007-01-19 07:51:42 +00005474 }
5475}
5476
5477//===----------------------------------------------------------------------===//
5478// ARM Optimization Hooks
5479//===----------------------------------------------------------------------===//
5480
Chris Lattnerd1980a52009-03-12 06:52:53 +00005481static
5482SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5483 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005484 SelectionDAG &DAG = DCI.DAG;
5485 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005486 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005487 unsigned Opc = N->getOpcode();
5488 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5489 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5490 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5491 ISD::CondCode CC = ISD::SETCC_INVALID;
5492
5493 if (isSlctCC) {
5494 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5495 } else {
5496 SDValue CCOp = Slct.getOperand(0);
5497 if (CCOp.getOpcode() == ISD::SETCC)
5498 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5499 }
5500
5501 bool DoXform = false;
5502 bool InvCC = false;
5503 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5504 "Bad input!");
5505
5506 if (LHS.getOpcode() == ISD::Constant &&
5507 cast<ConstantSDNode>(LHS)->isNullValue()) {
5508 DoXform = true;
5509 } else if (CC != ISD::SETCC_INVALID &&
5510 RHS.getOpcode() == ISD::Constant &&
5511 cast<ConstantSDNode>(RHS)->isNullValue()) {
5512 std::swap(LHS, RHS);
5513 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005514 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005515 Op0.getOperand(0).getValueType();
5516 bool isInt = OpVT.isInteger();
5517 CC = ISD::getSetCCInverse(CC, isInt);
5518
5519 if (!TLI.isCondCodeLegal(CC, OpVT))
5520 return SDValue(); // Inverse operator isn't legal.
5521
5522 DoXform = true;
5523 InvCC = true;
5524 }
5525
5526 if (DoXform) {
5527 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5528 if (isSlctCC)
5529 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5530 Slct.getOperand(0), Slct.getOperand(1), CC);
5531 SDValue CCOp = Slct.getOperand(0);
5532 if (InvCC)
5533 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5534 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5535 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5536 CCOp, OtherOp, Result);
5537 }
5538 return SDValue();
5539}
5540
Eric Christopherfa6f5912011-06-29 21:10:36 +00005541// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00005542// (only after legalization).
5543static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5544 TargetLowering::DAGCombinerInfo &DCI,
5545 const ARMSubtarget *Subtarget) {
5546
5547 // Only perform optimization if after legalize, and if NEON is available. We
5548 // also expected both operands to be BUILD_VECTORs.
5549 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5550 || N0.getOpcode() != ISD::BUILD_VECTOR
5551 || N1.getOpcode() != ISD::BUILD_VECTOR)
5552 return SDValue();
5553
5554 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5555 EVT VT = N->getValueType(0);
5556 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5557 return SDValue();
5558
5559 // Check that the vector operands are of the right form.
5560 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5561 // operands, where N is the size of the formed vector.
5562 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5563 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005564
5565 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005566 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005567 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005568 SDValue Vec = N0->getOperand(0)->getOperand(0);
5569 SDNode *V = Vec.getNode();
5570 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005571
Eric Christopherfa6f5912011-06-29 21:10:36 +00005572 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00005573 // check to see if each of their operands are an EXTRACT_VECTOR with
5574 // the same vector and appropriate index.
5575 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5576 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5577 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00005578
Tanya Lattner189531f2011-06-14 23:48:48 +00005579 SDValue ExtVec0 = N0->getOperand(i);
5580 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005581
Tanya Lattner189531f2011-06-14 23:48:48 +00005582 // First operand is the vector, verify its the same.
5583 if (V != ExtVec0->getOperand(0).getNode() ||
5584 V != ExtVec1->getOperand(0).getNode())
5585 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00005586
Tanya Lattner189531f2011-06-14 23:48:48 +00005587 // Second is the constant, verify its correct.
5588 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5589 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00005590
Tanya Lattner189531f2011-06-14 23:48:48 +00005591 // For the constant, we want to see all the even or all the odd.
5592 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5593 || C1->getZExtValue() != nextIndex+1)
5594 return SDValue();
5595
5596 // Increment index.
5597 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005598 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00005599 return SDValue();
5600 }
5601
5602 // Create VPADDL node.
5603 SelectionDAG &DAG = DCI.DAG;
5604 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00005605
5606 // Build operand list.
5607 SmallVector<SDValue, 8> Ops;
5608 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5609 TLI.getPointerTy()));
5610
5611 // Input is the vector.
5612 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005613
Tanya Lattner189531f2011-06-14 23:48:48 +00005614 // Get widened type and narrowed type.
5615 MVT widenType;
5616 unsigned numElem = VT.getVectorNumElements();
5617 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5618 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5619 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5620 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5621 default:
5622 assert(0 && "Invalid vector element type for padd optimization.");
5623 }
5624
5625 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5626 widenType, &Ops[0], Ops.size());
5627 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5628}
5629
Bob Wilson3d5792a2010-07-29 20:34:14 +00005630/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5631/// operands N0 and N1. This is a helper for PerformADDCombine that is
5632/// called with the default operands, and if that fails, with commuted
5633/// operands.
5634static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005635 TargetLowering::DAGCombinerInfo &DCI,
5636 const ARMSubtarget *Subtarget){
5637
5638 // Attempt to create vpaddl for this add.
5639 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5640 if (Result.getNode())
5641 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005642
Chris Lattnerd1980a52009-03-12 06:52:53 +00005643 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5644 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5645 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5646 if (Result.getNode()) return Result;
5647 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005648 return SDValue();
5649}
5650
Bob Wilson3d5792a2010-07-29 20:34:14 +00005651/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5652///
5653static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005654 TargetLowering::DAGCombinerInfo &DCI,
5655 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005656 SDValue N0 = N->getOperand(0);
5657 SDValue N1 = N->getOperand(1);
5658
5659 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005660 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005661 if (Result.getNode())
5662 return Result;
5663
5664 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005665 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005666}
5667
Chris Lattnerd1980a52009-03-12 06:52:53 +00005668/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005669///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005670static SDValue PerformSUBCombine(SDNode *N,
5671 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005672 SDValue N0 = N->getOperand(0);
5673 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005674
Chris Lattnerd1980a52009-03-12 06:52:53 +00005675 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5676 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5677 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5678 if (Result.getNode()) return Result;
5679 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005680
Chris Lattnerd1980a52009-03-12 06:52:53 +00005681 return SDValue();
5682}
5683
Evan Cheng463d3582011-03-31 19:38:48 +00005684/// PerformVMULCombine
5685/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5686/// special multiplier accumulator forwarding.
5687/// vmul d3, d0, d2
5688/// vmla d3, d1, d2
5689/// is faster than
5690/// vadd d3, d0, d1
5691/// vmul d3, d3, d2
5692static SDValue PerformVMULCombine(SDNode *N,
5693 TargetLowering::DAGCombinerInfo &DCI,
5694 const ARMSubtarget *Subtarget) {
5695 if (!Subtarget->hasVMLxForwarding())
5696 return SDValue();
5697
5698 SelectionDAG &DAG = DCI.DAG;
5699 SDValue N0 = N->getOperand(0);
5700 SDValue N1 = N->getOperand(1);
5701 unsigned Opcode = N0.getOpcode();
5702 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5703 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00005704 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00005705 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5706 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5707 return SDValue();
5708 std::swap(N0, N1);
5709 }
5710
5711 EVT VT = N->getValueType(0);
5712 DebugLoc DL = N->getDebugLoc();
5713 SDValue N00 = N0->getOperand(0);
5714 SDValue N01 = N0->getOperand(1);
5715 return DAG.getNode(Opcode, DL, VT,
5716 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5717 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5718}
5719
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005720static SDValue PerformMULCombine(SDNode *N,
5721 TargetLowering::DAGCombinerInfo &DCI,
5722 const ARMSubtarget *Subtarget) {
5723 SelectionDAG &DAG = DCI.DAG;
5724
5725 if (Subtarget->isThumb1Only())
5726 return SDValue();
5727
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005728 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5729 return SDValue();
5730
5731 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005732 if (VT.is64BitVector() || VT.is128BitVector())
5733 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005734 if (VT != MVT::i32)
5735 return SDValue();
5736
5737 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5738 if (!C)
5739 return SDValue();
5740
5741 uint64_t MulAmt = C->getZExtValue();
5742 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5743 ShiftAmt = ShiftAmt & (32 - 1);
5744 SDValue V = N->getOperand(0);
5745 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005746
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005747 SDValue Res;
5748 MulAmt >>= ShiftAmt;
5749 if (isPowerOf2_32(MulAmt - 1)) {
5750 // (mul x, 2^N + 1) => (add (shl x, N), x)
5751 Res = DAG.getNode(ISD::ADD, DL, VT,
5752 V, DAG.getNode(ISD::SHL, DL, VT,
5753 V, DAG.getConstant(Log2_32(MulAmt-1),
5754 MVT::i32)));
5755 } else if (isPowerOf2_32(MulAmt + 1)) {
5756 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5757 Res = DAG.getNode(ISD::SUB, DL, VT,
5758 DAG.getNode(ISD::SHL, DL, VT,
5759 V, DAG.getConstant(Log2_32(MulAmt+1),
5760 MVT::i32)),
5761 V);
5762 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005763 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005764
5765 if (ShiftAmt != 0)
5766 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5767 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005768
5769 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005770 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005771 return SDValue();
5772}
5773
Owen Anderson080c0922010-11-05 19:27:46 +00005774static SDValue PerformANDCombine(SDNode *N,
5775 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005776
Owen Anderson080c0922010-11-05 19:27:46 +00005777 // Attempt to use immediate-form VBIC
5778 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5779 DebugLoc dl = N->getDebugLoc();
5780 EVT VT = N->getValueType(0);
5781 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005782
Tanya Lattner0433b212011-04-07 15:24:20 +00005783 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5784 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005785
Owen Anderson080c0922010-11-05 19:27:46 +00005786 APInt SplatBits, SplatUndef;
5787 unsigned SplatBitSize;
5788 bool HasAnyUndefs;
5789 if (BVN &&
5790 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5791 if (SplatBitSize <= 64) {
5792 EVT VbicVT;
5793 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5794 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005795 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005796 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005797 if (Val.getNode()) {
5798 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005799 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005800 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005801 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005802 }
5803 }
5804 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005805
Owen Anderson080c0922010-11-05 19:27:46 +00005806 return SDValue();
5807}
5808
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005809/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5810static SDValue PerformORCombine(SDNode *N,
5811 TargetLowering::DAGCombinerInfo &DCI,
5812 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005813 // Attempt to use immediate-form VORR
5814 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5815 DebugLoc dl = N->getDebugLoc();
5816 EVT VT = N->getValueType(0);
5817 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005818
Tanya Lattner0433b212011-04-07 15:24:20 +00005819 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5820 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005821
Owen Anderson60f48702010-11-03 23:15:26 +00005822 APInt SplatBits, SplatUndef;
5823 unsigned SplatBitSize;
5824 bool HasAnyUndefs;
5825 if (BVN && Subtarget->hasNEON() &&
5826 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5827 if (SplatBitSize <= 64) {
5828 EVT VorrVT;
5829 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5830 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005831 DAG, VorrVT, VT.is128BitVector(),
5832 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005833 if (Val.getNode()) {
5834 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005835 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005836 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005837 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005838 }
5839 }
5840 }
5841
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005842 SDValue N0 = N->getOperand(0);
5843 if (N0.getOpcode() != ISD::AND)
5844 return SDValue();
5845 SDValue N1 = N->getOperand(1);
5846
5847 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5848 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5849 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5850 APInt SplatUndef;
5851 unsigned SplatBitSize;
5852 bool HasAnyUndefs;
5853
5854 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5855 APInt SplatBits0;
5856 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5857 HasAnyUndefs) && !HasAnyUndefs) {
5858 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5859 APInt SplatBits1;
5860 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5861 HasAnyUndefs) && !HasAnyUndefs &&
5862 SplatBits0 == ~SplatBits1) {
5863 // Canonicalize the vector type to make instruction selection simpler.
5864 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5865 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5866 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005867 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005868 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5869 }
5870 }
5871 }
5872
Jim Grosbach54238562010-07-17 03:30:54 +00005873 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5874 // reasonable.
5875
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005876 // BFI is only available on V6T2+
5877 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5878 return SDValue();
5879
Jim Grosbach54238562010-07-17 03:30:54 +00005880 DebugLoc DL = N->getDebugLoc();
5881 // 1) or (and A, mask), val => ARMbfi A, val, mask
5882 // iff (val & mask) == val
5883 //
5884 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5885 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005886 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005887 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005888 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005889 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005890
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005891 if (VT != MVT::i32)
5892 return SDValue();
5893
Evan Cheng30fb13f2010-12-13 20:32:54 +00005894 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005895
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005896 // The value and the mask need to be constants so we can verify this is
5897 // actually a bitfield set. If the mask is 0xffff, we can do better
5898 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005899 SDValue MaskOp = N0.getOperand(1);
5900 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5901 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005902 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005903 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005904 if (Mask == 0xffff)
5905 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005906 SDValue Res;
5907 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005908 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5909 if (N1C) {
5910 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005911 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005912 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005913
Evan Chenga9688c42010-12-11 04:11:38 +00005914 if (ARM::isBitFieldInvertedMask(Mask)) {
5915 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005916
Evan Cheng30fb13f2010-12-13 20:32:54 +00005917 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005918 DAG.getConstant(Val, MVT::i32),
5919 DAG.getConstant(Mask, MVT::i32));
5920
5921 // Do not add new nodes to DAG combiner worklist.
5922 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005923 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005924 }
Jim Grosbach54238562010-07-17 03:30:54 +00005925 } else if (N1.getOpcode() == ISD::AND) {
5926 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005927 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5928 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005929 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005930 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005931
Eric Christopher29aeed12011-03-26 01:21:03 +00005932 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5933 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005934 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005935 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005936 // The pack halfword instruction works better for masks that fit it,
5937 // so use that when it's available.
5938 if (Subtarget->hasT2ExtractPack() &&
5939 (Mask == 0xffff || Mask == 0xffff0000))
5940 return SDValue();
5941 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005942 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005943 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005944 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005945 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005946 DAG.getConstant(Mask, MVT::i32));
5947 // Do not add new nodes to DAG combiner worklist.
5948 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005949 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005950 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005951 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005952 // The pack halfword instruction works better for masks that fit it,
5953 // so use that when it's available.
5954 if (Subtarget->hasT2ExtractPack() &&
5955 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5956 return SDValue();
5957 // 2b
5958 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005959 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005960 DAG.getConstant(lsb, MVT::i32));
5961 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005962 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005963 // Do not add new nodes to DAG combiner worklist.
5964 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005965 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005966 }
5967 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005968
Evan Cheng30fb13f2010-12-13 20:32:54 +00005969 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5970 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5971 ARM::isBitFieldInvertedMask(~Mask)) {
5972 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5973 // where lsb(mask) == #shamt and masked bits of B are known zero.
5974 SDValue ShAmt = N00.getOperand(1);
5975 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5976 unsigned LSB = CountTrailingZeros_32(Mask);
5977 if (ShAmtC != LSB)
5978 return SDValue();
5979
5980 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5981 DAG.getConstant(~Mask, MVT::i32));
5982
5983 // Do not add new nodes to DAG combiner worklist.
5984 DCI.CombineTo(N, Res, false);
5985 }
5986
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005987 return SDValue();
5988}
5989
Evan Chengbf188ae2011-06-15 01:12:31 +00005990/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
5991/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00005992static SDValue PerformBFICombine(SDNode *N,
5993 TargetLowering::DAGCombinerInfo &DCI) {
5994 SDValue N1 = N->getOperand(1);
5995 if (N1.getOpcode() == ISD::AND) {
5996 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5997 if (!N11C)
5998 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00005999 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
6000 unsigned LSB = CountTrailingZeros_32(~InvMask);
6001 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
6002 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00006003 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006004 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00006005 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
6006 N->getOperand(0), N1.getOperand(0),
6007 N->getOperand(2));
6008 }
6009 return SDValue();
6010}
6011
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006012/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
6013/// ARMISD::VMOVRRD.
6014static SDValue PerformVMOVRRDCombine(SDNode *N,
6015 TargetLowering::DAGCombinerInfo &DCI) {
6016 // vmovrrd(vmovdrr x, y) -> x,y
6017 SDValue InDouble = N->getOperand(0);
6018 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6019 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006020
6021 // vmovrrd(load f64) -> (load i32), (load i32)
6022 SDNode *InNode = InDouble.getNode();
6023 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6024 InNode->getValueType(0) == MVT::f64 &&
6025 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6026 !cast<LoadSDNode>(InNode)->isVolatile()) {
6027 // TODO: Should this be done for non-FrameIndex operands?
6028 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6029
6030 SelectionDAG &DAG = DCI.DAG;
6031 DebugLoc DL = LD->getDebugLoc();
6032 SDValue BasePtr = LD->getBasePtr();
6033 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6034 LD->getPointerInfo(), LD->isVolatile(),
6035 LD->isNonTemporal(), LD->getAlignment());
6036
6037 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6038 DAG.getConstant(4, MVT::i32));
6039 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6040 LD->getPointerInfo(), LD->isVolatile(),
6041 LD->isNonTemporal(),
6042 std::min(4U, LD->getAlignment() / 2));
6043
6044 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6045 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6046 DCI.RemoveFromWorklist(LD);
6047 DAG.DeleteNode(LD);
6048 return Result;
6049 }
6050
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006051 return SDValue();
6052}
6053
6054/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6055/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6056static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6057 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6058 SDValue Op0 = N->getOperand(0);
6059 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006060 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006061 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006062 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006063 Op1 = Op1.getOperand(0);
6064 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6065 Op0.getNode() == Op1.getNode() &&
6066 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006067 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006068 N->getValueType(0), Op0.getOperand(0));
6069 return SDValue();
6070}
6071
Bob Wilson31600902010-12-21 06:43:19 +00006072/// PerformSTORECombine - Target-specific dag combine xforms for
6073/// ISD::STORE.
6074static SDValue PerformSTORECombine(SDNode *N,
6075 TargetLowering::DAGCombinerInfo &DCI) {
6076 // Bitcast an i64 store extracted from a vector to f64.
6077 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6078 StoreSDNode *St = cast<StoreSDNode>(N);
6079 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006080 if (!ISD::isNormalStore(St) || St->isVolatile())
6081 return SDValue();
6082
6083 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6084 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6085 SelectionDAG &DAG = DCI.DAG;
6086 DebugLoc DL = St->getDebugLoc();
6087 SDValue BasePtr = St->getBasePtr();
6088 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6089 StVal.getNode()->getOperand(0), BasePtr,
6090 St->getPointerInfo(), St->isVolatile(),
6091 St->isNonTemporal(), St->getAlignment());
6092
6093 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6094 DAG.getConstant(4, MVT::i32));
6095 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6096 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6097 St->isNonTemporal(),
6098 std::min(4U, St->getAlignment() / 2));
6099 }
6100
6101 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006102 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6103 return SDValue();
6104
6105 SelectionDAG &DAG = DCI.DAG;
6106 DebugLoc dl = StVal.getDebugLoc();
6107 SDValue IntVec = StVal.getOperand(0);
6108 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6109 IntVec.getValueType().getVectorNumElements());
6110 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6111 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6112 Vec, StVal.getOperand(1));
6113 dl = N->getDebugLoc();
6114 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6115 // Make the DAGCombiner fold the bitcasts.
6116 DCI.AddToWorklist(Vec.getNode());
6117 DCI.AddToWorklist(ExtElt.getNode());
6118 DCI.AddToWorklist(V.getNode());
6119 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6120 St->getPointerInfo(), St->isVolatile(),
6121 St->isNonTemporal(), St->getAlignment(),
6122 St->getTBAAInfo());
6123}
6124
6125/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6126/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6127/// i64 vector to have f64 elements, since the value can then be loaded
6128/// directly into a VFP register.
6129static bool hasNormalLoadOperand(SDNode *N) {
6130 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6131 for (unsigned i = 0; i < NumElts; ++i) {
6132 SDNode *Elt = N->getOperand(i).getNode();
6133 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6134 return true;
6135 }
6136 return false;
6137}
6138
Bob Wilson75f02882010-09-17 22:59:05 +00006139/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6140/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006141static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6142 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006143 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6144 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6145 // into a pair of GPRs, which is fine when the value is used as a scalar,
6146 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006147 SelectionDAG &DAG = DCI.DAG;
6148 if (N->getNumOperands() == 2) {
6149 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6150 if (RV.getNode())
6151 return RV;
6152 }
Bob Wilson75f02882010-09-17 22:59:05 +00006153
Bob Wilson31600902010-12-21 06:43:19 +00006154 // Load i64 elements as f64 values so that type legalization does not split
6155 // them up into i32 values.
6156 EVT VT = N->getValueType(0);
6157 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6158 return SDValue();
6159 DebugLoc dl = N->getDebugLoc();
6160 SmallVector<SDValue, 8> Ops;
6161 unsigned NumElts = VT.getVectorNumElements();
6162 for (unsigned i = 0; i < NumElts; ++i) {
6163 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6164 Ops.push_back(V);
6165 // Make the DAGCombiner fold the bitcast.
6166 DCI.AddToWorklist(V.getNode());
6167 }
6168 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6169 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6170 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6171}
6172
6173/// PerformInsertEltCombine - Target-specific dag combine xforms for
6174/// ISD::INSERT_VECTOR_ELT.
6175static SDValue PerformInsertEltCombine(SDNode *N,
6176 TargetLowering::DAGCombinerInfo &DCI) {
6177 // Bitcast an i64 load inserted into a vector to f64.
6178 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6179 EVT VT = N->getValueType(0);
6180 SDNode *Elt = N->getOperand(1).getNode();
6181 if (VT.getVectorElementType() != MVT::i64 ||
6182 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6183 return SDValue();
6184
6185 SelectionDAG &DAG = DCI.DAG;
6186 DebugLoc dl = N->getDebugLoc();
6187 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6188 VT.getVectorNumElements());
6189 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6190 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6191 // Make the DAGCombiner fold the bitcasts.
6192 DCI.AddToWorklist(Vec.getNode());
6193 DCI.AddToWorklist(V.getNode());
6194 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6195 Vec, V, N->getOperand(2));
6196 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006197}
6198
Bob Wilsonf20700c2010-10-27 20:38:28 +00006199/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6200/// ISD::VECTOR_SHUFFLE.
6201static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6202 // The LLVM shufflevector instruction does not require the shuffle mask
6203 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6204 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6205 // operands do not match the mask length, they are extended by concatenating
6206 // them with undef vectors. That is probably the right thing for other
6207 // targets, but for NEON it is better to concatenate two double-register
6208 // size vector operands into a single quad-register size vector. Do that
6209 // transformation here:
6210 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6211 // shuffle(concat(v1, v2), undef)
6212 SDValue Op0 = N->getOperand(0);
6213 SDValue Op1 = N->getOperand(1);
6214 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6215 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6216 Op0.getNumOperands() != 2 ||
6217 Op1.getNumOperands() != 2)
6218 return SDValue();
6219 SDValue Concat0Op1 = Op0.getOperand(1);
6220 SDValue Concat1Op1 = Op1.getOperand(1);
6221 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6222 Concat1Op1.getOpcode() != ISD::UNDEF)
6223 return SDValue();
6224 // Skip the transformation if any of the types are illegal.
6225 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6226 EVT VT = N->getValueType(0);
6227 if (!TLI.isTypeLegal(VT) ||
6228 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6229 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6230 return SDValue();
6231
6232 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6233 Op0.getOperand(0), Op1.getOperand(0));
6234 // Translate the shuffle mask.
6235 SmallVector<int, 16> NewMask;
6236 unsigned NumElts = VT.getVectorNumElements();
6237 unsigned HalfElts = NumElts/2;
6238 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6239 for (unsigned n = 0; n < NumElts; ++n) {
6240 int MaskElt = SVN->getMaskElt(n);
6241 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006242 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006243 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006244 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006245 NewElt = HalfElts + MaskElt - NumElts;
6246 NewMask.push_back(NewElt);
6247 }
6248 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6249 DAG.getUNDEF(VT), NewMask.data());
6250}
6251
Bob Wilson1c3ef902011-02-07 17:43:21 +00006252/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6253/// NEON load/store intrinsics to merge base address updates.
6254static SDValue CombineBaseUpdate(SDNode *N,
6255 TargetLowering::DAGCombinerInfo &DCI) {
6256 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6257 return SDValue();
6258
6259 SelectionDAG &DAG = DCI.DAG;
6260 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6261 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6262 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6263 SDValue Addr = N->getOperand(AddrOpIdx);
6264
6265 // Search for a use of the address operand that is an increment.
6266 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6267 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6268 SDNode *User = *UI;
6269 if (User->getOpcode() != ISD::ADD ||
6270 UI.getUse().getResNo() != Addr.getResNo())
6271 continue;
6272
6273 // Check that the add is independent of the load/store. Otherwise, folding
6274 // it would create a cycle.
6275 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6276 continue;
6277
6278 // Find the new opcode for the updating load/store.
6279 bool isLoad = true;
6280 bool isLaneOp = false;
6281 unsigned NewOpc = 0;
6282 unsigned NumVecs = 0;
6283 if (isIntrinsic) {
6284 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6285 switch (IntNo) {
6286 default: assert(0 && "unexpected intrinsic for Neon base update");
6287 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6288 NumVecs = 1; break;
6289 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6290 NumVecs = 2; break;
6291 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6292 NumVecs = 3; break;
6293 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6294 NumVecs = 4; break;
6295 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6296 NumVecs = 2; isLaneOp = true; break;
6297 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6298 NumVecs = 3; isLaneOp = true; break;
6299 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6300 NumVecs = 4; isLaneOp = true; break;
6301 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6302 NumVecs = 1; isLoad = false; break;
6303 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6304 NumVecs = 2; isLoad = false; break;
6305 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6306 NumVecs = 3; isLoad = false; break;
6307 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6308 NumVecs = 4; isLoad = false; break;
6309 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6310 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6311 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6312 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6313 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6314 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6315 }
6316 } else {
6317 isLaneOp = true;
6318 switch (N->getOpcode()) {
6319 default: assert(0 && "unexpected opcode for Neon base update");
6320 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6321 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6322 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6323 }
6324 }
6325
6326 // Find the size of memory referenced by the load/store.
6327 EVT VecTy;
6328 if (isLoad)
6329 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006330 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006331 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6332 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6333 if (isLaneOp)
6334 NumBytes /= VecTy.getVectorNumElements();
6335
6336 // If the increment is a constant, it must match the memory ref size.
6337 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6338 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6339 uint64_t IncVal = CInc->getZExtValue();
6340 if (IncVal != NumBytes)
6341 continue;
6342 } else if (NumBytes >= 3 * 16) {
6343 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6344 // separate instructions that make it harder to use a non-constant update.
6345 continue;
6346 }
6347
6348 // Create the new updating load/store node.
6349 EVT Tys[6];
6350 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6351 unsigned n;
6352 for (n = 0; n < NumResultVecs; ++n)
6353 Tys[n] = VecTy;
6354 Tys[n++] = MVT::i32;
6355 Tys[n] = MVT::Other;
6356 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6357 SmallVector<SDValue, 8> Ops;
6358 Ops.push_back(N->getOperand(0)); // incoming chain
6359 Ops.push_back(N->getOperand(AddrOpIdx));
6360 Ops.push_back(Inc);
6361 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6362 Ops.push_back(N->getOperand(i));
6363 }
6364 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6365 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6366 Ops.data(), Ops.size(),
6367 MemInt->getMemoryVT(),
6368 MemInt->getMemOperand());
6369
6370 // Update the uses.
6371 std::vector<SDValue> NewResults;
6372 for (unsigned i = 0; i < NumResultVecs; ++i) {
6373 NewResults.push_back(SDValue(UpdN.getNode(), i));
6374 }
6375 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6376 DCI.CombineTo(N, NewResults);
6377 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6378
6379 break;
Owen Anderson76706012011-04-05 21:48:57 +00006380 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006381 return SDValue();
6382}
6383
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006384/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6385/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6386/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6387/// return true.
6388static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6389 SelectionDAG &DAG = DCI.DAG;
6390 EVT VT = N->getValueType(0);
6391 // vldN-dup instructions only support 64-bit vectors for N > 1.
6392 if (!VT.is64BitVector())
6393 return false;
6394
6395 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6396 SDNode *VLD = N->getOperand(0).getNode();
6397 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6398 return false;
6399 unsigned NumVecs = 0;
6400 unsigned NewOpc = 0;
6401 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6402 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6403 NumVecs = 2;
6404 NewOpc = ARMISD::VLD2DUP;
6405 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6406 NumVecs = 3;
6407 NewOpc = ARMISD::VLD3DUP;
6408 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6409 NumVecs = 4;
6410 NewOpc = ARMISD::VLD4DUP;
6411 } else {
6412 return false;
6413 }
6414
6415 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6416 // numbers match the load.
6417 unsigned VLDLaneNo =
6418 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6419 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6420 UI != UE; ++UI) {
6421 // Ignore uses of the chain result.
6422 if (UI.getUse().getResNo() == NumVecs)
6423 continue;
6424 SDNode *User = *UI;
6425 if (User->getOpcode() != ARMISD::VDUPLANE ||
6426 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6427 return false;
6428 }
6429
6430 // Create the vldN-dup node.
6431 EVT Tys[5];
6432 unsigned n;
6433 for (n = 0; n < NumVecs; ++n)
6434 Tys[n] = VT;
6435 Tys[n] = MVT::Other;
6436 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6437 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6438 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6439 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6440 Ops, 2, VLDMemInt->getMemoryVT(),
6441 VLDMemInt->getMemOperand());
6442
6443 // Update the uses.
6444 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6445 UI != UE; ++UI) {
6446 unsigned ResNo = UI.getUse().getResNo();
6447 // Ignore uses of the chain result.
6448 if (ResNo == NumVecs)
6449 continue;
6450 SDNode *User = *UI;
6451 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6452 }
6453
6454 // Now the vldN-lane intrinsic is dead except for its chain result.
6455 // Update uses of the chain.
6456 std::vector<SDValue> VLDDupResults;
6457 for (unsigned n = 0; n < NumVecs; ++n)
6458 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6459 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6460 DCI.CombineTo(VLD, VLDDupResults);
6461
6462 return true;
6463}
6464
Bob Wilson9e82bf12010-07-14 01:22:12 +00006465/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6466/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006467static SDValue PerformVDUPLANECombine(SDNode *N,
6468 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006469 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006470
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006471 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6472 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6473 if (CombineVLDDUP(N, DCI))
6474 return SDValue(N, 0);
6475
6476 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6477 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006478 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006479 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006480 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006481 return SDValue();
6482
6483 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6484 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6485 // The canonical VMOV for a zero vector uses a 32-bit element size.
6486 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6487 unsigned EltBits;
6488 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6489 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006490 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006491 if (EltSize > VT.getVectorElementType().getSizeInBits())
6492 return SDValue();
6493
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006494 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006495}
6496
Eric Christopherfa6f5912011-06-29 21:10:36 +00006497// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00006498// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
6499static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
6500{
Chad Rosier118c9a02011-06-28 17:26:57 +00006501 integerPart cN;
6502 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00006503 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
6504 I != E; I++) {
6505 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
6506 if (!C)
6507 return false;
6508
Eric Christopherfa6f5912011-06-29 21:10:36 +00006509 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00006510 APFloat APF = C->getValueAPF();
6511 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
6512 != APFloat::opOK || !isExact)
6513 return false;
6514
6515 c0 = (I == 0) ? cN : c0;
6516 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
6517 return false;
6518 }
6519 C = c0;
6520 return true;
6521}
6522
6523/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
6524/// can replace combinations of VMUL and VCVT (floating-point to integer)
6525/// when the VMUL has a constant operand that is a power of 2.
6526///
6527/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6528/// vmul.f32 d16, d17, d16
6529/// vcvt.s32.f32 d16, d16
6530/// becomes:
6531/// vcvt.s32.f32 d16, d16, #3
6532static SDValue PerformVCVTCombine(SDNode *N,
6533 TargetLowering::DAGCombinerInfo &DCI,
6534 const ARMSubtarget *Subtarget) {
6535 SelectionDAG &DAG = DCI.DAG;
6536 SDValue Op = N->getOperand(0);
6537
6538 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
6539 Op.getOpcode() != ISD::FMUL)
6540 return SDValue();
6541
6542 uint64_t C;
6543 SDValue N0 = Op->getOperand(0);
6544 SDValue ConstVec = Op->getOperand(1);
6545 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
6546
Eric Christopherfa6f5912011-06-29 21:10:36 +00006547 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00006548 !isConstVecPow2(ConstVec, isSigned, C))
6549 return SDValue();
6550
6551 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
6552 Intrinsic::arm_neon_vcvtfp2fxu;
6553 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6554 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006555 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00006556 DAG.getConstant(Log2_64(C), MVT::i32));
6557}
6558
6559/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
6560/// can replace combinations of VCVT (integer to floating-point) and VDIV
6561/// when the VDIV has a constant operand that is a power of 2.
6562///
6563/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6564/// vcvt.f32.s32 d16, d16
6565/// vdiv.f32 d16, d17, d16
6566/// becomes:
6567/// vcvt.f32.s32 d16, d16, #3
6568static SDValue PerformVDIVCombine(SDNode *N,
6569 TargetLowering::DAGCombinerInfo &DCI,
6570 const ARMSubtarget *Subtarget) {
6571 SelectionDAG &DAG = DCI.DAG;
6572 SDValue Op = N->getOperand(0);
6573 unsigned OpOpcode = Op.getNode()->getOpcode();
6574
6575 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
6576 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
6577 return SDValue();
6578
6579 uint64_t C;
6580 SDValue ConstVec = N->getOperand(1);
6581 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
6582
6583 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
6584 !isConstVecPow2(ConstVec, isSigned, C))
6585 return SDValue();
6586
Eric Christopherfa6f5912011-06-29 21:10:36 +00006587 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00006588 Intrinsic::arm_neon_vcvtfxu2fp;
6589 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6590 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006591 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00006592 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
6593}
6594
6595/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00006596/// operand of a vector shift operation, where all the elements of the
6597/// build_vector must have the same constant integer value.
6598static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6599 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006600 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006601 Op = Op.getOperand(0);
6602 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6603 APInt SplatBits, SplatUndef;
6604 unsigned SplatBitSize;
6605 bool HasAnyUndefs;
6606 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6607 HasAnyUndefs, ElementBits) ||
6608 SplatBitSize > ElementBits)
6609 return false;
6610 Cnt = SplatBits.getSExtValue();
6611 return true;
6612}
6613
6614/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6615/// operand of a vector shift left operation. That value must be in the range:
6616/// 0 <= Value < ElementBits for a left shift; or
6617/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006618static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006619 assert(VT.isVector() && "vector shift count is not a vector type");
6620 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6621 if (! getVShiftImm(Op, ElementBits, Cnt))
6622 return false;
6623 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6624}
6625
6626/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6627/// operand of a vector shift right operation. For a shift opcode, the value
6628/// is positive, but for an intrinsic the value count must be negative. The
6629/// absolute value must be in the range:
6630/// 1 <= |Value| <= ElementBits for a right shift; or
6631/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006632static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006633 int64_t &Cnt) {
6634 assert(VT.isVector() && "vector shift count is not a vector type");
6635 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6636 if (! getVShiftImm(Op, ElementBits, Cnt))
6637 return false;
6638 if (isIntrinsic)
6639 Cnt = -Cnt;
6640 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6641}
6642
6643/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6644static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6645 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6646 switch (IntNo) {
6647 default:
6648 // Don't do anything for most intrinsics.
6649 break;
6650
6651 // Vector shifts: check for immediate versions and lower them.
6652 // Note: This is done during DAG combining instead of DAG legalizing because
6653 // the build_vectors for 64-bit vector element shift counts are generally
6654 // not legal, and it is hard to see their values after they get legalized to
6655 // loads from a constant pool.
6656 case Intrinsic::arm_neon_vshifts:
6657 case Intrinsic::arm_neon_vshiftu:
6658 case Intrinsic::arm_neon_vshiftls:
6659 case Intrinsic::arm_neon_vshiftlu:
6660 case Intrinsic::arm_neon_vshiftn:
6661 case Intrinsic::arm_neon_vrshifts:
6662 case Intrinsic::arm_neon_vrshiftu:
6663 case Intrinsic::arm_neon_vrshiftn:
6664 case Intrinsic::arm_neon_vqshifts:
6665 case Intrinsic::arm_neon_vqshiftu:
6666 case Intrinsic::arm_neon_vqshiftsu:
6667 case Intrinsic::arm_neon_vqshiftns:
6668 case Intrinsic::arm_neon_vqshiftnu:
6669 case Intrinsic::arm_neon_vqshiftnsu:
6670 case Intrinsic::arm_neon_vqrshiftns:
6671 case Intrinsic::arm_neon_vqrshiftnu:
6672 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006673 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006674 int64_t Cnt;
6675 unsigned VShiftOpc = 0;
6676
6677 switch (IntNo) {
6678 case Intrinsic::arm_neon_vshifts:
6679 case Intrinsic::arm_neon_vshiftu:
6680 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6681 VShiftOpc = ARMISD::VSHL;
6682 break;
6683 }
6684 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6685 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6686 ARMISD::VSHRs : ARMISD::VSHRu);
6687 break;
6688 }
6689 return SDValue();
6690
6691 case Intrinsic::arm_neon_vshiftls:
6692 case Intrinsic::arm_neon_vshiftlu:
6693 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6694 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006695 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006696
6697 case Intrinsic::arm_neon_vrshifts:
6698 case Intrinsic::arm_neon_vrshiftu:
6699 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6700 break;
6701 return SDValue();
6702
6703 case Intrinsic::arm_neon_vqshifts:
6704 case Intrinsic::arm_neon_vqshiftu:
6705 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6706 break;
6707 return SDValue();
6708
6709 case Intrinsic::arm_neon_vqshiftsu:
6710 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6711 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006712 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006713
6714 case Intrinsic::arm_neon_vshiftn:
6715 case Intrinsic::arm_neon_vrshiftn:
6716 case Intrinsic::arm_neon_vqshiftns:
6717 case Intrinsic::arm_neon_vqshiftnu:
6718 case Intrinsic::arm_neon_vqshiftnsu:
6719 case Intrinsic::arm_neon_vqrshiftns:
6720 case Intrinsic::arm_neon_vqrshiftnu:
6721 case Intrinsic::arm_neon_vqrshiftnsu:
6722 // Narrowing shifts require an immediate right shift.
6723 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6724 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006725 llvm_unreachable("invalid shift count for narrowing vector shift "
6726 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006727
6728 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006729 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006730 }
6731
6732 switch (IntNo) {
6733 case Intrinsic::arm_neon_vshifts:
6734 case Intrinsic::arm_neon_vshiftu:
6735 // Opcode already set above.
6736 break;
6737 case Intrinsic::arm_neon_vshiftls:
6738 case Intrinsic::arm_neon_vshiftlu:
6739 if (Cnt == VT.getVectorElementType().getSizeInBits())
6740 VShiftOpc = ARMISD::VSHLLi;
6741 else
6742 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6743 ARMISD::VSHLLs : ARMISD::VSHLLu);
6744 break;
6745 case Intrinsic::arm_neon_vshiftn:
6746 VShiftOpc = ARMISD::VSHRN; break;
6747 case Intrinsic::arm_neon_vrshifts:
6748 VShiftOpc = ARMISD::VRSHRs; break;
6749 case Intrinsic::arm_neon_vrshiftu:
6750 VShiftOpc = ARMISD::VRSHRu; break;
6751 case Intrinsic::arm_neon_vrshiftn:
6752 VShiftOpc = ARMISD::VRSHRN; break;
6753 case Intrinsic::arm_neon_vqshifts:
6754 VShiftOpc = ARMISD::VQSHLs; break;
6755 case Intrinsic::arm_neon_vqshiftu:
6756 VShiftOpc = ARMISD::VQSHLu; break;
6757 case Intrinsic::arm_neon_vqshiftsu:
6758 VShiftOpc = ARMISD::VQSHLsu; break;
6759 case Intrinsic::arm_neon_vqshiftns:
6760 VShiftOpc = ARMISD::VQSHRNs; break;
6761 case Intrinsic::arm_neon_vqshiftnu:
6762 VShiftOpc = ARMISD::VQSHRNu; break;
6763 case Intrinsic::arm_neon_vqshiftnsu:
6764 VShiftOpc = ARMISD::VQSHRNsu; break;
6765 case Intrinsic::arm_neon_vqrshiftns:
6766 VShiftOpc = ARMISD::VQRSHRNs; break;
6767 case Intrinsic::arm_neon_vqrshiftnu:
6768 VShiftOpc = ARMISD::VQRSHRNu; break;
6769 case Intrinsic::arm_neon_vqrshiftnsu:
6770 VShiftOpc = ARMISD::VQRSHRNsu; break;
6771 }
6772
6773 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006774 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006775 }
6776
6777 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006778 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006779 int64_t Cnt;
6780 unsigned VShiftOpc = 0;
6781
6782 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6783 VShiftOpc = ARMISD::VSLI;
6784 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6785 VShiftOpc = ARMISD::VSRI;
6786 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006787 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006788 }
6789
6790 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6791 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006792 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006793 }
6794
6795 case Intrinsic::arm_neon_vqrshifts:
6796 case Intrinsic::arm_neon_vqrshiftu:
6797 // No immediate versions of these to check for.
6798 break;
6799 }
6800
6801 return SDValue();
6802}
6803
6804/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6805/// lowers them. As with the vector shift intrinsics, this is done during DAG
6806/// combining instead of DAG legalizing because the build_vectors for 64-bit
6807/// vector element shift counts are generally not legal, and it is hard to see
6808/// their values after they get legalized to loads from a constant pool.
6809static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6810 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006811 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006812
6813 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006814 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6815 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006816 return SDValue();
6817
6818 assert(ST->hasNEON() && "unexpected vector shift");
6819 int64_t Cnt;
6820
6821 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006822 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006823
6824 case ISD::SHL:
6825 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6826 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006827 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006828 break;
6829
6830 case ISD::SRA:
6831 case ISD::SRL:
6832 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6833 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6834 ARMISD::VSHRs : ARMISD::VSHRu);
6835 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006836 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006837 }
6838 }
6839 return SDValue();
6840}
6841
6842/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6843/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6844static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6845 const ARMSubtarget *ST) {
6846 SDValue N0 = N->getOperand(0);
6847
6848 // Check for sign- and zero-extensions of vector extract operations of 8-
6849 // and 16-bit vector elements. NEON supports these directly. They are
6850 // handled during DAG combining because type legalization will promote them
6851 // to 32-bit types and it is messy to recognize the operations after that.
6852 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6853 SDValue Vec = N0.getOperand(0);
6854 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006855 EVT VT = N->getValueType(0);
6856 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006857 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6858
Owen Anderson825b72b2009-08-11 20:47:22 +00006859 if (VT == MVT::i32 &&
6860 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006861 TLI.isTypeLegal(Vec.getValueType()) &&
6862 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006863
6864 unsigned Opc = 0;
6865 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006866 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006867 case ISD::SIGN_EXTEND:
6868 Opc = ARMISD::VGETLANEs;
6869 break;
6870 case ISD::ZERO_EXTEND:
6871 case ISD::ANY_EXTEND:
6872 Opc = ARMISD::VGETLANEu;
6873 break;
6874 }
6875 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6876 }
6877 }
6878
6879 return SDValue();
6880}
6881
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006882/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6883/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6884static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6885 const ARMSubtarget *ST) {
6886 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006887 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006888 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6889 // a NaN; only do the transformation when it matches that behavior.
6890
6891 // For now only do this when using NEON for FP operations; if using VFP, it
6892 // is not obvious that the benefit outweighs the cost of switching to the
6893 // NEON pipeline.
6894 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6895 N->getValueType(0) != MVT::f32)
6896 return SDValue();
6897
6898 SDValue CondLHS = N->getOperand(0);
6899 SDValue CondRHS = N->getOperand(1);
6900 SDValue LHS = N->getOperand(2);
6901 SDValue RHS = N->getOperand(3);
6902 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6903
6904 unsigned Opcode = 0;
6905 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006906 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006907 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006908 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006909 IsReversed = true ; // x CC y ? y : x
6910 } else {
6911 return SDValue();
6912 }
6913
Bob Wilsone742bb52010-02-24 22:15:53 +00006914 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006915 switch (CC) {
6916 default: break;
6917 case ISD::SETOLT:
6918 case ISD::SETOLE:
6919 case ISD::SETLT:
6920 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006921 case ISD::SETULT:
6922 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006923 // If LHS is NaN, an ordered comparison will be false and the result will
6924 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6925 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6926 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6927 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6928 break;
6929 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6930 // will return -0, so vmin can only be used for unsafe math or if one of
6931 // the operands is known to be nonzero.
6932 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6933 !UnsafeFPMath &&
6934 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6935 break;
6936 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006937 break;
6938
6939 case ISD::SETOGT:
6940 case ISD::SETOGE:
6941 case ISD::SETGT:
6942 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006943 case ISD::SETUGT:
6944 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006945 // If LHS is NaN, an ordered comparison will be false and the result will
6946 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6947 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6948 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6949 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6950 break;
6951 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6952 // will return +0, so vmax can only be used for unsafe math or if one of
6953 // the operands is known to be nonzero.
6954 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6955 !UnsafeFPMath &&
6956 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6957 break;
6958 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006959 break;
6960 }
6961
6962 if (!Opcode)
6963 return SDValue();
6964 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6965}
6966
Evan Chenge721f5c2011-07-13 00:42:17 +00006967/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
6968SDValue
6969ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
6970 SDValue Cmp = N->getOperand(4);
6971 if (Cmp.getOpcode() != ARMISD::CMPZ)
6972 // Only looking at EQ and NE cases.
6973 return SDValue();
6974
6975 EVT VT = N->getValueType(0);
6976 DebugLoc dl = N->getDebugLoc();
6977 SDValue LHS = Cmp.getOperand(0);
6978 SDValue RHS = Cmp.getOperand(1);
6979 SDValue FalseVal = N->getOperand(0);
6980 SDValue TrueVal = N->getOperand(1);
6981 SDValue ARMcc = N->getOperand(2);
6982 ARMCC::CondCodes CC = (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
6983
6984 // Simplify
6985 // mov r1, r0
6986 // cmp r1, x
6987 // mov r0, y
6988 // moveq r0, x
6989 // to
6990 // cmp r0, x
6991 // movne r0, y
6992 //
6993 // mov r1, r0
6994 // cmp r1, x
6995 // mov r0, x
6996 // movne r0, y
6997 // to
6998 // cmp r0, x
6999 // movne r0, y
7000 /// FIXME: Turn this into a target neutral optimization?
7001 SDValue Res;
7002 if (CC == ARMCC::NE && FalseVal == RHS) {
7003 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
7004 N->getOperand(3), Cmp);
7005 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
7006 SDValue ARMcc;
7007 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
7008 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
7009 N->getOperand(3), NewCmp);
7010 }
7011
7012 if (Res.getNode()) {
7013 APInt KnownZero, KnownOne;
7014 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
7015 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
7016 // Capture demanded bits information that would be otherwise lost.
7017 if (KnownZero == 0xfffffffe)
7018 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7019 DAG.getValueType(MVT::i1));
7020 else if (KnownZero == 0xffffff00)
7021 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7022 DAG.getValueType(MVT::i8));
7023 else if (KnownZero == 0xffff0000)
7024 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7025 DAG.getValueType(MVT::i16));
7026 }
7027
7028 return Res;
7029}
7030
Dan Gohman475871a2008-07-27 21:46:04 +00007031SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007032 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007033 switch (N->getOpcode()) {
7034 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00007035 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007036 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007037 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007038 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00007039 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00007040 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00007041 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007042 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00007043 case ISD::STORE: return PerformSTORECombine(N, DCI);
7044 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
7045 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00007046 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007047 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00007048 case ISD::FP_TO_SINT:
7049 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
7050 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007051 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00007052 case ISD::SHL:
7053 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007054 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00007055 case ISD::SIGN_EXTEND:
7056 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007057 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
7058 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00007059 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00007060 case ARMISD::VLD2DUP:
7061 case ARMISD::VLD3DUP:
7062 case ARMISD::VLD4DUP:
7063 return CombineBaseUpdate(N, DCI);
7064 case ISD::INTRINSIC_VOID:
7065 case ISD::INTRINSIC_W_CHAIN:
7066 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7067 case Intrinsic::arm_neon_vld1:
7068 case Intrinsic::arm_neon_vld2:
7069 case Intrinsic::arm_neon_vld3:
7070 case Intrinsic::arm_neon_vld4:
7071 case Intrinsic::arm_neon_vld2lane:
7072 case Intrinsic::arm_neon_vld3lane:
7073 case Intrinsic::arm_neon_vld4lane:
7074 case Intrinsic::arm_neon_vst1:
7075 case Intrinsic::arm_neon_vst2:
7076 case Intrinsic::arm_neon_vst3:
7077 case Intrinsic::arm_neon_vst4:
7078 case Intrinsic::arm_neon_vst2lane:
7079 case Intrinsic::arm_neon_vst3lane:
7080 case Intrinsic::arm_neon_vst4lane:
7081 return CombineBaseUpdate(N, DCI);
7082 default: break;
7083 }
7084 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007085 }
Dan Gohman475871a2008-07-27 21:46:04 +00007086 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007087}
7088
Evan Cheng31959b12011-02-02 01:06:55 +00007089bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
7090 EVT VT) const {
7091 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
7092}
7093
Bill Wendlingaf566342009-08-15 21:21:19 +00007094bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00007095 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00007096 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00007097
7098 switch (VT.getSimpleVT().SimpleTy) {
7099 default:
7100 return false;
7101 case MVT::i8:
7102 case MVT::i16:
7103 case MVT::i32:
7104 return true;
7105 // FIXME: VLD1 etc with standard alignment is legal.
7106 }
7107}
7108
Evan Chenge6c835f2009-08-14 20:09:37 +00007109static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
7110 if (V < 0)
7111 return false;
7112
7113 unsigned Scale = 1;
7114 switch (VT.getSimpleVT().SimpleTy) {
7115 default: return false;
7116 case MVT::i1:
7117 case MVT::i8:
7118 // Scale == 1;
7119 break;
7120 case MVT::i16:
7121 // Scale == 2;
7122 Scale = 2;
7123 break;
7124 case MVT::i32:
7125 // Scale == 4;
7126 Scale = 4;
7127 break;
7128 }
7129
7130 if ((V & (Scale - 1)) != 0)
7131 return false;
7132 V /= Scale;
7133 return V == (V & ((1LL << 5) - 1));
7134}
7135
7136static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
7137 const ARMSubtarget *Subtarget) {
7138 bool isNeg = false;
7139 if (V < 0) {
7140 isNeg = true;
7141 V = - V;
7142 }
7143
7144 switch (VT.getSimpleVT().SimpleTy) {
7145 default: return false;
7146 case MVT::i1:
7147 case MVT::i8:
7148 case MVT::i16:
7149 case MVT::i32:
7150 // + imm12 or - imm8
7151 if (isNeg)
7152 return V == (V & ((1LL << 8) - 1));
7153 return V == (V & ((1LL << 12) - 1));
7154 case MVT::f32:
7155 case MVT::f64:
7156 // Same as ARM mode. FIXME: NEON?
7157 if (!Subtarget->hasVFP2())
7158 return false;
7159 if ((V & 3) != 0)
7160 return false;
7161 V >>= 2;
7162 return V == (V & ((1LL << 8) - 1));
7163 }
7164}
7165
Evan Chengb01fad62007-03-12 23:30:29 +00007166/// isLegalAddressImmediate - Return true if the integer value can be used
7167/// as the offset of the target addressing mode for load / store of the
7168/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00007169static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007170 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00007171 if (V == 0)
7172 return true;
7173
Evan Cheng65011532009-03-09 19:15:00 +00007174 if (!VT.isSimple())
7175 return false;
7176
Evan Chenge6c835f2009-08-14 20:09:37 +00007177 if (Subtarget->isThumb1Only())
7178 return isLegalT1AddressImmediate(V, VT);
7179 else if (Subtarget->isThumb2())
7180 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007181
Evan Chenge6c835f2009-08-14 20:09:37 +00007182 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007183 if (V < 0)
7184 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007185 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007186 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007187 case MVT::i1:
7188 case MVT::i8:
7189 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007190 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007191 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007192 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007193 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007194 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007195 case MVT::f32:
7196 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007197 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007198 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007199 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007200 return false;
7201 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007202 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007203 }
Evan Chenga8e29892007-01-19 07:51:42 +00007204}
7205
Evan Chenge6c835f2009-08-14 20:09:37 +00007206bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7207 EVT VT) const {
7208 int Scale = AM.Scale;
7209 if (Scale < 0)
7210 return false;
7211
7212 switch (VT.getSimpleVT().SimpleTy) {
7213 default: return false;
7214 case MVT::i1:
7215 case MVT::i8:
7216 case MVT::i16:
7217 case MVT::i32:
7218 if (Scale == 1)
7219 return true;
7220 // r + r << imm
7221 Scale = Scale & ~1;
7222 return Scale == 2 || Scale == 4 || Scale == 8;
7223 case MVT::i64:
7224 // r + r
7225 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7226 return true;
7227 return false;
7228 case MVT::isVoid:
7229 // Note, we allow "void" uses (basically, uses that aren't loads or
7230 // stores), because arm allows folding a scale into many arithmetic
7231 // operations. This should be made more precise and revisited later.
7232
7233 // Allow r << imm, but the imm has to be a multiple of two.
7234 if (Scale & 1) return false;
7235 return isPowerOf2_32(Scale);
7236 }
7237}
7238
Chris Lattner37caf8c2007-04-09 23:33:39 +00007239/// isLegalAddressingMode - Return true if the addressing mode represented
7240/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007241bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007242 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007243 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007244 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007245 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007246
Chris Lattner37caf8c2007-04-09 23:33:39 +00007247 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007248 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007249 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007250
Chris Lattner37caf8c2007-04-09 23:33:39 +00007251 switch (AM.Scale) {
7252 case 0: // no scale reg, must be "r+i" or "r", or "i".
7253 break;
7254 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007255 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007256 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007257 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007258 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007259 // ARM doesn't support any R+R*scale+imm addr modes.
7260 if (AM.BaseOffs)
7261 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007262
Bob Wilson2c7dab12009-04-08 17:55:28 +00007263 if (!VT.isSimple())
7264 return false;
7265
Evan Chenge6c835f2009-08-14 20:09:37 +00007266 if (Subtarget->isThumb2())
7267 return isLegalT2ScaledAddressingMode(AM, VT);
7268
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007269 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007270 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007271 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007272 case MVT::i1:
7273 case MVT::i8:
7274 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007275 if (Scale < 0) Scale = -Scale;
7276 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007277 return true;
7278 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007279 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007280 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007281 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007282 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007283 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007284 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007285 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007286
Owen Anderson825b72b2009-08-11 20:47:22 +00007287 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007288 // Note, we allow "void" uses (basically, uses that aren't loads or
7289 // stores), because arm allows folding a scale into many arithmetic
7290 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007291
Chris Lattner37caf8c2007-04-09 23:33:39 +00007292 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007293 if (Scale & 1) return false;
7294 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007295 }
7296 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007297 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007298 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007299}
7300
Evan Cheng77e47512009-11-11 19:05:52 +00007301/// isLegalICmpImmediate - Return true if the specified immediate is legal
7302/// icmp immediate, that is the target has icmp instructions which can compare
7303/// a register against the immediate without having to materialize the
7304/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007305bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007306 if (!Subtarget->isThumb())
7307 return ARM_AM::getSOImmVal(Imm) != -1;
7308 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007309 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007310 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007311}
7312
Dan Gohmancca82142011-05-03 00:46:49 +00007313/// isLegalAddImmediate - Return true if the specified immediate is legal
7314/// add immediate, that is the target has add instructions which can add
7315/// a register with the immediate without having to materialize the
7316/// immediate into a register.
7317bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7318 return ARM_AM::getSOImmVal(Imm) != -1;
7319}
7320
Owen Andersone50ed302009-08-10 22:56:29 +00007321static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007322 bool isSEXTLoad, SDValue &Base,
7323 SDValue &Offset, bool &isInc,
7324 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007325 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7326 return false;
7327
Owen Anderson825b72b2009-08-11 20:47:22 +00007328 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007329 // AddressingMode 3
7330 Base = Ptr->getOperand(0);
7331 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007332 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007333 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007334 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007335 isInc = false;
7336 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7337 return true;
7338 }
7339 }
7340 isInc = (Ptr->getOpcode() == ISD::ADD);
7341 Offset = Ptr->getOperand(1);
7342 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007343 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007344 // AddressingMode 2
7345 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007346 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007347 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007348 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007349 isInc = false;
7350 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7351 Base = Ptr->getOperand(0);
7352 return true;
7353 }
7354 }
7355
7356 if (Ptr->getOpcode() == ISD::ADD) {
7357 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00007358 ARM_AM::ShiftOpc ShOpcVal=
7359 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00007360 if (ShOpcVal != ARM_AM::no_shift) {
7361 Base = Ptr->getOperand(1);
7362 Offset = Ptr->getOperand(0);
7363 } else {
7364 Base = Ptr->getOperand(0);
7365 Offset = Ptr->getOperand(1);
7366 }
7367 return true;
7368 }
7369
7370 isInc = (Ptr->getOpcode() == ISD::ADD);
7371 Base = Ptr->getOperand(0);
7372 Offset = Ptr->getOperand(1);
7373 return true;
7374 }
7375
Jim Grosbache5165492009-11-09 00:11:35 +00007376 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007377 return false;
7378}
7379
Owen Andersone50ed302009-08-10 22:56:29 +00007380static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007381 bool isSEXTLoad, SDValue &Base,
7382 SDValue &Offset, bool &isInc,
7383 SelectionDAG &DAG) {
7384 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7385 return false;
7386
7387 Base = Ptr->getOperand(0);
7388 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7389 int RHSC = (int)RHS->getZExtValue();
7390 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7391 assert(Ptr->getOpcode() == ISD::ADD);
7392 isInc = false;
7393 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7394 return true;
7395 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7396 isInc = Ptr->getOpcode() == ISD::ADD;
7397 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7398 return true;
7399 }
7400 }
7401
7402 return false;
7403}
7404
Evan Chenga8e29892007-01-19 07:51:42 +00007405/// getPreIndexedAddressParts - returns true by value, base pointer and
7406/// offset pointer and addressing mode by reference if the node's address
7407/// can be legally represented as pre-indexed load / store address.
7408bool
Dan Gohman475871a2008-07-27 21:46:04 +00007409ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7410 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007411 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007412 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007413 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007414 return false;
7415
Owen Andersone50ed302009-08-10 22:56:29 +00007416 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007417 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007418 bool isSEXTLoad = false;
7419 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7420 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007421 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007422 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7423 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7424 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007425 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007426 } else
7427 return false;
7428
7429 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007430 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007431 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007432 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7433 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007434 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007435 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007436 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007437 if (!isLegal)
7438 return false;
7439
7440 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7441 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007442}
7443
7444/// getPostIndexedAddressParts - returns true by value, base pointer and
7445/// offset pointer and addressing mode by reference if this node can be
7446/// combined with a load / store to form a post-indexed load / store.
7447bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007448 SDValue &Base,
7449 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007450 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007451 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007452 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007453 return false;
7454
Owen Andersone50ed302009-08-10 22:56:29 +00007455 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007456 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007457 bool isSEXTLoad = false;
7458 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007459 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007460 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007461 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7462 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007463 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007464 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007465 } else
7466 return false;
7467
7468 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007469 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007470 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007471 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007472 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007473 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007474 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7475 isInc, DAG);
7476 if (!isLegal)
7477 return false;
7478
Evan Cheng28dad2a2010-05-18 21:31:17 +00007479 if (Ptr != Base) {
7480 // Swap base ptr and offset to catch more post-index load / store when
7481 // it's legal. In Thumb2 mode, offset must be an immediate.
7482 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7483 !Subtarget->isThumb2())
7484 std::swap(Base, Offset);
7485
7486 // Post-indexed load / store update the base pointer.
7487 if (Ptr != Base)
7488 return false;
7489 }
7490
Evan Chenge88d5ce2009-07-02 07:28:31 +00007491 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7492 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007493}
7494
Dan Gohman475871a2008-07-27 21:46:04 +00007495void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007496 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007497 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007498 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007499 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007500 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007501 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007502 switch (Op.getOpcode()) {
7503 default: break;
7504 case ARMISD::CMOV: {
7505 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007506 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007507 if (KnownZero == 0 && KnownOne == 0) return;
7508
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007509 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007510 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7511 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007512 KnownZero &= KnownZeroRHS;
7513 KnownOne &= KnownOneRHS;
7514 return;
7515 }
7516 }
7517}
7518
7519//===----------------------------------------------------------------------===//
7520// ARM Inline Assembly Support
7521//===----------------------------------------------------------------------===//
7522
Evan Cheng55d42002011-01-08 01:24:27 +00007523bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7524 // Looking for "rev" which is V6+.
7525 if (!Subtarget->hasV6Ops())
7526 return false;
7527
7528 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7529 std::string AsmStr = IA->getAsmString();
7530 SmallVector<StringRef, 4> AsmPieces;
7531 SplitString(AsmStr, AsmPieces, ";\n");
7532
7533 switch (AsmPieces.size()) {
7534 default: return false;
7535 case 1:
7536 AsmStr = AsmPieces[0];
7537 AsmPieces.clear();
7538 SplitString(AsmStr, AsmPieces, " \t,");
7539
7540 // rev $0, $1
7541 if (AsmPieces.size() == 3 &&
7542 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7543 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007544 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00007545 if (Ty && Ty->getBitWidth() == 32)
7546 return IntrinsicLowering::LowerToByteSwap(CI);
7547 }
7548 break;
7549 }
7550
7551 return false;
7552}
7553
Evan Chenga8e29892007-01-19 07:51:42 +00007554/// getConstraintType - Given a constraint letter, return the type of
7555/// constraint it is for this target.
7556ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007557ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7558 if (Constraint.size() == 1) {
7559 switch (Constraint[0]) {
7560 default: break;
7561 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007562 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00007563 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007564 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007565 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00007566 case 'j': return C_Other; // Constant for movw.
Chris Lattner4234f572007-03-25 02:14:49 +00007567 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007568 } else if (Constraint.size() == 2) {
7569 switch (Constraint[0]) {
7570 default: break;
7571 // All 'U+' constraints are addresses.
7572 case 'U': return C_Memory;
7573 }
Evan Chenga8e29892007-01-19 07:51:42 +00007574 }
Chris Lattner4234f572007-03-25 02:14:49 +00007575 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007576}
7577
John Thompson44ab89e2010-10-29 17:29:13 +00007578/// Examine constraint type and operand type and determine a weight value.
7579/// This object must already have been set up with the operand type
7580/// and the current alternative constraint selected.
7581TargetLowering::ConstraintWeight
7582ARMTargetLowering::getSingleConstraintMatchWeight(
7583 AsmOperandInfo &info, const char *constraint) const {
7584 ConstraintWeight weight = CW_Invalid;
7585 Value *CallOperandVal = info.CallOperandVal;
7586 // If we don't have a value, we can't do a match,
7587 // but allow it at the lowest weight.
7588 if (CallOperandVal == NULL)
7589 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007590 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007591 // Look at the constraint type.
7592 switch (*constraint) {
7593 default:
7594 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7595 break;
7596 case 'l':
7597 if (type->isIntegerTy()) {
7598 if (Subtarget->isThumb())
7599 weight = CW_SpecificReg;
7600 else
7601 weight = CW_Register;
7602 }
7603 break;
7604 case 'w':
7605 if (type->isFloatingPointTy())
7606 weight = CW_Register;
7607 break;
7608 }
7609 return weight;
7610}
7611
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007612typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7613RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007614ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007615 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007616 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007617 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007618 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007619 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007620 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007621 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007622 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007623 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007624 case 'h': // High regs or no regs.
7625 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007626 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007627 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007628 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007629 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007630 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007631 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007632 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007633 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007634 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007635 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007636 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007637 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007638 case 'x':
7639 if (VT == MVT::f32)
7640 return RCPair(0U, ARM::SPR_8RegisterClass);
7641 if (VT.getSizeInBits() == 64)
7642 return RCPair(0U, ARM::DPR_8RegisterClass);
7643 if (VT.getSizeInBits() == 128)
7644 return RCPair(0U, ARM::QPR_8RegisterClass);
7645 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007646 case 't':
7647 if (VT == MVT::f32)
7648 return RCPair(0U, ARM::SPRRegisterClass);
7649 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007650 }
7651 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007652 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007653 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007654
Evan Chenga8e29892007-01-19 07:51:42 +00007655 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7656}
7657
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007658/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7659/// vector. If it is invalid, don't add anything to Ops.
7660void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007661 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007662 std::vector<SDValue>&Ops,
7663 SelectionDAG &DAG) const {
7664 SDValue Result(0, 0);
7665
Eric Christopher100c8332011-06-02 23:16:42 +00007666 // Currently only support length 1 constraints.
7667 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007668
Eric Christopher100c8332011-06-02 23:16:42 +00007669 char ConstraintLetter = Constraint[0];
7670 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007671 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007672 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007673 case 'I': case 'J': case 'K': case 'L':
7674 case 'M': case 'N': case 'O':
7675 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7676 if (!C)
7677 return;
7678
7679 int64_t CVal64 = C->getSExtValue();
7680 int CVal = (int) CVal64;
7681 // None of these constraints allow values larger than 32 bits. Check
7682 // that the value fits in an int.
7683 if (CVal != CVal64)
7684 return;
7685
Eric Christopher100c8332011-06-02 23:16:42 +00007686 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007687 case 'j':
7688 // Constant suitable for movw, must be between 0 and
7689 // 65535.
7690 if (Subtarget->hasV6T2Ops())
7691 if (CVal >= 0 && CVal <= 65535)
7692 break;
7693 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007694 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007695 if (Subtarget->isThumb1Only()) {
7696 // This must be a constant between 0 and 255, for ADD
7697 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007698 if (CVal >= 0 && CVal <= 255)
7699 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007700 } else if (Subtarget->isThumb2()) {
7701 // A constant that can be used as an immediate value in a
7702 // data-processing instruction.
7703 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7704 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007705 } else {
7706 // A constant that can be used as an immediate value in a
7707 // data-processing instruction.
7708 if (ARM_AM::getSOImmVal(CVal) != -1)
7709 break;
7710 }
7711 return;
7712
7713 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007714 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007715 // This must be a constant between -255 and -1, for negated ADD
7716 // immediates. This can be used in GCC with an "n" modifier that
7717 // prints the negated value, for use with SUB instructions. It is
7718 // not useful otherwise but is implemented for compatibility.
7719 if (CVal >= -255 && CVal <= -1)
7720 break;
7721 } else {
7722 // This must be a constant between -4095 and 4095. It is not clear
7723 // what this constraint is intended for. Implemented for
7724 // compatibility with GCC.
7725 if (CVal >= -4095 && CVal <= 4095)
7726 break;
7727 }
7728 return;
7729
7730 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007731 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007732 // A 32-bit value where only one byte has a nonzero value. Exclude
7733 // zero to match GCC. This constraint is used by GCC internally for
7734 // constants that can be loaded with a move/shift combination.
7735 // It is not useful otherwise but is implemented for compatibility.
7736 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7737 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007738 } else if (Subtarget->isThumb2()) {
7739 // A constant whose bitwise inverse can be used as an immediate
7740 // value in a data-processing instruction. This can be used in GCC
7741 // with a "B" modifier that prints the inverted value, for use with
7742 // BIC and MVN instructions. It is not useful otherwise but is
7743 // implemented for compatibility.
7744 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7745 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007746 } else {
7747 // A constant whose bitwise inverse can be used as an immediate
7748 // value in a data-processing instruction. This can be used in GCC
7749 // with a "B" modifier that prints the inverted value, for use with
7750 // BIC and MVN instructions. It is not useful otherwise but is
7751 // implemented for compatibility.
7752 if (ARM_AM::getSOImmVal(~CVal) != -1)
7753 break;
7754 }
7755 return;
7756
7757 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007758 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007759 // This must be a constant between -7 and 7,
7760 // for 3-operand ADD/SUB immediate instructions.
7761 if (CVal >= -7 && CVal < 7)
7762 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007763 } else if (Subtarget->isThumb2()) {
7764 // A constant whose negation can be used as an immediate value in a
7765 // data-processing instruction. This can be used in GCC with an "n"
7766 // modifier that prints the negated value, for use with SUB
7767 // instructions. It is not useful otherwise but is implemented for
7768 // compatibility.
7769 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7770 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007771 } else {
7772 // A constant whose negation can be used as an immediate value in a
7773 // data-processing instruction. This can be used in GCC with an "n"
7774 // modifier that prints the negated value, for use with SUB
7775 // instructions. It is not useful otherwise but is implemented for
7776 // compatibility.
7777 if (ARM_AM::getSOImmVal(-CVal) != -1)
7778 break;
7779 }
7780 return;
7781
7782 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007783 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007784 // This must be a multiple of 4 between 0 and 1020, for
7785 // ADD sp + immediate.
7786 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7787 break;
7788 } else {
7789 // A power of two or a constant between 0 and 32. This is used in
7790 // GCC for the shift amount on shifted register operands, but it is
7791 // useful in general for any shift amounts.
7792 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7793 break;
7794 }
7795 return;
7796
7797 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007798 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007799 // This must be a constant between 0 and 31, for shift amounts.
7800 if (CVal >= 0 && CVal <= 31)
7801 break;
7802 }
7803 return;
7804
7805 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007806 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007807 // This must be a multiple of 4 between -508 and 508, for
7808 // ADD/SUB sp = sp + immediate.
7809 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7810 break;
7811 }
7812 return;
7813 }
7814 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7815 break;
7816 }
7817
7818 if (Result.getNode()) {
7819 Ops.push_back(Result);
7820 return;
7821 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007822 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007823}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007824
7825bool
7826ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7827 // The ARM target isn't yet aware of offsets.
7828 return false;
7829}
Evan Cheng39382422009-10-28 01:44:26 +00007830
7831int ARM::getVFPf32Imm(const APFloat &FPImm) {
7832 APInt Imm = FPImm.bitcastToAPInt();
7833 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7834 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7835 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7836
7837 // We can handle 4 bits of mantissa.
7838 // mantissa = (16+UInt(e:f:g:h))/16.
7839 if (Mantissa & 0x7ffff)
7840 return -1;
7841 Mantissa >>= 19;
7842 if ((Mantissa & 0xf) != Mantissa)
7843 return -1;
7844
7845 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7846 if (Exp < -3 || Exp > 4)
7847 return -1;
7848 Exp = ((Exp+3) & 0x7) ^ 4;
7849
7850 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7851}
7852
7853int ARM::getVFPf64Imm(const APFloat &FPImm) {
7854 APInt Imm = FPImm.bitcastToAPInt();
7855 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7856 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7857 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7858
7859 // We can handle 4 bits of mantissa.
7860 // mantissa = (16+UInt(e:f:g:h))/16.
7861 if (Mantissa & 0xffffffffffffLL)
7862 return -1;
7863 Mantissa >>= 48;
7864 if ((Mantissa & 0xf) != Mantissa)
7865 return -1;
7866
7867 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7868 if (Exp < -3 || Exp > 4)
7869 return -1;
7870 Exp = ((Exp+3) & 0x7) ^ 4;
7871
7872 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7873}
7874
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007875bool ARM::isBitFieldInvertedMask(unsigned v) {
7876 if (v == 0xffffffff)
7877 return 0;
7878 // there can be 1's on either or both "outsides", all the "inside"
7879 // bits must be 0's
7880 unsigned int lsb = 0, msb = 31;
7881 while (v & (1 << msb)) --msb;
7882 while (v & (1 << lsb)) ++lsb;
7883 for (unsigned int i = lsb; i <= msb; ++i) {
7884 if (v & (1 << i))
7885 return 0;
7886 }
7887 return 1;
7888}
7889
Evan Cheng39382422009-10-28 01:44:26 +00007890/// isFPImmLegal - Returns true if the target can instruction select the
7891/// specified FP immediate natively. If false, the legalizer will
7892/// materialize the FP immediate as a load from a constant pool.
7893bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7894 if (!Subtarget->hasVFP3())
7895 return false;
7896 if (VT == MVT::f32)
7897 return ARM::getVFPf32Imm(Imm) != -1;
7898 if (VT == MVT::f64)
7899 return ARM::getVFPf64Imm(Imm) != -1;
7900 return false;
7901}
Bob Wilson65ffec42010-09-21 17:56:22 +00007902
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007903/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007904/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7905/// specified in the intrinsic calls.
7906bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7907 const CallInst &I,
7908 unsigned Intrinsic) const {
7909 switch (Intrinsic) {
7910 case Intrinsic::arm_neon_vld1:
7911 case Intrinsic::arm_neon_vld2:
7912 case Intrinsic::arm_neon_vld3:
7913 case Intrinsic::arm_neon_vld4:
7914 case Intrinsic::arm_neon_vld2lane:
7915 case Intrinsic::arm_neon_vld3lane:
7916 case Intrinsic::arm_neon_vld4lane: {
7917 Info.opc = ISD::INTRINSIC_W_CHAIN;
7918 // Conservatively set memVT to the entire set of vectors loaded.
7919 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7920 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7921 Info.ptrVal = I.getArgOperand(0);
7922 Info.offset = 0;
7923 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7924 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7925 Info.vol = false; // volatile loads with NEON intrinsics not supported
7926 Info.readMem = true;
7927 Info.writeMem = false;
7928 return true;
7929 }
7930 case Intrinsic::arm_neon_vst1:
7931 case Intrinsic::arm_neon_vst2:
7932 case Intrinsic::arm_neon_vst3:
7933 case Intrinsic::arm_neon_vst4:
7934 case Intrinsic::arm_neon_vst2lane:
7935 case Intrinsic::arm_neon_vst3lane:
7936 case Intrinsic::arm_neon_vst4lane: {
7937 Info.opc = ISD::INTRINSIC_VOID;
7938 // Conservatively set memVT to the entire set of vectors stored.
7939 unsigned NumElts = 0;
7940 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007941 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00007942 if (!ArgTy->isVectorTy())
7943 break;
7944 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7945 }
7946 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7947 Info.ptrVal = I.getArgOperand(0);
7948 Info.offset = 0;
7949 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7950 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7951 Info.vol = false; // volatile stores with NEON intrinsics not supported
7952 Info.readMem = false;
7953 Info.writeMem = true;
7954 return true;
7955 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007956 case Intrinsic::arm_strexd: {
7957 Info.opc = ISD::INTRINSIC_W_CHAIN;
7958 Info.memVT = MVT::i64;
7959 Info.ptrVal = I.getArgOperand(2);
7960 Info.offset = 0;
7961 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007962 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007963 Info.readMem = false;
7964 Info.writeMem = true;
7965 return true;
7966 }
7967 case Intrinsic::arm_ldrexd: {
7968 Info.opc = ISD::INTRINSIC_W_CHAIN;
7969 Info.memVT = MVT::i64;
7970 Info.ptrVal = I.getArgOperand(0);
7971 Info.offset = 0;
7972 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007973 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007974 Info.readMem = true;
7975 Info.writeMem = false;
7976 return true;
7977 }
Bob Wilson65ffec42010-09-21 17:56:22 +00007978 default:
7979 break;
7980 }
7981
7982 return false;
7983}