blob: 29fb268ef439ba4e050d0b1060ae00e632ce0577 [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
Evan Cheng342e3162011-08-30 01:34:54 +0000554 if (!Subtarget->isThumb1Only()) {
555 // FIXME: We should do this for Thumb1 as well.
556 setOperationAction(ISD::ADDC, MVT::i32, Custom);
557 setOperationAction(ISD::ADDE, MVT::i32, Custom);
558 setOperationAction(ISD::SUBC, MVT::i32, Custom);
559 setOperationAction(ISD::SUBE, MVT::i32, Custom);
560 }
561
Evan Chenga8e29892007-01-19 07:51:42 +0000562 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000564 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000566 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000568
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000569 // Only ARMv6 has BSWAP.
570 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000572
Evan Chenga8e29892007-01-19 07:51:42 +0000573 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000574 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000575 // v7M has a hardware divider
576 setOperationAction(ISD::SDIV, MVT::i32, Expand);
577 setOperationAction(ISD::UDIV, MVT::i32, Expand);
578 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 setOperationAction(ISD::SREM, MVT::i32, Expand);
580 setOperationAction(ISD::UREM, MVT::i32, Expand);
581 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
582 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000583
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
585 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
586 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
587 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000588 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000589
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000590 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000591
Evan Chenga8e29892007-01-19 07:51:42 +0000592 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::VASTART, MVT::Other, Custom);
594 setOperationAction(ISD::VAARG, MVT::Other, Expand);
595 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
596 setOperationAction(ISD::VAEND, MVT::Other, Expand);
597 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
598 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000599 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000600 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
601 setExceptionPointerRegister(ARM::R0);
602 setExceptionSelectorRegister(ARM::R1);
603
Evan Cheng3a1588a2010-04-15 22:20:34 +0000604 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000605 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
606 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000607 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000608 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000609 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000610 // membarrier needs custom lowering; the rest are legal and handled
611 // normally.
612 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000613 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000614 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
615 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000616 } else {
617 // Set them all for expansion, which will force libcalls.
618 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000619 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000620 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000621 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000622 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000623 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000624 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000625 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000626 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000627 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000628 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000629 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000630 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000631 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman069e2ed2011-08-26 02:59:24 +0000632 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
633 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000634 // Since the libcalls include locking, fold in the fences
635 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000636 }
Evan Chenga8e29892007-01-19 07:51:42 +0000637
Evan Cheng416941d2010-11-04 05:19:35 +0000638 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000639
Eli Friedmana2c6f452010-06-26 04:36:50 +0000640 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
641 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
643 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000644 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000646
Nate Begemand1fb5832010-08-03 21:31:55 +0000647 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000648 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
649 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000650 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000651 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
652 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000653
654 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000656 if (Subtarget->isTargetDarwin()) {
657 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
658 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000659 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000660 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000661 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000662
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::SETCC, MVT::i32, Expand);
664 setOperationAction(ISD::SETCC, MVT::f32, Expand);
665 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000666 setOperationAction(ISD::SELECT, MVT::i32, Custom);
667 setOperationAction(ISD::SELECT, MVT::f32, Custom);
668 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
670 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
671 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000672
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
674 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
675 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
676 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
677 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000678
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000679 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::FSIN, MVT::f64, Expand);
681 setOperationAction(ISD::FSIN, MVT::f32, Expand);
682 setOperationAction(ISD::FCOS, MVT::f32, Expand);
683 setOperationAction(ISD::FCOS, MVT::f64, Expand);
684 setOperationAction(ISD::FREM, MVT::f64, Expand);
685 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000686 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
688 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000689 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::FPOW, MVT::f64, Expand);
691 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000692
Cameron Zwarich33390842011-07-08 21:39:21 +0000693 setOperationAction(ISD::FMA, MVT::f64, Expand);
694 setOperationAction(ISD::FMA, MVT::f32, Expand);
695
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000696 // Various VFP goodness
697 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000698 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
699 if (Subtarget->hasVFP2()) {
700 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
701 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
702 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
703 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
704 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000705 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000706 if (!Subtarget->hasFP16()) {
707 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
708 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000709 }
Evan Cheng110cf482008-04-01 01:50:16 +0000710 }
Evan Chenga8e29892007-01-19 07:51:42 +0000711
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000712 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000713 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000714 setTargetDAGCombine(ISD::ADD);
715 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000716 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000717
Owen Anderson080c0922010-11-05 19:27:46 +0000718 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000719 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000720 if (Subtarget->hasNEON())
721 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000722
Evan Chenga8e29892007-01-19 07:51:42 +0000723 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000724
Evan Chengf7d87ee2010-05-21 00:43:17 +0000725 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
726 setSchedulingPreference(Sched::RegPressure);
727 else
728 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000729
Evan Cheng05219282011-01-06 06:52:41 +0000730 //// temporary - rewrite interface to use type
731 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000732
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000733 // On ARM arguments smaller than 4 bytes are extended, so all arguments
734 // are at least 4 bytes aligned.
735 setMinStackArgumentAlignment(4);
736
Evan Chengfff606d2010-09-24 19:07:23 +0000737 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000738
739 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000740}
741
Andrew Trick32cec0a2011-01-19 02:35:27 +0000742// FIXME: It might make sense to define the representative register class as the
743// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
744// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
745// SPR's representative would be DPR_VFP2. This should work well if register
746// pressure tracking were modified such that a register use would increment the
747// pressure of the register class's representative and all of it's super
748// classes' representatives transitively. We have not implemented this because
749// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000750// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000751// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000752std::pair<const TargetRegisterClass*, uint8_t>
753ARMTargetLowering::findRepresentativeClass(EVT VT) const{
754 const TargetRegisterClass *RRC = 0;
755 uint8_t Cost = 1;
756 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000757 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000758 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000759 // Use DPR as representative register class for all floating point
760 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
761 // the cost is 1 for both f32 and f64.
762 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000763 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000764 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000765 // When NEON is used for SP, only half of the register file is available
766 // because operations that define both SP and DP results will be constrained
767 // to the VFP2 class (D0-D15). We currently model this constraint prior to
768 // coalescing by double-counting the SP regs. See the FIXME above.
769 if (Subtarget->useNEONForSinglePrecisionFP())
770 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000771 break;
772 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
773 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000774 RRC = ARM::DPRRegisterClass;
775 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000776 break;
777 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000778 RRC = ARM::DPRRegisterClass;
779 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000780 break;
781 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000782 RRC = ARM::DPRRegisterClass;
783 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000784 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000785 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000786 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000787}
788
Evan Chenga8e29892007-01-19 07:51:42 +0000789const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
790 switch (Opcode) {
791 default: return 0;
792 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000793 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000794 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000795 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
796 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000797 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000798 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
799 case ARMISD::tCALL: return "ARMISD::tCALL";
800 case ARMISD::BRCOND: return "ARMISD::BRCOND";
801 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000802 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000803 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
804 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
805 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000806 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000807 case ARMISD::CMPFP: return "ARMISD::CMPFP";
808 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000809 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000810 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
811 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000812
Jim Grosbach3482c802010-01-18 19:58:49 +0000813 case ARMISD::RBIT: return "ARMISD::RBIT";
814
Bob Wilson76a312b2010-03-19 22:51:32 +0000815 case ARMISD::FTOSI: return "ARMISD::FTOSI";
816 case ARMISD::FTOUI: return "ARMISD::FTOUI";
817 case ARMISD::SITOF: return "ARMISD::SITOF";
818 case ARMISD::UITOF: return "ARMISD::UITOF";
819
Evan Chenga8e29892007-01-19 07:51:42 +0000820 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
821 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
822 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000823
Evan Cheng342e3162011-08-30 01:34:54 +0000824 case ARMISD::ADDC: return "ARMISD::ADDC";
825 case ARMISD::ADDE: return "ARMISD::ADDE";
826 case ARMISD::SUBC: return "ARMISD::SUBC";
827 case ARMISD::SUBE: return "ARMISD::SUBE";
828
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000829 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
830 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000831
Evan Chengc5942082009-10-28 06:55:03 +0000832 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
833 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000834 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000835
Dale Johannesen51e28e62010-06-03 21:09:53 +0000836 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000837
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000838 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000839
Evan Cheng86198642009-08-07 00:34:42 +0000840 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
841
Jim Grosbach3728e962009-12-10 00:11:09 +0000842 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000843 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000844
Evan Chengdfed19f2010-11-03 06:34:55 +0000845 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
846
Bob Wilson5bafff32009-06-22 23:27:02 +0000847 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000848 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000849 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000850 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
851 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000852 case ARMISD::VCGEU: return "ARMISD::VCGEU";
853 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000854 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
855 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000856 case ARMISD::VCGTU: return "ARMISD::VCGTU";
857 case ARMISD::VTST: return "ARMISD::VTST";
858
859 case ARMISD::VSHL: return "ARMISD::VSHL";
860 case ARMISD::VSHRs: return "ARMISD::VSHRs";
861 case ARMISD::VSHRu: return "ARMISD::VSHRu";
862 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
863 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
864 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
865 case ARMISD::VSHRN: return "ARMISD::VSHRN";
866 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
867 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
868 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
869 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
870 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
871 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
872 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
873 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
874 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
875 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
876 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
877 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
878 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
879 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000880 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000881 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000882 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000883 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000884 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000885 case ARMISD::VREV64: return "ARMISD::VREV64";
886 case ARMISD::VREV32: return "ARMISD::VREV32";
887 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000888 case ARMISD::VZIP: return "ARMISD::VZIP";
889 case ARMISD::VUZP: return "ARMISD::VUZP";
890 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000891 case ARMISD::VTBL1: return "ARMISD::VTBL1";
892 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000893 case ARMISD::VMULLs: return "ARMISD::VMULLs";
894 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000895 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000896 case ARMISD::FMAX: return "ARMISD::FMAX";
897 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000898 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000899 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
900 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000901 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000902 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
903 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
904 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000905 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
906 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
907 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
908 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
909 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
910 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
911 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
912 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
913 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
914 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
915 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
916 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
917 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
918 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
919 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
920 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
921 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000922 }
923}
924
Evan Cheng06b666c2010-05-15 02:18:07 +0000925/// getRegClassFor - Return the register class that should be used for the
926/// specified value type.
927TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
928 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
929 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
930 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000931 if (Subtarget->hasNEON()) {
932 if (VT == MVT::v4i64)
933 return ARM::QQPRRegisterClass;
934 else if (VT == MVT::v8i64)
935 return ARM::QQQQPRRegisterClass;
936 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000937 return TargetLowering::getRegClassFor(VT);
938}
939
Eric Christopherab695882010-07-21 22:26:11 +0000940// Create a fast isel object.
941FastISel *
942ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
943 return ARM::createFastISel(funcInfo);
944}
945
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000946/// getMaximalGlobalOffset - Returns the maximal possible offset which can
947/// be used for loads / stores from the global.
948unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
949 return (Subtarget->isThumb1Only() ? 127 : 4095);
950}
951
Evan Cheng1cc39842010-05-20 23:26:43 +0000952Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000953 unsigned NumVals = N->getNumValues();
954 if (!NumVals)
955 return Sched::RegPressure;
956
957 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000958 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000959 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000960 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000961 if (VT.isFloatingPoint() || VT.isVector())
962 return Sched::Latency;
963 }
Evan Chengc10f5432010-05-28 23:25:23 +0000964
965 if (!N->isMachineOpcode())
966 return Sched::RegPressure;
967
968 // Load are scheduled for latency even if there instruction itinerary
969 // is not available.
970 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000971 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000972
Evan Chenge837dea2011-06-28 19:10:37 +0000973 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +0000974 return Sched::RegPressure;
975 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +0000976 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000977 return Sched::Latency;
978
Evan Cheng1cc39842010-05-20 23:26:43 +0000979 return Sched::RegPressure;
980}
981
Evan Chenga8e29892007-01-19 07:51:42 +0000982//===----------------------------------------------------------------------===//
983// Lowering Code
984//===----------------------------------------------------------------------===//
985
Evan Chenga8e29892007-01-19 07:51:42 +0000986/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
987static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
988 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000989 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000990 case ISD::SETNE: return ARMCC::NE;
991 case ISD::SETEQ: return ARMCC::EQ;
992 case ISD::SETGT: return ARMCC::GT;
993 case ISD::SETGE: return ARMCC::GE;
994 case ISD::SETLT: return ARMCC::LT;
995 case ISD::SETLE: return ARMCC::LE;
996 case ISD::SETUGT: return ARMCC::HI;
997 case ISD::SETUGE: return ARMCC::HS;
998 case ISD::SETULT: return ARMCC::LO;
999 case ISD::SETULE: return ARMCC::LS;
1000 }
1001}
1002
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001003/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1004static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001005 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001006 CondCode2 = ARMCC::AL;
1007 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001008 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001009 case ISD::SETEQ:
1010 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1011 case ISD::SETGT:
1012 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1013 case ISD::SETGE:
1014 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1015 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001016 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001017 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1018 case ISD::SETO: CondCode = ARMCC::VC; break;
1019 case ISD::SETUO: CondCode = ARMCC::VS; break;
1020 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1021 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1022 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1023 case ISD::SETLT:
1024 case ISD::SETULT: CondCode = ARMCC::LT; break;
1025 case ISD::SETLE:
1026 case ISD::SETULE: CondCode = ARMCC::LE; break;
1027 case ISD::SETNE:
1028 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1029 }
Evan Chenga8e29892007-01-19 07:51:42 +00001030}
1031
Bob Wilson1f595bb2009-04-17 19:07:39 +00001032//===----------------------------------------------------------------------===//
1033// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001034//===----------------------------------------------------------------------===//
1035
1036#include "ARMGenCallingConv.inc"
1037
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001038/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1039/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001040CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001041 bool Return,
1042 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001043 switch (CC) {
1044 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001045 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001046 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001047 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001048 if (!Subtarget->isAAPCS_ABI())
1049 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1050 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1051 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1052 }
1053 // Fallthrough
1054 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001055 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001056 if (!Subtarget->isAAPCS_ABI())
1057 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1058 else if (Subtarget->hasVFP2() &&
1059 FloatABIType == FloatABI::Hard && !isVarArg)
1060 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1061 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1062 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001063 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001064 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001065 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001066 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001067 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001068 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001069 }
1070}
1071
Dan Gohman98ca4f22009-08-05 01:29:28 +00001072/// LowerCallResult - Lower the result values of a call into the
1073/// appropriate copies out of appropriate physical registers.
1074SDValue
1075ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001076 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001077 const SmallVectorImpl<ISD::InputArg> &Ins,
1078 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001079 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001080
Bob Wilson1f595bb2009-04-17 19:07:39 +00001081 // Assign locations to each value returned by this call.
1082 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001083 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1084 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001086 CCAssignFnForNode(CallConv, /* Return*/ true,
1087 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001088
1089 // Copy all of the result registers out of their specified physreg.
1090 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1091 CCValAssign VA = RVLocs[i];
1092
Bob Wilson80915242009-04-25 00:33:20 +00001093 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001094 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001095 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001097 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001098 Chain = Lo.getValue(1);
1099 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001100 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001101 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001102 InFlag);
1103 Chain = Hi.getValue(1);
1104 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001105 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001106
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 if (VA.getLocVT() == MVT::v2f64) {
1108 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1109 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1110 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001111
1112 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001113 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001114 Chain = Lo.getValue(1);
1115 InFlag = Lo.getValue(2);
1116 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001117 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001118 Chain = Hi.getValue(1);
1119 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001120 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001121 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1122 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001123 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001124 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001125 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1126 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001127 Chain = Val.getValue(1);
1128 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129 }
Bob Wilson80915242009-04-25 00:33:20 +00001130
1131 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001132 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001133 case CCValAssign::Full: break;
1134 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001135 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001136 break;
1137 }
1138
Dan Gohman98ca4f22009-08-05 01:29:28 +00001139 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001140 }
1141
Dan Gohman98ca4f22009-08-05 01:29:28 +00001142 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001143}
1144
Bob Wilsondee46d72009-04-17 20:35:10 +00001145/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001147ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1148 SDValue StackPtr, SDValue Arg,
1149 DebugLoc dl, SelectionDAG &DAG,
1150 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001151 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001152 unsigned LocMemOffset = VA.getLocMemOffset();
1153 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1154 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001155 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001156 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001157 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001158}
1159
Dan Gohman98ca4f22009-08-05 01:29:28 +00001160void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001161 SDValue Chain, SDValue &Arg,
1162 RegsToPassVector &RegsToPass,
1163 CCValAssign &VA, CCValAssign &NextVA,
1164 SDValue &StackPtr,
1165 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001166 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001167
Jim Grosbache5165492009-11-09 00:11:35 +00001168 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001169 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001170 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1171
1172 if (NextVA.isRegLoc())
1173 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1174 else {
1175 assert(NextVA.isMemLoc());
1176 if (StackPtr.getNode() == 0)
1177 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1178
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1180 dl, DAG, NextVA,
1181 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001182 }
1183}
1184
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001186/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1187/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001188SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001189ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001190 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001191 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001193 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194 const SmallVectorImpl<ISD::InputArg> &Ins,
1195 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001196 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001197 MachineFunction &MF = DAG.getMachineFunction();
1198 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1199 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001200 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001201 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001202 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001203 if (isTailCall) {
1204 // Check if it's really possible to do a tail call.
1205 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1206 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001207 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001208 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1209 // detected sibcalls.
1210 if (isTailCall) {
1211 ++NumTailCalls;
1212 IsSibCall = true;
1213 }
1214 }
Evan Chenga8e29892007-01-19 07:51:42 +00001215
Bob Wilson1f595bb2009-04-17 19:07:39 +00001216 // Analyze operands of the call, assigning locations to each operand.
1217 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001218 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1219 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001221 CCAssignFnForNode(CallConv, /* Return*/ false,
1222 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001223
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224 // Get a count of how many bytes are to be pushed on the stack.
1225 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001226
Dale Johannesen51e28e62010-06-03 21:09:53 +00001227 // For tail calls, memory operands are available in our caller's stack.
1228 if (IsSibCall)
1229 NumBytes = 0;
1230
Evan Chenga8e29892007-01-19 07:51:42 +00001231 // Adjust the stack pointer for the new arguments...
1232 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001233 if (!IsSibCall)
1234 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001235
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001236 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001237
Bob Wilson5bafff32009-06-22 23:27:02 +00001238 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001239 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001240
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001242 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001243 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1244 i != e;
1245 ++i, ++realArgIdx) {
1246 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001247 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001248 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001249 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001250
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251 // Promote the value if needed.
1252 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001253 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 case CCValAssign::Full: break;
1255 case CCValAssign::SExt:
1256 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1257 break;
1258 case CCValAssign::ZExt:
1259 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1260 break;
1261 case CCValAssign::AExt:
1262 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1263 break;
1264 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001265 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001266 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001267 }
1268
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001269 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001270 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001271 if (VA.getLocVT() == MVT::v2f64) {
1272 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1273 DAG.getConstant(0, MVT::i32));
1274 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1275 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276
Dan Gohman98ca4f22009-08-05 01:29:28 +00001277 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001278 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1279
1280 VA = ArgLocs[++i]; // skip ahead to next loc
1281 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001282 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001283 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1284 } else {
1285 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001286
Dan Gohman98ca4f22009-08-05 01:29:28 +00001287 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1288 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001289 }
1290 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001291 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001292 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001293 }
1294 } else if (VA.isRegLoc()) {
1295 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001296 } else if (isByVal) {
1297 assert(VA.isMemLoc());
1298 unsigned offset = 0;
1299
1300 // True if this byval aggregate will be split between registers
1301 // and memory.
1302 if (CCInfo.isFirstByValRegValid()) {
1303 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1304 unsigned int i, j;
1305 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1306 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1307 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1308 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1309 MachinePointerInfo(),
1310 false, false, 0);
1311 MemOpChains.push_back(Load.getValue(1));
1312 RegsToPass.push_back(std::make_pair(j, Load));
1313 }
1314 offset = ARM::R4 - CCInfo.getFirstByValReg();
1315 CCInfo.clearFirstByValReg();
1316 }
1317
1318 unsigned LocMemOffset = VA.getLocMemOffset();
1319 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1320 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1321 StkPtrOff);
1322 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1323 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1324 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1325 MVT::i32);
1326 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1327 Flags.getByValAlign(),
1328 /*isVolatile=*/false,
1329 /*AlwaysInline=*/false,
1330 MachinePointerInfo(0),
1331 MachinePointerInfo(0)));
1332
1333 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001334 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001335
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1337 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001338 }
Evan Chenga8e29892007-01-19 07:51:42 +00001339 }
1340
1341 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001342 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001343 &MemOpChains[0], MemOpChains.size());
1344
1345 // Build a sequence of copy-to-reg nodes chained together with token chain
1346 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001347 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001348 // Tail call byval lowering might overwrite argument registers so in case of
1349 // tail call optimization the copies to registers are lowered later.
1350 if (!isTailCall)
1351 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1352 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1353 RegsToPass[i].second, InFlag);
1354 InFlag = Chain.getValue(1);
1355 }
Evan Chenga8e29892007-01-19 07:51:42 +00001356
Dale Johannesen51e28e62010-06-03 21:09:53 +00001357 // For tail calls lower the arguments to the 'real' stack slot.
1358 if (isTailCall) {
1359 // Force all the incoming stack arguments to be loaded from the stack
1360 // before any new outgoing arguments are stored to the stack, because the
1361 // outgoing stack slots may alias the incoming argument stack slots, and
1362 // the alias isn't otherwise explicit. This is slightly more conservative
1363 // than necessary, because it means that each store effectively depends
1364 // on every argument instead of just those arguments it would clobber.
1365
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001366 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001367 InFlag = SDValue();
1368 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1369 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1370 RegsToPass[i].second, InFlag);
1371 InFlag = Chain.getValue(1);
1372 }
1373 InFlag =SDValue();
1374 }
1375
Bill Wendling056292f2008-09-16 21:48:12 +00001376 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1377 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1378 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001379 bool isDirect = false;
1380 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001381 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001382 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001383
1384 if (EnableARMLongCalls) {
1385 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1386 && "long-calls with non-static relocation model!");
1387 // Handle a global address or an external symbol. If it's not one of
1388 // those, the target's already in a register, so we don't need to do
1389 // anything extra.
1390 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001391 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001392 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001393 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001394 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1395 ARMPCLabelIndex,
1396 ARMCP::CPValue, 0);
1397 // Get the address of the callee into a register
1398 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1399 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1400 Callee = DAG.getLoad(getPointerTy(), dl,
1401 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001402 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001403 false, false, 0);
1404 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1405 const char *Sym = S->getSymbol();
1406
1407 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001408 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001409 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1410 Sym, ARMPCLabelIndex, 0);
1411 // Get the address of the callee into a register
1412 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1413 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1414 Callee = DAG.getLoad(getPointerTy(), dl,
1415 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001416 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001417 false, false, 0);
1418 }
1419 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001420 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001421 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001422 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001423 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001424 getTargetMachine().getRelocationModel() != Reloc::Static;
1425 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001426 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001427 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001428 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001429 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001430 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001431 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001432 ARMPCLabelIndex,
1433 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001434 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001435 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001436 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001437 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001438 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001439 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001440 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001441 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001442 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001443 } else {
1444 // On ELF targets for PIC code, direct calls should go through the PLT
1445 unsigned OpFlags = 0;
1446 if (Subtarget->isTargetELF() &&
1447 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1448 OpFlags = ARMII::MO_PLT;
1449 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1450 }
Bill Wendling056292f2008-09-16 21:48:12 +00001451 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001452 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001453 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001454 getTargetMachine().getRelocationModel() != Reloc::Static;
1455 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001456 // tBX takes a register source operand.
1457 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001458 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001459 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001460 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001461 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001462 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001463 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001464 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001465 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001466 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001467 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001468 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001469 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001470 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001471 } else {
1472 unsigned OpFlags = 0;
1473 // On ELF targets for PIC code, direct calls should go through the PLT
1474 if (Subtarget->isTargetELF() &&
1475 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1476 OpFlags = ARMII::MO_PLT;
1477 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1478 }
Evan Chenga8e29892007-01-19 07:51:42 +00001479 }
1480
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001481 // FIXME: handle tail calls differently.
1482 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001483 if (Subtarget->isThumb()) {
1484 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001485 CallOpc = ARMISD::CALL_NOLINK;
1486 else
1487 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1488 } else {
1489 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001490 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1491 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001492 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001493
Dan Gohman475871a2008-07-27 21:46:04 +00001494 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001495 Ops.push_back(Chain);
1496 Ops.push_back(Callee);
1497
1498 // Add argument registers to the end of the list so that they are known live
1499 // into the call.
1500 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1501 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1502 RegsToPass[i].second.getValueType()));
1503
Gabor Greifba36cb52008-08-28 21:40:38 +00001504 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001505 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001506
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001507 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001508 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001509 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001510
Duncan Sands4bdcb612008-07-02 17:40:58 +00001511 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001512 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001513 InFlag = Chain.getValue(1);
1514
Chris Lattnere563bbc2008-10-11 22:08:30 +00001515 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1516 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001517 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001518 InFlag = Chain.getValue(1);
1519
Bob Wilson1f595bb2009-04-17 19:07:39 +00001520 // Handle result values, copying them out of physregs into vregs that we
1521 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001522 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1523 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001524}
1525
Stuart Hastingsf222e592011-02-28 17:17:53 +00001526/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001527/// on the stack. Remember the next parameter register to allocate,
1528/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001529/// this.
1530void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001531llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1532 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1533 assert((State->getCallOrPrologue() == Prologue ||
1534 State->getCallOrPrologue() == Call) &&
1535 "unhandled ParmContext");
1536 if ((!State->isFirstByValRegValid()) &&
1537 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1538 State->setFirstByValReg(reg);
1539 // At a call site, a byval parameter that is split between
1540 // registers and memory needs its size truncated here. In a
1541 // function prologue, such byval parameters are reassembled in
1542 // memory, and are not truncated.
1543 if (State->getCallOrPrologue() == Call) {
1544 unsigned excess = 4 * (ARM::R4 - reg);
1545 assert(size >= excess && "expected larger existing stack allocation");
1546 size -= excess;
1547 }
1548 }
1549 // Confiscate any remaining parameter registers to preclude their
1550 // assignment to subsequent parameters.
1551 while (State->AllocateReg(GPRArgRegs, 4))
1552 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001553}
1554
Dale Johannesen51e28e62010-06-03 21:09:53 +00001555/// MatchingStackOffset - Return true if the given stack call argument is
1556/// already available in the same position (relatively) of the caller's
1557/// incoming argument stack.
1558static
1559bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1560 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1561 const ARMInstrInfo *TII) {
1562 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1563 int FI = INT_MAX;
1564 if (Arg.getOpcode() == ISD::CopyFromReg) {
1565 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001566 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001567 return false;
1568 MachineInstr *Def = MRI->getVRegDef(VR);
1569 if (!Def)
1570 return false;
1571 if (!Flags.isByVal()) {
1572 if (!TII->isLoadFromStackSlot(Def, FI))
1573 return false;
1574 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001575 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001576 }
1577 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1578 if (Flags.isByVal())
1579 // ByVal argument is passed in as a pointer but it's now being
1580 // dereferenced. e.g.
1581 // define @foo(%struct.X* %A) {
1582 // tail call @bar(%struct.X* byval %A)
1583 // }
1584 return false;
1585 SDValue Ptr = Ld->getBasePtr();
1586 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1587 if (!FINode)
1588 return false;
1589 FI = FINode->getIndex();
1590 } else
1591 return false;
1592
1593 assert(FI != INT_MAX);
1594 if (!MFI->isFixedObjectIndex(FI))
1595 return false;
1596 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1597}
1598
1599/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1600/// for tail call optimization. Targets which want to do tail call
1601/// optimization should implement this function.
1602bool
1603ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1604 CallingConv::ID CalleeCC,
1605 bool isVarArg,
1606 bool isCalleeStructRet,
1607 bool isCallerStructRet,
1608 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001609 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001610 const SmallVectorImpl<ISD::InputArg> &Ins,
1611 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001612 const Function *CallerF = DAG.getMachineFunction().getFunction();
1613 CallingConv::ID CallerCC = CallerF->getCallingConv();
1614 bool CCMatch = CallerCC == CalleeCC;
1615
1616 // Look for obvious safe cases to perform tail call optimization that do not
1617 // require ABI changes. This is what gcc calls sibcall.
1618
Jim Grosbach7616b642010-06-16 23:45:49 +00001619 // Do not sibcall optimize vararg calls unless the call site is not passing
1620 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001621 if (isVarArg && !Outs.empty())
1622 return false;
1623
1624 // Also avoid sibcall optimization if either caller or callee uses struct
1625 // return semantics.
1626 if (isCalleeStructRet || isCallerStructRet)
1627 return false;
1628
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001629 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001630 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1631 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1632 // support in the assembler and linker to be used. This would need to be
1633 // fixed to fully support tail calls in Thumb1.
1634 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001635 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1636 // LR. This means if we need to reload LR, it takes an extra instructions,
1637 // which outweighs the value of the tail call; but here we don't know yet
1638 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001639 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001640 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001641
1642 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1643 // but we need to make sure there are enough registers; the only valid
1644 // registers are the 4 used for parameters. We don't currently do this
1645 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001646 if (Subtarget->isThumb1Only())
1647 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001648
Dale Johannesen51e28e62010-06-03 21:09:53 +00001649 // If the calling conventions do not match, then we'd better make sure the
1650 // results are returned in the same way as what the caller expects.
1651 if (!CCMatch) {
1652 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001653 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1654 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001655 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1656
1657 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001658 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1659 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001660 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1661
1662 if (RVLocs1.size() != RVLocs2.size())
1663 return false;
1664 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1665 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1666 return false;
1667 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1668 return false;
1669 if (RVLocs1[i].isRegLoc()) {
1670 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1671 return false;
1672 } else {
1673 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1674 return false;
1675 }
1676 }
1677 }
1678
1679 // If the callee takes no arguments then go on to check the results of the
1680 // call.
1681 if (!Outs.empty()) {
1682 // Check if stack adjustment is needed. For now, do not do this if any
1683 // argument is passed on the stack.
1684 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001685 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1686 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001687 CCInfo.AnalyzeCallOperands(Outs,
1688 CCAssignFnForNode(CalleeCC, false, isVarArg));
1689 if (CCInfo.getNextStackOffset()) {
1690 MachineFunction &MF = DAG.getMachineFunction();
1691
1692 // Check if the arguments are already laid out in the right way as
1693 // the caller's fixed stack objects.
1694 MachineFrameInfo *MFI = MF.getFrameInfo();
1695 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1696 const ARMInstrInfo *TII =
1697 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001698 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1699 i != e;
1700 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001701 CCValAssign &VA = ArgLocs[i];
1702 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001703 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001704 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001705 if (VA.getLocInfo() == CCValAssign::Indirect)
1706 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001707 if (VA.needsCustom()) {
1708 // f64 and vector types are split into multiple registers or
1709 // register/stack-slot combinations. The types will not match
1710 // the registers; give up on memory f64 refs until we figure
1711 // out what to do about this.
1712 if (!VA.isRegLoc())
1713 return false;
1714 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001715 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001716 if (RegVT == MVT::v2f64) {
1717 if (!ArgLocs[++i].isRegLoc())
1718 return false;
1719 if (!ArgLocs[++i].isRegLoc())
1720 return false;
1721 }
1722 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001723 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1724 MFI, MRI, TII))
1725 return false;
1726 }
1727 }
1728 }
1729 }
1730
1731 return true;
1732}
1733
Dan Gohman98ca4f22009-08-05 01:29:28 +00001734SDValue
1735ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001736 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001737 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001738 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001739 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001740
Bob Wilsondee46d72009-04-17 20:35:10 +00001741 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001742 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001743
Bob Wilsondee46d72009-04-17 20:35:10 +00001744 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001745 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1746 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001747
Dan Gohman98ca4f22009-08-05 01:29:28 +00001748 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001749 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1750 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001751
1752 // If this is the first return lowered for this function, add
1753 // the regs to the liveout set for the function.
1754 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1755 for (unsigned i = 0; i != RVLocs.size(); ++i)
1756 if (RVLocs[i].isRegLoc())
1757 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001758 }
1759
Bob Wilson1f595bb2009-04-17 19:07:39 +00001760 SDValue Flag;
1761
1762 // Copy the result values into the output registers.
1763 for (unsigned i = 0, realRVLocIdx = 0;
1764 i != RVLocs.size();
1765 ++i, ++realRVLocIdx) {
1766 CCValAssign &VA = RVLocs[i];
1767 assert(VA.isRegLoc() && "Can only return in registers!");
1768
Dan Gohmanc9403652010-07-07 15:54:55 +00001769 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001770
1771 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001772 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001773 case CCValAssign::Full: break;
1774 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001775 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001776 break;
1777 }
1778
Bob Wilson1f595bb2009-04-17 19:07:39 +00001779 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001781 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001782 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1783 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001784 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001785 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001786
1787 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1788 Flag = Chain.getValue(1);
1789 VA = RVLocs[++i]; // skip ahead to next loc
1790 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1791 HalfGPRs.getValue(1), Flag);
1792 Flag = Chain.getValue(1);
1793 VA = RVLocs[++i]; // skip ahead to next loc
1794
1795 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1797 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001798 }
1799 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1800 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001801 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001802 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001803 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001804 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001805 VA = RVLocs[++i]; // skip ahead to next loc
1806 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1807 Flag);
1808 } else
1809 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1810
Bob Wilsondee46d72009-04-17 20:35:10 +00001811 // Guarantee that all emitted copies are
1812 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001813 Flag = Chain.getValue(1);
1814 }
1815
1816 SDValue result;
1817 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001818 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001819 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821
1822 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001823}
1824
Evan Cheng3d2125c2010-11-30 23:55:39 +00001825bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1826 if (N->getNumValues() != 1)
1827 return false;
1828 if (!N->hasNUsesOfValue(1, 0))
1829 return false;
1830
1831 unsigned NumCopies = 0;
1832 SDNode* Copies[2];
1833 SDNode *Use = *N->use_begin();
1834 if (Use->getOpcode() == ISD::CopyToReg) {
1835 Copies[NumCopies++] = Use;
1836 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1837 // f64 returned in a pair of GPRs.
1838 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1839 UI != UE; ++UI) {
1840 if (UI->getOpcode() != ISD::CopyToReg)
1841 return false;
1842 Copies[UI.getUse().getResNo()] = *UI;
1843 ++NumCopies;
1844 }
1845 } else if (Use->getOpcode() == ISD::BITCAST) {
1846 // f32 returned in a single GPR.
1847 if (!Use->hasNUsesOfValue(1, 0))
1848 return false;
1849 Use = *Use->use_begin();
1850 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1851 return false;
1852 Copies[NumCopies++] = Use;
1853 } else {
1854 return false;
1855 }
1856
1857 if (NumCopies != 1 && NumCopies != 2)
1858 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001859
1860 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001861 for (unsigned i = 0; i < NumCopies; ++i) {
1862 SDNode *Copy = Copies[i];
1863 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1864 UI != UE; ++UI) {
1865 if (UI->getOpcode() == ISD::CopyToReg) {
1866 SDNode *Use = *UI;
1867 if (Use == Copies[0] || Use == Copies[1])
1868 continue;
1869 return false;
1870 }
1871 if (UI->getOpcode() != ARMISD::RET_FLAG)
1872 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001873 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001874 }
1875 }
1876
Evan Cheng1bf891a2010-12-01 22:59:46 +00001877 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001878}
1879
Evan Cheng485fafc2011-03-21 01:19:09 +00001880bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1881 if (!EnableARMTailCalls)
1882 return false;
1883
1884 if (!CI->isTailCall())
1885 return false;
1886
1887 return !Subtarget->isThumb1Only();
1888}
1889
Bob Wilsonb62d2572009-11-03 00:02:05 +00001890// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1891// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1892// one of the above mentioned nodes. It has to be wrapped because otherwise
1893// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1894// be used to form addressing mode. These wrapped nodes will be selected
1895// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001896static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001897 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001898 // FIXME there is no actual debug info here
1899 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001900 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001901 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001902 if (CP->isMachineConstantPoolEntry())
1903 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1904 CP->getAlignment());
1905 else
1906 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1907 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001909}
1910
Jim Grosbache1102ca2010-07-19 17:20:38 +00001911unsigned ARMTargetLowering::getJumpTableEncoding() const {
1912 return MachineJumpTableInfo::EK_Inline;
1913}
1914
Dan Gohmand858e902010-04-17 15:26:15 +00001915SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1916 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001917 MachineFunction &MF = DAG.getMachineFunction();
1918 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1919 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001920 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001921 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001922 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001923 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1924 SDValue CPAddr;
1925 if (RelocM == Reloc::Static) {
1926 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1927 } else {
1928 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001929 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001930 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1931 ARMCP::CPBlockAddress,
1932 PCAdj);
1933 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1934 }
1935 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1936 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001937 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001938 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001939 if (RelocM == Reloc::Static)
1940 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001941 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001942 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001943}
1944
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001945// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001946SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001947ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001948 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001949 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001950 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001951 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001952 MachineFunction &MF = DAG.getMachineFunction();
1953 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001954 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001955 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001956 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001957 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001958 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001960 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001961 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001962 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001964
Evan Chenge7e0d622009-11-06 22:24:13 +00001965 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001966 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001967
1968 // call __tls_get_addr.
1969 ArgListTy Args;
1970 ArgListEntry Entry;
1971 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001972 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001973 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001974 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001975 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001976 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00001977 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001979 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001980 return CallResult.first;
1981}
1982
1983// Lower ISD::GlobalTLSAddress using the "initial exec" or
1984// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001985SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001986ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001987 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001988 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001989 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001990 SDValue Offset;
1991 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001992 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001993 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001994 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001995
Chris Lattner4fb63d02009-07-15 04:12:33 +00001996 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001997 MachineFunction &MF = DAG.getMachineFunction();
1998 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001999 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002000 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002001 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2002 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002003 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002004 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002005 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002006 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002007 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002008 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002009 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002010 Chain = Offset.getValue(1);
2011
Evan Chenge7e0d622009-11-06 22:24:13 +00002012 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002013 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002014
Evan Cheng9eda6892009-10-31 03:39:36 +00002015 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002016 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002017 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002018 } else {
2019 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002020 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002021 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002023 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002024 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002025 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002026 }
2027
2028 // The address of the thread local variable is the add of the thread
2029 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002030 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002031}
2032
Dan Gohman475871a2008-07-27 21:46:04 +00002033SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002034ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002035 // TODO: implement the "local dynamic" model
2036 assert(Subtarget->isTargetELF() &&
2037 "TLS not implemented for non-ELF targets");
2038 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2039 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2040 // otherwise use the "Local Exec" TLS Model
2041 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2042 return LowerToTLSGeneralDynamicModel(GA, DAG);
2043 else
2044 return LowerToTLSExecModels(GA, DAG);
2045}
2046
Dan Gohman475871a2008-07-27 21:46:04 +00002047SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002048 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002049 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002050 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002051 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002052 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2053 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002054 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002055 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002056 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002057 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002058 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002059 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002060 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002061 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002062 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002063 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002064 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002065 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002066 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002067 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002068 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002069 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002070 }
2071
2072 // If we have T2 ops, we can materialize the address directly via movt/movw
2073 // pair. This is always cheaper.
2074 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002075 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002076 // FIXME: Once remat is capable of dealing with instructions with register
2077 // operands, expand this into two nodes.
2078 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2079 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002080 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002081 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2082 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2083 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2084 MachinePointerInfo::getConstantPool(),
2085 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002086 }
2087}
2088
Dan Gohman475871a2008-07-27 21:46:04 +00002089SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002090 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002091 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002092 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002093 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002094 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002095 MachineFunction &MF = DAG.getMachineFunction();
2096 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2097
Evan Cheng4abce0c2011-05-27 20:11:27 +00002098 // FIXME: Enable this for static codegen when tool issues are fixed.
2099 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002100 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002101 // FIXME: Once remat is capable of dealing with instructions with register
2102 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002103 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002104 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2105 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2106
Evan Cheng53519f02011-01-21 18:55:51 +00002107 unsigned Wrapper = (RelocM == Reloc::PIC_)
2108 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2109 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002110 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002111 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2112 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2113 MachinePointerInfo::getGOT(), false, false, 0);
2114 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002115 }
2116
2117 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002118 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002119 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002120 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002121 } else {
2122 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002123 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2124 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002125 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002126 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002127 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002128 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002129
Evan Cheng9eda6892009-10-31 03:39:36 +00002130 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002131 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002132 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002133 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002134
2135 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002136 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002137 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002138 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002139
Evan Cheng63476a82009-09-03 07:04:02 +00002140 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002141 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002142 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002143
2144 return Result;
2145}
2146
Dan Gohman475871a2008-07-27 21:46:04 +00002147SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002148 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002149 assert(Subtarget->isTargetELF() &&
2150 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002151 MachineFunction &MF = DAG.getMachineFunction();
2152 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002153 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002154 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002155 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002156 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002157 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2158 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002159 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002160 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002161 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002162 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002163 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002164 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002165 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002166 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002167}
2168
Jim Grosbach0e0da732009-05-12 23:59:14 +00002169SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002170ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2171 const {
2172 DebugLoc dl = Op.getDebugLoc();
2173 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002174 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002175}
2176
2177SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002178ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2179 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002180 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002181 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2182 Op.getOperand(1), Val);
2183}
2184
2185SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002186ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2187 DebugLoc dl = Op.getDebugLoc();
2188 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2189 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2190}
2191
2192SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002193ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002194 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002195 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002196 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002197 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002198 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002199 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002200 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002201 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2202 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002203 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002204 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002205 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002206 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002207 EVT PtrVT = getPointerTy();
2208 DebugLoc dl = Op.getDebugLoc();
2209 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2210 SDValue CPAddr;
2211 unsigned PCAdj = (RelocM != Reloc::PIC_)
2212 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002213 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002214 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2215 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002216 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002217 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002218 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002219 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002220 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002221 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002222
2223 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002224 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002225 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2226 }
2227 return Result;
2228 }
Evan Cheng92e39162011-03-29 23:06:19 +00002229 case Intrinsic::arm_neon_vmulls:
2230 case Intrinsic::arm_neon_vmullu: {
2231 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2232 ? ARMISD::VMULLs : ARMISD::VMULLu;
2233 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2234 Op.getOperand(1), Op.getOperand(2));
2235 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002236 }
2237}
2238
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002239static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002240 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002241 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002242 if (!Subtarget->hasDataBarrier()) {
2243 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2244 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2245 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002246 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002247 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002248 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002249 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002250 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002251
2252 SDValue Op5 = Op.getOperand(5);
2253 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2254 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2255 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2256 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2257
2258 ARM_MB::MemBOpt DMBOpt;
2259 if (isDeviceBarrier)
2260 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2261 else
2262 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2263 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2264 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002265}
2266
Eli Friedman26689ac2011-08-03 21:06:02 +00002267
2268static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2269 const ARMSubtarget *Subtarget) {
2270 // FIXME: handle "fence singlethread" more efficiently.
2271 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002272 if (!Subtarget->hasDataBarrier()) {
2273 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2274 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2275 // here.
2276 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2277 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002278 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002279 DAG.getConstant(0, MVT::i32));
2280 }
2281
Eli Friedman26689ac2011-08-03 21:06:02 +00002282 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002283 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002284}
2285
Evan Chengdfed19f2010-11-03 06:34:55 +00002286static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2287 const ARMSubtarget *Subtarget) {
2288 // ARM pre v5TE and Thumb1 does not have preload instructions.
2289 if (!(Subtarget->isThumb2() ||
2290 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2291 // Just preserve the chain.
2292 return Op.getOperand(0);
2293
2294 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002295 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2296 if (!isRead &&
2297 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2298 // ARMv7 with MP extension has PLDW.
2299 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002300
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002301 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2302 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002303 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002304 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002305 isData = ~isData & 1;
2306 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002307
2308 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002309 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2310 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002311}
2312
Dan Gohman1e93df62010-04-17 14:41:14 +00002313static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2314 MachineFunction &MF = DAG.getMachineFunction();
2315 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2316
Evan Chenga8e29892007-01-19 07:51:42 +00002317 // vastart just stores the address of the VarArgsFrameIndex slot into the
2318 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002319 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002320 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002321 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002322 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002323 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2324 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002325}
2326
Dan Gohman475871a2008-07-27 21:46:04 +00002327SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002328ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2329 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002330 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002331 MachineFunction &MF = DAG.getMachineFunction();
2332 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2333
2334 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002335 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002336 RC = ARM::tGPRRegisterClass;
2337 else
2338 RC = ARM::GPRRegisterClass;
2339
2340 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002341 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002343
2344 SDValue ArgValue2;
2345 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002346 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002347 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002348
2349 // Create load node to retrieve arguments from the stack.
2350 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002351 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002352 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002353 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002354 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002355 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002357 }
2358
Jim Grosbache5165492009-11-09 00:11:35 +00002359 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002360}
2361
Stuart Hastingsc7315872011-04-20 16:47:52 +00002362void
2363ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2364 unsigned &VARegSize, unsigned &VARegSaveSize)
2365 const {
2366 unsigned NumGPRs;
2367 if (CCInfo.isFirstByValRegValid())
2368 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2369 else {
2370 unsigned int firstUnalloced;
2371 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2372 sizeof(GPRArgRegs) /
2373 sizeof(GPRArgRegs[0]));
2374 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2375 }
2376
2377 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2378 VARegSize = NumGPRs * 4;
2379 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2380}
2381
2382// The remaining GPRs hold either the beginning of variable-argument
2383// data, or the beginning of an aggregate passed by value (usuall
2384// byval). Either way, we allocate stack slots adjacent to the data
2385// provided by our caller, and store the unallocated registers there.
2386// If this is a variadic function, the va_list pointer will begin with
2387// these values; otherwise, this reassembles a (byval) structure that
2388// was split between registers and memory.
2389void
2390ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2391 DebugLoc dl, SDValue &Chain,
2392 unsigned ArgOffset) const {
2393 MachineFunction &MF = DAG.getMachineFunction();
2394 MachineFrameInfo *MFI = MF.getFrameInfo();
2395 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2396 unsigned firstRegToSaveIndex;
2397 if (CCInfo.isFirstByValRegValid())
2398 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2399 else {
2400 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2401 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2402 }
2403
2404 unsigned VARegSize, VARegSaveSize;
2405 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2406 if (VARegSaveSize) {
2407 // If this function is vararg, store any remaining integer argument regs
2408 // to their spots on the stack so that they may be loaded by deferencing
2409 // the result of va_next.
2410 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002411 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2412 ArgOffset + VARegSaveSize
2413 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002414 false));
2415 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2416 getPointerTy());
2417
2418 SmallVector<SDValue, 4> MemOps;
2419 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2420 TargetRegisterClass *RC;
2421 if (AFI->isThumb1OnlyFunction())
2422 RC = ARM::tGPRRegisterClass;
2423 else
2424 RC = ARM::GPRRegisterClass;
2425
2426 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2427 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2428 SDValue Store =
2429 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002430 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002431 false, false, 0);
2432 MemOps.push_back(Store);
2433 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2434 DAG.getConstant(4, getPointerTy()));
2435 }
2436 if (!MemOps.empty())
2437 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2438 &MemOps[0], MemOps.size());
2439 } else
2440 // This will point to the next argument passed via stack.
2441 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2442}
2443
Bob Wilson5bafff32009-06-22 23:27:02 +00002444SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002445ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002446 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002447 const SmallVectorImpl<ISD::InputArg>
2448 &Ins,
2449 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002450 SmallVectorImpl<SDValue> &InVals)
2451 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002452 MachineFunction &MF = DAG.getMachineFunction();
2453 MachineFrameInfo *MFI = MF.getFrameInfo();
2454
Bob Wilson1f595bb2009-04-17 19:07:39 +00002455 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2456
2457 // Assign locations to all of the incoming arguments.
2458 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002459 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2460 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002461 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002462 CCAssignFnForNode(CallConv, /* Return*/ false,
2463 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002464
2465 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002466 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002467
Stuart Hastingsf222e592011-02-28 17:17:53 +00002468 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002469 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2470 CCValAssign &VA = ArgLocs[i];
2471
Bob Wilsondee46d72009-04-17 20:35:10 +00002472 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002473 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002474 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002475
Bob Wilson1f595bb2009-04-17 19:07:39 +00002476 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002477 // f64 and vector types are split up into multiple registers or
2478 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002479 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002480 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002481 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002482 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002483 SDValue ArgValue2;
2484 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002485 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002486 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2487 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002488 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002489 false, false, 0);
2490 } else {
2491 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2492 Chain, DAG, dl);
2493 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002494 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2495 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002496 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002497 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002498 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2499 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002500 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002501
Bob Wilson5bafff32009-06-22 23:27:02 +00002502 } else {
2503 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002504
Owen Anderson825b72b2009-08-11 20:47:22 +00002505 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002506 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002507 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002508 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002509 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002510 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002512 RC = (AFI->isThumb1OnlyFunction() ?
2513 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002514 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002515 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002516
2517 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002518 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002519 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002520 }
2521
2522 // If this is an 8 or 16-bit value, it is really passed promoted
2523 // to 32 bits. Insert an assert[sz]ext to capture this, then
2524 // truncate to the right size.
2525 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002526 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002527 case CCValAssign::Full: break;
2528 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002529 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002530 break;
2531 case CCValAssign::SExt:
2532 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2533 DAG.getValueType(VA.getValVT()));
2534 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2535 break;
2536 case CCValAssign::ZExt:
2537 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2538 DAG.getValueType(VA.getValVT()));
2539 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2540 break;
2541 }
2542
Dan Gohman98ca4f22009-08-05 01:29:28 +00002543 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002544
2545 } else { // VA.isRegLoc()
2546
2547 // sanity check
2548 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002549 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002550
Stuart Hastingsf222e592011-02-28 17:17:53 +00002551 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002552
Stuart Hastingsf222e592011-02-28 17:17:53 +00002553 // Some Ins[] entries become multiple ArgLoc[] entries.
2554 // Process them only once.
2555 if (index != lastInsIndex)
2556 {
2557 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002558 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002559 // This can be changed with more analysis.
2560 // In case of tail call optimization mark all arguments mutable.
2561 // Since they could be overwritten by lowering of arguments in case of
2562 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002563 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002564 unsigned VARegSize, VARegSaveSize;
2565 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2566 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2567 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002568 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002569 int FI = MFI->CreateFixedObject(Bytes,
2570 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002571 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2572 } else {
2573 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2574 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002575
Stuart Hastingsf222e592011-02-28 17:17:53 +00002576 // Create load nodes to retrieve arguments from the stack.
2577 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2578 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2579 MachinePointerInfo::getFixedStack(FI),
2580 false, false, 0));
2581 }
2582 lastInsIndex = index;
2583 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002584 }
2585 }
2586
2587 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002588 if (isVarArg)
2589 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002590
Dan Gohman98ca4f22009-08-05 01:29:28 +00002591 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002592}
2593
2594/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002595static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002596 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002597 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002598 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002599 // Maybe this has already been legalized into the constant pool?
2600 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002601 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002602 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002603 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002604 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002605 }
2606 }
2607 return false;
2608}
2609
Evan Chenga8e29892007-01-19 07:51:42 +00002610/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2611/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002612SDValue
2613ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002614 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002615 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002616 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002617 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002618 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002619 // Constant does not fit, try adjusting it by one?
2620 switch (CC) {
2621 default: break;
2622 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002623 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002624 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002625 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002626 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002627 }
2628 break;
2629 case ISD::SETULT:
2630 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002631 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002632 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002633 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002634 }
2635 break;
2636 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002637 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002638 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002639 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002640 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002641 }
2642 break;
2643 case ISD::SETULE:
2644 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002645 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002646 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002647 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002648 }
2649 break;
2650 }
2651 }
2652 }
2653
2654 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002655 ARMISD::NodeType CompareType;
2656 switch (CondCode) {
2657 default:
2658 CompareType = ARMISD::CMP;
2659 break;
2660 case ARMCC::EQ:
2661 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002662 // Uses only Z Flag
2663 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002664 break;
2665 }
Evan Cheng218977b2010-07-13 19:27:42 +00002666 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002667 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002668}
2669
2670/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002671SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002672ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002673 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002674 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002675 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002676 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002677 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002678 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2679 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002680}
2681
Bob Wilson79f56c92011-03-08 01:17:20 +00002682/// duplicateCmp - Glue values can have only one use, so this function
2683/// duplicates a comparison node.
2684SDValue
2685ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2686 unsigned Opc = Cmp.getOpcode();
2687 DebugLoc DL = Cmp.getDebugLoc();
2688 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2689 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2690
2691 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2692 Cmp = Cmp.getOperand(0);
2693 Opc = Cmp.getOpcode();
2694 if (Opc == ARMISD::CMPFP)
2695 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2696 else {
2697 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2698 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2699 }
2700 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2701}
2702
Bill Wendlingde2b1512010-08-11 08:43:16 +00002703SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2704 SDValue Cond = Op.getOperand(0);
2705 SDValue SelectTrue = Op.getOperand(1);
2706 SDValue SelectFalse = Op.getOperand(2);
2707 DebugLoc dl = Op.getDebugLoc();
2708
2709 // Convert:
2710 //
2711 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2712 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2713 //
2714 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2715 const ConstantSDNode *CMOVTrue =
2716 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2717 const ConstantSDNode *CMOVFalse =
2718 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2719
2720 if (CMOVTrue && CMOVFalse) {
2721 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2722 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2723
2724 SDValue True;
2725 SDValue False;
2726 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2727 True = SelectTrue;
2728 False = SelectFalse;
2729 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2730 True = SelectFalse;
2731 False = SelectTrue;
2732 }
2733
2734 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002735 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002736 SDValue ARMcc = Cond.getOperand(2);
2737 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002738 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002739 assert(True.getValueType() == VT);
2740 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002741 }
2742 }
2743 }
2744
2745 return DAG.getSelectCC(dl, Cond,
2746 DAG.getConstant(0, Cond.getValueType()),
2747 SelectTrue, SelectFalse, ISD::SETNE);
2748}
2749
Dan Gohmand858e902010-04-17 15:26:15 +00002750SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002751 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002752 SDValue LHS = Op.getOperand(0);
2753 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002754 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002755 SDValue TrueVal = Op.getOperand(2);
2756 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002757 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002758
Owen Anderson825b72b2009-08-11 20:47:22 +00002759 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002760 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002761 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002762 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Evan Chenge721f5c2011-07-13 00:42:17 +00002763 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002764 }
2765
2766 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002767 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002768
Evan Cheng218977b2010-07-13 19:27:42 +00002769 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2770 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002771 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002772 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002773 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002774 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002775 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002776 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002777 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002778 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002779 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002780 }
2781 return Result;
2782}
2783
Evan Cheng218977b2010-07-13 19:27:42 +00002784/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2785/// to morph to an integer compare sequence.
2786static bool canChangeToInt(SDValue Op, bool &SeenZero,
2787 const ARMSubtarget *Subtarget) {
2788 SDNode *N = Op.getNode();
2789 if (!N->hasOneUse())
2790 // Otherwise it requires moving the value from fp to integer registers.
2791 return false;
2792 if (!N->getNumValues())
2793 return false;
2794 EVT VT = Op.getValueType();
2795 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2796 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2797 // vmrs are very slow, e.g. cortex-a8.
2798 return false;
2799
2800 if (isFloatingPointZero(Op)) {
2801 SeenZero = true;
2802 return true;
2803 }
2804 return ISD::isNormalLoad(N);
2805}
2806
2807static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2808 if (isFloatingPointZero(Op))
2809 return DAG.getConstant(0, MVT::i32);
2810
2811 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2812 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002813 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002814 Ld->isVolatile(), Ld->isNonTemporal(),
2815 Ld->getAlignment());
2816
2817 llvm_unreachable("Unknown VFP cmp argument!");
2818}
2819
2820static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2821 SDValue &RetVal1, SDValue &RetVal2) {
2822 if (isFloatingPointZero(Op)) {
2823 RetVal1 = DAG.getConstant(0, MVT::i32);
2824 RetVal2 = DAG.getConstant(0, MVT::i32);
2825 return;
2826 }
2827
2828 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2829 SDValue Ptr = Ld->getBasePtr();
2830 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2831 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002832 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002833 Ld->isVolatile(), Ld->isNonTemporal(),
2834 Ld->getAlignment());
2835
2836 EVT PtrType = Ptr.getValueType();
2837 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2838 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2839 PtrType, Ptr, DAG.getConstant(4, PtrType));
2840 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2841 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002842 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002843 Ld->isVolatile(), Ld->isNonTemporal(),
2844 NewAlign);
2845 return;
2846 }
2847
2848 llvm_unreachable("Unknown VFP cmp argument!");
2849}
2850
2851/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2852/// f32 and even f64 comparisons to integer ones.
2853SDValue
2854ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2855 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002856 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002857 SDValue LHS = Op.getOperand(2);
2858 SDValue RHS = Op.getOperand(3);
2859 SDValue Dest = Op.getOperand(4);
2860 DebugLoc dl = Op.getDebugLoc();
2861
2862 bool SeenZero = false;
2863 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2864 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002865 // If one of the operand is zero, it's safe to ignore the NaN case since
2866 // we only care about equality comparisons.
2867 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002868 // If unsafe fp math optimization is enabled and there are no other uses of
2869 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002870 // to an integer comparison.
2871 if (CC == ISD::SETOEQ)
2872 CC = ISD::SETEQ;
2873 else if (CC == ISD::SETUNE)
2874 CC = ISD::SETNE;
2875
2876 SDValue ARMcc;
2877 if (LHS.getValueType() == MVT::f32) {
2878 LHS = bitcastf32Toi32(LHS, DAG);
2879 RHS = bitcastf32Toi32(RHS, DAG);
2880 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2881 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2882 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2883 Chain, Dest, ARMcc, CCR, Cmp);
2884 }
2885
2886 SDValue LHS1, LHS2;
2887 SDValue RHS1, RHS2;
2888 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2889 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2890 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2891 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002892 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002893 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2894 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2895 }
2896
2897 return SDValue();
2898}
2899
2900SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2901 SDValue Chain = Op.getOperand(0);
2902 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2903 SDValue LHS = Op.getOperand(2);
2904 SDValue RHS = Op.getOperand(3);
2905 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002906 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002907
Owen Anderson825b72b2009-08-11 20:47:22 +00002908 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002909 SDValue ARMcc;
2910 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002911 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002912 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002913 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002914 }
2915
Owen Anderson825b72b2009-08-11 20:47:22 +00002916 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002917
2918 if (UnsafeFPMath &&
2919 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2920 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2921 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2922 if (Result.getNode())
2923 return Result;
2924 }
2925
Evan Chenga8e29892007-01-19 07:51:42 +00002926 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002927 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002928
Evan Cheng218977b2010-07-13 19:27:42 +00002929 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2930 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002931 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002932 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002933 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002934 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002935 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002936 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2937 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002938 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002939 }
2940 return Res;
2941}
2942
Dan Gohmand858e902010-04-17 15:26:15 +00002943SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002944 SDValue Chain = Op.getOperand(0);
2945 SDValue Table = Op.getOperand(1);
2946 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002947 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002948
Owen Andersone50ed302009-08-10 22:56:29 +00002949 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002950 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2951 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002952 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002953 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002954 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002955 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2956 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002957 if (Subtarget->isThumb2()) {
2958 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2959 // which does another jump to the destination. This also makes it easier
2960 // to translate it to TBB / TBH later.
2961 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002962 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002963 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002964 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002965 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002966 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002967 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002968 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002969 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002970 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002971 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002972 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002973 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002974 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002975 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002976 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002977 }
Evan Chenga8e29892007-01-19 07:51:42 +00002978}
2979
Bob Wilson76a312b2010-03-19 22:51:32 +00002980static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2981 DebugLoc dl = Op.getDebugLoc();
2982 unsigned Opc;
2983
2984 switch (Op.getOpcode()) {
2985 default:
2986 assert(0 && "Invalid opcode!");
2987 case ISD::FP_TO_SINT:
2988 Opc = ARMISD::FTOSI;
2989 break;
2990 case ISD::FP_TO_UINT:
2991 Opc = ARMISD::FTOUI;
2992 break;
2993 }
2994 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002995 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002996}
2997
Cameron Zwarich3007d332011-03-29 21:41:55 +00002998static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2999 EVT VT = Op.getValueType();
3000 DebugLoc dl = Op.getDebugLoc();
3001
Duncan Sands1f6a3292011-08-12 14:54:45 +00003002 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3003 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003004 if (VT != MVT::v4f32)
3005 return DAG.UnrollVectorOp(Op.getNode());
3006
3007 unsigned CastOpc;
3008 unsigned Opc;
3009 switch (Op.getOpcode()) {
3010 default:
3011 assert(0 && "Invalid opcode!");
3012 case ISD::SINT_TO_FP:
3013 CastOpc = ISD::SIGN_EXTEND;
3014 Opc = ISD::SINT_TO_FP;
3015 break;
3016 case ISD::UINT_TO_FP:
3017 CastOpc = ISD::ZERO_EXTEND;
3018 Opc = ISD::UINT_TO_FP;
3019 break;
3020 }
3021
3022 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3023 return DAG.getNode(Opc, dl, VT, Op);
3024}
3025
Bob Wilson76a312b2010-03-19 22:51:32 +00003026static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3027 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003028 if (VT.isVector())
3029 return LowerVectorINT_TO_FP(Op, DAG);
3030
Bob Wilson76a312b2010-03-19 22:51:32 +00003031 DebugLoc dl = Op.getDebugLoc();
3032 unsigned Opc;
3033
3034 switch (Op.getOpcode()) {
3035 default:
3036 assert(0 && "Invalid opcode!");
3037 case ISD::SINT_TO_FP:
3038 Opc = ARMISD::SITOF;
3039 break;
3040 case ISD::UINT_TO_FP:
3041 Opc = ARMISD::UITOF;
3042 break;
3043 }
3044
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003045 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003046 return DAG.getNode(Opc, dl, VT, Op);
3047}
3048
Evan Cheng515fe3a2010-07-08 02:08:50 +00003049SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003050 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003051 SDValue Tmp0 = Op.getOperand(0);
3052 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003053 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003054 EVT VT = Op.getValueType();
3055 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003056 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3057 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3058 bool UseNEON = !InGPR && Subtarget->hasNEON();
3059
3060 if (UseNEON) {
3061 // Use VBSL to copy the sign bit.
3062 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3063 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3064 DAG.getTargetConstant(EncodedVal, MVT::i32));
3065 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3066 if (VT == MVT::f64)
3067 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3068 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3069 DAG.getConstant(32, MVT::i32));
3070 else /*if (VT == MVT::f32)*/
3071 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3072 if (SrcVT == MVT::f32) {
3073 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3074 if (VT == MVT::f64)
3075 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3076 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3077 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003078 } else if (VT == MVT::f32)
3079 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3080 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3081 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003082 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3083 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3084
3085 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3086 MVT::i32);
3087 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3088 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3089 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003090
Evan Chenge573fb32011-02-23 02:24:55 +00003091 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3092 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3093 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003094 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003095 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3096 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3097 DAG.getConstant(0, MVT::i32));
3098 } else {
3099 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3100 }
3101
3102 return Res;
3103 }
Evan Chengc143dd42011-02-11 02:28:55 +00003104
3105 // Bitcast operand 1 to i32.
3106 if (SrcVT == MVT::f64)
3107 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3108 &Tmp1, 1).getValue(1);
3109 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3110
Evan Chenge573fb32011-02-23 02:24:55 +00003111 // Or in the signbit with integer operations.
3112 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3113 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3114 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3115 if (VT == MVT::f32) {
3116 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3117 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3118 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3119 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003120 }
3121
Evan Chenge573fb32011-02-23 02:24:55 +00003122 // f64: Or the high part with signbit and then combine two parts.
3123 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3124 &Tmp0, 1);
3125 SDValue Lo = Tmp0.getValue(0);
3126 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3127 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3128 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003129}
3130
Evan Cheng2457f2c2010-05-22 01:47:14 +00003131SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3132 MachineFunction &MF = DAG.getMachineFunction();
3133 MachineFrameInfo *MFI = MF.getFrameInfo();
3134 MFI->setReturnAddressIsTaken(true);
3135
3136 EVT VT = Op.getValueType();
3137 DebugLoc dl = Op.getDebugLoc();
3138 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3139 if (Depth) {
3140 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3141 SDValue Offset = DAG.getConstant(4, MVT::i32);
3142 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3143 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003144 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003145 }
3146
3147 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003148 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003149 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3150}
3151
Dan Gohmand858e902010-04-17 15:26:15 +00003152SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003153 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3154 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003155
Owen Andersone50ed302009-08-10 22:56:29 +00003156 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003157 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3158 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003159 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003160 ? ARM::R7 : ARM::R11;
3161 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3162 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003163 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3164 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003165 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003166 return FrameAddr;
3167}
3168
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003169/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003170/// expand a bit convert where either the source or destination type is i64 to
3171/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3172/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3173/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003174static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003175 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3176 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003177 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003178
Bob Wilson9f3f0612010-04-17 05:30:19 +00003179 // This function is only supposed to be called for i64 types, either as the
3180 // source or destination of the bit convert.
3181 EVT SrcVT = Op.getValueType();
3182 EVT DstVT = N->getValueType(0);
3183 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003184 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003185
Bob Wilson9f3f0612010-04-17 05:30:19 +00003186 // Turn i64->f64 into VMOVDRR.
3187 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003188 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3189 DAG.getConstant(0, MVT::i32));
3190 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3191 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003192 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003193 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003194 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003195
Jim Grosbache5165492009-11-09 00:11:35 +00003196 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003197 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3198 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3199 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3200 // Merge the pieces into a single i64 value.
3201 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3202 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003203
Bob Wilson9f3f0612010-04-17 05:30:19 +00003204 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003205}
3206
Bob Wilson5bafff32009-06-22 23:27:02 +00003207/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003208/// Zero vectors are used to represent vector negation and in those cases
3209/// will be implemented with the NEON VNEG instruction. However, VNEG does
3210/// not support i64 elements, so sometimes the zero vectors will need to be
3211/// explicitly constructed. Regardless, use a canonical VMOV to create the
3212/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003213static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003214 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003215 // The canonical modified immediate encoding of a zero vector is....0!
3216 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3217 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3218 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003219 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003220}
3221
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003222/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3223/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003224SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3225 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003226 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3227 EVT VT = Op.getValueType();
3228 unsigned VTBits = VT.getSizeInBits();
3229 DebugLoc dl = Op.getDebugLoc();
3230 SDValue ShOpLo = Op.getOperand(0);
3231 SDValue ShOpHi = Op.getOperand(1);
3232 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003233 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003234 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003235
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003236 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3237
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003238 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3239 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3240 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3241 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3242 DAG.getConstant(VTBits, MVT::i32));
3243 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3244 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003245 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003246
3247 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3248 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003249 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003250 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003251 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003252 CCR, Cmp);
3253
3254 SDValue Ops[2] = { Lo, Hi };
3255 return DAG.getMergeValues(Ops, 2, dl);
3256}
3257
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003258/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3259/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003260SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3261 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003262 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3263 EVT VT = Op.getValueType();
3264 unsigned VTBits = VT.getSizeInBits();
3265 DebugLoc dl = Op.getDebugLoc();
3266 SDValue ShOpLo = Op.getOperand(0);
3267 SDValue ShOpHi = Op.getOperand(1);
3268 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003269 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003270
3271 assert(Op.getOpcode() == ISD::SHL_PARTS);
3272 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3273 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3274 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3275 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3276 DAG.getConstant(VTBits, MVT::i32));
3277 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3278 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3279
3280 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3281 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3282 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003283 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003284 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003285 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003286 CCR, Cmp);
3287
3288 SDValue Ops[2] = { Lo, Hi };
3289 return DAG.getMergeValues(Ops, 2, dl);
3290}
3291
Jim Grosbach4725ca72010-09-08 03:54:02 +00003292SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003293 SelectionDAG &DAG) const {
3294 // The rounding mode is in bits 23:22 of the FPSCR.
3295 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3296 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3297 // so that the shift + and get folded into a bitfield extract.
3298 DebugLoc dl = Op.getDebugLoc();
3299 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3300 DAG.getConstant(Intrinsic::arm_get_fpscr,
3301 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003302 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003303 DAG.getConstant(1U << 22, MVT::i32));
3304 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3305 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003306 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003307 DAG.getConstant(3, MVT::i32));
3308}
3309
Jim Grosbach3482c802010-01-18 19:58:49 +00003310static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3311 const ARMSubtarget *ST) {
3312 EVT VT = N->getValueType(0);
3313 DebugLoc dl = N->getDebugLoc();
3314
3315 if (!ST->hasV6T2Ops())
3316 return SDValue();
3317
3318 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3319 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3320}
3321
Bob Wilson5bafff32009-06-22 23:27:02 +00003322static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3323 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003324 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003325 DebugLoc dl = N->getDebugLoc();
3326
Bob Wilsond5448bb2010-11-18 21:16:28 +00003327 if (!VT.isVector())
3328 return SDValue();
3329
Bob Wilson5bafff32009-06-22 23:27:02 +00003330 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003331 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003332
Bob Wilsond5448bb2010-11-18 21:16:28 +00003333 // Left shifts translate directly to the vshiftu intrinsic.
3334 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003335 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003336 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3337 N->getOperand(0), N->getOperand(1));
3338
3339 assert((N->getOpcode() == ISD::SRA ||
3340 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3341
3342 // NEON uses the same intrinsics for both left and right shifts. For
3343 // right shifts, the shift amounts are negative, so negate the vector of
3344 // shift amounts.
3345 EVT ShiftVT = N->getOperand(1).getValueType();
3346 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3347 getZeroVector(ShiftVT, DAG, dl),
3348 N->getOperand(1));
3349 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3350 Intrinsic::arm_neon_vshifts :
3351 Intrinsic::arm_neon_vshiftu);
3352 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3353 DAG.getConstant(vshiftInt, MVT::i32),
3354 N->getOperand(0), NegatedCount);
3355}
3356
3357static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3358 const ARMSubtarget *ST) {
3359 EVT VT = N->getValueType(0);
3360 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003361
Eli Friedmance392eb2009-08-22 03:13:10 +00003362 // We can get here for a node like i32 = ISD::SHL i32, i64
3363 if (VT != MVT::i64)
3364 return SDValue();
3365
3366 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003367 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003368
Chris Lattner27a6c732007-11-24 07:07:01 +00003369 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3370 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003371 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003372 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003373
Chris Lattner27a6c732007-11-24 07:07:01 +00003374 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003375 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003376
Chris Lattner27a6c732007-11-24 07:07:01 +00003377 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003378 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003379 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003380 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003381 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003382
Chris Lattner27a6c732007-11-24 07:07:01 +00003383 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3384 // captures the result into a carry flag.
3385 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003386 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003387
Chris Lattner27a6c732007-11-24 07:07:01 +00003388 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003389 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003390
Chris Lattner27a6c732007-11-24 07:07:01 +00003391 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003392 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003393}
3394
Bob Wilson5bafff32009-06-22 23:27:02 +00003395static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3396 SDValue TmpOp0, TmpOp1;
3397 bool Invert = false;
3398 bool Swap = false;
3399 unsigned Opc = 0;
3400
3401 SDValue Op0 = Op.getOperand(0);
3402 SDValue Op1 = Op.getOperand(1);
3403 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003404 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003405 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3406 DebugLoc dl = Op.getDebugLoc();
3407
3408 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3409 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003410 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003411 case ISD::SETUNE:
3412 case ISD::SETNE: Invert = true; // Fallthrough
3413 case ISD::SETOEQ:
3414 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3415 case ISD::SETOLT:
3416 case ISD::SETLT: Swap = true; // Fallthrough
3417 case ISD::SETOGT:
3418 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3419 case ISD::SETOLE:
3420 case ISD::SETLE: Swap = true; // Fallthrough
3421 case ISD::SETOGE:
3422 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3423 case ISD::SETUGE: Swap = true; // Fallthrough
3424 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3425 case ISD::SETUGT: Swap = true; // Fallthrough
3426 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3427 case ISD::SETUEQ: Invert = true; // Fallthrough
3428 case ISD::SETONE:
3429 // Expand this to (OLT | OGT).
3430 TmpOp0 = Op0;
3431 TmpOp1 = Op1;
3432 Opc = ISD::OR;
3433 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3434 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3435 break;
3436 case ISD::SETUO: Invert = true; // Fallthrough
3437 case ISD::SETO:
3438 // Expand this to (OLT | OGE).
3439 TmpOp0 = Op0;
3440 TmpOp1 = Op1;
3441 Opc = ISD::OR;
3442 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3443 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3444 break;
3445 }
3446 } else {
3447 // Integer comparisons.
3448 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003449 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003450 case ISD::SETNE: Invert = true;
3451 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3452 case ISD::SETLT: Swap = true;
3453 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3454 case ISD::SETLE: Swap = true;
3455 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3456 case ISD::SETULT: Swap = true;
3457 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3458 case ISD::SETULE: Swap = true;
3459 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3460 }
3461
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003462 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003463 if (Opc == ARMISD::VCEQ) {
3464
3465 SDValue AndOp;
3466 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3467 AndOp = Op0;
3468 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3469 AndOp = Op1;
3470
3471 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003472 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003473 AndOp = AndOp.getOperand(0);
3474
3475 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3476 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003477 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3478 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003479 Invert = !Invert;
3480 }
3481 }
3482 }
3483
3484 if (Swap)
3485 std::swap(Op0, Op1);
3486
Owen Andersonc24cb352010-11-08 23:21:22 +00003487 // If one of the operands is a constant vector zero, attempt to fold the
3488 // comparison to a specialized compare-against-zero form.
3489 SDValue SingleOp;
3490 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3491 SingleOp = Op0;
3492 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3493 if (Opc == ARMISD::VCGE)
3494 Opc = ARMISD::VCLEZ;
3495 else if (Opc == ARMISD::VCGT)
3496 Opc = ARMISD::VCLTZ;
3497 SingleOp = Op1;
3498 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003499
Owen Andersonc24cb352010-11-08 23:21:22 +00003500 SDValue Result;
3501 if (SingleOp.getNode()) {
3502 switch (Opc) {
3503 case ARMISD::VCEQ:
3504 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3505 case ARMISD::VCGE:
3506 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3507 case ARMISD::VCLEZ:
3508 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3509 case ARMISD::VCGT:
3510 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3511 case ARMISD::VCLTZ:
3512 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3513 default:
3514 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3515 }
3516 } else {
3517 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3518 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003519
3520 if (Invert)
3521 Result = DAG.getNOT(dl, Result, VT);
3522
3523 return Result;
3524}
3525
Bob Wilsond3c42842010-06-14 22:19:57 +00003526/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3527/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003528/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003529static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3530 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003531 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003532 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003533
Bob Wilson827b2102010-06-15 19:05:35 +00003534 // SplatBitSize is set to the smallest size that splats the vector, so a
3535 // zero vector will always have SplatBitSize == 8. However, NEON modified
3536 // immediate instructions others than VMOV do not support the 8-bit encoding
3537 // of a zero vector, and the default encoding of zero is supposed to be the
3538 // 32-bit version.
3539 if (SplatBits == 0)
3540 SplatBitSize = 32;
3541
Bob Wilson5bafff32009-06-22 23:27:02 +00003542 switch (SplatBitSize) {
3543 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003544 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003545 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003546 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003547 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003548 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003549 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003550 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003551 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003552
3553 case 16:
3554 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003555 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003556 if ((SplatBits & ~0xff) == 0) {
3557 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003558 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003559 Imm = SplatBits;
3560 break;
3561 }
3562 if ((SplatBits & ~0xff00) == 0) {
3563 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003564 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003565 Imm = SplatBits >> 8;
3566 break;
3567 }
3568 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003569
3570 case 32:
3571 // NEON's 32-bit VMOV supports splat values where:
3572 // * only one byte is nonzero, or
3573 // * the least significant byte is 0xff and the second byte is nonzero, or
3574 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003575 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003576 if ((SplatBits & ~0xff) == 0) {
3577 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003578 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003579 Imm = SplatBits;
3580 break;
3581 }
3582 if ((SplatBits & ~0xff00) == 0) {
3583 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003584 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003585 Imm = SplatBits >> 8;
3586 break;
3587 }
3588 if ((SplatBits & ~0xff0000) == 0) {
3589 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003590 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003591 Imm = SplatBits >> 16;
3592 break;
3593 }
3594 if ((SplatBits & ~0xff000000) == 0) {
3595 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003596 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003597 Imm = SplatBits >> 24;
3598 break;
3599 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003600
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003601 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3602 if (type == OtherModImm) return SDValue();
3603
Bob Wilson5bafff32009-06-22 23:27:02 +00003604 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003605 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3606 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003607 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003608 Imm = SplatBits >> 8;
3609 SplatBits |= 0xff;
3610 break;
3611 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003612
3613 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003614 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3615 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003616 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003617 Imm = SplatBits >> 16;
3618 SplatBits |= 0xffff;
3619 break;
3620 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003621
3622 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3623 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3624 // VMOV.I32. A (very) minor optimization would be to replicate the value
3625 // and fall through here to test for a valid 64-bit splat. But, then the
3626 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003627 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003628
3629 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003630 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003631 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003632 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003633 uint64_t BitMask = 0xff;
3634 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003635 unsigned ImmMask = 1;
3636 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003637 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003638 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003639 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003640 Imm |= ImmMask;
3641 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003642 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003643 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003644 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003645 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003646 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003647 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003648 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003649 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003650 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003651 break;
3652 }
3653
Bob Wilson1a913ed2010-06-11 21:34:50 +00003654 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003655 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003656 return SDValue();
3657 }
3658
Bob Wilsoncba270d2010-07-13 21:16:48 +00003659 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3660 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003661}
3662
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003663static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3664 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003665 unsigned NumElts = VT.getVectorNumElements();
3666 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003667
3668 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3669 if (M[0] < 0)
3670 return false;
3671
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003672 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003673
3674 // If this is a VEXT shuffle, the immediate value is the index of the first
3675 // element. The other shuffle indices must be the successive elements after
3676 // the first one.
3677 unsigned ExpectedElt = Imm;
3678 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003679 // Increment the expected index. If it wraps around, it may still be
3680 // a VEXT but the source vectors must be swapped.
3681 ExpectedElt += 1;
3682 if (ExpectedElt == NumElts * 2) {
3683 ExpectedElt = 0;
3684 ReverseVEXT = true;
3685 }
3686
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003687 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003688 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003689 return false;
3690 }
3691
3692 // Adjust the index value if the source operands will be swapped.
3693 if (ReverseVEXT)
3694 Imm -= NumElts;
3695
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003696 return true;
3697}
3698
Bob Wilson8bb9e482009-07-26 00:39:34 +00003699/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3700/// instruction with the specified blocksize. (The order of the elements
3701/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003702static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3703 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003704 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3705 "Only possible block sizes for VREV are: 16, 32, 64");
3706
Bob Wilson8bb9e482009-07-26 00:39:34 +00003707 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003708 if (EltSz == 64)
3709 return false;
3710
3711 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003712 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003713 // If the first shuffle index is UNDEF, be optimistic.
3714 if (M[0] < 0)
3715 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003716
3717 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3718 return false;
3719
3720 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003721 if (M[i] < 0) continue; // ignore UNDEF indices
3722 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003723 return false;
3724 }
3725
3726 return true;
3727}
3728
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003729static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3730 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3731 // range, then 0 is placed into the resulting vector. So pretty much any mask
3732 // of 8 elements can work here.
3733 return VT == MVT::v8i8 && M.size() == 8;
3734}
3735
Bob Wilsonc692cb72009-08-21 20:54:19 +00003736static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3737 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003738 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3739 if (EltSz == 64)
3740 return false;
3741
Bob Wilsonc692cb72009-08-21 20:54:19 +00003742 unsigned NumElts = VT.getVectorNumElements();
3743 WhichResult = (M[0] == 0 ? 0 : 1);
3744 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003745 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3746 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003747 return false;
3748 }
3749 return true;
3750}
3751
Bob Wilson324f4f12009-12-03 06:40:55 +00003752/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3753/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3754/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3755static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3756 unsigned &WhichResult) {
3757 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3758 if (EltSz == 64)
3759 return false;
3760
3761 unsigned NumElts = VT.getVectorNumElements();
3762 WhichResult = (M[0] == 0 ? 0 : 1);
3763 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003764 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3765 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003766 return false;
3767 }
3768 return true;
3769}
3770
Bob Wilsonc692cb72009-08-21 20:54:19 +00003771static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3772 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003773 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3774 if (EltSz == 64)
3775 return false;
3776
Bob Wilsonc692cb72009-08-21 20:54:19 +00003777 unsigned NumElts = VT.getVectorNumElements();
3778 WhichResult = (M[0] == 0 ? 0 : 1);
3779 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003780 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003781 if ((unsigned) M[i] != 2 * i + WhichResult)
3782 return false;
3783 }
3784
3785 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003786 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003787 return false;
3788
3789 return true;
3790}
3791
Bob Wilson324f4f12009-12-03 06:40:55 +00003792/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3793/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3794/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3795static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3796 unsigned &WhichResult) {
3797 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3798 if (EltSz == 64)
3799 return false;
3800
3801 unsigned Half = VT.getVectorNumElements() / 2;
3802 WhichResult = (M[0] == 0 ? 0 : 1);
3803 for (unsigned j = 0; j != 2; ++j) {
3804 unsigned Idx = WhichResult;
3805 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003806 int MIdx = M[i + j * Half];
3807 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003808 return false;
3809 Idx += 2;
3810 }
3811 }
3812
3813 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3814 if (VT.is64BitVector() && EltSz == 32)
3815 return false;
3816
3817 return true;
3818}
3819
Bob Wilsonc692cb72009-08-21 20:54:19 +00003820static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3821 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003822 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3823 if (EltSz == 64)
3824 return false;
3825
Bob Wilsonc692cb72009-08-21 20:54:19 +00003826 unsigned NumElts = VT.getVectorNumElements();
3827 WhichResult = (M[0] == 0 ? 0 : 1);
3828 unsigned Idx = WhichResult * NumElts / 2;
3829 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003830 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3831 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003832 return false;
3833 Idx += 1;
3834 }
3835
3836 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003837 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003838 return false;
3839
3840 return true;
3841}
3842
Bob Wilson324f4f12009-12-03 06:40:55 +00003843/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3844/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3845/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3846static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3847 unsigned &WhichResult) {
3848 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3849 if (EltSz == 64)
3850 return false;
3851
3852 unsigned NumElts = VT.getVectorNumElements();
3853 WhichResult = (M[0] == 0 ? 0 : 1);
3854 unsigned Idx = WhichResult * NumElts / 2;
3855 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003856 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3857 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003858 return false;
3859 Idx += 1;
3860 }
3861
3862 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3863 if (VT.is64BitVector() && EltSz == 32)
3864 return false;
3865
3866 return true;
3867}
3868
Dale Johannesenf630c712010-07-29 20:10:08 +00003869// If N is an integer constant that can be moved into a register in one
3870// instruction, return an SDValue of such a constant (will become a MOV
3871// instruction). Otherwise return null.
3872static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3873 const ARMSubtarget *ST, DebugLoc dl) {
3874 uint64_t Val;
3875 if (!isa<ConstantSDNode>(N))
3876 return SDValue();
3877 Val = cast<ConstantSDNode>(N)->getZExtValue();
3878
3879 if (ST->isThumb1Only()) {
3880 if (Val <= 255 || ~Val <= 255)
3881 return DAG.getConstant(Val, MVT::i32);
3882 } else {
3883 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3884 return DAG.getConstant(Val, MVT::i32);
3885 }
3886 return SDValue();
3887}
3888
Bob Wilson5bafff32009-06-22 23:27:02 +00003889// If this is a case we can't handle, return null and let the default
3890// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003891SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3892 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003893 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003894 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003895 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003896
3897 APInt SplatBits, SplatUndef;
3898 unsigned SplatBitSize;
3899 bool HasAnyUndefs;
3900 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003901 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003902 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003903 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003904 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003905 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003906 DAG, VmovVT, VT.is128BitVector(),
3907 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003908 if (Val.getNode()) {
3909 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003910 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003911 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003912
3913 // Try an immediate VMVN.
3914 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3915 ((1LL << SplatBitSize) - 1));
3916 Val = isNEONModifiedImm(NegatedImm,
3917 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003918 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003919 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003920 if (Val.getNode()) {
3921 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003922 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003923 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003924 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003925 }
3926
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003927 // Scan through the operands to see if only one value is used.
3928 unsigned NumElts = VT.getVectorNumElements();
3929 bool isOnlyLowElement = true;
3930 bool usesOnlyOneValue = true;
3931 bool isConstant = true;
3932 SDValue Value;
3933 for (unsigned i = 0; i < NumElts; ++i) {
3934 SDValue V = Op.getOperand(i);
3935 if (V.getOpcode() == ISD::UNDEF)
3936 continue;
3937 if (i > 0)
3938 isOnlyLowElement = false;
3939 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3940 isConstant = false;
3941
3942 if (!Value.getNode())
3943 Value = V;
3944 else if (V != Value)
3945 usesOnlyOneValue = false;
3946 }
3947
3948 if (!Value.getNode())
3949 return DAG.getUNDEF(VT);
3950
3951 if (isOnlyLowElement)
3952 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3953
Dale Johannesenf630c712010-07-29 20:10:08 +00003954 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3955
Dale Johannesen575cd142010-10-19 20:00:17 +00003956 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3957 // i32 and try again.
3958 if (usesOnlyOneValue && EltSize <= 32) {
3959 if (!isConstant)
3960 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3961 if (VT.getVectorElementType().isFloatingPoint()) {
3962 SmallVector<SDValue, 8> Ops;
3963 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003964 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003965 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003966 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3967 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003968 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3969 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003970 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003971 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003972 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3973 if (Val.getNode())
3974 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003975 }
3976
3977 // If all elements are constants and the case above didn't get hit, fall back
3978 // to the default expansion, which will generate a load from the constant
3979 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003980 if (isConstant)
3981 return SDValue();
3982
Bob Wilson11a1dff2011-01-07 21:37:30 +00003983 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3984 if (NumElts >= 4) {
3985 SDValue shuffle = ReconstructShuffle(Op, DAG);
3986 if (shuffle != SDValue())
3987 return shuffle;
3988 }
3989
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003990 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003991 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3992 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003993 if (EltSize >= 32) {
3994 // Do the expansion with floating-point types, since that is what the VFP
3995 // registers are defined to use, and since i64 is not legal.
3996 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3997 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003998 SmallVector<SDValue, 8> Ops;
3999 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004000 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004001 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004002 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004003 }
4004
4005 return SDValue();
4006}
4007
Bob Wilson11a1dff2011-01-07 21:37:30 +00004008// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004009// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004010SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4011 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004012 DebugLoc dl = Op.getDebugLoc();
4013 EVT VT = Op.getValueType();
4014 unsigned NumElts = VT.getVectorNumElements();
4015
4016 SmallVector<SDValue, 2> SourceVecs;
4017 SmallVector<unsigned, 2> MinElts;
4018 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004019
Bob Wilson11a1dff2011-01-07 21:37:30 +00004020 for (unsigned i = 0; i < NumElts; ++i) {
4021 SDValue V = Op.getOperand(i);
4022 if (V.getOpcode() == ISD::UNDEF)
4023 continue;
4024 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4025 // A shuffle can only come from building a vector from various
4026 // elements of other vectors.
4027 return SDValue();
4028 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004029
Bob Wilson11a1dff2011-01-07 21:37:30 +00004030 // Record this extraction against the appropriate vector if possible...
4031 SDValue SourceVec = V.getOperand(0);
4032 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4033 bool FoundSource = false;
4034 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4035 if (SourceVecs[j] == SourceVec) {
4036 if (MinElts[j] > EltNo)
4037 MinElts[j] = EltNo;
4038 if (MaxElts[j] < EltNo)
4039 MaxElts[j] = EltNo;
4040 FoundSource = true;
4041 break;
4042 }
4043 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004044
Bob Wilson11a1dff2011-01-07 21:37:30 +00004045 // Or record a new source if not...
4046 if (!FoundSource) {
4047 SourceVecs.push_back(SourceVec);
4048 MinElts.push_back(EltNo);
4049 MaxElts.push_back(EltNo);
4050 }
4051 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004052
Bob Wilson11a1dff2011-01-07 21:37:30 +00004053 // Currently only do something sane when at most two source vectors
4054 // involved.
4055 if (SourceVecs.size() > 2)
4056 return SDValue();
4057
4058 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4059 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004060
Bob Wilson11a1dff2011-01-07 21:37:30 +00004061 // This loop extracts the usage patterns of the source vectors
4062 // and prepares appropriate SDValues for a shuffle if possible.
4063 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4064 if (SourceVecs[i].getValueType() == VT) {
4065 // No VEXT necessary
4066 ShuffleSrcs[i] = SourceVecs[i];
4067 VEXTOffsets[i] = 0;
4068 continue;
4069 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4070 // It probably isn't worth padding out a smaller vector just to
4071 // break it down again in a shuffle.
4072 return SDValue();
4073 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004074
Bob Wilson11a1dff2011-01-07 21:37:30 +00004075 // Since only 64-bit and 128-bit vectors are legal on ARM and
4076 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004077 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4078 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004079
Bob Wilson11a1dff2011-01-07 21:37:30 +00004080 if (MaxElts[i] - MinElts[i] >= NumElts) {
4081 // Span too large for a VEXT to cope
4082 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004083 }
4084
Bob Wilson11a1dff2011-01-07 21:37:30 +00004085 if (MinElts[i] >= NumElts) {
4086 // The extraction can just take the second half
4087 VEXTOffsets[i] = NumElts;
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(NumElts));
4091 } else if (MaxElts[i] < NumElts) {
4092 // The extraction can just take the first half
4093 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004094 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4095 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004096 DAG.getIntPtrConstant(0));
4097 } else {
4098 // An actual VEXT is needed
4099 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004100 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4101 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004102 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004103 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4104 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004105 DAG.getIntPtrConstant(NumElts));
4106 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4107 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4108 }
4109 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004110
Bob Wilson11a1dff2011-01-07 21:37:30 +00004111 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004112
Bob Wilson11a1dff2011-01-07 21:37:30 +00004113 for (unsigned i = 0; i < NumElts; ++i) {
4114 SDValue Entry = Op.getOperand(i);
4115 if (Entry.getOpcode() == ISD::UNDEF) {
4116 Mask.push_back(-1);
4117 continue;
4118 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004119
Bob Wilson11a1dff2011-01-07 21:37:30 +00004120 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004121 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4122 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004123 if (ExtractVec == SourceVecs[0]) {
4124 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4125 } else {
4126 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4127 }
4128 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004129
Bob Wilson11a1dff2011-01-07 21:37:30 +00004130 // Final check before we try to produce nonsense...
4131 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004132 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4133 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004134
Bob Wilson11a1dff2011-01-07 21:37:30 +00004135 return SDValue();
4136}
4137
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004138/// isShuffleMaskLegal - Targets can use this to indicate that they only
4139/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4140/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4141/// are assumed to be legal.
4142bool
4143ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4144 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004145 if (VT.getVectorNumElements() == 4 &&
4146 (VT.is128BitVector() || VT.is64BitVector())) {
4147 unsigned PFIndexes[4];
4148 for (unsigned i = 0; i != 4; ++i) {
4149 if (M[i] < 0)
4150 PFIndexes[i] = 8;
4151 else
4152 PFIndexes[i] = M[i];
4153 }
4154
4155 // Compute the index in the perfect shuffle table.
4156 unsigned PFTableIndex =
4157 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4158 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4159 unsigned Cost = (PFEntry >> 30);
4160
4161 if (Cost <= 4)
4162 return true;
4163 }
4164
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004165 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004166 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004167
Bob Wilson53dd2452010-06-07 23:53:38 +00004168 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4169 return (EltSize >= 32 ||
4170 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004171 isVREVMask(M, VT, 64) ||
4172 isVREVMask(M, VT, 32) ||
4173 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004174 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004175 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004176 isVTRNMask(M, VT, WhichResult) ||
4177 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004178 isVZIPMask(M, VT, WhichResult) ||
4179 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4180 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4181 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004182}
4183
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004184/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4185/// the specified operations to build the shuffle.
4186static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4187 SDValue RHS, SelectionDAG &DAG,
4188 DebugLoc dl) {
4189 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4190 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4191 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4192
4193 enum {
4194 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4195 OP_VREV,
4196 OP_VDUP0,
4197 OP_VDUP1,
4198 OP_VDUP2,
4199 OP_VDUP3,
4200 OP_VEXT1,
4201 OP_VEXT2,
4202 OP_VEXT3,
4203 OP_VUZPL, // VUZP, left result
4204 OP_VUZPR, // VUZP, right result
4205 OP_VZIPL, // VZIP, left result
4206 OP_VZIPR, // VZIP, right result
4207 OP_VTRNL, // VTRN, left result
4208 OP_VTRNR // VTRN, right result
4209 };
4210
4211 if (OpNum == OP_COPY) {
4212 if (LHSID == (1*9+2)*9+3) return LHS;
4213 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4214 return RHS;
4215 }
4216
4217 SDValue OpLHS, OpRHS;
4218 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4219 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4220 EVT VT = OpLHS.getValueType();
4221
4222 switch (OpNum) {
4223 default: llvm_unreachable("Unknown shuffle opcode!");
4224 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004225 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004226 if (VT.getVectorElementType() == MVT::i32 ||
4227 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004228 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4229 // vrev <4 x i16> -> VREV32
4230 if (VT.getVectorElementType() == MVT::i16)
4231 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4232 // vrev <4 x i8> -> VREV16
4233 assert(VT.getVectorElementType() == MVT::i8);
4234 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004235 case OP_VDUP0:
4236 case OP_VDUP1:
4237 case OP_VDUP2:
4238 case OP_VDUP3:
4239 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004240 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004241 case OP_VEXT1:
4242 case OP_VEXT2:
4243 case OP_VEXT3:
4244 return DAG.getNode(ARMISD::VEXT, dl, VT,
4245 OpLHS, OpRHS,
4246 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4247 case OP_VUZPL:
4248 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004249 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004250 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4251 case OP_VZIPL:
4252 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004253 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004254 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4255 case OP_VTRNL:
4256 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004257 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4258 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004259 }
4260}
4261
Bill Wendling69a05a72011-03-14 23:02:38 +00004262static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4263 SmallVectorImpl<int> &ShuffleMask,
4264 SelectionDAG &DAG) {
4265 // Check to see if we can use the VTBL instruction.
4266 SDValue V1 = Op.getOperand(0);
4267 SDValue V2 = Op.getOperand(1);
4268 DebugLoc DL = Op.getDebugLoc();
4269
4270 SmallVector<SDValue, 8> VTBLMask;
4271 for (SmallVectorImpl<int>::iterator
4272 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4273 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4274
4275 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4276 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4277 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4278 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004279
Owen Anderson76706012011-04-05 21:48:57 +00004280 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004281 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4282 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004283}
4284
Bob Wilson5bafff32009-06-22 23:27:02 +00004285static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004286 SDValue V1 = Op.getOperand(0);
4287 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004288 DebugLoc dl = Op.getDebugLoc();
4289 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004290 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004291 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004292
Bob Wilson28865062009-08-13 02:13:04 +00004293 // Convert shuffles that are directly supported on NEON to target-specific
4294 // DAG nodes, instead of keeping them as shuffles and matching them again
4295 // during code selection. This is more efficient and avoids the possibility
4296 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004297 // FIXME: floating-point vectors should be canonicalized to integer vectors
4298 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004299 SVN->getMask(ShuffleMask);
4300
Bob Wilson53dd2452010-06-07 23:53:38 +00004301 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4302 if (EltSize <= 32) {
4303 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4304 int Lane = SVN->getSplatIndex();
4305 // If this is undef splat, generate it via "just" vdup, if possible.
4306 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004307
Bob Wilson53dd2452010-06-07 23:53:38 +00004308 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4309 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4310 }
4311 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4312 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004313 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004314
4315 bool ReverseVEXT;
4316 unsigned Imm;
4317 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4318 if (ReverseVEXT)
4319 std::swap(V1, V2);
4320 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4321 DAG.getConstant(Imm, MVT::i32));
4322 }
4323
4324 if (isVREVMask(ShuffleMask, VT, 64))
4325 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4326 if (isVREVMask(ShuffleMask, VT, 32))
4327 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4328 if (isVREVMask(ShuffleMask, VT, 16))
4329 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4330
4331 // Check for Neon shuffles that modify both input vectors in place.
4332 // If both results are used, i.e., if there are two shuffles with the same
4333 // source operands and with masks corresponding to both results of one of
4334 // these operations, DAG memoization will ensure that a single node is
4335 // used for both shuffles.
4336 unsigned WhichResult;
4337 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4338 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4339 V1, V2).getValue(WhichResult);
4340 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4341 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4342 V1, V2).getValue(WhichResult);
4343 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4344 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4345 V1, V2).getValue(WhichResult);
4346
4347 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4348 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4349 V1, V1).getValue(WhichResult);
4350 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4351 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4352 V1, V1).getValue(WhichResult);
4353 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4354 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4355 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004356 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004357
Bob Wilsonc692cb72009-08-21 20:54:19 +00004358 // If the shuffle is not directly supported and it has 4 elements, use
4359 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004360 unsigned NumElts = VT.getVectorNumElements();
4361 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004362 unsigned PFIndexes[4];
4363 for (unsigned i = 0; i != 4; ++i) {
4364 if (ShuffleMask[i] < 0)
4365 PFIndexes[i] = 8;
4366 else
4367 PFIndexes[i] = ShuffleMask[i];
4368 }
4369
4370 // Compute the index in the perfect shuffle table.
4371 unsigned PFTableIndex =
4372 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004373 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4374 unsigned Cost = (PFEntry >> 30);
4375
4376 if (Cost <= 4)
4377 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4378 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004379
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004380 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004381 if (EltSize >= 32) {
4382 // Do the expansion with floating-point types, since that is what the VFP
4383 // registers are defined to use, and since i64 is not legal.
4384 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4385 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004386 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4387 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004388 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004389 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004390 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004391 Ops.push_back(DAG.getUNDEF(EltVT));
4392 else
4393 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4394 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4395 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4396 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004397 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004398 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004399 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004400 }
4401
Bill Wendling69a05a72011-03-14 23:02:38 +00004402 if (VT == MVT::v8i8) {
4403 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4404 if (NewOp.getNode())
4405 return NewOp;
4406 }
4407
Bob Wilson22cac0d2009-08-14 05:16:33 +00004408 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004409}
4410
Bob Wilson5bafff32009-06-22 23:27:02 +00004411static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004412 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004413 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004414 if (!isa<ConstantSDNode>(Lane))
4415 return SDValue();
4416
4417 SDValue Vec = Op.getOperand(0);
4418 if (Op.getValueType() == MVT::i32 &&
4419 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4420 DebugLoc dl = Op.getDebugLoc();
4421 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4422 }
4423
4424 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004425}
4426
Bob Wilsona6d65862009-08-03 20:36:38 +00004427static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4428 // The only time a CONCAT_VECTORS operation can have legal types is when
4429 // two 64-bit vectors are concatenated to a 128-bit vector.
4430 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4431 "unexpected CONCAT_VECTORS");
4432 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004433 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004434 SDValue Op0 = Op.getOperand(0);
4435 SDValue Op1 = Op.getOperand(1);
4436 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004437 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004438 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004439 DAG.getIntPtrConstant(0));
4440 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004441 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004442 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004443 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004444 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004445}
4446
Bob Wilson626613d2010-11-23 19:38:38 +00004447/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4448/// element has been zero/sign-extended, depending on the isSigned parameter,
4449/// from an integer type half its size.
4450static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4451 bool isSigned) {
4452 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4453 EVT VT = N->getValueType(0);
4454 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4455 SDNode *BVN = N->getOperand(0).getNode();
4456 if (BVN->getValueType(0) != MVT::v4i32 ||
4457 BVN->getOpcode() != ISD::BUILD_VECTOR)
4458 return false;
4459 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4460 unsigned HiElt = 1 - LoElt;
4461 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4462 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4463 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4464 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4465 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4466 return false;
4467 if (isSigned) {
4468 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4469 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4470 return true;
4471 } else {
4472 if (Hi0->isNullValue() && Hi1->isNullValue())
4473 return true;
4474 }
4475 return false;
4476 }
4477
4478 if (N->getOpcode() != ISD::BUILD_VECTOR)
4479 return false;
4480
4481 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4482 SDNode *Elt = N->getOperand(i).getNode();
4483 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4484 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4485 unsigned HalfSize = EltSize / 2;
4486 if (isSigned) {
4487 int64_t SExtVal = C->getSExtValue();
4488 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4489 return false;
4490 } else {
4491 if ((C->getZExtValue() >> HalfSize) != 0)
4492 return false;
4493 }
4494 continue;
4495 }
4496 return false;
4497 }
4498
4499 return true;
4500}
4501
4502/// isSignExtended - Check if a node is a vector value that is sign-extended
4503/// or a constant BUILD_VECTOR with sign-extended elements.
4504static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4505 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4506 return true;
4507 if (isExtendedBUILD_VECTOR(N, DAG, true))
4508 return true;
4509 return false;
4510}
4511
4512/// isZeroExtended - Check if a node is a vector value that is zero-extended
4513/// or a constant BUILD_VECTOR with zero-extended elements.
4514static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4515 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4516 return true;
4517 if (isExtendedBUILD_VECTOR(N, DAG, false))
4518 return true;
4519 return false;
4520}
4521
4522/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4523/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004524static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4525 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4526 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004527 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4528 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4529 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4530 LD->isNonTemporal(), LD->getAlignment());
4531 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4532 // have been legalized as a BITCAST from v4i32.
4533 if (N->getOpcode() == ISD::BITCAST) {
4534 SDNode *BVN = N->getOperand(0).getNode();
4535 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4536 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4537 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4538 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4539 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4540 }
4541 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4542 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4543 EVT VT = N->getValueType(0);
4544 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4545 unsigned NumElts = VT.getVectorNumElements();
4546 MVT TruncVT = MVT::getIntegerVT(EltSize);
4547 SmallVector<SDValue, 8> Ops;
4548 for (unsigned i = 0; i != NumElts; ++i) {
4549 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4550 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004551 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004552 }
4553 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4554 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004555}
4556
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004557static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4558 unsigned Opcode = N->getOpcode();
4559 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4560 SDNode *N0 = N->getOperand(0).getNode();
4561 SDNode *N1 = N->getOperand(1).getNode();
4562 return N0->hasOneUse() && N1->hasOneUse() &&
4563 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4564 }
4565 return false;
4566}
4567
4568static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4569 unsigned Opcode = N->getOpcode();
4570 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4571 SDNode *N0 = N->getOperand(0).getNode();
4572 SDNode *N1 = N->getOperand(1).getNode();
4573 return N0->hasOneUse() && N1->hasOneUse() &&
4574 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4575 }
4576 return false;
4577}
4578
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004579static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4580 // Multiplications are only custom-lowered for 128-bit vectors so that
4581 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4582 EVT VT = Op.getValueType();
4583 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4584 SDNode *N0 = Op.getOperand(0).getNode();
4585 SDNode *N1 = Op.getOperand(1).getNode();
4586 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004587 bool isMLA = false;
4588 bool isN0SExt = isSignExtended(N0, DAG);
4589 bool isN1SExt = isSignExtended(N1, DAG);
4590 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004591 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004592 else {
4593 bool isN0ZExt = isZeroExtended(N0, DAG);
4594 bool isN1ZExt = isZeroExtended(N1, DAG);
4595 if (isN0ZExt && isN1ZExt)
4596 NewOpc = ARMISD::VMULLu;
4597 else if (isN1SExt || isN1ZExt) {
4598 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4599 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4600 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4601 NewOpc = ARMISD::VMULLs;
4602 isMLA = true;
4603 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4604 NewOpc = ARMISD::VMULLu;
4605 isMLA = true;
4606 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4607 std::swap(N0, N1);
4608 NewOpc = ARMISD::VMULLu;
4609 isMLA = true;
4610 }
4611 }
4612
4613 if (!NewOpc) {
4614 if (VT == MVT::v2i64)
4615 // Fall through to expand this. It is not legal.
4616 return SDValue();
4617 else
4618 // Other vector multiplications are legal.
4619 return Op;
4620 }
4621 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004622
4623 // Legalize to a VMULL instruction.
4624 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004625 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004626 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004627 if (!isMLA) {
4628 Op0 = SkipExtension(N0, DAG);
4629 assert(Op0.getValueType().is64BitVector() &&
4630 Op1.getValueType().is64BitVector() &&
4631 "unexpected types for extended operands to VMULL");
4632 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4633 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004634
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004635 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4636 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4637 // vmull q0, d4, d6
4638 // vmlal q0, d5, d6
4639 // is faster than
4640 // vaddl q0, d4, d5
4641 // vmovl q1, d6
4642 // vmul q0, q0, q1
4643 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4644 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4645 EVT Op1VT = Op1.getValueType();
4646 return DAG.getNode(N0->getOpcode(), DL, VT,
4647 DAG.getNode(NewOpc, DL, VT,
4648 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4649 DAG.getNode(NewOpc, DL, VT,
4650 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004651}
4652
Owen Anderson76706012011-04-05 21:48:57 +00004653static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004654LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4655 // Convert to float
4656 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4657 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4658 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4659 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4660 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4661 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4662 // Get reciprocal estimate.
4663 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004664 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004665 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4666 // Because char has a smaller range than uchar, we can actually get away
4667 // without any newton steps. This requires that we use a weird bias
4668 // of 0xb000, however (again, this has been exhaustively tested).
4669 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4670 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4671 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4672 Y = DAG.getConstant(0xb000, MVT::i32);
4673 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4674 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4675 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4676 // Convert back to short.
4677 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4678 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4679 return X;
4680}
4681
Owen Anderson76706012011-04-05 21:48:57 +00004682static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004683LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4684 SDValue N2;
4685 // Convert to float.
4686 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4687 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4688 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4689 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4690 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4691 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004692
Nate Begeman7973f352011-02-11 20:53:29 +00004693 // Use reciprocal estimate and one refinement step.
4694 // float4 recip = vrecpeq_f32(yf);
4695 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004696 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004697 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004698 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004699 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4700 N1, N2);
4701 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4702 // Because short has a smaller range than ushort, we can actually get away
4703 // with only a single newton step. This requires that we use a weird bias
4704 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004705 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004706 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4707 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004708 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004709 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4710 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4711 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4712 // Convert back to integer and return.
4713 // return vmovn_s32(vcvt_s32_f32(result));
4714 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4715 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4716 return N0;
4717}
4718
4719static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4720 EVT VT = Op.getValueType();
4721 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4722 "unexpected type for custom-lowering ISD::SDIV");
4723
4724 DebugLoc dl = Op.getDebugLoc();
4725 SDValue N0 = Op.getOperand(0);
4726 SDValue N1 = Op.getOperand(1);
4727 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004728
Nate Begeman7973f352011-02-11 20:53:29 +00004729 if (VT == MVT::v8i8) {
4730 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4731 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004732
Nate Begeman7973f352011-02-11 20:53:29 +00004733 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4734 DAG.getIntPtrConstant(4));
4735 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004736 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004737 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4738 DAG.getIntPtrConstant(0));
4739 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4740 DAG.getIntPtrConstant(0));
4741
4742 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4743 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4744
4745 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4746 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004747
Nate Begeman7973f352011-02-11 20:53:29 +00004748 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4749 return N0;
4750 }
4751 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4752}
4753
4754static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4755 EVT VT = Op.getValueType();
4756 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4757 "unexpected type for custom-lowering ISD::UDIV");
4758
4759 DebugLoc dl = Op.getDebugLoc();
4760 SDValue N0 = Op.getOperand(0);
4761 SDValue N1 = Op.getOperand(1);
4762 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004763
Nate Begeman7973f352011-02-11 20:53:29 +00004764 if (VT == MVT::v8i8) {
4765 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4766 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004767
Nate Begeman7973f352011-02-11 20:53:29 +00004768 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4769 DAG.getIntPtrConstant(4));
4770 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004771 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004772 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4773 DAG.getIntPtrConstant(0));
4774 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4775 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004776
Nate Begeman7973f352011-02-11 20:53:29 +00004777 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4778 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004779
Nate Begeman7973f352011-02-11 20:53:29 +00004780 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4781 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004782
4783 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004784 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4785 N0);
4786 return N0;
4787 }
Owen Anderson76706012011-04-05 21:48:57 +00004788
Nate Begeman7973f352011-02-11 20:53:29 +00004789 // v4i16 sdiv ... Convert to float.
4790 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4791 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4792 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4793 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4794 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004795 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004796
4797 // Use reciprocal estimate and two refinement steps.
4798 // float4 recip = vrecpeq_f32(yf);
4799 // recip *= vrecpsq_f32(yf, recip);
4800 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004801 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004802 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004803 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004804 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004805 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004806 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004807 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004808 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004809 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004810 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4811 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4812 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4813 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004814 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004815 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4816 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4817 N1 = DAG.getConstant(2, MVT::i32);
4818 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4819 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4820 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4821 // Convert back to integer and return.
4822 // return vmovn_u32(vcvt_s32_f32(result));
4823 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4824 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4825 return N0;
4826}
4827
Evan Cheng342e3162011-08-30 01:34:54 +00004828static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4829 EVT VT = Op.getNode()->getValueType(0);
4830 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4831
4832 unsigned Opc;
4833 bool ExtraOp = false;
4834 switch (Op.getOpcode()) {
4835 default: assert(0 && "Invalid code");
4836 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4837 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4838 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4839 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4840 }
4841
4842 if (!ExtraOp)
4843 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4844 Op.getOperand(1));
4845 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4846 Op.getOperand(1), Op.getOperand(2));
4847}
4848
Dan Gohmand858e902010-04-17 15:26:15 +00004849SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004850 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004851 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004852 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004853 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004854 case ISD::GlobalAddress:
4855 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4856 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004857 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004858 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004859 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4860 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004861 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004862 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004863 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00004864 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004865 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004866 case ISD::SINT_TO_FP:
4867 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4868 case ISD::FP_TO_SINT:
4869 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004870 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004871 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004872 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004873 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004874 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004875 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004876 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004877 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4878 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004879 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004880 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004881 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004882 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004883 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004884 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004885 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004886 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004887 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004888 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004889 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004890 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004891 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004892 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004893 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004894 case ISD::SDIV: return LowerSDIV(Op, DAG);
4895 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00004896 case ISD::ADDC:
4897 case ISD::ADDE:
4898 case ISD::SUBC:
4899 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004900 }
Dan Gohman475871a2008-07-27 21:46:04 +00004901 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004902}
4903
Duncan Sands1607f052008-12-01 11:39:25 +00004904/// ReplaceNodeResults - Replace the results of node with an illegal result
4905/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004906void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4907 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004908 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004909 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004910 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004911 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004912 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004913 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004914 case ISD::BITCAST:
4915 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004916 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004917 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004918 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004919 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004920 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004921 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004922 if (Res.getNode())
4923 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004924}
Chris Lattner27a6c732007-11-24 07:07:01 +00004925
Evan Chenga8e29892007-01-19 07:51:42 +00004926//===----------------------------------------------------------------------===//
4927// ARM Scheduler Hooks
4928//===----------------------------------------------------------------------===//
4929
4930MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004931ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4932 MachineBasicBlock *BB,
4933 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004934 unsigned dest = MI->getOperand(0).getReg();
4935 unsigned ptr = MI->getOperand(1).getReg();
4936 unsigned oldval = MI->getOperand(2).getReg();
4937 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004938 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4939 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004940 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004941
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004942 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4943 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004944 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004945 : ARM::GPRRegisterClass);
4946
4947 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004948 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4949 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4950 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004951 }
4952
Jim Grosbach5278eb82009-12-11 01:42:04 +00004953 unsigned ldrOpc, strOpc;
4954 switch (Size) {
4955 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004956 case 1:
4957 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004958 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004959 break;
4960 case 2:
4961 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4962 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4963 break;
4964 case 4:
4965 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4966 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4967 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004968 }
4969
4970 MachineFunction *MF = BB->getParent();
4971 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4972 MachineFunction::iterator It = BB;
4973 ++It; // insert the new blocks after the current block
4974
4975 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4976 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4977 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4978 MF->insert(It, loop1MBB);
4979 MF->insert(It, loop2MBB);
4980 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004981
4982 // Transfer the remainder of BB and its successor edges to exitMBB.
4983 exitMBB->splice(exitMBB->begin(), BB,
4984 llvm::next(MachineBasicBlock::iterator(MI)),
4985 BB->end());
4986 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004987
4988 // thisMBB:
4989 // ...
4990 // fallthrough --> loop1MBB
4991 BB->addSuccessor(loop1MBB);
4992
4993 // loop1MBB:
4994 // ldrex dest, [ptr]
4995 // cmp dest, oldval
4996 // bne exitMBB
4997 BB = loop1MBB;
4998 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004999 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005000 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005001 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5002 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005003 BB->addSuccessor(loop2MBB);
5004 BB->addSuccessor(exitMBB);
5005
5006 // loop2MBB:
5007 // strex scratch, newval, [ptr]
5008 // cmp scratch, #0
5009 // bne loop1MBB
5010 BB = loop2MBB;
5011 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
5012 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005013 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005014 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005015 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5016 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005017 BB->addSuccessor(loop1MBB);
5018 BB->addSuccessor(exitMBB);
5019
5020 // exitMBB:
5021 // ...
5022 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005023
Dan Gohman14152b42010-07-06 20:24:04 +00005024 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005025
Jim Grosbach5278eb82009-12-11 01:42:04 +00005026 return BB;
5027}
5028
5029MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005030ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5031 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005032 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5033 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5034
5035 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005036 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005037 MachineFunction::iterator It = BB;
5038 ++It;
5039
5040 unsigned dest = MI->getOperand(0).getReg();
5041 unsigned ptr = MI->getOperand(1).getReg();
5042 unsigned incr = MI->getOperand(2).getReg();
5043 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005044 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005045
5046 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5047 if (isThumb2) {
5048 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5049 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5050 }
5051
Jim Grosbachc3c23542009-12-14 04:22:04 +00005052 unsigned ldrOpc, strOpc;
5053 switch (Size) {
5054 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005055 case 1:
5056 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005057 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005058 break;
5059 case 2:
5060 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5061 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5062 break;
5063 case 4:
5064 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5065 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5066 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005067 }
5068
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005069 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5070 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5071 MF->insert(It, loopMBB);
5072 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005073
5074 // Transfer the remainder of BB and its successor edges to exitMBB.
5075 exitMBB->splice(exitMBB->begin(), BB,
5076 llvm::next(MachineBasicBlock::iterator(MI)),
5077 BB->end());
5078 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005079
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005080 TargetRegisterClass *TRC =
5081 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5082 unsigned scratch = MRI.createVirtualRegister(TRC);
5083 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005084
5085 // thisMBB:
5086 // ...
5087 // fallthrough --> loopMBB
5088 BB->addSuccessor(loopMBB);
5089
5090 // loopMBB:
5091 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005092 // <binop> scratch2, dest, incr
5093 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005094 // cmp scratch, #0
5095 // bne- loopMBB
5096 // fallthrough --> exitMBB
5097 BB = loopMBB;
5098 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005099 if (BinOpcode) {
5100 // operand order needs to go the other way for NAND
5101 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5102 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5103 addReg(incr).addReg(dest)).addReg(0);
5104 else
5105 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5106 addReg(dest).addReg(incr)).addReg(0);
5107 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005108
5109 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5110 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005111 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005112 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005113 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5114 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005115
5116 BB->addSuccessor(loopMBB);
5117 BB->addSuccessor(exitMBB);
5118
5119 // exitMBB:
5120 // ...
5121 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005122
Dan Gohman14152b42010-07-06 20:24:04 +00005123 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005124
Jim Grosbachc3c23542009-12-14 04:22:04 +00005125 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005126}
5127
Jim Grosbachf7da8822011-04-26 19:44:18 +00005128MachineBasicBlock *
5129ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5130 MachineBasicBlock *BB,
5131 unsigned Size,
5132 bool signExtend,
5133 ARMCC::CondCodes Cond) const {
5134 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5135
5136 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5137 MachineFunction *MF = BB->getParent();
5138 MachineFunction::iterator It = BB;
5139 ++It;
5140
5141 unsigned dest = MI->getOperand(0).getReg();
5142 unsigned ptr = MI->getOperand(1).getReg();
5143 unsigned incr = MI->getOperand(2).getReg();
5144 unsigned oldval = dest;
5145 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005146 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005147
5148 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5149 if (isThumb2) {
5150 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5151 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5152 }
5153
Jim Grosbachf7da8822011-04-26 19:44:18 +00005154 unsigned ldrOpc, strOpc, extendOpc;
5155 switch (Size) {
5156 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5157 case 1:
5158 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5159 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005160 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005161 break;
5162 case 2:
5163 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5164 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005165 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005166 break;
5167 case 4:
5168 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5169 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5170 extendOpc = 0;
5171 break;
5172 }
5173
5174 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5175 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5176 MF->insert(It, loopMBB);
5177 MF->insert(It, exitMBB);
5178
5179 // Transfer the remainder of BB and its successor edges to exitMBB.
5180 exitMBB->splice(exitMBB->begin(), BB,
5181 llvm::next(MachineBasicBlock::iterator(MI)),
5182 BB->end());
5183 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5184
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005185 TargetRegisterClass *TRC =
5186 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5187 unsigned scratch = MRI.createVirtualRegister(TRC);
5188 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005189
5190 // thisMBB:
5191 // ...
5192 // fallthrough --> loopMBB
5193 BB->addSuccessor(loopMBB);
5194
5195 // loopMBB:
5196 // ldrex dest, ptr
5197 // (sign extend dest, if required)
5198 // cmp dest, incr
5199 // cmov.cond scratch2, dest, incr
5200 // strex scratch, scratch2, ptr
5201 // cmp scratch, #0
5202 // bne- loopMBB
5203 // fallthrough --> exitMBB
5204 BB = loopMBB;
5205 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5206
5207 // Sign extend the value, if necessary.
5208 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005209 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005210 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5211 .addReg(dest)
5212 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005213 }
5214
5215 // Build compare and cmov instructions.
5216 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5217 .addReg(oldval).addReg(incr));
5218 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5219 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5220
5221 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5222 .addReg(ptr));
5223 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5224 .addReg(scratch).addImm(0));
5225 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5226 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5227
5228 BB->addSuccessor(loopMBB);
5229 BB->addSuccessor(exitMBB);
5230
5231 // exitMBB:
5232 // ...
5233 BB = exitMBB;
5234
5235 MI->eraseFromParent(); // The instruction is gone now.
5236
5237 return BB;
5238}
5239
Evan Cheng218977b2010-07-13 19:27:42 +00005240static
5241MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5242 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5243 E = MBB->succ_end(); I != E; ++I)
5244 if (*I != Succ)
5245 return *I;
5246 llvm_unreachable("Expecting a BB with two successors!");
5247}
5248
Jim Grosbache801dc42009-12-12 01:40:06 +00005249MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005250ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005251 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005252 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005253 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005254 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005255 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005256 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005257 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005258 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005259 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005260 case ARM::STRi_preidx:
5261 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00005262 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00005263 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
5264 // Decode the offset.
5265 unsigned Offset = MI->getOperand(4).getImm();
5266 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
5267 Offset = ARM_AM::getAM2Offset(Offset);
5268 if (isSub)
5269 Offset = -Offset;
5270
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005271 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00005272 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00005273 .addOperand(MI->getOperand(0)) // Rn_wb
5274 .addOperand(MI->getOperand(1)) // Rt
5275 .addOperand(MI->getOperand(2)) // Rn
5276 .addImm(Offset) // offset (skip GPR==zero_reg)
5277 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005278 .addOperand(MI->getOperand(6))
5279 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00005280 MI->eraseFromParent();
5281 return BB;
5282 }
5283 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00005284 case ARM::STRBr_preidx:
5285 case ARM::STRH_preidx: {
5286 unsigned NewOpc;
5287 switch (MI->getOpcode()) {
5288 default: llvm_unreachable("unexpected opcode!");
5289 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
5290 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
5291 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
5292 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005293 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5294 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5295 MIB.addOperand(MI->getOperand(i));
5296 MI->eraseFromParent();
5297 return BB;
5298 }
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.
Eric Christopheref7f1e72011-07-29 21:18:58 +00007567 // An address with a single base register. Due to the way we
7568 // currently handle addresses it is the same as an 'r' memory constraint.
7569 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007570 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007571 } else if (Constraint.size() == 2) {
7572 switch (Constraint[0]) {
7573 default: break;
7574 // All 'U+' constraints are addresses.
7575 case 'U': return C_Memory;
7576 }
Evan Chenga8e29892007-01-19 07:51:42 +00007577 }
Chris Lattner4234f572007-03-25 02:14:49 +00007578 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007579}
7580
John Thompson44ab89e2010-10-29 17:29:13 +00007581/// Examine constraint type and operand type and determine a weight value.
7582/// This object must already have been set up with the operand type
7583/// and the current alternative constraint selected.
7584TargetLowering::ConstraintWeight
7585ARMTargetLowering::getSingleConstraintMatchWeight(
7586 AsmOperandInfo &info, const char *constraint) const {
7587 ConstraintWeight weight = CW_Invalid;
7588 Value *CallOperandVal = info.CallOperandVal;
7589 // If we don't have a value, we can't do a match,
7590 // but allow it at the lowest weight.
7591 if (CallOperandVal == NULL)
7592 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007593 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007594 // Look at the constraint type.
7595 switch (*constraint) {
7596 default:
7597 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7598 break;
7599 case 'l':
7600 if (type->isIntegerTy()) {
7601 if (Subtarget->isThumb())
7602 weight = CW_SpecificReg;
7603 else
7604 weight = CW_Register;
7605 }
7606 break;
7607 case 'w':
7608 if (type->isFloatingPointTy())
7609 weight = CW_Register;
7610 break;
7611 }
7612 return weight;
7613}
7614
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007615typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7616RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007617ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007618 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007619 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007620 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007621 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007622 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007623 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007624 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007625 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007626 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007627 case 'h': // High regs or no regs.
7628 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007629 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007630 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007631 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007632 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007633 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007634 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007635 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007636 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007637 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007638 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007639 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007640 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007641 case 'x':
7642 if (VT == MVT::f32)
7643 return RCPair(0U, ARM::SPR_8RegisterClass);
7644 if (VT.getSizeInBits() == 64)
7645 return RCPair(0U, ARM::DPR_8RegisterClass);
7646 if (VT.getSizeInBits() == 128)
7647 return RCPair(0U, ARM::QPR_8RegisterClass);
7648 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007649 case 't':
7650 if (VT == MVT::f32)
7651 return RCPair(0U, ARM::SPRRegisterClass);
7652 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007653 }
7654 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007655 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007656 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007657
Evan Chenga8e29892007-01-19 07:51:42 +00007658 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7659}
7660
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007661/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7662/// vector. If it is invalid, don't add anything to Ops.
7663void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007664 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007665 std::vector<SDValue>&Ops,
7666 SelectionDAG &DAG) const {
7667 SDValue Result(0, 0);
7668
Eric Christopher100c8332011-06-02 23:16:42 +00007669 // Currently only support length 1 constraints.
7670 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007671
Eric Christopher100c8332011-06-02 23:16:42 +00007672 char ConstraintLetter = Constraint[0];
7673 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007674 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007675 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007676 case 'I': case 'J': case 'K': case 'L':
7677 case 'M': case 'N': case 'O':
7678 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7679 if (!C)
7680 return;
7681
7682 int64_t CVal64 = C->getSExtValue();
7683 int CVal = (int) CVal64;
7684 // None of these constraints allow values larger than 32 bits. Check
7685 // that the value fits in an int.
7686 if (CVal != CVal64)
7687 return;
7688
Eric Christopher100c8332011-06-02 23:16:42 +00007689 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007690 case 'j':
7691 // Constant suitable for movw, must be between 0 and
7692 // 65535.
7693 if (Subtarget->hasV6T2Ops())
7694 if (CVal >= 0 && CVal <= 65535)
7695 break;
7696 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007697 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007698 if (Subtarget->isThumb1Only()) {
7699 // This must be a constant between 0 and 255, for ADD
7700 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007701 if (CVal >= 0 && CVal <= 255)
7702 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007703 } else if (Subtarget->isThumb2()) {
7704 // A constant that can be used as an immediate value in a
7705 // data-processing instruction.
7706 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7707 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007708 } else {
7709 // A constant that can be used as an immediate value in a
7710 // data-processing instruction.
7711 if (ARM_AM::getSOImmVal(CVal) != -1)
7712 break;
7713 }
7714 return;
7715
7716 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007717 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007718 // This must be a constant between -255 and -1, for negated ADD
7719 // immediates. This can be used in GCC with an "n" modifier that
7720 // prints the negated value, for use with SUB instructions. It is
7721 // not useful otherwise but is implemented for compatibility.
7722 if (CVal >= -255 && CVal <= -1)
7723 break;
7724 } else {
7725 // This must be a constant between -4095 and 4095. It is not clear
7726 // what this constraint is intended for. Implemented for
7727 // compatibility with GCC.
7728 if (CVal >= -4095 && CVal <= 4095)
7729 break;
7730 }
7731 return;
7732
7733 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007734 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007735 // A 32-bit value where only one byte has a nonzero value. Exclude
7736 // zero to match GCC. This constraint is used by GCC internally for
7737 // constants that can be loaded with a move/shift combination.
7738 // It is not useful otherwise but is implemented for compatibility.
7739 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7740 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007741 } else if (Subtarget->isThumb2()) {
7742 // A constant whose bitwise inverse can be used as an immediate
7743 // value in a data-processing instruction. This can be used in GCC
7744 // with a "B" modifier that prints the inverted value, for use with
7745 // BIC and MVN instructions. It is not useful otherwise but is
7746 // implemented for compatibility.
7747 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7748 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007749 } else {
7750 // A constant whose bitwise inverse can be used as an immediate
7751 // value in a data-processing instruction. This can be used in GCC
7752 // with a "B" modifier that prints the inverted value, for use with
7753 // BIC and MVN instructions. It is not useful otherwise but is
7754 // implemented for compatibility.
7755 if (ARM_AM::getSOImmVal(~CVal) != -1)
7756 break;
7757 }
7758 return;
7759
7760 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007761 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007762 // This must be a constant between -7 and 7,
7763 // for 3-operand ADD/SUB immediate instructions.
7764 if (CVal >= -7 && CVal < 7)
7765 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007766 } else if (Subtarget->isThumb2()) {
7767 // A constant whose negation can be used as an immediate value in a
7768 // data-processing instruction. This can be used in GCC with an "n"
7769 // modifier that prints the negated value, for use with SUB
7770 // instructions. It is not useful otherwise but is implemented for
7771 // compatibility.
7772 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7773 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007774 } else {
7775 // A constant whose negation can be used as an immediate value in a
7776 // data-processing instruction. This can be used in GCC with an "n"
7777 // modifier that prints the negated value, for use with SUB
7778 // instructions. It is not useful otherwise but is implemented for
7779 // compatibility.
7780 if (ARM_AM::getSOImmVal(-CVal) != -1)
7781 break;
7782 }
7783 return;
7784
7785 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007786 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007787 // This must be a multiple of 4 between 0 and 1020, for
7788 // ADD sp + immediate.
7789 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7790 break;
7791 } else {
7792 // A power of two or a constant between 0 and 32. This is used in
7793 // GCC for the shift amount on shifted register operands, but it is
7794 // useful in general for any shift amounts.
7795 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7796 break;
7797 }
7798 return;
7799
7800 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007801 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007802 // This must be a constant between 0 and 31, for shift amounts.
7803 if (CVal >= 0 && CVal <= 31)
7804 break;
7805 }
7806 return;
7807
7808 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007809 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007810 // This must be a multiple of 4 between -508 and 508, for
7811 // ADD/SUB sp = sp + immediate.
7812 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7813 break;
7814 }
7815 return;
7816 }
7817 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7818 break;
7819 }
7820
7821 if (Result.getNode()) {
7822 Ops.push_back(Result);
7823 return;
7824 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007825 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007826}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007827
7828bool
7829ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7830 // The ARM target isn't yet aware of offsets.
7831 return false;
7832}
Evan Cheng39382422009-10-28 01:44:26 +00007833
7834int ARM::getVFPf32Imm(const APFloat &FPImm) {
7835 APInt Imm = FPImm.bitcastToAPInt();
7836 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7837 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7838 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7839
7840 // We can handle 4 bits of mantissa.
7841 // mantissa = (16+UInt(e:f:g:h))/16.
7842 if (Mantissa & 0x7ffff)
7843 return -1;
7844 Mantissa >>= 19;
7845 if ((Mantissa & 0xf) != Mantissa)
7846 return -1;
7847
7848 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7849 if (Exp < -3 || Exp > 4)
7850 return -1;
7851 Exp = ((Exp+3) & 0x7) ^ 4;
7852
7853 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7854}
7855
7856int ARM::getVFPf64Imm(const APFloat &FPImm) {
7857 APInt Imm = FPImm.bitcastToAPInt();
7858 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7859 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7860 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7861
7862 // We can handle 4 bits of mantissa.
7863 // mantissa = (16+UInt(e:f:g:h))/16.
7864 if (Mantissa & 0xffffffffffffLL)
7865 return -1;
7866 Mantissa >>= 48;
7867 if ((Mantissa & 0xf) != Mantissa)
7868 return -1;
7869
7870 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7871 if (Exp < -3 || Exp > 4)
7872 return -1;
7873 Exp = ((Exp+3) & 0x7) ^ 4;
7874
7875 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7876}
7877
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007878bool ARM::isBitFieldInvertedMask(unsigned v) {
7879 if (v == 0xffffffff)
7880 return 0;
7881 // there can be 1's on either or both "outsides", all the "inside"
7882 // bits must be 0's
7883 unsigned int lsb = 0, msb = 31;
7884 while (v & (1 << msb)) --msb;
7885 while (v & (1 << lsb)) ++lsb;
7886 for (unsigned int i = lsb; i <= msb; ++i) {
7887 if (v & (1 << i))
7888 return 0;
7889 }
7890 return 1;
7891}
7892
Evan Cheng39382422009-10-28 01:44:26 +00007893/// isFPImmLegal - Returns true if the target can instruction select the
7894/// specified FP immediate natively. If false, the legalizer will
7895/// materialize the FP immediate as a load from a constant pool.
7896bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7897 if (!Subtarget->hasVFP3())
7898 return false;
7899 if (VT == MVT::f32)
7900 return ARM::getVFPf32Imm(Imm) != -1;
7901 if (VT == MVT::f64)
7902 return ARM::getVFPf64Imm(Imm) != -1;
7903 return false;
7904}
Bob Wilson65ffec42010-09-21 17:56:22 +00007905
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007906/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007907/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7908/// specified in the intrinsic calls.
7909bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7910 const CallInst &I,
7911 unsigned Intrinsic) const {
7912 switch (Intrinsic) {
7913 case Intrinsic::arm_neon_vld1:
7914 case Intrinsic::arm_neon_vld2:
7915 case Intrinsic::arm_neon_vld3:
7916 case Intrinsic::arm_neon_vld4:
7917 case Intrinsic::arm_neon_vld2lane:
7918 case Intrinsic::arm_neon_vld3lane:
7919 case Intrinsic::arm_neon_vld4lane: {
7920 Info.opc = ISD::INTRINSIC_W_CHAIN;
7921 // Conservatively set memVT to the entire set of vectors loaded.
7922 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7923 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7924 Info.ptrVal = I.getArgOperand(0);
7925 Info.offset = 0;
7926 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7927 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7928 Info.vol = false; // volatile loads with NEON intrinsics not supported
7929 Info.readMem = true;
7930 Info.writeMem = false;
7931 return true;
7932 }
7933 case Intrinsic::arm_neon_vst1:
7934 case Intrinsic::arm_neon_vst2:
7935 case Intrinsic::arm_neon_vst3:
7936 case Intrinsic::arm_neon_vst4:
7937 case Intrinsic::arm_neon_vst2lane:
7938 case Intrinsic::arm_neon_vst3lane:
7939 case Intrinsic::arm_neon_vst4lane: {
7940 Info.opc = ISD::INTRINSIC_VOID;
7941 // Conservatively set memVT to the entire set of vectors stored.
7942 unsigned NumElts = 0;
7943 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007944 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00007945 if (!ArgTy->isVectorTy())
7946 break;
7947 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7948 }
7949 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7950 Info.ptrVal = I.getArgOperand(0);
7951 Info.offset = 0;
7952 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7953 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7954 Info.vol = false; // volatile stores with NEON intrinsics not supported
7955 Info.readMem = false;
7956 Info.writeMem = true;
7957 return true;
7958 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007959 case Intrinsic::arm_strexd: {
7960 Info.opc = ISD::INTRINSIC_W_CHAIN;
7961 Info.memVT = MVT::i64;
7962 Info.ptrVal = I.getArgOperand(2);
7963 Info.offset = 0;
7964 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007965 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007966 Info.readMem = false;
7967 Info.writeMem = true;
7968 return true;
7969 }
7970 case Intrinsic::arm_ldrexd: {
7971 Info.opc = ISD::INTRINSIC_W_CHAIN;
7972 Info.memVT = MVT::i64;
7973 Info.ptrVal = I.getArgOperand(0);
7974 Info.offset = 0;
7975 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00007976 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00007977 Info.readMem = true;
7978 Info.writeMem = false;
7979 return true;
7980 }
Bob Wilson65ffec42010-09-21 17:56:22 +00007981 default:
7982 break;
7983 }
7984
7985 return false;
7986}