blob: b0d3e70918ee4817dc609dbe5194888fd7da7c1b [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"
17#include "ARMAddressingModes.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
20#include "ARMISelLowering.h"
21#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000022#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000023#include "ARMRegisterInfo.h"
24#include "ARMSubtarget.h"
25#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000026#include "ARMTargetObjectFile.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
Owen Andersone50ed302009-08-10 22:56:29 +000075void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
76 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000077 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000078 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000079 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
80 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000081
Owen Anderson70671842009-08-10 20:18:46 +000082 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000083 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000084 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000085 }
86
Owen Andersone50ed302009-08-10 22:56:29 +000087 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000088 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000089 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +000090 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000091 if (ElemTy != MVT::i32) {
92 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
94 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
95 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
96 }
Owen Anderson70671842009-08-10 20:18:46 +000097 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
98 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +000099 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000100 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000101 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
102 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000103 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000104 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
105 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000107 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
108 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000109 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
110 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
111 setTruncStoreAction(VT.getSimpleVT(),
112 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000113 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000114 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000115
116 // Promote all bit-wise operations.
117 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000118 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000119 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
120 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000121 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000122 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000123 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000125 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000126 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000127 }
Bob Wilson16330762009-09-16 00:17:28 +0000128
129 // Neon does not support vector divide/remainder operations.
130 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
131 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
132 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
133 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
134 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
135 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000136}
137
Owen Andersone50ed302009-08-10 22:56:29 +0000138void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000139 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000141}
142
Owen Andersone50ed302009-08-10 22:56:29 +0000143void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000144 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000146}
147
Chris Lattnerf0144122009-07-28 03:13:23 +0000148static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
149 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000150 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000151
Chris Lattner80ec2792009-08-02 00:34:36 +0000152 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000153}
154
Evan Chenga8e29892007-01-19 07:51:42 +0000155ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000156 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000157 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000158 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000159 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000160
Evan Chengb1df8f22007-04-27 08:15:43 +0000161 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000162 // Uses VFP for Thumb libfuncs if available.
163 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
164 // Single-precision floating-point arithmetic.
165 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
166 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
167 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
168 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000169
Evan Chengb1df8f22007-04-27 08:15:43 +0000170 // Double-precision floating-point arithmetic.
171 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
172 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
173 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
174 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000175
Evan Chengb1df8f22007-04-27 08:15:43 +0000176 // Single-precision comparisons.
177 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
178 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
179 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
180 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
181 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
182 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
183 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
184 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000185
Evan Chengb1df8f22007-04-27 08:15:43 +0000186 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000194
Evan Chengb1df8f22007-04-27 08:15:43 +0000195 // Double-precision comparisons.
196 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
197 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
198 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
199 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
200 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
201 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
202 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
203 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000204
Evan Chengb1df8f22007-04-27 08:15:43 +0000205 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Floating-point to integer conversions.
215 // i64 conversions are done via library routines even when generating VFP
216 // instructions, so use the same ones.
217 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
218 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
219 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
220 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000221
Evan Chengb1df8f22007-04-27 08:15:43 +0000222 // Conversions between floating types.
223 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
224 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
225
226 // Integer to floating-point conversions.
227 // i64 conversions are done via library routines even when generating VFP
228 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000229 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
230 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000231 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
232 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
233 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
234 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
235 }
Evan Chenga8e29892007-01-19 07:51:42 +0000236 }
237
Bob Wilson2f954612009-05-22 17:38:41 +0000238 // These libcalls are not available in 32-bit.
239 setLibcallName(RTLIB::SHL_I128, 0);
240 setLibcallName(RTLIB::SRL_I128, 0);
241 setLibcallName(RTLIB::SRA_I128, 0);
242
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000243 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000244 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000245 // RTABI chapter 4.1.2, Table 2
246 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
247 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
248 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
249 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
250 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
251 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
252 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
253 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
254
255 // Double-precision floating-point comparison helper functions
256 // RTABI chapter 4.1.2, Table 3
257 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
258 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
259 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
260 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
261 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
262 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
263 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
264 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
265 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
266 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
267 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
268 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
269 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
270 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
271 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
272 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
273 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
276 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
277 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
278 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
279 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
280 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
281
282 // Single-precision floating-point arithmetic helper functions
283 // RTABI chapter 4.1.2, Table 4
284 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
285 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
286 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
287 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
288 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
292
293 // Single-precision floating-point comparison helper functions
294 // RTABI chapter 4.1.2, Table 5
295 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
296 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
297 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
298 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
299 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
300 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
301 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
302 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
303 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
304 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
305 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
306 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
307 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
308 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
309 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
310 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
311 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
314 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
315 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
316 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
317 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
318 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
319
320 // Floating-point to integer conversions.
321 // RTABI chapter 4.1.2, Table 6
322 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
323 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
324 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
325 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
326 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
327 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
328 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
329 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
330 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
338
339 // Conversions between floating types.
340 // RTABI chapter 4.1.2, Table 7
341 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
342 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
343 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000344 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000345
346 // Integer to floating-point conversions.
347 // RTABI chapter 4.1.2, Table 8
348 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
349 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
350 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
351 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
352 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
353 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
354 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
355 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
356 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
360 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
361 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
362 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
363 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
364
365 // Long long helper functions
366 // RTABI chapter 4.2, Table 9
367 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
368 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
369 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
370 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
371 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
372 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
373 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
379
380 // Integer division functions
381 // RTABI chapter 4.3.1
382 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
383 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
384 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
385 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
386 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
387 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
388 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000393 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000394 }
395
David Goodwinf1daf7d2009-07-08 23:10:31 +0000396 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000398 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000400 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000402 if (!Subtarget->isFPOnlySP())
403 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000404
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000406 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000407
408 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 addDRTypeForNEON(MVT::v2f32);
410 addDRTypeForNEON(MVT::v8i8);
411 addDRTypeForNEON(MVT::v4i16);
412 addDRTypeForNEON(MVT::v2i32);
413 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000414
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 addQRTypeForNEON(MVT::v4f32);
416 addQRTypeForNEON(MVT::v2f64);
417 addQRTypeForNEON(MVT::v16i8);
418 addQRTypeForNEON(MVT::v8i16);
419 addQRTypeForNEON(MVT::v4i32);
420 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000421
Bob Wilson74dc72e2009-09-15 23:55:57 +0000422 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
423 // neither Neon nor VFP support any arithmetic operations on it.
424 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
425 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
426 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
427 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
428 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
429 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
430 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
431 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
432 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
433 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
434 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
435 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
436 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
437 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
438 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
439 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
440 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
441 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
442 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
443 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
444 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
445 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
446 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
447 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
448
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000449 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
450
Bob Wilson642b3292009-09-16 00:32:15 +0000451 // Neon does not support some operations on v1i64 and v2i64 types.
452 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000453 // Custom handling for some quad-vector types to detect VMULL.
454 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
455 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
456 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000457 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
458 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
459
Bob Wilson5bafff32009-06-22 23:27:02 +0000460 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
461 setTargetDAGCombine(ISD::SHL);
462 setTargetDAGCombine(ISD::SRL);
463 setTargetDAGCombine(ISD::SRA);
464 setTargetDAGCombine(ISD::SIGN_EXTEND);
465 setTargetDAGCombine(ISD::ZERO_EXTEND);
466 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000467 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000468 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000469 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000470 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
471 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000472 }
473
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000474 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000475
476 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000477 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000478
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000479 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000481
Evan Chenga8e29892007-01-19 07:51:42 +0000482 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000483 if (!Subtarget->isThumb1Only()) {
484 for (unsigned im = (unsigned)ISD::PRE_INC;
485 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setIndexedLoadAction(im, MVT::i1, Legal);
487 setIndexedLoadAction(im, MVT::i8, Legal);
488 setIndexedLoadAction(im, MVT::i16, Legal);
489 setIndexedLoadAction(im, MVT::i32, Legal);
490 setIndexedStoreAction(im, MVT::i1, Legal);
491 setIndexedStoreAction(im, MVT::i8, Legal);
492 setIndexedStoreAction(im, MVT::i16, Legal);
493 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000494 }
Evan Chenga8e29892007-01-19 07:51:42 +0000495 }
496
497 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000498 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::MUL, MVT::i64, Expand);
500 setOperationAction(ISD::MULHU, MVT::i32, Expand);
501 setOperationAction(ISD::MULHS, MVT::i32, Expand);
502 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
503 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000504 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::MUL, MVT::i64, Expand);
506 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000507 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000509 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000510 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000511 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000512 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::SRL, MVT::i64, Custom);
514 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000515
516 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000518 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000520 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000522
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000523 // Only ARMv6 has BSWAP.
524 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000526
Evan Chenga8e29892007-01-19 07:51:42 +0000527 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000528 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000529 // v7M has a hardware divider
530 setOperationAction(ISD::SDIV, MVT::i32, Expand);
531 setOperationAction(ISD::UDIV, MVT::i32, Expand);
532 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::SREM, MVT::i32, Expand);
534 setOperationAction(ISD::UREM, MVT::i32, Expand);
535 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
536 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
539 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
540 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
541 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000542 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000543
Evan Chengfb3611d2010-05-11 07:26:32 +0000544 setOperationAction(ISD::TRAP, MVT::Other, Legal);
545
Evan Chenga8e29892007-01-19 07:51:42 +0000546 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::VASTART, MVT::Other, Custom);
548 setOperationAction(ISD::VAARG, MVT::Other, Expand);
549 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
550 setOperationAction(ISD::VAEND, MVT::Other, Expand);
551 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
552 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000553 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
554 // FIXME: Shouldn't need this, since no register is used, but the legalizer
555 // doesn't yet know how to not do that for SjLj.
556 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000557 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000558 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
559 // the default expansion.
560 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000561 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000562 // membarrier needs custom lowering; the rest are legal and handled
563 // normally.
564 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
565 } else {
566 // Set them all for expansion, which will force libcalls.
567 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
568 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
569 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
570 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000571 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
572 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
573 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000574 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
575 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
576 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
577 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
578 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
579 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
580 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
581 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
582 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
583 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
584 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
585 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
586 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
587 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
588 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
589 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
590 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
591 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000592 // Since the libcalls include locking, fold in the fences
593 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000594 }
595 // 64-bit versions are always libcalls (for now)
596 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000597 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000598 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000604
Evan Cheng416941d2010-11-04 05:19:35 +0000605 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000606
Eli Friedmana2c6f452010-06-26 04:36:50 +0000607 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
608 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
610 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000611 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000613
Nate Begemand1fb5832010-08-03 21:31:55 +0000614 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000615 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
616 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000617 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000618 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
619 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000620
621 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000623 if (Subtarget->isTargetDarwin()) {
624 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
625 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000626 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000627 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000628
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::SETCC, MVT::i32, Expand);
630 setOperationAction(ISD::SETCC, MVT::f32, Expand);
631 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000632 setOperationAction(ISD::SELECT, MVT::i32, Custom);
633 setOperationAction(ISD::SELECT, MVT::f32, Custom);
634 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
636 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
637 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000638
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
640 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
641 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
642 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
643 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000644
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000645 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::FSIN, MVT::f64, Expand);
647 setOperationAction(ISD::FSIN, MVT::f32, Expand);
648 setOperationAction(ISD::FCOS, MVT::f32, Expand);
649 setOperationAction(ISD::FCOS, MVT::f64, Expand);
650 setOperationAction(ISD::FREM, MVT::f64, Expand);
651 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000652 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
654 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000655 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::FPOW, MVT::f64, Expand);
657 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000658
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000659 // Various VFP goodness
660 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000661 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
662 if (Subtarget->hasVFP2()) {
663 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
664 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
665 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
666 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
667 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000668 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000669 if (!Subtarget->hasFP16()) {
670 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
671 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000672 }
Evan Cheng110cf482008-04-01 01:50:16 +0000673 }
Evan Chenga8e29892007-01-19 07:51:42 +0000674
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000675 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000676 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000677 setTargetDAGCombine(ISD::ADD);
678 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000679 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000680
Owen Anderson080c0922010-11-05 19:27:46 +0000681 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000682 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000683 if (Subtarget->hasNEON())
684 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000685
Evan Chenga8e29892007-01-19 07:51:42 +0000686 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000687
Evan Chengf7d87ee2010-05-21 00:43:17 +0000688 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
689 setSchedulingPreference(Sched::RegPressure);
690 else
691 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000692
Evan Cheng05219282011-01-06 06:52:41 +0000693 //// temporary - rewrite interface to use type
694 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000695
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000696 // On ARM arguments smaller than 4 bytes are extended, so all arguments
697 // are at least 4 bytes aligned.
698 setMinStackArgumentAlignment(4);
699
Evan Chengfff606d2010-09-24 19:07:23 +0000700 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000701}
702
Evan Cheng4f6b4672010-07-21 06:09:07 +0000703std::pair<const TargetRegisterClass*, uint8_t>
704ARMTargetLowering::findRepresentativeClass(EVT VT) const{
705 const TargetRegisterClass *RRC = 0;
706 uint8_t Cost = 1;
707 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000708 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000709 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000710 // Use DPR as representative register class for all floating point
711 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
712 // the cost is 1 for both f32 and f64.
713 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000714 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000715 RRC = ARM::DPRRegisterClass;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000716 break;
717 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
718 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000719 RRC = ARM::DPRRegisterClass;
720 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000721 break;
722 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000723 RRC = ARM::DPRRegisterClass;
724 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000725 break;
726 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000727 RRC = ARM::DPRRegisterClass;
728 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000729 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000730 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000731 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000732}
733
Evan Chenga8e29892007-01-19 07:51:42 +0000734const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
735 switch (Opcode) {
736 default: return 0;
737 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000738 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000739 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
740 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000741 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000742 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
743 case ARMISD::tCALL: return "ARMISD::tCALL";
744 case ARMISD::BRCOND: return "ARMISD::BRCOND";
745 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000746 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000747 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
748 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
749 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000750 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000751 case ARMISD::CMPFP: return "ARMISD::CMPFP";
752 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000753 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000754 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
755 case ARMISD::CMOV: return "ARMISD::CMOV";
756 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000757
Jim Grosbach3482c802010-01-18 19:58:49 +0000758 case ARMISD::RBIT: return "ARMISD::RBIT";
759
Bob Wilson76a312b2010-03-19 22:51:32 +0000760 case ARMISD::FTOSI: return "ARMISD::FTOSI";
761 case ARMISD::FTOUI: return "ARMISD::FTOUI";
762 case ARMISD::SITOF: return "ARMISD::SITOF";
763 case ARMISD::UITOF: return "ARMISD::UITOF";
764
Evan Chenga8e29892007-01-19 07:51:42 +0000765 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
766 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
767 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000768
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000769 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
770 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000771
Evan Chengc5942082009-10-28 06:55:03 +0000772 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
773 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000774 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000775
Dale Johannesen51e28e62010-06-03 21:09:53 +0000776 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000777
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000778 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000779
Evan Cheng86198642009-08-07 00:34:42 +0000780 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
781
Jim Grosbach3728e962009-12-10 00:11:09 +0000782 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000783 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000784
Evan Chengdfed19f2010-11-03 06:34:55 +0000785 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
786
Bob Wilson5bafff32009-06-22 23:27:02 +0000787 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000788 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000789 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000790 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
791 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000792 case ARMISD::VCGEU: return "ARMISD::VCGEU";
793 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000794 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
795 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000796 case ARMISD::VCGTU: return "ARMISD::VCGTU";
797 case ARMISD::VTST: return "ARMISD::VTST";
798
799 case ARMISD::VSHL: return "ARMISD::VSHL";
800 case ARMISD::VSHRs: return "ARMISD::VSHRs";
801 case ARMISD::VSHRu: return "ARMISD::VSHRu";
802 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
803 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
804 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
805 case ARMISD::VSHRN: return "ARMISD::VSHRN";
806 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
807 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
808 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
809 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
810 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
811 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
812 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
813 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
814 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
815 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
816 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
817 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
818 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
819 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000820 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000821 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000822 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000823 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000824 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000825 case ARMISD::VREV64: return "ARMISD::VREV64";
826 case ARMISD::VREV32: return "ARMISD::VREV32";
827 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000828 case ARMISD::VZIP: return "ARMISD::VZIP";
829 case ARMISD::VUZP: return "ARMISD::VUZP";
830 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000831 case ARMISD::VMULLs: return "ARMISD::VMULLs";
832 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000833 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000834 case ARMISD::FMAX: return "ARMISD::FMAX";
835 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000836 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000837 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
838 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000839 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
840 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
841 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Evan Chenga8e29892007-01-19 07:51:42 +0000842 }
843}
844
Evan Cheng06b666c2010-05-15 02:18:07 +0000845/// getRegClassFor - Return the register class that should be used for the
846/// specified value type.
847TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
848 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
849 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
850 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000851 if (Subtarget->hasNEON()) {
852 if (VT == MVT::v4i64)
853 return ARM::QQPRRegisterClass;
854 else if (VT == MVT::v8i64)
855 return ARM::QQQQPRRegisterClass;
856 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000857 return TargetLowering::getRegClassFor(VT);
858}
859
Eric Christopherab695882010-07-21 22:26:11 +0000860// Create a fast isel object.
861FastISel *
862ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
863 return ARM::createFastISel(funcInfo);
864}
865
Bill Wendlingb4202b82009-07-01 18:50:55 +0000866/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000867unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000868 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000869}
870
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000871/// getMaximalGlobalOffset - Returns the maximal possible offset which can
872/// be used for loads / stores from the global.
873unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
874 return (Subtarget->isThumb1Only() ? 127 : 4095);
875}
876
Evan Cheng1cc39842010-05-20 23:26:43 +0000877Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000878 unsigned NumVals = N->getNumValues();
879 if (!NumVals)
880 return Sched::RegPressure;
881
882 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000883 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000884 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000885 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000886 if (VT.isFloatingPoint() || VT.isVector())
887 return Sched::Latency;
888 }
Evan Chengc10f5432010-05-28 23:25:23 +0000889
890 if (!N->isMachineOpcode())
891 return Sched::RegPressure;
892
893 // Load are scheduled for latency even if there instruction itinerary
894 // is not available.
895 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
896 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000897
898 if (TID.getNumDefs() == 0)
899 return Sched::RegPressure;
900 if (!Itins->isEmpty() &&
901 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000902 return Sched::Latency;
903
Evan Cheng1cc39842010-05-20 23:26:43 +0000904 return Sched::RegPressure;
905}
906
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000907// FIXME: Move to RegInfo
Evan Cheng31446872010-07-23 22:39:59 +0000908unsigned
909ARMTargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
910 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000911 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000912
Evan Cheng31446872010-07-23 22:39:59 +0000913 switch (RC->getID()) {
914 default:
915 return 0;
916 case ARM::tGPRRegClassID:
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000917 return TFI->hasFP(MF) ? 4 : 5;
Evan Chengac096802010-08-10 19:30:19 +0000918 case ARM::GPRRegClassID: {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000919 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
Evan Chengac096802010-08-10 19:30:19 +0000920 return 10 - FP - (Subtarget->isR9Reserved() ? 1 : 0);
921 }
Evan Cheng31446872010-07-23 22:39:59 +0000922 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
923 case ARM::DPRRegClassID:
924 return 32 - 10;
925 }
926}
927
Evan Chenga8e29892007-01-19 07:51:42 +0000928//===----------------------------------------------------------------------===//
929// Lowering Code
930//===----------------------------------------------------------------------===//
931
Evan Chenga8e29892007-01-19 07:51:42 +0000932/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
933static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
934 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000935 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000936 case ISD::SETNE: return ARMCC::NE;
937 case ISD::SETEQ: return ARMCC::EQ;
938 case ISD::SETGT: return ARMCC::GT;
939 case ISD::SETGE: return ARMCC::GE;
940 case ISD::SETLT: return ARMCC::LT;
941 case ISD::SETLE: return ARMCC::LE;
942 case ISD::SETUGT: return ARMCC::HI;
943 case ISD::SETUGE: return ARMCC::HS;
944 case ISD::SETULT: return ARMCC::LO;
945 case ISD::SETULE: return ARMCC::LS;
946 }
947}
948
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000949/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
950static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000951 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000952 CondCode2 = ARMCC::AL;
953 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000954 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000955 case ISD::SETEQ:
956 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
957 case ISD::SETGT:
958 case ISD::SETOGT: CondCode = ARMCC::GT; break;
959 case ISD::SETGE:
960 case ISD::SETOGE: CondCode = ARMCC::GE; break;
961 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000962 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000963 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
964 case ISD::SETO: CondCode = ARMCC::VC; break;
965 case ISD::SETUO: CondCode = ARMCC::VS; break;
966 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
967 case ISD::SETUGT: CondCode = ARMCC::HI; break;
968 case ISD::SETUGE: CondCode = ARMCC::PL; break;
969 case ISD::SETLT:
970 case ISD::SETULT: CondCode = ARMCC::LT; break;
971 case ISD::SETLE:
972 case ISD::SETULE: CondCode = ARMCC::LE; break;
973 case ISD::SETNE:
974 case ISD::SETUNE: CondCode = ARMCC::NE; break;
975 }
Evan Chenga8e29892007-01-19 07:51:42 +0000976}
977
Bob Wilson1f595bb2009-04-17 19:07:39 +0000978//===----------------------------------------------------------------------===//
979// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000980//===----------------------------------------------------------------------===//
981
982#include "ARMGenCallingConv.inc"
983
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000984/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
985/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000986CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000987 bool Return,
988 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000989 switch (CC) {
990 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000991 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000992 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +0000993 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +0000994 if (!Subtarget->isAAPCS_ABI())
995 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
996 // For AAPCS ABI targets, just use VFP variant of the calling convention.
997 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
998 }
999 // Fallthrough
1000 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001001 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001002 if (!Subtarget->isAAPCS_ABI())
1003 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1004 else if (Subtarget->hasVFP2() &&
1005 FloatABIType == FloatABI::Hard && !isVarArg)
1006 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1007 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1008 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001009 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001010 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001011 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001012 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001013 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001014 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001015 }
1016}
1017
Dan Gohman98ca4f22009-08-05 01:29:28 +00001018/// LowerCallResult - Lower the result values of a call into the
1019/// appropriate copies out of appropriate physical registers.
1020SDValue
1021ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001022 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001023 const SmallVectorImpl<ISD::InputArg> &Ins,
1024 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001025 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001026
Bob Wilson1f595bb2009-04-17 19:07:39 +00001027 // Assign locations to each value returned by this call.
1028 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001029 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001030 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001031 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001032 CCAssignFnForNode(CallConv, /* Return*/ true,
1033 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001034
1035 // Copy all of the result registers out of their specified physreg.
1036 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1037 CCValAssign VA = RVLocs[i];
1038
Bob Wilson80915242009-04-25 00:33:20 +00001039 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001040 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001041 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001042 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001043 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001044 Chain = Lo.getValue(1);
1045 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001046 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001047 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001048 InFlag);
1049 Chain = Hi.getValue(1);
1050 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001051 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001052
Owen Anderson825b72b2009-08-11 20:47:22 +00001053 if (VA.getLocVT() == MVT::v2f64) {
1054 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1055 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1056 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001057
1058 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001059 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001060 Chain = Lo.getValue(1);
1061 InFlag = Lo.getValue(2);
1062 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001064 Chain = Hi.getValue(1);
1065 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001066 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001067 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1068 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001069 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001070 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001071 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1072 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001073 Chain = Val.getValue(1);
1074 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001075 }
Bob Wilson80915242009-04-25 00:33:20 +00001076
1077 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001078 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001079 case CCValAssign::Full: break;
1080 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001081 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001082 break;
1083 }
1084
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086 }
1087
Dan Gohman98ca4f22009-08-05 01:29:28 +00001088 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001089}
1090
1091/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1092/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001093/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001094/// a byval function parameter.
1095/// Sometimes what we are copying is the end of a larger object, the part that
1096/// does not fit in registers.
1097static SDValue
1098CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1099 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1100 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001101 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001102 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001103 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001104 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001105}
1106
Bob Wilsondee46d72009-04-17 20:35:10 +00001107/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001108SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001109ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1110 SDValue StackPtr, SDValue Arg,
1111 DebugLoc dl, SelectionDAG &DAG,
1112 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001113 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001114 unsigned LocMemOffset = VA.getLocMemOffset();
1115 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1116 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001117 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001119
Bob Wilson1f595bb2009-04-17 19:07:39 +00001120 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001121 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001122 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001123}
1124
Dan Gohman98ca4f22009-08-05 01:29:28 +00001125void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001126 SDValue Chain, SDValue &Arg,
1127 RegsToPassVector &RegsToPass,
1128 CCValAssign &VA, CCValAssign &NextVA,
1129 SDValue &StackPtr,
1130 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001131 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001132
Jim Grosbache5165492009-11-09 00:11:35 +00001133 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001135 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1136
1137 if (NextVA.isRegLoc())
1138 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1139 else {
1140 assert(NextVA.isMemLoc());
1141 if (StackPtr.getNode() == 0)
1142 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1143
Dan Gohman98ca4f22009-08-05 01:29:28 +00001144 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1145 dl, DAG, NextVA,
1146 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001147 }
1148}
1149
Dan Gohman98ca4f22009-08-05 01:29:28 +00001150/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001151/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1152/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001153SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001154ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001155 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001156 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001157 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001158 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001159 const SmallVectorImpl<ISD::InputArg> &Ins,
1160 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001161 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001162 MachineFunction &MF = DAG.getMachineFunction();
1163 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1164 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001165 // Temporarily disable tail calls so things don't break.
1166 if (!EnableARMTailCalls)
1167 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001168 if (isTailCall) {
1169 // Check if it's really possible to do a tail call.
1170 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1171 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001172 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001173 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1174 // detected sibcalls.
1175 if (isTailCall) {
1176 ++NumTailCalls;
1177 IsSibCall = true;
1178 }
1179 }
Evan Chenga8e29892007-01-19 07:51:42 +00001180
Bob Wilson1f595bb2009-04-17 19:07:39 +00001181 // Analyze operands of the call, assigning locations to each operand.
1182 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1184 *DAG.getContext());
1185 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001186 CCAssignFnForNode(CallConv, /* Return*/ false,
1187 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001188
Bob Wilson1f595bb2009-04-17 19:07:39 +00001189 // Get a count of how many bytes are to be pushed on the stack.
1190 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001191
Dale Johannesen51e28e62010-06-03 21:09:53 +00001192 // For tail calls, memory operands are available in our caller's stack.
1193 if (IsSibCall)
1194 NumBytes = 0;
1195
Evan Chenga8e29892007-01-19 07:51:42 +00001196 // Adjust the stack pointer for the new arguments...
1197 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001198 if (!IsSibCall)
1199 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001200
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001201 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001202
Bob Wilson5bafff32009-06-22 23:27:02 +00001203 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001204 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001205
Bob Wilson1f595bb2009-04-17 19:07:39 +00001206 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001207 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001208 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1209 i != e;
1210 ++i, ++realArgIdx) {
1211 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001212 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001214
Bob Wilson1f595bb2009-04-17 19:07:39 +00001215 // Promote the value if needed.
1216 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001217 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218 case CCValAssign::Full: break;
1219 case CCValAssign::SExt:
1220 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1221 break;
1222 case CCValAssign::ZExt:
1223 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1224 break;
1225 case CCValAssign::AExt:
1226 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1227 break;
1228 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001229 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001231 }
1232
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001233 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001234 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001235 if (VA.getLocVT() == MVT::v2f64) {
1236 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1237 DAG.getConstant(0, MVT::i32));
1238 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1239 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001240
Dan Gohman98ca4f22009-08-05 01:29:28 +00001241 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001242 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1243
1244 VA = ArgLocs[++i]; // skip ahead to next loc
1245 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001246 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001247 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1248 } else {
1249 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001250
Dan Gohman98ca4f22009-08-05 01:29:28 +00001251 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1252 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001253 }
1254 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001255 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001256 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 }
1258 } else if (VA.isRegLoc()) {
1259 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001260 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262
Dan Gohman98ca4f22009-08-05 01:29:28 +00001263 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1264 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001265 }
Evan Chenga8e29892007-01-19 07:51:42 +00001266 }
1267
1268 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001270 &MemOpChains[0], MemOpChains.size());
1271
1272 // Build a sequence of copy-to-reg nodes chained together with token chain
1273 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001274 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001275 // Tail call byval lowering might overwrite argument registers so in case of
1276 // tail call optimization the copies to registers are lowered later.
1277 if (!isTailCall)
1278 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1279 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1280 RegsToPass[i].second, InFlag);
1281 InFlag = Chain.getValue(1);
1282 }
Evan Chenga8e29892007-01-19 07:51:42 +00001283
Dale Johannesen51e28e62010-06-03 21:09:53 +00001284 // For tail calls lower the arguments to the 'real' stack slot.
1285 if (isTailCall) {
1286 // Force all the incoming stack arguments to be loaded from the stack
1287 // before any new outgoing arguments are stored to the stack, because the
1288 // outgoing stack slots may alias the incoming argument stack slots, and
1289 // the alias isn't otherwise explicit. This is slightly more conservative
1290 // than necessary, because it means that each store effectively depends
1291 // on every argument instead of just those arguments it would clobber.
1292
1293 // Do not flag preceeding copytoreg stuff together with the following stuff.
1294 InFlag = SDValue();
1295 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1296 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1297 RegsToPass[i].second, InFlag);
1298 InFlag = Chain.getValue(1);
1299 }
1300 InFlag =SDValue();
1301 }
1302
Bill Wendling056292f2008-09-16 21:48:12 +00001303 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1304 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1305 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001306 bool isDirect = false;
1307 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001308 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001309 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001310
1311 if (EnableARMLongCalls) {
1312 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1313 && "long-calls with non-static relocation model!");
1314 // Handle a global address or an external symbol. If it's not one of
1315 // those, the target's already in a register, so we don't need to do
1316 // anything extra.
1317 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001318 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001319 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001320 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001321 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1322 ARMPCLabelIndex,
1323 ARMCP::CPValue, 0);
1324 // Get the address of the callee into a register
1325 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1326 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1327 Callee = DAG.getLoad(getPointerTy(), dl,
1328 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001329 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001330 false, false, 0);
1331 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1332 const char *Sym = S->getSymbol();
1333
1334 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001335 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001336 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1337 Sym, ARMPCLabelIndex, 0);
1338 // Get the address of the callee into a register
1339 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1340 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1341 Callee = DAG.getLoad(getPointerTy(), dl,
1342 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001343 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001344 false, false, 0);
1345 }
1346 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001347 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001348 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001349 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001350 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001351 getTargetMachine().getRelocationModel() != Reloc::Static;
1352 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001353 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001354 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001355 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001356 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001357 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001358 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001359 ARMPCLabelIndex,
1360 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001361 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001362 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001363 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001364 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001365 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001366 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001367 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001368 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001369 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001370 } else {
1371 // On ELF targets for PIC code, direct calls should go through the PLT
1372 unsigned OpFlags = 0;
1373 if (Subtarget->isTargetELF() &&
1374 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1375 OpFlags = ARMII::MO_PLT;
1376 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1377 }
Bill Wendling056292f2008-09-16 21:48:12 +00001378 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001379 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001380 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001381 getTargetMachine().getRelocationModel() != Reloc::Static;
1382 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001383 // tBX takes a register source operand.
1384 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001385 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001386 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001387 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001388 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001389 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001390 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001391 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001392 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001393 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001394 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001395 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001396 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001397 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001398 } else {
1399 unsigned OpFlags = 0;
1400 // On ELF targets for PIC code, direct calls should go through the PLT
1401 if (Subtarget->isTargetELF() &&
1402 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1403 OpFlags = ARMII::MO_PLT;
1404 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1405 }
Evan Chenga8e29892007-01-19 07:51:42 +00001406 }
1407
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001408 // FIXME: handle tail calls differently.
1409 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001410 if (Subtarget->isThumb()) {
1411 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001412 CallOpc = ARMISD::CALL_NOLINK;
1413 else
1414 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1415 } else {
1416 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001417 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1418 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001419 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001420
Dan Gohman475871a2008-07-27 21:46:04 +00001421 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001422 Ops.push_back(Chain);
1423 Ops.push_back(Callee);
1424
1425 // Add argument registers to the end of the list so that they are known live
1426 // into the call.
1427 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1428 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1429 RegsToPass[i].second.getValueType()));
1430
Gabor Greifba36cb52008-08-28 21:40:38 +00001431 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001432 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001433
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001434 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001435 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001436 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001437
Duncan Sands4bdcb612008-07-02 17:40:58 +00001438 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001439 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001440 InFlag = Chain.getValue(1);
1441
Chris Lattnere563bbc2008-10-11 22:08:30 +00001442 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1443 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001445 InFlag = Chain.getValue(1);
1446
Bob Wilson1f595bb2009-04-17 19:07:39 +00001447 // Handle result values, copying them out of physregs into vregs that we
1448 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001449 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1450 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001451}
1452
Dale Johannesen51e28e62010-06-03 21:09:53 +00001453/// MatchingStackOffset - Return true if the given stack call argument is
1454/// already available in the same position (relatively) of the caller's
1455/// incoming argument stack.
1456static
1457bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1458 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1459 const ARMInstrInfo *TII) {
1460 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1461 int FI = INT_MAX;
1462 if (Arg.getOpcode() == ISD::CopyFromReg) {
1463 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001464 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001465 return false;
1466 MachineInstr *Def = MRI->getVRegDef(VR);
1467 if (!Def)
1468 return false;
1469 if (!Flags.isByVal()) {
1470 if (!TII->isLoadFromStackSlot(Def, FI))
1471 return false;
1472 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001473 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001474 }
1475 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1476 if (Flags.isByVal())
1477 // ByVal argument is passed in as a pointer but it's now being
1478 // dereferenced. e.g.
1479 // define @foo(%struct.X* %A) {
1480 // tail call @bar(%struct.X* byval %A)
1481 // }
1482 return false;
1483 SDValue Ptr = Ld->getBasePtr();
1484 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1485 if (!FINode)
1486 return false;
1487 FI = FINode->getIndex();
1488 } else
1489 return false;
1490
1491 assert(FI != INT_MAX);
1492 if (!MFI->isFixedObjectIndex(FI))
1493 return false;
1494 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1495}
1496
1497/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1498/// for tail call optimization. Targets which want to do tail call
1499/// optimization should implement this function.
1500bool
1501ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1502 CallingConv::ID CalleeCC,
1503 bool isVarArg,
1504 bool isCalleeStructRet,
1505 bool isCallerStructRet,
1506 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001507 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001508 const SmallVectorImpl<ISD::InputArg> &Ins,
1509 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001510 const Function *CallerF = DAG.getMachineFunction().getFunction();
1511 CallingConv::ID CallerCC = CallerF->getCallingConv();
1512 bool CCMatch = CallerCC == CalleeCC;
1513
1514 // Look for obvious safe cases to perform tail call optimization that do not
1515 // require ABI changes. This is what gcc calls sibcall.
1516
Jim Grosbach7616b642010-06-16 23:45:49 +00001517 // Do not sibcall optimize vararg calls unless the call site is not passing
1518 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001519 if (isVarArg && !Outs.empty())
1520 return false;
1521
1522 // Also avoid sibcall optimization if either caller or callee uses struct
1523 // return semantics.
1524 if (isCalleeStructRet || isCallerStructRet)
1525 return false;
1526
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001527 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001528 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001529 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1530 // LR. This means if we need to reload LR, it takes an extra instructions,
1531 // which outweighs the value of the tail call; but here we don't know yet
1532 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001533 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001534 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001535
1536 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1537 // but we need to make sure there are enough registers; the only valid
1538 // registers are the 4 used for parameters. We don't currently do this
1539 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001540 if (Subtarget->isThumb1Only())
1541 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001542
Dale Johannesen51e28e62010-06-03 21:09:53 +00001543 // If the calling conventions do not match, then we'd better make sure the
1544 // results are returned in the same way as what the caller expects.
1545 if (!CCMatch) {
1546 SmallVector<CCValAssign, 16> RVLocs1;
1547 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1548 RVLocs1, *DAG.getContext());
1549 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1550
1551 SmallVector<CCValAssign, 16> RVLocs2;
1552 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1553 RVLocs2, *DAG.getContext());
1554 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1555
1556 if (RVLocs1.size() != RVLocs2.size())
1557 return false;
1558 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1559 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1560 return false;
1561 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1562 return false;
1563 if (RVLocs1[i].isRegLoc()) {
1564 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1565 return false;
1566 } else {
1567 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1568 return false;
1569 }
1570 }
1571 }
1572
1573 // If the callee takes no arguments then go on to check the results of the
1574 // call.
1575 if (!Outs.empty()) {
1576 // Check if stack adjustment is needed. For now, do not do this if any
1577 // argument is passed on the stack.
1578 SmallVector<CCValAssign, 16> ArgLocs;
1579 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1580 ArgLocs, *DAG.getContext());
1581 CCInfo.AnalyzeCallOperands(Outs,
1582 CCAssignFnForNode(CalleeCC, false, isVarArg));
1583 if (CCInfo.getNextStackOffset()) {
1584 MachineFunction &MF = DAG.getMachineFunction();
1585
1586 // Check if the arguments are already laid out in the right way as
1587 // the caller's fixed stack objects.
1588 MachineFrameInfo *MFI = MF.getFrameInfo();
1589 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1590 const ARMInstrInfo *TII =
1591 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001592 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1593 i != e;
1594 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001595 CCValAssign &VA = ArgLocs[i];
1596 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001597 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001598 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001599 if (VA.getLocInfo() == CCValAssign::Indirect)
1600 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001601 if (VA.needsCustom()) {
1602 // f64 and vector types are split into multiple registers or
1603 // register/stack-slot combinations. The types will not match
1604 // the registers; give up on memory f64 refs until we figure
1605 // out what to do about this.
1606 if (!VA.isRegLoc())
1607 return false;
1608 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001609 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001610 if (RegVT == MVT::v2f64) {
1611 if (!ArgLocs[++i].isRegLoc())
1612 return false;
1613 if (!ArgLocs[++i].isRegLoc())
1614 return false;
1615 }
1616 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001617 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1618 MFI, MRI, TII))
1619 return false;
1620 }
1621 }
1622 }
1623 }
1624
1625 return true;
1626}
1627
Dan Gohman98ca4f22009-08-05 01:29:28 +00001628SDValue
1629ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001630 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001631 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001632 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001633 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001634
Bob Wilsondee46d72009-04-17 20:35:10 +00001635 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001636 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001637
Bob Wilsondee46d72009-04-17 20:35:10 +00001638 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001639 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1640 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001641
Dan Gohman98ca4f22009-08-05 01:29:28 +00001642 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001643 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1644 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001645
1646 // If this is the first return lowered for this function, add
1647 // the regs to the liveout set for the function.
1648 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1649 for (unsigned i = 0; i != RVLocs.size(); ++i)
1650 if (RVLocs[i].isRegLoc())
1651 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001652 }
1653
Bob Wilson1f595bb2009-04-17 19:07:39 +00001654 SDValue Flag;
1655
1656 // Copy the result values into the output registers.
1657 for (unsigned i = 0, realRVLocIdx = 0;
1658 i != RVLocs.size();
1659 ++i, ++realRVLocIdx) {
1660 CCValAssign &VA = RVLocs[i];
1661 assert(VA.isRegLoc() && "Can only return in registers!");
1662
Dan Gohmanc9403652010-07-07 15:54:55 +00001663 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001664
1665 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001666 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001667 case CCValAssign::Full: break;
1668 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001669 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001670 break;
1671 }
1672
Bob Wilson1f595bb2009-04-17 19:07:39 +00001673 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001674 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001675 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001676 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1677 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001678 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001679 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001680
1681 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1682 Flag = Chain.getValue(1);
1683 VA = RVLocs[++i]; // skip ahead to next loc
1684 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1685 HalfGPRs.getValue(1), Flag);
1686 Flag = Chain.getValue(1);
1687 VA = RVLocs[++i]; // skip ahead to next loc
1688
1689 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001690 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1691 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001692 }
1693 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1694 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001695 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001696 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001697 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001698 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001699 VA = RVLocs[++i]; // skip ahead to next loc
1700 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1701 Flag);
1702 } else
1703 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1704
Bob Wilsondee46d72009-04-17 20:35:10 +00001705 // Guarantee that all emitted copies are
1706 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001707 Flag = Chain.getValue(1);
1708 }
1709
1710 SDValue result;
1711 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001713 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001714 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001715
1716 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001717}
1718
Evan Cheng3d2125c2010-11-30 23:55:39 +00001719bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1720 if (N->getNumValues() != 1)
1721 return false;
1722 if (!N->hasNUsesOfValue(1, 0))
1723 return false;
1724
1725 unsigned NumCopies = 0;
1726 SDNode* Copies[2];
1727 SDNode *Use = *N->use_begin();
1728 if (Use->getOpcode() == ISD::CopyToReg) {
1729 Copies[NumCopies++] = Use;
1730 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1731 // f64 returned in a pair of GPRs.
1732 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1733 UI != UE; ++UI) {
1734 if (UI->getOpcode() != ISD::CopyToReg)
1735 return false;
1736 Copies[UI.getUse().getResNo()] = *UI;
1737 ++NumCopies;
1738 }
1739 } else if (Use->getOpcode() == ISD::BITCAST) {
1740 // f32 returned in a single GPR.
1741 if (!Use->hasNUsesOfValue(1, 0))
1742 return false;
1743 Use = *Use->use_begin();
1744 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1745 return false;
1746 Copies[NumCopies++] = Use;
1747 } else {
1748 return false;
1749 }
1750
1751 if (NumCopies != 1 && NumCopies != 2)
1752 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001753
1754 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001755 for (unsigned i = 0; i < NumCopies; ++i) {
1756 SDNode *Copy = Copies[i];
1757 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1758 UI != UE; ++UI) {
1759 if (UI->getOpcode() == ISD::CopyToReg) {
1760 SDNode *Use = *UI;
1761 if (Use == Copies[0] || Use == Copies[1])
1762 continue;
1763 return false;
1764 }
1765 if (UI->getOpcode() != ARMISD::RET_FLAG)
1766 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001767 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001768 }
1769 }
1770
Evan Cheng1bf891a2010-12-01 22:59:46 +00001771 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001772}
1773
Bob Wilsonb62d2572009-11-03 00:02:05 +00001774// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1775// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1776// one of the above mentioned nodes. It has to be wrapped because otherwise
1777// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1778// be used to form addressing mode. These wrapped nodes will be selected
1779// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001780static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001781 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001782 // FIXME there is no actual debug info here
1783 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001784 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001785 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001786 if (CP->isMachineConstantPoolEntry())
1787 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1788 CP->getAlignment());
1789 else
1790 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1791 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001792 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001793}
1794
Jim Grosbache1102ca2010-07-19 17:20:38 +00001795unsigned ARMTargetLowering::getJumpTableEncoding() const {
1796 return MachineJumpTableInfo::EK_Inline;
1797}
1798
Dan Gohmand858e902010-04-17 15:26:15 +00001799SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1800 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001801 MachineFunction &MF = DAG.getMachineFunction();
1802 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1803 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001804 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001805 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001806 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001807 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1808 SDValue CPAddr;
1809 if (RelocM == Reloc::Static) {
1810 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1811 } else {
1812 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001813 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001814 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1815 ARMCP::CPBlockAddress,
1816 PCAdj);
1817 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1818 }
1819 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1820 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001821 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001822 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001823 if (RelocM == Reloc::Static)
1824 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001825 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001826 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001827}
1828
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001829// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001830SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001831ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001832 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001833 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001834 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001835 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001836 MachineFunction &MF = DAG.getMachineFunction();
1837 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001838 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001839 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001840 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001841 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001842 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001843 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001844 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001845 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001846 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001847 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001848
Evan Chenge7e0d622009-11-06 22:24:13 +00001849 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001850 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001851
1852 // call __tls_get_addr.
1853 ArgListTy Args;
1854 ArgListEntry Entry;
1855 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001856 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001857 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001858 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001859 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001860 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1861 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001863 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001864 return CallResult.first;
1865}
1866
1867// Lower ISD::GlobalTLSAddress using the "initial exec" or
1868// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001869SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001870ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001871 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001872 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001873 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001874 SDValue Offset;
1875 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001876 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001877 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001878 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001879
Chris Lattner4fb63d02009-07-15 04:12:33 +00001880 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001881 MachineFunction &MF = DAG.getMachineFunction();
1882 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001883 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001884 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001885 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1886 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001887 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001888 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001889 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001890 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001891 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001892 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001893 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001894 Chain = Offset.getValue(1);
1895
Evan Chenge7e0d622009-11-06 22:24:13 +00001896 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001897 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001898
Evan Cheng9eda6892009-10-31 03:39:36 +00001899 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001900 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001901 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001902 } else {
1903 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001904 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001905 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001907 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001908 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001909 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001910 }
1911
1912 // The address of the thread local variable is the add of the thread
1913 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001914 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001915}
1916
Dan Gohman475871a2008-07-27 21:46:04 +00001917SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001918ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001919 // TODO: implement the "local dynamic" model
1920 assert(Subtarget->isTargetELF() &&
1921 "TLS not implemented for non-ELF targets");
1922 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1923 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1924 // otherwise use the "Local Exec" TLS Model
1925 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1926 return LowerToTLSGeneralDynamicModel(GA, DAG);
1927 else
1928 return LowerToTLSExecModels(GA, DAG);
1929}
1930
Dan Gohman475871a2008-07-27 21:46:04 +00001931SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001932 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001933 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001934 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001935 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001936 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1937 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001938 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001939 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001940 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001941 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001942 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001943 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001944 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001945 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001946 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001947 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001948 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001949 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001950 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001951 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001952 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001953 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001954 }
1955
1956 // If we have T2 ops, we can materialize the address directly via movt/movw
1957 // pair. This is always cheaper.
1958 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00001959 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001960 // FIXME: Once remat is capable of dealing with instructions with register
1961 // operands, expand this into two nodes.
1962 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1963 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001964 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001965 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1966 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1967 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1968 MachinePointerInfo::getConstantPool(),
1969 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001970 }
1971}
1972
Dan Gohman475871a2008-07-27 21:46:04 +00001973SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001974 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001975 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001976 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001977 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001978 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001979 MachineFunction &MF = DAG.getMachineFunction();
1980 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1981
1982 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00001983 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001984 // FIXME: Once remat is capable of dealing with instructions with register
1985 // operands, expand this into two nodes.
1986 if (RelocM != Reloc::PIC_)
1987 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1988 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
1989
1990 // FIXME: Not a constant pool!
1991 unsigned PICLabelIndex = AFI->createPICLabelUId();
1992 SDValue PICLabel = DAG.getConstant(PICLabelIndex, MVT::i32);
1993 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT,
1994 DAG.getTargetGlobalAddress(GV, dl, PtrVT),
1995 PICLabel);
Evan Chengfc8475b2011-01-19 02:16:49 +00001996 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1997 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
1998 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
1999 MachinePointerInfo::getGOT(), false, false, 0);
2000 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002001 }
2002
2003 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002004 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002005 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002006 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002007 } else {
2008 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002009 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2010 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002011 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002012 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002013 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002014 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002015
Evan Cheng9eda6892009-10-31 03:39:36 +00002016 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002017 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002018 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002020
2021 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002022 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002023 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002024 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002025
Evan Cheng63476a82009-09-03 07:04:02 +00002026 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002027 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002028 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002029
2030 return Result;
2031}
2032
Dan Gohman475871a2008-07-27 21:46:04 +00002033SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002034 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002035 assert(Subtarget->isTargetELF() &&
2036 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002037 MachineFunction &MF = DAG.getMachineFunction();
2038 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002039 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002040 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002041 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002042 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002043 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2044 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002045 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002046 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002048 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002049 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002050 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002051 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002052 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002053}
2054
Jim Grosbach0e0da732009-05-12 23:59:14 +00002055SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002056ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2057 const {
2058 DebugLoc dl = Op.getDebugLoc();
2059 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
2060 Op.getOperand(0), Op.getOperand(1));
2061}
2062
2063SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002064ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2065 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002066 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002067 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2068 Op.getOperand(1), Val);
2069}
2070
2071SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002072ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2073 DebugLoc dl = Op.getDebugLoc();
2074 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2075 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2076}
2077
2078SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002079ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002080 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002081 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002082 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002083 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002084 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002085 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002086 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002087 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2088 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002089 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002090 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002091 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002092 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002093 EVT PtrVT = getPointerTy();
2094 DebugLoc dl = Op.getDebugLoc();
2095 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2096 SDValue CPAddr;
2097 unsigned PCAdj = (RelocM != Reloc::PIC_)
2098 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002099 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002100 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2101 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002102 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002104 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002105 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002106 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002107 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002108
2109 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002110 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002111 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2112 }
2113 return Result;
2114 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002115 }
2116}
2117
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002118static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002119 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002120 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002121 if (!Subtarget->hasDataBarrier()) {
2122 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2123 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2124 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002125 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002126 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002127 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002128 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002129 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002130
2131 SDValue Op5 = Op.getOperand(5);
2132 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2133 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2134 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2135 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2136
2137 ARM_MB::MemBOpt DMBOpt;
2138 if (isDeviceBarrier)
2139 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2140 else
2141 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2142 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2143 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002144}
2145
Evan Chengdfed19f2010-11-03 06:34:55 +00002146static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2147 const ARMSubtarget *Subtarget) {
2148 // ARM pre v5TE and Thumb1 does not have preload instructions.
2149 if (!(Subtarget->isThumb2() ||
2150 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2151 // Just preserve the chain.
2152 return Op.getOperand(0);
2153
2154 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002155 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2156 if (!isRead &&
2157 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2158 // ARMv7 with MP extension has PLDW.
2159 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002160
2161 if (Subtarget->isThumb())
2162 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002163 isRead = ~isRead & 1;
2164 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002165
Evan Cheng416941d2010-11-04 05:19:35 +00002166 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002167 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002168 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2169 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002170}
2171
Dan Gohman1e93df62010-04-17 14:41:14 +00002172static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2173 MachineFunction &MF = DAG.getMachineFunction();
2174 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2175
Evan Chenga8e29892007-01-19 07:51:42 +00002176 // vastart just stores the address of the VarArgsFrameIndex slot into the
2177 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002178 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002179 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002180 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002181 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002182 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2183 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002184}
2185
Dan Gohman475871a2008-07-27 21:46:04 +00002186SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002187ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2188 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002189 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002190 MachineFunction &MF = DAG.getMachineFunction();
2191 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2192
2193 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002194 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002195 RC = ARM::tGPRRegisterClass;
2196 else
2197 RC = ARM::GPRRegisterClass;
2198
2199 // Transform the arguments stored in physical registers into virtual ones.
Jim Grosbach4725ca72010-09-08 03:54:02 +00002200 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002201 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002202
2203 SDValue ArgValue2;
2204 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002205 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002206 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002207
2208 // Create load node to retrieve arguments from the stack.
2209 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002210 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002211 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002212 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002213 } else {
2214 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002215 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002216 }
2217
Jim Grosbache5165492009-11-09 00:11:35 +00002218 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002219}
2220
2221SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002222ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002223 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002224 const SmallVectorImpl<ISD::InputArg>
2225 &Ins,
2226 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002227 SmallVectorImpl<SDValue> &InVals)
2228 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002229
Bob Wilson1f595bb2009-04-17 19:07:39 +00002230 MachineFunction &MF = DAG.getMachineFunction();
2231 MachineFrameInfo *MFI = MF.getFrameInfo();
2232
Bob Wilson1f595bb2009-04-17 19:07:39 +00002233 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2234
2235 // Assign locations to all of the incoming arguments.
2236 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002237 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2238 *DAG.getContext());
2239 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002240 CCAssignFnForNode(CallConv, /* Return*/ false,
2241 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002242
2243 SmallVector<SDValue, 16> ArgValues;
2244
2245 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2246 CCValAssign &VA = ArgLocs[i];
2247
Bob Wilsondee46d72009-04-17 20:35:10 +00002248 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002249 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002250 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002251
Bob Wilson5bafff32009-06-22 23:27:02 +00002252 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002253 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002254 // f64 and vector types are split up into multiple registers or
2255 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002256 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002257 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002258 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002259 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002260 SDValue ArgValue2;
2261 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002262 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002263 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2264 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002265 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002266 false, false, 0);
2267 } else {
2268 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2269 Chain, DAG, dl);
2270 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002271 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2272 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002273 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002274 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002275 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2276 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002277 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002278
Bob Wilson5bafff32009-06-22 23:27:02 +00002279 } else {
2280 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002281
Owen Anderson825b72b2009-08-11 20:47:22 +00002282 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002283 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002284 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002285 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002286 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002287 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002288 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002289 RC = (AFI->isThumb1OnlyFunction() ?
2290 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002291 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002292 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002293
2294 // Transform the arguments in physical registers into virtual ones.
2295 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002296 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002297 }
2298
2299 // If this is an 8 or 16-bit value, it is really passed promoted
2300 // to 32 bits. Insert an assert[sz]ext to capture this, then
2301 // truncate to the right size.
2302 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002303 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002304 case CCValAssign::Full: break;
2305 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002306 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002307 break;
2308 case CCValAssign::SExt:
2309 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2310 DAG.getValueType(VA.getValVT()));
2311 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2312 break;
2313 case CCValAssign::ZExt:
2314 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2315 DAG.getValueType(VA.getValVT()));
2316 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2317 break;
2318 }
2319
Dan Gohman98ca4f22009-08-05 01:29:28 +00002320 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002321
2322 } else { // VA.isRegLoc()
2323
2324 // sanity check
2325 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002326 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002327
2328 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002329 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002330
Bob Wilsondee46d72009-04-17 20:35:10 +00002331 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002332 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002333 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002334 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002335 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002336 }
2337 }
2338
2339 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002340 if (isVarArg) {
2341 static const unsigned GPRArgRegs[] = {
2342 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2343 };
2344
Bob Wilsondee46d72009-04-17 20:35:10 +00002345 unsigned NumGPRs = CCInfo.getFirstUnallocated
2346 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002347
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002348 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002349 unsigned VARegSize = (4 - NumGPRs) * 4;
2350 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002351 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002352 if (VARegSaveSize) {
2353 // If this function is vararg, store any remaining integer argument regs
2354 // to their spots on the stack so that they may be loaded by deferencing
2355 // the result of va_next.
2356 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002357 AFI->setVarArgsFrameIndex(
2358 MFI->CreateFixedObject(VARegSaveSize,
2359 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002360 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002361 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2362 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002363
Dan Gohman475871a2008-07-27 21:46:04 +00002364 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002365 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002366 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002367 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002368 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002369 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002370 RC = ARM::GPRRegisterClass;
2371
Bob Wilson998e1252009-04-20 18:36:57 +00002372 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002373 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002374 SDValue Store =
2375 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002376 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2377 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002378 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002379 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002380 DAG.getConstant(4, getPointerTy()));
2381 }
2382 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002383 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002384 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002385 } else
2386 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002387 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002388 }
2389
Dan Gohman98ca4f22009-08-05 01:29:28 +00002390 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002391}
2392
2393/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002394static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002395 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002396 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002397 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002398 // Maybe this has already been legalized into the constant pool?
2399 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002400 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002401 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002402 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002403 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002404 }
2405 }
2406 return false;
2407}
2408
Evan Chenga8e29892007-01-19 07:51:42 +00002409/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2410/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002411SDValue
2412ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002413 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002414 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002415 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002416 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002417 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002418 // Constant does not fit, try adjusting it by one?
2419 switch (CC) {
2420 default: break;
2421 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002422 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002423 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002424 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002425 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002426 }
2427 break;
2428 case ISD::SETULT:
2429 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002430 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002431 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002432 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002433 }
2434 break;
2435 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002436 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002437 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002438 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002439 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002440 }
2441 break;
2442 case ISD::SETULE:
2443 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002444 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002445 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002446 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002447 }
2448 break;
2449 }
2450 }
2451 }
2452
2453 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002454 ARMISD::NodeType CompareType;
2455 switch (CondCode) {
2456 default:
2457 CompareType = ARMISD::CMP;
2458 break;
2459 case ARMCC::EQ:
2460 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002461 // Uses only Z Flag
2462 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002463 break;
2464 }
Evan Cheng218977b2010-07-13 19:27:42 +00002465 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002466 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002467}
2468
2469/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002470SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002471ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002472 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002473 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002474 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002475 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002476 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002477 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2478 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002479}
2480
Bill Wendlingde2b1512010-08-11 08:43:16 +00002481SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2482 SDValue Cond = Op.getOperand(0);
2483 SDValue SelectTrue = Op.getOperand(1);
2484 SDValue SelectFalse = Op.getOperand(2);
2485 DebugLoc dl = Op.getDebugLoc();
2486
2487 // Convert:
2488 //
2489 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2490 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2491 //
2492 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2493 const ConstantSDNode *CMOVTrue =
2494 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2495 const ConstantSDNode *CMOVFalse =
2496 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2497
2498 if (CMOVTrue && CMOVFalse) {
2499 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2500 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2501
2502 SDValue True;
2503 SDValue False;
2504 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2505 True = SelectTrue;
2506 False = SelectFalse;
2507 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2508 True = SelectFalse;
2509 False = SelectTrue;
2510 }
2511
2512 if (True.getNode() && False.getNode()) {
2513 EVT VT = Cond.getValueType();
2514 SDValue ARMcc = Cond.getOperand(2);
2515 SDValue CCR = Cond.getOperand(3);
2516 SDValue Cmp = Cond.getOperand(4);
2517 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2518 }
2519 }
2520 }
2521
2522 return DAG.getSelectCC(dl, Cond,
2523 DAG.getConstant(0, Cond.getValueType()),
2524 SelectTrue, SelectFalse, ISD::SETNE);
2525}
2526
Dan Gohmand858e902010-04-17 15:26:15 +00002527SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002528 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002529 SDValue LHS = Op.getOperand(0);
2530 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002531 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002532 SDValue TrueVal = Op.getOperand(2);
2533 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002534 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002535
Owen Anderson825b72b2009-08-11 20:47:22 +00002536 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002537 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002539 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2540 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002541 }
2542
2543 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002544 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002545
Evan Cheng218977b2010-07-13 19:27:42 +00002546 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2547 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002548 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002549 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002550 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002551 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002552 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002553 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002554 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002555 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002556 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002557 }
2558 return Result;
2559}
2560
Evan Cheng218977b2010-07-13 19:27:42 +00002561/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2562/// to morph to an integer compare sequence.
2563static bool canChangeToInt(SDValue Op, bool &SeenZero,
2564 const ARMSubtarget *Subtarget) {
2565 SDNode *N = Op.getNode();
2566 if (!N->hasOneUse())
2567 // Otherwise it requires moving the value from fp to integer registers.
2568 return false;
2569 if (!N->getNumValues())
2570 return false;
2571 EVT VT = Op.getValueType();
2572 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2573 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2574 // vmrs are very slow, e.g. cortex-a8.
2575 return false;
2576
2577 if (isFloatingPointZero(Op)) {
2578 SeenZero = true;
2579 return true;
2580 }
2581 return ISD::isNormalLoad(N);
2582}
2583
2584static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2585 if (isFloatingPointZero(Op))
2586 return DAG.getConstant(0, MVT::i32);
2587
2588 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2589 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002590 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002591 Ld->isVolatile(), Ld->isNonTemporal(),
2592 Ld->getAlignment());
2593
2594 llvm_unreachable("Unknown VFP cmp argument!");
2595}
2596
2597static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2598 SDValue &RetVal1, SDValue &RetVal2) {
2599 if (isFloatingPointZero(Op)) {
2600 RetVal1 = DAG.getConstant(0, MVT::i32);
2601 RetVal2 = DAG.getConstant(0, MVT::i32);
2602 return;
2603 }
2604
2605 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2606 SDValue Ptr = Ld->getBasePtr();
2607 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2608 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002609 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002610 Ld->isVolatile(), Ld->isNonTemporal(),
2611 Ld->getAlignment());
2612
2613 EVT PtrType = Ptr.getValueType();
2614 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2615 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2616 PtrType, Ptr, DAG.getConstant(4, PtrType));
2617 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2618 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002619 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002620 Ld->isVolatile(), Ld->isNonTemporal(),
2621 NewAlign);
2622 return;
2623 }
2624
2625 llvm_unreachable("Unknown VFP cmp argument!");
2626}
2627
2628/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2629/// f32 and even f64 comparisons to integer ones.
2630SDValue
2631ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2632 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002633 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002634 SDValue LHS = Op.getOperand(2);
2635 SDValue RHS = Op.getOperand(3);
2636 SDValue Dest = Op.getOperand(4);
2637 DebugLoc dl = Op.getDebugLoc();
2638
2639 bool SeenZero = false;
2640 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2641 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002642 // If one of the operand is zero, it's safe to ignore the NaN case since
2643 // we only care about equality comparisons.
2644 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002645 // If unsafe fp math optimization is enabled and there are no othter uses of
2646 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2647 // to an integer comparison.
2648 if (CC == ISD::SETOEQ)
2649 CC = ISD::SETEQ;
2650 else if (CC == ISD::SETUNE)
2651 CC = ISD::SETNE;
2652
2653 SDValue ARMcc;
2654 if (LHS.getValueType() == MVT::f32) {
2655 LHS = bitcastf32Toi32(LHS, DAG);
2656 RHS = bitcastf32Toi32(RHS, DAG);
2657 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2658 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2659 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2660 Chain, Dest, ARMcc, CCR, Cmp);
2661 }
2662
2663 SDValue LHS1, LHS2;
2664 SDValue RHS1, RHS2;
2665 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2666 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2667 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2668 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002669 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002670 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2671 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2672 }
2673
2674 return SDValue();
2675}
2676
2677SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2678 SDValue Chain = Op.getOperand(0);
2679 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2680 SDValue LHS = Op.getOperand(2);
2681 SDValue RHS = Op.getOperand(3);
2682 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002683 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002684
Owen Anderson825b72b2009-08-11 20:47:22 +00002685 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002686 SDValue ARMcc;
2687 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002688 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002689 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002690 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002691 }
2692
Owen Anderson825b72b2009-08-11 20:47:22 +00002693 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002694
2695 if (UnsafeFPMath &&
2696 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2697 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2698 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2699 if (Result.getNode())
2700 return Result;
2701 }
2702
Evan Chenga8e29892007-01-19 07:51:42 +00002703 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002704 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002705
Evan Cheng218977b2010-07-13 19:27:42 +00002706 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2707 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002708 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002709 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002710 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002711 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002712 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002713 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2714 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002715 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002716 }
2717 return Res;
2718}
2719
Dan Gohmand858e902010-04-17 15:26:15 +00002720SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002721 SDValue Chain = Op.getOperand(0);
2722 SDValue Table = Op.getOperand(1);
2723 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002724 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002725
Owen Andersone50ed302009-08-10 22:56:29 +00002726 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002727 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2728 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002729 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002730 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002731 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002732 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2733 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002734 if (Subtarget->isThumb2()) {
2735 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2736 // which does another jump to the destination. This also makes it easier
2737 // to translate it to TBB / TBH later.
2738 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002739 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002740 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002741 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002742 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002743 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002744 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002745 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002746 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002747 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002748 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002749 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002750 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002751 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002752 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002753 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002754 }
Evan Chenga8e29892007-01-19 07:51:42 +00002755}
2756
Bob Wilson76a312b2010-03-19 22:51:32 +00002757static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2758 DebugLoc dl = Op.getDebugLoc();
2759 unsigned Opc;
2760
2761 switch (Op.getOpcode()) {
2762 default:
2763 assert(0 && "Invalid opcode!");
2764 case ISD::FP_TO_SINT:
2765 Opc = ARMISD::FTOSI;
2766 break;
2767 case ISD::FP_TO_UINT:
2768 Opc = ARMISD::FTOUI;
2769 break;
2770 }
2771 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002772 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002773}
2774
2775static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2776 EVT VT = Op.getValueType();
2777 DebugLoc dl = Op.getDebugLoc();
2778 unsigned Opc;
2779
2780 switch (Op.getOpcode()) {
2781 default:
2782 assert(0 && "Invalid opcode!");
2783 case ISD::SINT_TO_FP:
2784 Opc = ARMISD::SITOF;
2785 break;
2786 case ISD::UINT_TO_FP:
2787 Opc = ARMISD::UITOF;
2788 break;
2789 }
2790
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002791 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002792 return DAG.getNode(Opc, dl, VT, Op);
2793}
2794
Evan Cheng515fe3a2010-07-08 02:08:50 +00002795SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002796 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002797 SDValue Tmp0 = Op.getOperand(0);
2798 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002799 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002800 EVT VT = Op.getValueType();
2801 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002802 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002803 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002804 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002805 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002806 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002807 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002808}
2809
Evan Cheng2457f2c2010-05-22 01:47:14 +00002810SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2811 MachineFunction &MF = DAG.getMachineFunction();
2812 MachineFrameInfo *MFI = MF.getFrameInfo();
2813 MFI->setReturnAddressIsTaken(true);
2814
2815 EVT VT = Op.getValueType();
2816 DebugLoc dl = Op.getDebugLoc();
2817 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2818 if (Depth) {
2819 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2820 SDValue Offset = DAG.getConstant(4, MVT::i32);
2821 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2822 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002823 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002824 }
2825
2826 // Return LR, which contains the return address. Mark it an implicit live-in.
Jim Grosbachc2723a52010-07-23 23:50:35 +00002827 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00002828 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2829}
2830
Dan Gohmand858e902010-04-17 15:26:15 +00002831SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002832 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2833 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002834
Owen Andersone50ed302009-08-10 22:56:29 +00002835 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002836 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2837 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002838 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002839 ? ARM::R7 : ARM::R11;
2840 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2841 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002842 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
2843 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00002844 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002845 return FrameAddr;
2846}
2847
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002848/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00002849/// expand a bit convert where either the source or destination type is i64 to
2850/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2851/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2852/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002853static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002854 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2855 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002856 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002857
Bob Wilson9f3f0612010-04-17 05:30:19 +00002858 // This function is only supposed to be called for i64 types, either as the
2859 // source or destination of the bit convert.
2860 EVT SrcVT = Op.getValueType();
2861 EVT DstVT = N->getValueType(0);
2862 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002863 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002864
Bob Wilson9f3f0612010-04-17 05:30:19 +00002865 // Turn i64->f64 into VMOVDRR.
2866 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002867 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2868 DAG.getConstant(0, MVT::i32));
2869 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2870 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002871 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00002872 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002873 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002874
Jim Grosbache5165492009-11-09 00:11:35 +00002875 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002876 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2877 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2878 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2879 // Merge the pieces into a single i64 value.
2880 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2881 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002882
Bob Wilson9f3f0612010-04-17 05:30:19 +00002883 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002884}
2885
Bob Wilson5bafff32009-06-22 23:27:02 +00002886/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002887/// Zero vectors are used to represent vector negation and in those cases
2888/// will be implemented with the NEON VNEG instruction. However, VNEG does
2889/// not support i64 elements, so sometimes the zero vectors will need to be
2890/// explicitly constructed. Regardless, use a canonical VMOV to create the
2891/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002892static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002893 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002894 // The canonical modified immediate encoding of a zero vector is....0!
2895 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2896 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2897 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002898 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002899}
2900
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002901/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2902/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002903SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2904 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002905 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2906 EVT VT = Op.getValueType();
2907 unsigned VTBits = VT.getSizeInBits();
2908 DebugLoc dl = Op.getDebugLoc();
2909 SDValue ShOpLo = Op.getOperand(0);
2910 SDValue ShOpHi = Op.getOperand(1);
2911 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002912 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002913 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002914
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002915 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2916
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002917 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2918 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2919 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2920 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2921 DAG.getConstant(VTBits, MVT::i32));
2922 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2923 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002924 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002925
2926 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2927 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002928 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002929 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002930 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002931 CCR, Cmp);
2932
2933 SDValue Ops[2] = { Lo, Hi };
2934 return DAG.getMergeValues(Ops, 2, dl);
2935}
2936
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002937/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2938/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002939SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2940 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002941 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2942 EVT VT = Op.getValueType();
2943 unsigned VTBits = VT.getSizeInBits();
2944 DebugLoc dl = Op.getDebugLoc();
2945 SDValue ShOpLo = Op.getOperand(0);
2946 SDValue ShOpHi = Op.getOperand(1);
2947 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002948 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002949
2950 assert(Op.getOpcode() == ISD::SHL_PARTS);
2951 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2952 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2953 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2954 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2955 DAG.getConstant(VTBits, MVT::i32));
2956 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2957 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2958
2959 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2960 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2961 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002962 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002963 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002964 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002965 CCR, Cmp);
2966
2967 SDValue Ops[2] = { Lo, Hi };
2968 return DAG.getMergeValues(Ops, 2, dl);
2969}
2970
Jim Grosbach4725ca72010-09-08 03:54:02 +00002971SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00002972 SelectionDAG &DAG) const {
2973 // The rounding mode is in bits 23:22 of the FPSCR.
2974 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
2975 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
2976 // so that the shift + and get folded into a bitfield extract.
2977 DebugLoc dl = Op.getDebugLoc();
2978 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
2979 DAG.getConstant(Intrinsic::arm_get_fpscr,
2980 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00002981 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00002982 DAG.getConstant(1U << 22, MVT::i32));
2983 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
2984 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00002985 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00002986 DAG.getConstant(3, MVT::i32));
2987}
2988
Jim Grosbach3482c802010-01-18 19:58:49 +00002989static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2990 const ARMSubtarget *ST) {
2991 EVT VT = N->getValueType(0);
2992 DebugLoc dl = N->getDebugLoc();
2993
2994 if (!ST->hasV6T2Ops())
2995 return SDValue();
2996
2997 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2998 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2999}
3000
Bob Wilson5bafff32009-06-22 23:27:02 +00003001static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3002 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003003 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003004 DebugLoc dl = N->getDebugLoc();
3005
Bob Wilsond5448bb2010-11-18 21:16:28 +00003006 if (!VT.isVector())
3007 return SDValue();
3008
Bob Wilson5bafff32009-06-22 23:27:02 +00003009 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003010 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003011
Bob Wilsond5448bb2010-11-18 21:16:28 +00003012 // Left shifts translate directly to the vshiftu intrinsic.
3013 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003014 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003015 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3016 N->getOperand(0), N->getOperand(1));
3017
3018 assert((N->getOpcode() == ISD::SRA ||
3019 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3020
3021 // NEON uses the same intrinsics for both left and right shifts. For
3022 // right shifts, the shift amounts are negative, so negate the vector of
3023 // shift amounts.
3024 EVT ShiftVT = N->getOperand(1).getValueType();
3025 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3026 getZeroVector(ShiftVT, DAG, dl),
3027 N->getOperand(1));
3028 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3029 Intrinsic::arm_neon_vshifts :
3030 Intrinsic::arm_neon_vshiftu);
3031 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3032 DAG.getConstant(vshiftInt, MVT::i32),
3033 N->getOperand(0), NegatedCount);
3034}
3035
3036static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3037 const ARMSubtarget *ST) {
3038 EVT VT = N->getValueType(0);
3039 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003040
Eli Friedmance392eb2009-08-22 03:13:10 +00003041 // We can get here for a node like i32 = ISD::SHL i32, i64
3042 if (VT != MVT::i64)
3043 return SDValue();
3044
3045 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003046 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003047
Chris Lattner27a6c732007-11-24 07:07:01 +00003048 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3049 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003050 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003051 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003052
Chris Lattner27a6c732007-11-24 07:07:01 +00003053 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003054 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003055
Chris Lattner27a6c732007-11-24 07:07:01 +00003056 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003057 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003058 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003059 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003060 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003061
Chris Lattner27a6c732007-11-24 07:07:01 +00003062 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3063 // captures the result into a carry flag.
3064 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003065 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003066
Chris Lattner27a6c732007-11-24 07:07:01 +00003067 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003068 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003069
Chris Lattner27a6c732007-11-24 07:07:01 +00003070 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003071 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003072}
3073
Bob Wilson5bafff32009-06-22 23:27:02 +00003074static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3075 SDValue TmpOp0, TmpOp1;
3076 bool Invert = false;
3077 bool Swap = false;
3078 unsigned Opc = 0;
3079
3080 SDValue Op0 = Op.getOperand(0);
3081 SDValue Op1 = Op.getOperand(1);
3082 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003083 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003084 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3085 DebugLoc dl = Op.getDebugLoc();
3086
3087 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3088 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003089 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003090 case ISD::SETUNE:
3091 case ISD::SETNE: Invert = true; // Fallthrough
3092 case ISD::SETOEQ:
3093 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3094 case ISD::SETOLT:
3095 case ISD::SETLT: Swap = true; // Fallthrough
3096 case ISD::SETOGT:
3097 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3098 case ISD::SETOLE:
3099 case ISD::SETLE: Swap = true; // Fallthrough
3100 case ISD::SETOGE:
3101 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3102 case ISD::SETUGE: Swap = true; // Fallthrough
3103 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3104 case ISD::SETUGT: Swap = true; // Fallthrough
3105 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3106 case ISD::SETUEQ: Invert = true; // Fallthrough
3107 case ISD::SETONE:
3108 // Expand this to (OLT | OGT).
3109 TmpOp0 = Op0;
3110 TmpOp1 = Op1;
3111 Opc = ISD::OR;
3112 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3113 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3114 break;
3115 case ISD::SETUO: Invert = true; // Fallthrough
3116 case ISD::SETO:
3117 // Expand this to (OLT | OGE).
3118 TmpOp0 = Op0;
3119 TmpOp1 = Op1;
3120 Opc = ISD::OR;
3121 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3122 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3123 break;
3124 }
3125 } else {
3126 // Integer comparisons.
3127 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003128 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003129 case ISD::SETNE: Invert = true;
3130 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3131 case ISD::SETLT: Swap = true;
3132 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3133 case ISD::SETLE: Swap = true;
3134 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3135 case ISD::SETULT: Swap = true;
3136 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3137 case ISD::SETULE: Swap = true;
3138 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3139 }
3140
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003141 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003142 if (Opc == ARMISD::VCEQ) {
3143
3144 SDValue AndOp;
3145 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3146 AndOp = Op0;
3147 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3148 AndOp = Op1;
3149
3150 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003151 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003152 AndOp = AndOp.getOperand(0);
3153
3154 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3155 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003156 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3157 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003158 Invert = !Invert;
3159 }
3160 }
3161 }
3162
3163 if (Swap)
3164 std::swap(Op0, Op1);
3165
Owen Andersonc24cb352010-11-08 23:21:22 +00003166 // If one of the operands is a constant vector zero, attempt to fold the
3167 // comparison to a specialized compare-against-zero form.
3168 SDValue SingleOp;
3169 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3170 SingleOp = Op0;
3171 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3172 if (Opc == ARMISD::VCGE)
3173 Opc = ARMISD::VCLEZ;
3174 else if (Opc == ARMISD::VCGT)
3175 Opc = ARMISD::VCLTZ;
3176 SingleOp = Op1;
3177 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003178
Owen Andersonc24cb352010-11-08 23:21:22 +00003179 SDValue Result;
3180 if (SingleOp.getNode()) {
3181 switch (Opc) {
3182 case ARMISD::VCEQ:
3183 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3184 case ARMISD::VCGE:
3185 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3186 case ARMISD::VCLEZ:
3187 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3188 case ARMISD::VCGT:
3189 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3190 case ARMISD::VCLTZ:
3191 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3192 default:
3193 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3194 }
3195 } else {
3196 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3197 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003198
3199 if (Invert)
3200 Result = DAG.getNOT(dl, Result, VT);
3201
3202 return Result;
3203}
3204
Bob Wilsond3c42842010-06-14 22:19:57 +00003205/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3206/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003207/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003208static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3209 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003210 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003211 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003212
Bob Wilson827b2102010-06-15 19:05:35 +00003213 // SplatBitSize is set to the smallest size that splats the vector, so a
3214 // zero vector will always have SplatBitSize == 8. However, NEON modified
3215 // immediate instructions others than VMOV do not support the 8-bit encoding
3216 // of a zero vector, and the default encoding of zero is supposed to be the
3217 // 32-bit version.
3218 if (SplatBits == 0)
3219 SplatBitSize = 32;
3220
Bob Wilson5bafff32009-06-22 23:27:02 +00003221 switch (SplatBitSize) {
3222 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003223 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003224 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003225 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003226 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003227 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003228 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003229 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003230 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003231
3232 case 16:
3233 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003234 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003235 if ((SplatBits & ~0xff) == 0) {
3236 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003237 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003238 Imm = SplatBits;
3239 break;
3240 }
3241 if ((SplatBits & ~0xff00) == 0) {
3242 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003243 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003244 Imm = SplatBits >> 8;
3245 break;
3246 }
3247 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003248
3249 case 32:
3250 // NEON's 32-bit VMOV supports splat values where:
3251 // * only one byte is nonzero, or
3252 // * the least significant byte is 0xff and the second byte is nonzero, or
3253 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003254 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003255 if ((SplatBits & ~0xff) == 0) {
3256 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003257 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003258 Imm = SplatBits;
3259 break;
3260 }
3261 if ((SplatBits & ~0xff00) == 0) {
3262 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003263 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003264 Imm = SplatBits >> 8;
3265 break;
3266 }
3267 if ((SplatBits & ~0xff0000) == 0) {
3268 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003269 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003270 Imm = SplatBits >> 16;
3271 break;
3272 }
3273 if ((SplatBits & ~0xff000000) == 0) {
3274 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003275 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003276 Imm = SplatBits >> 24;
3277 break;
3278 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003279
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003280 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3281 if (type == OtherModImm) return SDValue();
3282
Bob Wilson5bafff32009-06-22 23:27:02 +00003283 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003284 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3285 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003286 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003287 Imm = SplatBits >> 8;
3288 SplatBits |= 0xff;
3289 break;
3290 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003291
3292 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003293 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3294 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003295 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003296 Imm = SplatBits >> 16;
3297 SplatBits |= 0xffff;
3298 break;
3299 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003300
3301 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3302 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3303 // VMOV.I32. A (very) minor optimization would be to replicate the value
3304 // and fall through here to test for a valid 64-bit splat. But, then the
3305 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003306 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003307
3308 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003309 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003310 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003311 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003312 uint64_t BitMask = 0xff;
3313 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003314 unsigned ImmMask = 1;
3315 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003316 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003317 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003318 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003319 Imm |= ImmMask;
3320 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003321 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003322 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003323 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003324 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003325 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003326 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003327 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003328 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003329 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003330 break;
3331 }
3332
Bob Wilson1a913ed2010-06-11 21:34:50 +00003333 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003334 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003335 return SDValue();
3336 }
3337
Bob Wilsoncba270d2010-07-13 21:16:48 +00003338 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3339 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003340}
3341
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003342static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3343 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003344 unsigned NumElts = VT.getVectorNumElements();
3345 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003346
3347 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3348 if (M[0] < 0)
3349 return false;
3350
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003351 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003352
3353 // If this is a VEXT shuffle, the immediate value is the index of the first
3354 // element. The other shuffle indices must be the successive elements after
3355 // the first one.
3356 unsigned ExpectedElt = Imm;
3357 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003358 // Increment the expected index. If it wraps around, it may still be
3359 // a VEXT but the source vectors must be swapped.
3360 ExpectedElt += 1;
3361 if (ExpectedElt == NumElts * 2) {
3362 ExpectedElt = 0;
3363 ReverseVEXT = true;
3364 }
3365
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003366 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003367 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003368 return false;
3369 }
3370
3371 // Adjust the index value if the source operands will be swapped.
3372 if (ReverseVEXT)
3373 Imm -= NumElts;
3374
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003375 return true;
3376}
3377
Bob Wilson8bb9e482009-07-26 00:39:34 +00003378/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3379/// instruction with the specified blocksize. (The order of the elements
3380/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003381static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3382 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003383 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3384 "Only possible block sizes for VREV are: 16, 32, 64");
3385
Bob Wilson8bb9e482009-07-26 00:39:34 +00003386 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003387 if (EltSz == 64)
3388 return false;
3389
3390 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003391 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003392 // If the first shuffle index is UNDEF, be optimistic.
3393 if (M[0] < 0)
3394 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003395
3396 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3397 return false;
3398
3399 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003400 if (M[i] < 0) continue; // ignore UNDEF indices
3401 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003402 return false;
3403 }
3404
3405 return true;
3406}
3407
Bob Wilsonc692cb72009-08-21 20:54:19 +00003408static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3409 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003410 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3411 if (EltSz == 64)
3412 return false;
3413
Bob Wilsonc692cb72009-08-21 20:54:19 +00003414 unsigned NumElts = VT.getVectorNumElements();
3415 WhichResult = (M[0] == 0 ? 0 : 1);
3416 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003417 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3418 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003419 return false;
3420 }
3421 return true;
3422}
3423
Bob Wilson324f4f12009-12-03 06:40:55 +00003424/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3425/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3426/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3427static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3428 unsigned &WhichResult) {
3429 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3430 if (EltSz == 64)
3431 return false;
3432
3433 unsigned NumElts = VT.getVectorNumElements();
3434 WhichResult = (M[0] == 0 ? 0 : 1);
3435 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003436 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3437 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003438 return false;
3439 }
3440 return true;
3441}
3442
Bob Wilsonc692cb72009-08-21 20:54:19 +00003443static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3444 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003445 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3446 if (EltSz == 64)
3447 return false;
3448
Bob Wilsonc692cb72009-08-21 20:54:19 +00003449 unsigned NumElts = VT.getVectorNumElements();
3450 WhichResult = (M[0] == 0 ? 0 : 1);
3451 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003452 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003453 if ((unsigned) M[i] != 2 * i + WhichResult)
3454 return false;
3455 }
3456
3457 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003458 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003459 return false;
3460
3461 return true;
3462}
3463
Bob Wilson324f4f12009-12-03 06:40:55 +00003464/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3465/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3466/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3467static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3468 unsigned &WhichResult) {
3469 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3470 if (EltSz == 64)
3471 return false;
3472
3473 unsigned Half = VT.getVectorNumElements() / 2;
3474 WhichResult = (M[0] == 0 ? 0 : 1);
3475 for (unsigned j = 0; j != 2; ++j) {
3476 unsigned Idx = WhichResult;
3477 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003478 int MIdx = M[i + j * Half];
3479 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003480 return false;
3481 Idx += 2;
3482 }
3483 }
3484
3485 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3486 if (VT.is64BitVector() && EltSz == 32)
3487 return false;
3488
3489 return true;
3490}
3491
Bob Wilsonc692cb72009-08-21 20:54:19 +00003492static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3493 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003494 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3495 if (EltSz == 64)
3496 return false;
3497
Bob Wilsonc692cb72009-08-21 20:54:19 +00003498 unsigned NumElts = VT.getVectorNumElements();
3499 WhichResult = (M[0] == 0 ? 0 : 1);
3500 unsigned Idx = WhichResult * NumElts / 2;
3501 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003502 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3503 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003504 return false;
3505 Idx += 1;
3506 }
3507
3508 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003509 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003510 return false;
3511
3512 return true;
3513}
3514
Bob Wilson324f4f12009-12-03 06:40:55 +00003515/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3516/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3517/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3518static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3519 unsigned &WhichResult) {
3520 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3521 if (EltSz == 64)
3522 return false;
3523
3524 unsigned NumElts = VT.getVectorNumElements();
3525 WhichResult = (M[0] == 0 ? 0 : 1);
3526 unsigned Idx = WhichResult * NumElts / 2;
3527 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003528 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3529 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003530 return false;
3531 Idx += 1;
3532 }
3533
3534 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3535 if (VT.is64BitVector() && EltSz == 32)
3536 return false;
3537
3538 return true;
3539}
3540
Dale Johannesenf630c712010-07-29 20:10:08 +00003541// If N is an integer constant that can be moved into a register in one
3542// instruction, return an SDValue of such a constant (will become a MOV
3543// instruction). Otherwise return null.
3544static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3545 const ARMSubtarget *ST, DebugLoc dl) {
3546 uint64_t Val;
3547 if (!isa<ConstantSDNode>(N))
3548 return SDValue();
3549 Val = cast<ConstantSDNode>(N)->getZExtValue();
3550
3551 if (ST->isThumb1Only()) {
3552 if (Val <= 255 || ~Val <= 255)
3553 return DAG.getConstant(Val, MVT::i32);
3554 } else {
3555 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3556 return DAG.getConstant(Val, MVT::i32);
3557 }
3558 return SDValue();
3559}
3560
Bob Wilson5bafff32009-06-22 23:27:02 +00003561// If this is a case we can't handle, return null and let the default
3562// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003563SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3564 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003565 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003566 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003567 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003568
3569 APInt SplatBits, SplatUndef;
3570 unsigned SplatBitSize;
3571 bool HasAnyUndefs;
3572 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003573 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003574 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003575 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003576 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003577 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003578 DAG, VmovVT, VT.is128BitVector(),
3579 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003580 if (Val.getNode()) {
3581 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003582 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003583 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003584
3585 // Try an immediate VMVN.
3586 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3587 ((1LL << SplatBitSize) - 1));
3588 Val = isNEONModifiedImm(NegatedImm,
3589 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003590 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003591 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003592 if (Val.getNode()) {
3593 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003594 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003595 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003596 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003597 }
3598
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003599 // Scan through the operands to see if only one value is used.
3600 unsigned NumElts = VT.getVectorNumElements();
3601 bool isOnlyLowElement = true;
3602 bool usesOnlyOneValue = true;
3603 bool isConstant = true;
3604 SDValue Value;
3605 for (unsigned i = 0; i < NumElts; ++i) {
3606 SDValue V = Op.getOperand(i);
3607 if (V.getOpcode() == ISD::UNDEF)
3608 continue;
3609 if (i > 0)
3610 isOnlyLowElement = false;
3611 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3612 isConstant = false;
3613
3614 if (!Value.getNode())
3615 Value = V;
3616 else if (V != Value)
3617 usesOnlyOneValue = false;
3618 }
3619
3620 if (!Value.getNode())
3621 return DAG.getUNDEF(VT);
3622
3623 if (isOnlyLowElement)
3624 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3625
Dale Johannesenf630c712010-07-29 20:10:08 +00003626 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3627
Dale Johannesen575cd142010-10-19 20:00:17 +00003628 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3629 // i32 and try again.
3630 if (usesOnlyOneValue && EltSize <= 32) {
3631 if (!isConstant)
3632 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3633 if (VT.getVectorElementType().isFloatingPoint()) {
3634 SmallVector<SDValue, 8> Ops;
3635 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003636 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003637 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003638 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3639 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003640 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3641 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003642 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003643 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003644 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3645 if (Val.getNode())
3646 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003647 }
3648
3649 // If all elements are constants and the case above didn't get hit, fall back
3650 // to the default expansion, which will generate a load from the constant
3651 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003652 if (isConstant)
3653 return SDValue();
3654
Bob Wilson11a1dff2011-01-07 21:37:30 +00003655 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3656 if (NumElts >= 4) {
3657 SDValue shuffle = ReconstructShuffle(Op, DAG);
3658 if (shuffle != SDValue())
3659 return shuffle;
3660 }
3661
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003662 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003663 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3664 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003665 if (EltSize >= 32) {
3666 // Do the expansion with floating-point types, since that is what the VFP
3667 // registers are defined to use, and since i64 is not legal.
3668 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3669 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003670 SmallVector<SDValue, 8> Ops;
3671 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003672 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003673 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003674 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003675 }
3676
3677 return SDValue();
3678}
3679
Bob Wilson11a1dff2011-01-07 21:37:30 +00003680// Gather data to see if the operation can be modelled as a
3681// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003682SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3683 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003684 DebugLoc dl = Op.getDebugLoc();
3685 EVT VT = Op.getValueType();
3686 unsigned NumElts = VT.getVectorNumElements();
3687
3688 SmallVector<SDValue, 2> SourceVecs;
3689 SmallVector<unsigned, 2> MinElts;
3690 SmallVector<unsigned, 2> MaxElts;
3691
3692 for (unsigned i = 0; i < NumElts; ++i) {
3693 SDValue V = Op.getOperand(i);
3694 if (V.getOpcode() == ISD::UNDEF)
3695 continue;
3696 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3697 // A shuffle can only come from building a vector from various
3698 // elements of other vectors.
3699 return SDValue();
3700 }
3701
3702 // Record this extraction against the appropriate vector if possible...
3703 SDValue SourceVec = V.getOperand(0);
3704 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3705 bool FoundSource = false;
3706 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3707 if (SourceVecs[j] == SourceVec) {
3708 if (MinElts[j] > EltNo)
3709 MinElts[j] = EltNo;
3710 if (MaxElts[j] < EltNo)
3711 MaxElts[j] = EltNo;
3712 FoundSource = true;
3713 break;
3714 }
3715 }
3716
3717 // Or record a new source if not...
3718 if (!FoundSource) {
3719 SourceVecs.push_back(SourceVec);
3720 MinElts.push_back(EltNo);
3721 MaxElts.push_back(EltNo);
3722 }
3723 }
3724
3725 // Currently only do something sane when at most two source vectors
3726 // involved.
3727 if (SourceVecs.size() > 2)
3728 return SDValue();
3729
3730 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3731 int VEXTOffsets[2] = {0, 0};
3732
3733 // This loop extracts the usage patterns of the source vectors
3734 // and prepares appropriate SDValues for a shuffle if possible.
3735 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3736 if (SourceVecs[i].getValueType() == VT) {
3737 // No VEXT necessary
3738 ShuffleSrcs[i] = SourceVecs[i];
3739 VEXTOffsets[i] = 0;
3740 continue;
3741 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3742 // It probably isn't worth padding out a smaller vector just to
3743 // break it down again in a shuffle.
3744 return SDValue();
3745 }
Bob Wilson11a1dff2011-01-07 21:37:30 +00003746
3747 // Since only 64-bit and 128-bit vectors are legal on ARM and
3748 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003749 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3750 "unexpected vector sizes in ReconstructShuffle");
Bob Wilson11a1dff2011-01-07 21:37:30 +00003751
3752 if (MaxElts[i] - MinElts[i] >= NumElts) {
3753 // Span too large for a VEXT to cope
3754 return SDValue();
3755 }
3756
3757 if (MinElts[i] >= NumElts) {
3758 // The extraction can just take the second half
3759 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003760 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3761 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003762 DAG.getIntPtrConstant(NumElts));
3763 } else if (MaxElts[i] < NumElts) {
3764 // The extraction can just take the first half
3765 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003766 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3767 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003768 DAG.getIntPtrConstant(0));
3769 } else {
3770 // An actual VEXT is needed
3771 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003772 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3773 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003774 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003775 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3776 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003777 DAG.getIntPtrConstant(NumElts));
3778 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3779 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3780 }
3781 }
3782
3783 SmallVector<int, 8> Mask;
3784
3785 for (unsigned i = 0; i < NumElts; ++i) {
3786 SDValue Entry = Op.getOperand(i);
3787 if (Entry.getOpcode() == ISD::UNDEF) {
3788 Mask.push_back(-1);
3789 continue;
3790 }
3791
3792 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00003793 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
3794 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00003795 if (ExtractVec == SourceVecs[0]) {
3796 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3797 } else {
3798 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3799 }
3800 }
3801
3802 // Final check before we try to produce nonsense...
3803 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00003804 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
3805 &Mask[0]);
Bob Wilson11a1dff2011-01-07 21:37:30 +00003806
3807 return SDValue();
3808}
3809
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003810/// isShuffleMaskLegal - Targets can use this to indicate that they only
3811/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3812/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3813/// are assumed to be legal.
3814bool
3815ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3816 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003817 if (VT.getVectorNumElements() == 4 &&
3818 (VT.is128BitVector() || VT.is64BitVector())) {
3819 unsigned PFIndexes[4];
3820 for (unsigned i = 0; i != 4; ++i) {
3821 if (M[i] < 0)
3822 PFIndexes[i] = 8;
3823 else
3824 PFIndexes[i] = M[i];
3825 }
3826
3827 // Compute the index in the perfect shuffle table.
3828 unsigned PFTableIndex =
3829 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3830 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3831 unsigned Cost = (PFEntry >> 30);
3832
3833 if (Cost <= 4)
3834 return true;
3835 }
3836
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003837 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003838 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003839
Bob Wilson53dd2452010-06-07 23:53:38 +00003840 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3841 return (EltSize >= 32 ||
3842 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003843 isVREVMask(M, VT, 64) ||
3844 isVREVMask(M, VT, 32) ||
3845 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003846 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3847 isVTRNMask(M, VT, WhichResult) ||
3848 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003849 isVZIPMask(M, VT, WhichResult) ||
3850 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3851 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3852 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003853}
3854
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003855/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3856/// the specified operations to build the shuffle.
3857static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3858 SDValue RHS, SelectionDAG &DAG,
3859 DebugLoc dl) {
3860 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3861 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3862 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3863
3864 enum {
3865 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3866 OP_VREV,
3867 OP_VDUP0,
3868 OP_VDUP1,
3869 OP_VDUP2,
3870 OP_VDUP3,
3871 OP_VEXT1,
3872 OP_VEXT2,
3873 OP_VEXT3,
3874 OP_VUZPL, // VUZP, left result
3875 OP_VUZPR, // VUZP, right result
3876 OP_VZIPL, // VZIP, left result
3877 OP_VZIPR, // VZIP, right result
3878 OP_VTRNL, // VTRN, left result
3879 OP_VTRNR // VTRN, right result
3880 };
3881
3882 if (OpNum == OP_COPY) {
3883 if (LHSID == (1*9+2)*9+3) return LHS;
3884 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3885 return RHS;
3886 }
3887
3888 SDValue OpLHS, OpRHS;
3889 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3890 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3891 EVT VT = OpLHS.getValueType();
3892
3893 switch (OpNum) {
3894 default: llvm_unreachable("Unknown shuffle opcode!");
3895 case OP_VREV:
3896 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3897 case OP_VDUP0:
3898 case OP_VDUP1:
3899 case OP_VDUP2:
3900 case OP_VDUP3:
3901 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003902 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003903 case OP_VEXT1:
3904 case OP_VEXT2:
3905 case OP_VEXT3:
3906 return DAG.getNode(ARMISD::VEXT, dl, VT,
3907 OpLHS, OpRHS,
3908 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3909 case OP_VUZPL:
3910 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003911 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003912 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3913 case OP_VZIPL:
3914 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003915 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003916 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3917 case OP_VTRNL:
3918 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003919 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3920 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003921 }
3922}
3923
Bob Wilson5bafff32009-06-22 23:27:02 +00003924static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003925 SDValue V1 = Op.getOperand(0);
3926 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003927 DebugLoc dl = Op.getDebugLoc();
3928 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003929 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003930 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003931
Bob Wilson28865062009-08-13 02:13:04 +00003932 // Convert shuffles that are directly supported on NEON to target-specific
3933 // DAG nodes, instead of keeping them as shuffles and matching them again
3934 // during code selection. This is more efficient and avoids the possibility
3935 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003936 // FIXME: floating-point vectors should be canonicalized to integer vectors
3937 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003938 SVN->getMask(ShuffleMask);
3939
Bob Wilson53dd2452010-06-07 23:53:38 +00003940 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3941 if (EltSize <= 32) {
3942 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3943 int Lane = SVN->getSplatIndex();
3944 // If this is undef splat, generate it via "just" vdup, if possible.
3945 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003946
Bob Wilson53dd2452010-06-07 23:53:38 +00003947 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3948 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3949 }
3950 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3951 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003952 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003953
3954 bool ReverseVEXT;
3955 unsigned Imm;
3956 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3957 if (ReverseVEXT)
3958 std::swap(V1, V2);
3959 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3960 DAG.getConstant(Imm, MVT::i32));
3961 }
3962
3963 if (isVREVMask(ShuffleMask, VT, 64))
3964 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3965 if (isVREVMask(ShuffleMask, VT, 32))
3966 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3967 if (isVREVMask(ShuffleMask, VT, 16))
3968 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3969
3970 // Check for Neon shuffles that modify both input vectors in place.
3971 // If both results are used, i.e., if there are two shuffles with the same
3972 // source operands and with masks corresponding to both results of one of
3973 // these operations, DAG memoization will ensure that a single node is
3974 // used for both shuffles.
3975 unsigned WhichResult;
3976 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3977 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3978 V1, V2).getValue(WhichResult);
3979 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3980 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3981 V1, V2).getValue(WhichResult);
3982 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3983 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3984 V1, V2).getValue(WhichResult);
3985
3986 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3987 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3988 V1, V1).getValue(WhichResult);
3989 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3990 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3991 V1, V1).getValue(WhichResult);
3992 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3993 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3994 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003995 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003996
Bob Wilsonc692cb72009-08-21 20:54:19 +00003997 // If the shuffle is not directly supported and it has 4 elements, use
3998 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003999 unsigned NumElts = VT.getVectorNumElements();
4000 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004001 unsigned PFIndexes[4];
4002 for (unsigned i = 0; i != 4; ++i) {
4003 if (ShuffleMask[i] < 0)
4004 PFIndexes[i] = 8;
4005 else
4006 PFIndexes[i] = ShuffleMask[i];
4007 }
4008
4009 // Compute the index in the perfect shuffle table.
4010 unsigned PFTableIndex =
4011 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004012 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4013 unsigned Cost = (PFEntry >> 30);
4014
4015 if (Cost <= 4)
4016 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4017 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004018
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004019 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004020 if (EltSize >= 32) {
4021 // Do the expansion with floating-point types, since that is what the VFP
4022 // registers are defined to use, and since i64 is not legal.
4023 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4024 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004025 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4026 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004027 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004028 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004029 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004030 Ops.push_back(DAG.getUNDEF(EltVT));
4031 else
4032 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4033 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4034 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4035 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004036 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004037 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004038 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004039 }
4040
Bob Wilson22cac0d2009-08-14 05:16:33 +00004041 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004042}
4043
Bob Wilson5bafff32009-06-22 23:27:02 +00004044static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004045 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004046 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004047 if (!isa<ConstantSDNode>(Lane))
4048 return SDValue();
4049
4050 SDValue Vec = Op.getOperand(0);
4051 if (Op.getValueType() == MVT::i32 &&
4052 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4053 DebugLoc dl = Op.getDebugLoc();
4054 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4055 }
4056
4057 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004058}
4059
Bob Wilsona6d65862009-08-03 20:36:38 +00004060static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4061 // The only time a CONCAT_VECTORS operation can have legal types is when
4062 // two 64-bit vectors are concatenated to a 128-bit vector.
4063 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4064 "unexpected CONCAT_VECTORS");
4065 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004067 SDValue Op0 = Op.getOperand(0);
4068 SDValue Op1 = Op.getOperand(1);
4069 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004070 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004071 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004072 DAG.getIntPtrConstant(0));
4073 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004074 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004075 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004076 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004077 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004078}
4079
Bob Wilson626613d2010-11-23 19:38:38 +00004080/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4081/// element has been zero/sign-extended, depending on the isSigned parameter,
4082/// from an integer type half its size.
4083static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4084 bool isSigned) {
4085 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4086 EVT VT = N->getValueType(0);
4087 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4088 SDNode *BVN = N->getOperand(0).getNode();
4089 if (BVN->getValueType(0) != MVT::v4i32 ||
4090 BVN->getOpcode() != ISD::BUILD_VECTOR)
4091 return false;
4092 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4093 unsigned HiElt = 1 - LoElt;
4094 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4095 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4096 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4097 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4098 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4099 return false;
4100 if (isSigned) {
4101 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4102 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4103 return true;
4104 } else {
4105 if (Hi0->isNullValue() && Hi1->isNullValue())
4106 return true;
4107 }
4108 return false;
4109 }
4110
4111 if (N->getOpcode() != ISD::BUILD_VECTOR)
4112 return false;
4113
4114 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4115 SDNode *Elt = N->getOperand(i).getNode();
4116 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4117 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4118 unsigned HalfSize = EltSize / 2;
4119 if (isSigned) {
4120 int64_t SExtVal = C->getSExtValue();
4121 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4122 return false;
4123 } else {
4124 if ((C->getZExtValue() >> HalfSize) != 0)
4125 return false;
4126 }
4127 continue;
4128 }
4129 return false;
4130 }
4131
4132 return true;
4133}
4134
4135/// isSignExtended - Check if a node is a vector value that is sign-extended
4136/// or a constant BUILD_VECTOR with sign-extended elements.
4137static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4138 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4139 return true;
4140 if (isExtendedBUILD_VECTOR(N, DAG, true))
4141 return true;
4142 return false;
4143}
4144
4145/// isZeroExtended - Check if a node is a vector value that is zero-extended
4146/// or a constant BUILD_VECTOR with zero-extended elements.
4147static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4148 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4149 return true;
4150 if (isExtendedBUILD_VECTOR(N, DAG, false))
4151 return true;
4152 return false;
4153}
4154
4155/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4156/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004157static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4158 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4159 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004160 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4161 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4162 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4163 LD->isNonTemporal(), LD->getAlignment());
4164 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4165 // have been legalized as a BITCAST from v4i32.
4166 if (N->getOpcode() == ISD::BITCAST) {
4167 SDNode *BVN = N->getOperand(0).getNode();
4168 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4169 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4170 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4171 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4172 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4173 }
4174 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4175 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4176 EVT VT = N->getValueType(0);
4177 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4178 unsigned NumElts = VT.getVectorNumElements();
4179 MVT TruncVT = MVT::getIntegerVT(EltSize);
4180 SmallVector<SDValue, 8> Ops;
4181 for (unsigned i = 0; i != NumElts; ++i) {
4182 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4183 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004184 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004185 }
4186 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4187 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004188}
4189
4190static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4191 // Multiplications are only custom-lowered for 128-bit vectors so that
4192 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4193 EVT VT = Op.getValueType();
4194 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4195 SDNode *N0 = Op.getOperand(0).getNode();
4196 SDNode *N1 = Op.getOperand(1).getNode();
4197 unsigned NewOpc = 0;
Bob Wilson626613d2010-11-23 19:38:38 +00004198 if (isSignExtended(N0, DAG) && isSignExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004199 NewOpc = ARMISD::VMULLs;
Bob Wilson626613d2010-11-23 19:38:38 +00004200 else if (isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004201 NewOpc = ARMISD::VMULLu;
Bob Wilson626613d2010-11-23 19:38:38 +00004202 else if (VT == MVT::v2i64)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004203 // Fall through to expand this. It is not legal.
4204 return SDValue();
Bob Wilson626613d2010-11-23 19:38:38 +00004205 else
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004206 // Other vector multiplications are legal.
4207 return Op;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004208
4209 // Legalize to a VMULL instruction.
4210 DebugLoc DL = Op.getDebugLoc();
4211 SDValue Op0 = SkipExtension(N0, DAG);
4212 SDValue Op1 = SkipExtension(N1, DAG);
4213
4214 assert(Op0.getValueType().is64BitVector() &&
4215 Op1.getValueType().is64BitVector() &&
4216 "unexpected types for extended operands to VMULL");
4217 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4218}
4219
Dan Gohmand858e902010-04-17 15:26:15 +00004220SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004221 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004222 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004223 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004224 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004225 case ISD::GlobalAddress:
4226 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4227 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00004228 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004229 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004230 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4231 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004232 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004233 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004234 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004235 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004236 case ISD::SINT_TO_FP:
4237 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4238 case ISD::FP_TO_SINT:
4239 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004240 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004241 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004242 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004243 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004244 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004245 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004246 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004247 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4248 Subtarget);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004249 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004250 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004251 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004252 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004253 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004254 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004255 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004256 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004257 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004258 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004259 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004260 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004261 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004262 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004263 case ISD::MUL: return LowerMUL(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004264 }
Dan Gohman475871a2008-07-27 21:46:04 +00004265 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004266}
4267
Duncan Sands1607f052008-12-01 11:39:25 +00004268/// ReplaceNodeResults - Replace the results of node with an illegal result
4269/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004270void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4271 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004272 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004273 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004274 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004275 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004276 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004277 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004278 case ISD::BITCAST:
4279 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004280 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004281 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004282 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004283 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004284 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004285 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004286 if (Res.getNode())
4287 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004288}
Chris Lattner27a6c732007-11-24 07:07:01 +00004289
Evan Chenga8e29892007-01-19 07:51:42 +00004290//===----------------------------------------------------------------------===//
4291// ARM Scheduler Hooks
4292//===----------------------------------------------------------------------===//
4293
4294MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004295ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4296 MachineBasicBlock *BB,
4297 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004298 unsigned dest = MI->getOperand(0).getReg();
4299 unsigned ptr = MI->getOperand(1).getReg();
4300 unsigned oldval = MI->getOperand(2).getReg();
4301 unsigned newval = MI->getOperand(3).getReg();
4302 unsigned scratch = BB->getParent()->getRegInfo()
4303 .createVirtualRegister(ARM::GPRRegisterClass);
4304 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4305 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004306 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004307
4308 unsigned ldrOpc, strOpc;
4309 switch (Size) {
4310 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004311 case 1:
4312 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
4313 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
4314 break;
4315 case 2:
4316 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4317 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4318 break;
4319 case 4:
4320 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4321 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4322 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004323 }
4324
4325 MachineFunction *MF = BB->getParent();
4326 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4327 MachineFunction::iterator It = BB;
4328 ++It; // insert the new blocks after the current block
4329
4330 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4331 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4332 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4333 MF->insert(It, loop1MBB);
4334 MF->insert(It, loop2MBB);
4335 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004336
4337 // Transfer the remainder of BB and its successor edges to exitMBB.
4338 exitMBB->splice(exitMBB->begin(), BB,
4339 llvm::next(MachineBasicBlock::iterator(MI)),
4340 BB->end());
4341 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004342
4343 // thisMBB:
4344 // ...
4345 // fallthrough --> loop1MBB
4346 BB->addSuccessor(loop1MBB);
4347
4348 // loop1MBB:
4349 // ldrex dest, [ptr]
4350 // cmp dest, oldval
4351 // bne exitMBB
4352 BB = loop1MBB;
4353 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004354 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004355 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004356 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4357 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004358 BB->addSuccessor(loop2MBB);
4359 BB->addSuccessor(exitMBB);
4360
4361 // loop2MBB:
4362 // strex scratch, newval, [ptr]
4363 // cmp scratch, #0
4364 // bne loop1MBB
4365 BB = loop2MBB;
4366 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4367 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004368 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004369 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004370 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4371 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004372 BB->addSuccessor(loop1MBB);
4373 BB->addSuccessor(exitMBB);
4374
4375 // exitMBB:
4376 // ...
4377 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004378
Dan Gohman14152b42010-07-06 20:24:04 +00004379 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004380
Jim Grosbach5278eb82009-12-11 01:42:04 +00004381 return BB;
4382}
4383
4384MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004385ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4386 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004387 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4388 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4389
4390 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004391 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004392 MachineFunction::iterator It = BB;
4393 ++It;
4394
4395 unsigned dest = MI->getOperand(0).getReg();
4396 unsigned ptr = MI->getOperand(1).getReg();
4397 unsigned incr = MI->getOperand(2).getReg();
4398 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004399
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004400 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004401 unsigned ldrOpc, strOpc;
4402 switch (Size) {
4403 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004404 case 1:
4405 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004406 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004407 break;
4408 case 2:
4409 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4410 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4411 break;
4412 case 4:
4413 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4414 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4415 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004416 }
4417
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004418 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4419 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4420 MF->insert(It, loopMBB);
4421 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004422
4423 // Transfer the remainder of BB and its successor edges to exitMBB.
4424 exitMBB->splice(exitMBB->begin(), BB,
4425 llvm::next(MachineBasicBlock::iterator(MI)),
4426 BB->end());
4427 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004428
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004429 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004430 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4431 unsigned scratch2 = (!BinOpcode) ? incr :
4432 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4433
4434 // thisMBB:
4435 // ...
4436 // fallthrough --> loopMBB
4437 BB->addSuccessor(loopMBB);
4438
4439 // loopMBB:
4440 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004441 // <binop> scratch2, dest, incr
4442 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004443 // cmp scratch, #0
4444 // bne- loopMBB
4445 // fallthrough --> exitMBB
4446 BB = loopMBB;
4447 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004448 if (BinOpcode) {
4449 // operand order needs to go the other way for NAND
4450 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4451 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4452 addReg(incr).addReg(dest)).addReg(0);
4453 else
4454 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4455 addReg(dest).addReg(incr)).addReg(0);
4456 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004457
4458 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4459 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004460 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004461 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004462 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4463 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004464
4465 BB->addSuccessor(loopMBB);
4466 BB->addSuccessor(exitMBB);
4467
4468 // exitMBB:
4469 // ...
4470 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004471
Dan Gohman14152b42010-07-06 20:24:04 +00004472 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004473
Jim Grosbachc3c23542009-12-14 04:22:04 +00004474 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004475}
4476
Evan Cheng218977b2010-07-13 19:27:42 +00004477static
4478MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4479 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4480 E = MBB->succ_end(); I != E; ++I)
4481 if (*I != Succ)
4482 return *I;
4483 llvm_unreachable("Expecting a BB with two successors!");
4484}
4485
Jim Grosbache801dc42009-12-12 01:40:06 +00004486MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004487ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004488 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004489 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004490 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004491 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004492 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004493 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004494 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004495 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004496
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004497 case ARM::ATOMIC_LOAD_ADD_I8:
4498 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4499 case ARM::ATOMIC_LOAD_ADD_I16:
4500 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4501 case ARM::ATOMIC_LOAD_ADD_I32:
4502 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004503
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004504 case ARM::ATOMIC_LOAD_AND_I8:
4505 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4506 case ARM::ATOMIC_LOAD_AND_I16:
4507 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4508 case ARM::ATOMIC_LOAD_AND_I32:
4509 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004510
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004511 case ARM::ATOMIC_LOAD_OR_I8:
4512 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4513 case ARM::ATOMIC_LOAD_OR_I16:
4514 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4515 case ARM::ATOMIC_LOAD_OR_I32:
4516 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004517
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004518 case ARM::ATOMIC_LOAD_XOR_I8:
4519 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4520 case ARM::ATOMIC_LOAD_XOR_I16:
4521 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4522 case ARM::ATOMIC_LOAD_XOR_I32:
4523 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004524
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004525 case ARM::ATOMIC_LOAD_NAND_I8:
4526 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4527 case ARM::ATOMIC_LOAD_NAND_I16:
4528 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4529 case ARM::ATOMIC_LOAD_NAND_I32:
4530 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004531
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004532 case ARM::ATOMIC_LOAD_SUB_I8:
4533 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4534 case ARM::ATOMIC_LOAD_SUB_I16:
4535 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4536 case ARM::ATOMIC_LOAD_SUB_I32:
4537 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004538
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004539 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4540 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4541 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004542
4543 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4544 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4545 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004546
Evan Cheng007ea272009-08-12 05:17:19 +00004547 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004548 // To "insert" a SELECT_CC instruction, we actually have to insert the
4549 // diamond control-flow pattern. The incoming instruction knows the
4550 // destination vreg to set, the condition code register to branch on, the
4551 // true/false values to select between, and a branch opcode to use.
4552 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004553 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004554 ++It;
4555
4556 // thisMBB:
4557 // ...
4558 // TrueVal = ...
4559 // cmpTY ccX, r1, r2
4560 // bCC copy1MBB
4561 // fallthrough --> copy0MBB
4562 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004563 MachineFunction *F = BB->getParent();
4564 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4565 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004566 F->insert(It, copy0MBB);
4567 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004568
4569 // Transfer the remainder of BB and its successor edges to sinkMBB.
4570 sinkMBB->splice(sinkMBB->begin(), BB,
4571 llvm::next(MachineBasicBlock::iterator(MI)),
4572 BB->end());
4573 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4574
Dan Gohman258c58c2010-07-06 15:49:48 +00004575 BB->addSuccessor(copy0MBB);
4576 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004577
Dan Gohman14152b42010-07-06 20:24:04 +00004578 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4579 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4580
Evan Chenga8e29892007-01-19 07:51:42 +00004581 // copy0MBB:
4582 // %FalseValue = ...
4583 // # fallthrough to sinkMBB
4584 BB = copy0MBB;
4585
4586 // Update machine-CFG edges
4587 BB->addSuccessor(sinkMBB);
4588
4589 // sinkMBB:
4590 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4591 // ...
4592 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004593 BuildMI(*BB, BB->begin(), dl,
4594 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004595 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4596 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4597
Dan Gohman14152b42010-07-06 20:24:04 +00004598 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004599 return BB;
4600 }
Evan Cheng86198642009-08-07 00:34:42 +00004601
Evan Cheng218977b2010-07-13 19:27:42 +00004602 case ARM::BCCi64:
4603 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00004604 // If there is an unconditional branch to the other successor, remove it.
4605 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
4606
Evan Cheng218977b2010-07-13 19:27:42 +00004607 // Compare both parts that make up the double comparison separately for
4608 // equality.
4609 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4610
4611 unsigned LHS1 = MI->getOperand(1).getReg();
4612 unsigned LHS2 = MI->getOperand(2).getReg();
4613 if (RHSisZero) {
4614 AddDefaultPred(BuildMI(BB, dl,
4615 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4616 .addReg(LHS1).addImm(0));
4617 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4618 .addReg(LHS2).addImm(0)
4619 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4620 } else {
4621 unsigned RHS1 = MI->getOperand(3).getReg();
4622 unsigned RHS2 = MI->getOperand(4).getReg();
4623 AddDefaultPred(BuildMI(BB, dl,
4624 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4625 .addReg(LHS1).addReg(RHS1));
4626 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4627 .addReg(LHS2).addReg(RHS2)
4628 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4629 }
4630
4631 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4632 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4633 if (MI->getOperand(0).getImm() == ARMCC::NE)
4634 std::swap(destMBB, exitMBB);
4635
4636 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4637 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4638 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4639 .addMBB(exitMBB);
4640
4641 MI->eraseFromParent(); // The pseudo instruction is gone now.
4642 return BB;
4643 }
Evan Chenga8e29892007-01-19 07:51:42 +00004644 }
4645}
4646
4647//===----------------------------------------------------------------------===//
4648// ARM Optimization Hooks
4649//===----------------------------------------------------------------------===//
4650
Chris Lattnerd1980a52009-03-12 06:52:53 +00004651static
4652SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4653 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004654 SelectionDAG &DAG = DCI.DAG;
4655 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004656 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004657 unsigned Opc = N->getOpcode();
4658 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4659 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4660 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4661 ISD::CondCode CC = ISD::SETCC_INVALID;
4662
4663 if (isSlctCC) {
4664 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4665 } else {
4666 SDValue CCOp = Slct.getOperand(0);
4667 if (CCOp.getOpcode() == ISD::SETCC)
4668 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4669 }
4670
4671 bool DoXform = false;
4672 bool InvCC = false;
4673 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4674 "Bad input!");
4675
4676 if (LHS.getOpcode() == ISD::Constant &&
4677 cast<ConstantSDNode>(LHS)->isNullValue()) {
4678 DoXform = true;
4679 } else if (CC != ISD::SETCC_INVALID &&
4680 RHS.getOpcode() == ISD::Constant &&
4681 cast<ConstantSDNode>(RHS)->isNullValue()) {
4682 std::swap(LHS, RHS);
4683 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004684 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004685 Op0.getOperand(0).getValueType();
4686 bool isInt = OpVT.isInteger();
4687 CC = ISD::getSetCCInverse(CC, isInt);
4688
4689 if (!TLI.isCondCodeLegal(CC, OpVT))
4690 return SDValue(); // Inverse operator isn't legal.
4691
4692 DoXform = true;
4693 InvCC = true;
4694 }
4695
4696 if (DoXform) {
4697 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4698 if (isSlctCC)
4699 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4700 Slct.getOperand(0), Slct.getOperand(1), CC);
4701 SDValue CCOp = Slct.getOperand(0);
4702 if (InvCC)
4703 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4704 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4705 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4706 CCOp, OtherOp, Result);
4707 }
4708 return SDValue();
4709}
4710
Bob Wilson3d5792a2010-07-29 20:34:14 +00004711/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4712/// operands N0 and N1. This is a helper for PerformADDCombine that is
4713/// called with the default operands, and if that fails, with commuted
4714/// operands.
4715static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4716 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004717 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4718 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4719 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4720 if (Result.getNode()) return Result;
4721 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00004722 return SDValue();
4723}
4724
Bob Wilson3d5792a2010-07-29 20:34:14 +00004725/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4726///
4727static SDValue PerformADDCombine(SDNode *N,
4728 TargetLowering::DAGCombinerInfo &DCI) {
4729 SDValue N0 = N->getOperand(0);
4730 SDValue N1 = N->getOperand(1);
4731
4732 // First try with the default operand order.
4733 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4734 if (Result.getNode())
4735 return Result;
4736
4737 // If that didn't work, try again with the operands commuted.
4738 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4739}
4740
Chris Lattnerd1980a52009-03-12 06:52:53 +00004741/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004742///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004743static SDValue PerformSUBCombine(SDNode *N,
4744 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004745 SDValue N0 = N->getOperand(0);
4746 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004747
Chris Lattnerd1980a52009-03-12 06:52:53 +00004748 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4749 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4750 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4751 if (Result.getNode()) return Result;
4752 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004753
Chris Lattnerd1980a52009-03-12 06:52:53 +00004754 return SDValue();
4755}
4756
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004757static SDValue PerformMULCombine(SDNode *N,
4758 TargetLowering::DAGCombinerInfo &DCI,
4759 const ARMSubtarget *Subtarget) {
4760 SelectionDAG &DAG = DCI.DAG;
4761
4762 if (Subtarget->isThumb1Only())
4763 return SDValue();
4764
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004765 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4766 return SDValue();
4767
4768 EVT VT = N->getValueType(0);
4769 if (VT != MVT::i32)
4770 return SDValue();
4771
4772 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4773 if (!C)
4774 return SDValue();
4775
4776 uint64_t MulAmt = C->getZExtValue();
4777 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4778 ShiftAmt = ShiftAmt & (32 - 1);
4779 SDValue V = N->getOperand(0);
4780 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004781
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004782 SDValue Res;
4783 MulAmt >>= ShiftAmt;
4784 if (isPowerOf2_32(MulAmt - 1)) {
4785 // (mul x, 2^N + 1) => (add (shl x, N), x)
4786 Res = DAG.getNode(ISD::ADD, DL, VT,
4787 V, DAG.getNode(ISD::SHL, DL, VT,
4788 V, DAG.getConstant(Log2_32(MulAmt-1),
4789 MVT::i32)));
4790 } else if (isPowerOf2_32(MulAmt + 1)) {
4791 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4792 Res = DAG.getNode(ISD::SUB, DL, VT,
4793 DAG.getNode(ISD::SHL, DL, VT,
4794 V, DAG.getConstant(Log2_32(MulAmt+1),
4795 MVT::i32)),
4796 V);
4797 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004798 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004799
4800 if (ShiftAmt != 0)
4801 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4802 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004803
4804 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004805 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004806 return SDValue();
4807}
4808
Owen Anderson080c0922010-11-05 19:27:46 +00004809static SDValue PerformANDCombine(SDNode *N,
4810 TargetLowering::DAGCombinerInfo &DCI) {
4811 // Attempt to use immediate-form VBIC
4812 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4813 DebugLoc dl = N->getDebugLoc();
4814 EVT VT = N->getValueType(0);
4815 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004816
Owen Anderson080c0922010-11-05 19:27:46 +00004817 APInt SplatBits, SplatUndef;
4818 unsigned SplatBitSize;
4819 bool HasAnyUndefs;
4820 if (BVN &&
4821 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4822 if (SplatBitSize <= 64) {
4823 EVT VbicVT;
4824 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
4825 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004826 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004827 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00004828 if (Val.getNode()) {
4829 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004830 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00004831 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004832 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00004833 }
4834 }
4835 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004836
Owen Anderson080c0922010-11-05 19:27:46 +00004837 return SDValue();
4838}
4839
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004840/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4841static SDValue PerformORCombine(SDNode *N,
4842 TargetLowering::DAGCombinerInfo &DCI,
4843 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00004844 // Attempt to use immediate-form VORR
4845 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4846 DebugLoc dl = N->getDebugLoc();
4847 EVT VT = N->getValueType(0);
4848 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004849
Owen Anderson60f48702010-11-03 23:15:26 +00004850 APInt SplatBits, SplatUndef;
4851 unsigned SplatBitSize;
4852 bool HasAnyUndefs;
4853 if (BVN && Subtarget->hasNEON() &&
4854 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4855 if (SplatBitSize <= 64) {
4856 EVT VorrVT;
4857 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
4858 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004859 DAG, VorrVT, VT.is128BitVector(),
4860 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00004861 if (Val.getNode()) {
4862 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004863 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00004864 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004865 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00004866 }
4867 }
4868 }
4869
Jim Grosbach54238562010-07-17 03:30:54 +00004870 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4871 // reasonable.
4872
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004873 // BFI is only available on V6T2+
4874 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4875 return SDValue();
4876
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004877 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004878 DebugLoc DL = N->getDebugLoc();
4879 // 1) or (and A, mask), val => ARMbfi A, val, mask
4880 // iff (val & mask) == val
4881 //
4882 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4883 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4884 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4885 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4886 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4887 // (i.e., copy a bitfield value into another bitfield of the same width)
4888 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004889 return SDValue();
4890
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004891 if (VT != MVT::i32)
4892 return SDValue();
4893
Evan Cheng30fb13f2010-12-13 20:32:54 +00004894 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00004895
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004896 // The value and the mask need to be constants so we can verify this is
4897 // actually a bitfield set. If the mask is 0xffff, we can do better
4898 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00004899 SDValue MaskOp = N0.getOperand(1);
4900 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
4901 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004902 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004903 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004904 if (Mask == 0xffff)
4905 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004906 SDValue Res;
4907 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004908 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4909 if (N1C) {
4910 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004911 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00004912 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004913
Evan Chenga9688c42010-12-11 04:11:38 +00004914 if (ARM::isBitFieldInvertedMask(Mask)) {
4915 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004916
Evan Cheng30fb13f2010-12-13 20:32:54 +00004917 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00004918 DAG.getConstant(Val, MVT::i32),
4919 DAG.getConstant(Mask, MVT::i32));
4920
4921 // Do not add new nodes to DAG combiner worklist.
4922 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004923 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004924 }
Jim Grosbach54238562010-07-17 03:30:54 +00004925 } else if (N1.getOpcode() == ISD::AND) {
4926 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004927 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4928 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00004929 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004930 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004931
4932 if (ARM::isBitFieldInvertedMask(Mask) &&
4933 ARM::isBitFieldInvertedMask(~Mask2) &&
4934 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4935 // The pack halfword instruction works better for masks that fit it,
4936 // so use that when it's available.
4937 if (Subtarget->hasT2ExtractPack() &&
4938 (Mask == 0xffff || Mask == 0xffff0000))
4939 return SDValue();
4940 // 2a
4941 unsigned lsb = CountTrailingZeros_32(Mask2);
4942 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4943 DAG.getConstant(lsb, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00004944 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00004945 DAG.getConstant(Mask, MVT::i32));
4946 // Do not add new nodes to DAG combiner worklist.
4947 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004948 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004949 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4950 ARM::isBitFieldInvertedMask(Mask2) &&
4951 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4952 // The pack halfword instruction works better for masks that fit it,
4953 // so use that when it's available.
4954 if (Subtarget->hasT2ExtractPack() &&
4955 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4956 return SDValue();
4957 // 2b
4958 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004959 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00004960 DAG.getConstant(lsb, MVT::i32));
4961 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4962 DAG.getConstant(Mask2, MVT::i32));
4963 // Do not add new nodes to DAG combiner worklist.
4964 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004965 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004966 }
4967 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004968
Evan Cheng30fb13f2010-12-13 20:32:54 +00004969 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
4970 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
4971 ARM::isBitFieldInvertedMask(~Mask)) {
4972 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
4973 // where lsb(mask) == #shamt and masked bits of B are known zero.
4974 SDValue ShAmt = N00.getOperand(1);
4975 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
4976 unsigned LSB = CountTrailingZeros_32(Mask);
4977 if (ShAmtC != LSB)
4978 return SDValue();
4979
4980 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
4981 DAG.getConstant(~Mask, MVT::i32));
4982
4983 // Do not add new nodes to DAG combiner worklist.
4984 DCI.CombineTo(N, Res, false);
4985 }
4986
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004987 return SDValue();
4988}
4989
Evan Cheng0c1aec12010-12-14 03:22:07 +00004990/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
4991/// C1 & C2 == C1.
4992static SDValue PerformBFICombine(SDNode *N,
4993 TargetLowering::DAGCombinerInfo &DCI) {
4994 SDValue N1 = N->getOperand(1);
4995 if (N1.getOpcode() == ISD::AND) {
4996 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4997 if (!N11C)
4998 return SDValue();
4999 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5000 unsigned Mask2 = N11C->getZExtValue();
5001 if ((Mask & Mask2) == Mask2)
5002 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5003 N->getOperand(0), N1.getOperand(0),
5004 N->getOperand(2));
5005 }
5006 return SDValue();
5007}
5008
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005009/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5010/// ARMISD::VMOVRRD.
5011static SDValue PerformVMOVRRDCombine(SDNode *N,
5012 TargetLowering::DAGCombinerInfo &DCI) {
5013 // vmovrrd(vmovdrr x, y) -> x,y
5014 SDValue InDouble = N->getOperand(0);
5015 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5016 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
5017 return SDValue();
5018}
5019
5020/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5021/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5022static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5023 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5024 SDValue Op0 = N->getOperand(0);
5025 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005026 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005027 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005028 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005029 Op1 = Op1.getOperand(0);
5030 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5031 Op0.getNode() == Op1.getNode() &&
5032 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005033 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005034 N->getValueType(0), Op0.getOperand(0));
5035 return SDValue();
5036}
5037
Bob Wilson31600902010-12-21 06:43:19 +00005038/// PerformSTORECombine - Target-specific dag combine xforms for
5039/// ISD::STORE.
5040static SDValue PerformSTORECombine(SDNode *N,
5041 TargetLowering::DAGCombinerInfo &DCI) {
5042 // Bitcast an i64 store extracted from a vector to f64.
5043 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5044 StoreSDNode *St = cast<StoreSDNode>(N);
5045 SDValue StVal = St->getValue();
5046 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5047 StVal.getValueType() != MVT::i64 ||
5048 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5049 return SDValue();
5050
5051 SelectionDAG &DAG = DCI.DAG;
5052 DebugLoc dl = StVal.getDebugLoc();
5053 SDValue IntVec = StVal.getOperand(0);
5054 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5055 IntVec.getValueType().getVectorNumElements());
5056 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5057 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5058 Vec, StVal.getOperand(1));
5059 dl = N->getDebugLoc();
5060 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5061 // Make the DAGCombiner fold the bitcasts.
5062 DCI.AddToWorklist(Vec.getNode());
5063 DCI.AddToWorklist(ExtElt.getNode());
5064 DCI.AddToWorklist(V.getNode());
5065 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5066 St->getPointerInfo(), St->isVolatile(),
5067 St->isNonTemporal(), St->getAlignment(),
5068 St->getTBAAInfo());
5069}
5070
5071/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5072/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5073/// i64 vector to have f64 elements, since the value can then be loaded
5074/// directly into a VFP register.
5075static bool hasNormalLoadOperand(SDNode *N) {
5076 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5077 for (unsigned i = 0; i < NumElts; ++i) {
5078 SDNode *Elt = N->getOperand(i).getNode();
5079 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5080 return true;
5081 }
5082 return false;
5083}
5084
Bob Wilson75f02882010-09-17 22:59:05 +00005085/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5086/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005087static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5088 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005089 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5090 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5091 // into a pair of GPRs, which is fine when the value is used as a scalar,
5092 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005093 SelectionDAG &DAG = DCI.DAG;
5094 if (N->getNumOperands() == 2) {
5095 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5096 if (RV.getNode())
5097 return RV;
5098 }
Bob Wilson75f02882010-09-17 22:59:05 +00005099
Bob Wilson31600902010-12-21 06:43:19 +00005100 // Load i64 elements as f64 values so that type legalization does not split
5101 // them up into i32 values.
5102 EVT VT = N->getValueType(0);
5103 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5104 return SDValue();
5105 DebugLoc dl = N->getDebugLoc();
5106 SmallVector<SDValue, 8> Ops;
5107 unsigned NumElts = VT.getVectorNumElements();
5108 for (unsigned i = 0; i < NumElts; ++i) {
5109 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5110 Ops.push_back(V);
5111 // Make the DAGCombiner fold the bitcast.
5112 DCI.AddToWorklist(V.getNode());
5113 }
5114 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5115 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5116 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5117}
5118
5119/// PerformInsertEltCombine - Target-specific dag combine xforms for
5120/// ISD::INSERT_VECTOR_ELT.
5121static SDValue PerformInsertEltCombine(SDNode *N,
5122 TargetLowering::DAGCombinerInfo &DCI) {
5123 // Bitcast an i64 load inserted into a vector to f64.
5124 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5125 EVT VT = N->getValueType(0);
5126 SDNode *Elt = N->getOperand(1).getNode();
5127 if (VT.getVectorElementType() != MVT::i64 ||
5128 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5129 return SDValue();
5130
5131 SelectionDAG &DAG = DCI.DAG;
5132 DebugLoc dl = N->getDebugLoc();
5133 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5134 VT.getVectorNumElements());
5135 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5136 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5137 // Make the DAGCombiner fold the bitcasts.
5138 DCI.AddToWorklist(Vec.getNode());
5139 DCI.AddToWorklist(V.getNode());
5140 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5141 Vec, V, N->getOperand(2));
5142 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005143}
5144
Bob Wilsonf20700c2010-10-27 20:38:28 +00005145/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5146/// ISD::VECTOR_SHUFFLE.
5147static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5148 // The LLVM shufflevector instruction does not require the shuffle mask
5149 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5150 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5151 // operands do not match the mask length, they are extended by concatenating
5152 // them with undef vectors. That is probably the right thing for other
5153 // targets, but for NEON it is better to concatenate two double-register
5154 // size vector operands into a single quad-register size vector. Do that
5155 // transformation here:
5156 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5157 // shuffle(concat(v1, v2), undef)
5158 SDValue Op0 = N->getOperand(0);
5159 SDValue Op1 = N->getOperand(1);
5160 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5161 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5162 Op0.getNumOperands() != 2 ||
5163 Op1.getNumOperands() != 2)
5164 return SDValue();
5165 SDValue Concat0Op1 = Op0.getOperand(1);
5166 SDValue Concat1Op1 = Op1.getOperand(1);
5167 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5168 Concat1Op1.getOpcode() != ISD::UNDEF)
5169 return SDValue();
5170 // Skip the transformation if any of the types are illegal.
5171 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5172 EVT VT = N->getValueType(0);
5173 if (!TLI.isTypeLegal(VT) ||
5174 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5175 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5176 return SDValue();
5177
5178 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5179 Op0.getOperand(0), Op1.getOperand(0));
5180 // Translate the shuffle mask.
5181 SmallVector<int, 16> NewMask;
5182 unsigned NumElts = VT.getVectorNumElements();
5183 unsigned HalfElts = NumElts/2;
5184 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5185 for (unsigned n = 0; n < NumElts; ++n) {
5186 int MaskElt = SVN->getMaskElt(n);
5187 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005188 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005189 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005190 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005191 NewElt = HalfElts + MaskElt - NumElts;
5192 NewMask.push_back(NewElt);
5193 }
5194 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5195 DAG.getUNDEF(VT), NewMask.data());
5196}
5197
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005198/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5199/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5200/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5201/// return true.
5202static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5203 SelectionDAG &DAG = DCI.DAG;
5204 EVT VT = N->getValueType(0);
5205 // vldN-dup instructions only support 64-bit vectors for N > 1.
5206 if (!VT.is64BitVector())
5207 return false;
5208
5209 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5210 SDNode *VLD = N->getOperand(0).getNode();
5211 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5212 return false;
5213 unsigned NumVecs = 0;
5214 unsigned NewOpc = 0;
5215 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5216 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5217 NumVecs = 2;
5218 NewOpc = ARMISD::VLD2DUP;
5219 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5220 NumVecs = 3;
5221 NewOpc = ARMISD::VLD3DUP;
5222 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5223 NumVecs = 4;
5224 NewOpc = ARMISD::VLD4DUP;
5225 } else {
5226 return false;
5227 }
5228
5229 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5230 // numbers match the load.
5231 unsigned VLDLaneNo =
5232 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5233 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5234 UI != UE; ++UI) {
5235 // Ignore uses of the chain result.
5236 if (UI.getUse().getResNo() == NumVecs)
5237 continue;
5238 SDNode *User = *UI;
5239 if (User->getOpcode() != ARMISD::VDUPLANE ||
5240 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5241 return false;
5242 }
5243
5244 // Create the vldN-dup node.
5245 EVT Tys[5];
5246 unsigned n;
5247 for (n = 0; n < NumVecs; ++n)
5248 Tys[n] = VT;
5249 Tys[n] = MVT::Other;
5250 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5251 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5252 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5253 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5254 Ops, 2, VLDMemInt->getMemoryVT(),
5255 VLDMemInt->getMemOperand());
5256
5257 // Update the uses.
5258 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5259 UI != UE; ++UI) {
5260 unsigned ResNo = UI.getUse().getResNo();
5261 // Ignore uses of the chain result.
5262 if (ResNo == NumVecs)
5263 continue;
5264 SDNode *User = *UI;
5265 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5266 }
5267
5268 // Now the vldN-lane intrinsic is dead except for its chain result.
5269 // Update uses of the chain.
5270 std::vector<SDValue> VLDDupResults;
5271 for (unsigned n = 0; n < NumVecs; ++n)
5272 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5273 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5274 DCI.CombineTo(VLD, VLDDupResults);
5275
5276 return true;
5277}
5278
Bob Wilson9e82bf12010-07-14 01:22:12 +00005279/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5280/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005281static SDValue PerformVDUPLANECombine(SDNode *N,
5282 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005283 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005284
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005285 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5286 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5287 if (CombineVLDDUP(N, DCI))
5288 return SDValue(N, 0);
5289
5290 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5291 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005292 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005293 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005294 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005295 return SDValue();
5296
5297 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5298 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5299 // The canonical VMOV for a zero vector uses a 32-bit element size.
5300 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5301 unsigned EltBits;
5302 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5303 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005304 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005305 if (EltSize > VT.getVectorElementType().getSizeInBits())
5306 return SDValue();
5307
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005308 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005309}
5310
Bob Wilson5bafff32009-06-22 23:27:02 +00005311/// getVShiftImm - Check if this is a valid build_vector for the immediate
5312/// operand of a vector shift operation, where all the elements of the
5313/// build_vector must have the same constant integer value.
5314static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5315 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005316 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005317 Op = Op.getOperand(0);
5318 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5319 APInt SplatBits, SplatUndef;
5320 unsigned SplatBitSize;
5321 bool HasAnyUndefs;
5322 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5323 HasAnyUndefs, ElementBits) ||
5324 SplatBitSize > ElementBits)
5325 return false;
5326 Cnt = SplatBits.getSExtValue();
5327 return true;
5328}
5329
5330/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5331/// operand of a vector shift left operation. That value must be in the range:
5332/// 0 <= Value < ElementBits for a left shift; or
5333/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005334static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005335 assert(VT.isVector() && "vector shift count is not a vector type");
5336 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5337 if (! getVShiftImm(Op, ElementBits, Cnt))
5338 return false;
5339 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5340}
5341
5342/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5343/// operand of a vector shift right operation. For a shift opcode, the value
5344/// is positive, but for an intrinsic the value count must be negative. The
5345/// absolute value must be in the range:
5346/// 1 <= |Value| <= ElementBits for a right shift; or
5347/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005348static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005349 int64_t &Cnt) {
5350 assert(VT.isVector() && "vector shift count is not a vector type");
5351 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5352 if (! getVShiftImm(Op, ElementBits, Cnt))
5353 return false;
5354 if (isIntrinsic)
5355 Cnt = -Cnt;
5356 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5357}
5358
5359/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5360static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5361 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5362 switch (IntNo) {
5363 default:
5364 // Don't do anything for most intrinsics.
5365 break;
5366
5367 // Vector shifts: check for immediate versions and lower them.
5368 // Note: This is done during DAG combining instead of DAG legalizing because
5369 // the build_vectors for 64-bit vector element shift counts are generally
5370 // not legal, and it is hard to see their values after they get legalized to
5371 // loads from a constant pool.
5372 case Intrinsic::arm_neon_vshifts:
5373 case Intrinsic::arm_neon_vshiftu:
5374 case Intrinsic::arm_neon_vshiftls:
5375 case Intrinsic::arm_neon_vshiftlu:
5376 case Intrinsic::arm_neon_vshiftn:
5377 case Intrinsic::arm_neon_vrshifts:
5378 case Intrinsic::arm_neon_vrshiftu:
5379 case Intrinsic::arm_neon_vrshiftn:
5380 case Intrinsic::arm_neon_vqshifts:
5381 case Intrinsic::arm_neon_vqshiftu:
5382 case Intrinsic::arm_neon_vqshiftsu:
5383 case Intrinsic::arm_neon_vqshiftns:
5384 case Intrinsic::arm_neon_vqshiftnu:
5385 case Intrinsic::arm_neon_vqshiftnsu:
5386 case Intrinsic::arm_neon_vqrshiftns:
5387 case Intrinsic::arm_neon_vqrshiftnu:
5388 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00005389 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005390 int64_t Cnt;
5391 unsigned VShiftOpc = 0;
5392
5393 switch (IntNo) {
5394 case Intrinsic::arm_neon_vshifts:
5395 case Intrinsic::arm_neon_vshiftu:
5396 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
5397 VShiftOpc = ARMISD::VSHL;
5398 break;
5399 }
5400 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
5401 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
5402 ARMISD::VSHRs : ARMISD::VSHRu);
5403 break;
5404 }
5405 return SDValue();
5406
5407 case Intrinsic::arm_neon_vshiftls:
5408 case Intrinsic::arm_neon_vshiftlu:
5409 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
5410 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005411 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005412
5413 case Intrinsic::arm_neon_vrshifts:
5414 case Intrinsic::arm_neon_vrshiftu:
5415 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
5416 break;
5417 return SDValue();
5418
5419 case Intrinsic::arm_neon_vqshifts:
5420 case Intrinsic::arm_neon_vqshiftu:
5421 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5422 break;
5423 return SDValue();
5424
5425 case Intrinsic::arm_neon_vqshiftsu:
5426 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5427 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005428 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005429
5430 case Intrinsic::arm_neon_vshiftn:
5431 case Intrinsic::arm_neon_vrshiftn:
5432 case Intrinsic::arm_neon_vqshiftns:
5433 case Intrinsic::arm_neon_vqshiftnu:
5434 case Intrinsic::arm_neon_vqshiftnsu:
5435 case Intrinsic::arm_neon_vqrshiftns:
5436 case Intrinsic::arm_neon_vqrshiftnu:
5437 case Intrinsic::arm_neon_vqrshiftnsu:
5438 // Narrowing shifts require an immediate right shift.
5439 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
5440 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00005441 llvm_unreachable("invalid shift count for narrowing vector shift "
5442 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005443
5444 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005445 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00005446 }
5447
5448 switch (IntNo) {
5449 case Intrinsic::arm_neon_vshifts:
5450 case Intrinsic::arm_neon_vshiftu:
5451 // Opcode already set above.
5452 break;
5453 case Intrinsic::arm_neon_vshiftls:
5454 case Intrinsic::arm_neon_vshiftlu:
5455 if (Cnt == VT.getVectorElementType().getSizeInBits())
5456 VShiftOpc = ARMISD::VSHLLi;
5457 else
5458 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
5459 ARMISD::VSHLLs : ARMISD::VSHLLu);
5460 break;
5461 case Intrinsic::arm_neon_vshiftn:
5462 VShiftOpc = ARMISD::VSHRN; break;
5463 case Intrinsic::arm_neon_vrshifts:
5464 VShiftOpc = ARMISD::VRSHRs; break;
5465 case Intrinsic::arm_neon_vrshiftu:
5466 VShiftOpc = ARMISD::VRSHRu; break;
5467 case Intrinsic::arm_neon_vrshiftn:
5468 VShiftOpc = ARMISD::VRSHRN; break;
5469 case Intrinsic::arm_neon_vqshifts:
5470 VShiftOpc = ARMISD::VQSHLs; break;
5471 case Intrinsic::arm_neon_vqshiftu:
5472 VShiftOpc = ARMISD::VQSHLu; break;
5473 case Intrinsic::arm_neon_vqshiftsu:
5474 VShiftOpc = ARMISD::VQSHLsu; break;
5475 case Intrinsic::arm_neon_vqshiftns:
5476 VShiftOpc = ARMISD::VQSHRNs; break;
5477 case Intrinsic::arm_neon_vqshiftnu:
5478 VShiftOpc = ARMISD::VQSHRNu; break;
5479 case Intrinsic::arm_neon_vqshiftnsu:
5480 VShiftOpc = ARMISD::VQSHRNsu; break;
5481 case Intrinsic::arm_neon_vqrshiftns:
5482 VShiftOpc = ARMISD::VQRSHRNs; break;
5483 case Intrinsic::arm_neon_vqrshiftnu:
5484 VShiftOpc = ARMISD::VQRSHRNu; break;
5485 case Intrinsic::arm_neon_vqrshiftnsu:
5486 VShiftOpc = ARMISD::VQRSHRNsu; break;
5487 }
5488
5489 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005490 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005491 }
5492
5493 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00005494 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005495 int64_t Cnt;
5496 unsigned VShiftOpc = 0;
5497
5498 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
5499 VShiftOpc = ARMISD::VSLI;
5500 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
5501 VShiftOpc = ARMISD::VSRI;
5502 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005503 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005504 }
5505
5506 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
5507 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00005508 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005509 }
5510
5511 case Intrinsic::arm_neon_vqrshifts:
5512 case Intrinsic::arm_neon_vqrshiftu:
5513 // No immediate versions of these to check for.
5514 break;
5515 }
5516
5517 return SDValue();
5518}
5519
5520/// PerformShiftCombine - Checks for immediate versions of vector shifts and
5521/// lowers them. As with the vector shift intrinsics, this is done during DAG
5522/// combining instead of DAG legalizing because the build_vectors for 64-bit
5523/// vector element shift counts are generally not legal, and it is hard to see
5524/// their values after they get legalized to loads from a constant pool.
5525static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
5526 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00005527 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00005528
5529 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00005530 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5531 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00005532 return SDValue();
5533
5534 assert(ST->hasNEON() && "unexpected vector shift");
5535 int64_t Cnt;
5536
5537 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005538 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005539
5540 case ISD::SHL:
5541 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
5542 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005543 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005544 break;
5545
5546 case ISD::SRA:
5547 case ISD::SRL:
5548 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
5549 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
5550 ARMISD::VSHRs : ARMISD::VSHRu);
5551 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005552 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005553 }
5554 }
5555 return SDValue();
5556}
5557
5558/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
5559/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
5560static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
5561 const ARMSubtarget *ST) {
5562 SDValue N0 = N->getOperand(0);
5563
5564 // Check for sign- and zero-extensions of vector extract operations of 8-
5565 // and 16-bit vector elements. NEON supports these directly. They are
5566 // handled during DAG combining because type legalization will promote them
5567 // to 32-bit types and it is messy to recognize the operations after that.
5568 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5569 SDValue Vec = N0.getOperand(0);
5570 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005571 EVT VT = N->getValueType(0);
5572 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005573 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5574
Owen Anderson825b72b2009-08-11 20:47:22 +00005575 if (VT == MVT::i32 &&
5576 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00005577 TLI.isTypeLegal(Vec.getValueType()) &&
5578 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005579
5580 unsigned Opc = 0;
5581 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005582 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005583 case ISD::SIGN_EXTEND:
5584 Opc = ARMISD::VGETLANEs;
5585 break;
5586 case ISD::ZERO_EXTEND:
5587 case ISD::ANY_EXTEND:
5588 Opc = ARMISD::VGETLANEu;
5589 break;
5590 }
5591 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
5592 }
5593 }
5594
5595 return SDValue();
5596}
5597
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005598/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
5599/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
5600static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
5601 const ARMSubtarget *ST) {
5602 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00005603 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005604 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
5605 // a NaN; only do the transformation when it matches that behavior.
5606
5607 // For now only do this when using NEON for FP operations; if using VFP, it
5608 // is not obvious that the benefit outweighs the cost of switching to the
5609 // NEON pipeline.
5610 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
5611 N->getValueType(0) != MVT::f32)
5612 return SDValue();
5613
5614 SDValue CondLHS = N->getOperand(0);
5615 SDValue CondRHS = N->getOperand(1);
5616 SDValue LHS = N->getOperand(2);
5617 SDValue RHS = N->getOperand(3);
5618 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
5619
5620 unsigned Opcode = 0;
5621 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00005622 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005623 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00005624 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005625 IsReversed = true ; // x CC y ? y : x
5626 } else {
5627 return SDValue();
5628 }
5629
Bob Wilsone742bb52010-02-24 22:15:53 +00005630 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005631 switch (CC) {
5632 default: break;
5633 case ISD::SETOLT:
5634 case ISD::SETOLE:
5635 case ISD::SETLT:
5636 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005637 case ISD::SETULT:
5638 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005639 // If LHS is NaN, an ordered comparison will be false and the result will
5640 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
5641 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5642 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
5643 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5644 break;
5645 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
5646 // will return -0, so vmin can only be used for unsafe math or if one of
5647 // the operands is known to be nonzero.
5648 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
5649 !UnsafeFPMath &&
5650 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5651 break;
5652 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005653 break;
5654
5655 case ISD::SETOGT:
5656 case ISD::SETOGE:
5657 case ISD::SETGT:
5658 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005659 case ISD::SETUGT:
5660 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005661 // If LHS is NaN, an ordered comparison will be false and the result will
5662 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
5663 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5664 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
5665 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5666 break;
5667 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
5668 // will return +0, so vmax can only be used for unsafe math or if one of
5669 // the operands is known to be nonzero.
5670 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
5671 !UnsafeFPMath &&
5672 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5673 break;
5674 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005675 break;
5676 }
5677
5678 if (!Opcode)
5679 return SDValue();
5680 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
5681}
5682
Dan Gohman475871a2008-07-27 21:46:04 +00005683SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005684 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005685 switch (N->getOpcode()) {
5686 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005687 case ISD::ADD: return PerformADDCombine(N, DCI);
5688 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005689 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005690 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00005691 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00005692 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00005693 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005694 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00005695 case ISD::STORE: return PerformSTORECombine(N, DCI);
5696 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
5697 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00005698 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005699 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005700 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005701 case ISD::SHL:
5702 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005703 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005704 case ISD::SIGN_EXTEND:
5705 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005706 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
5707 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005708 }
Dan Gohman475871a2008-07-27 21:46:04 +00005709 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005710}
5711
Bill Wendlingaf566342009-08-15 21:21:19 +00005712bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00005713 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00005714 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00005715
5716 switch (VT.getSimpleVT().SimpleTy) {
5717 default:
5718 return false;
5719 case MVT::i8:
5720 case MVT::i16:
5721 case MVT::i32:
5722 return true;
5723 // FIXME: VLD1 etc with standard alignment is legal.
5724 }
5725}
5726
Evan Chenge6c835f2009-08-14 20:09:37 +00005727static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
5728 if (V < 0)
5729 return false;
5730
5731 unsigned Scale = 1;
5732 switch (VT.getSimpleVT().SimpleTy) {
5733 default: return false;
5734 case MVT::i1:
5735 case MVT::i8:
5736 // Scale == 1;
5737 break;
5738 case MVT::i16:
5739 // Scale == 2;
5740 Scale = 2;
5741 break;
5742 case MVT::i32:
5743 // Scale == 4;
5744 Scale = 4;
5745 break;
5746 }
5747
5748 if ((V & (Scale - 1)) != 0)
5749 return false;
5750 V /= Scale;
5751 return V == (V & ((1LL << 5) - 1));
5752}
5753
5754static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
5755 const ARMSubtarget *Subtarget) {
5756 bool isNeg = false;
5757 if (V < 0) {
5758 isNeg = true;
5759 V = - V;
5760 }
5761
5762 switch (VT.getSimpleVT().SimpleTy) {
5763 default: return false;
5764 case MVT::i1:
5765 case MVT::i8:
5766 case MVT::i16:
5767 case MVT::i32:
5768 // + imm12 or - imm8
5769 if (isNeg)
5770 return V == (V & ((1LL << 8) - 1));
5771 return V == (V & ((1LL << 12) - 1));
5772 case MVT::f32:
5773 case MVT::f64:
5774 // Same as ARM mode. FIXME: NEON?
5775 if (!Subtarget->hasVFP2())
5776 return false;
5777 if ((V & 3) != 0)
5778 return false;
5779 V >>= 2;
5780 return V == (V & ((1LL << 8) - 1));
5781 }
5782}
5783
Evan Chengb01fad62007-03-12 23:30:29 +00005784/// isLegalAddressImmediate - Return true if the integer value can be used
5785/// as the offset of the target addressing mode for load / store of the
5786/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00005787static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005788 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00005789 if (V == 0)
5790 return true;
5791
Evan Cheng65011532009-03-09 19:15:00 +00005792 if (!VT.isSimple())
5793 return false;
5794
Evan Chenge6c835f2009-08-14 20:09:37 +00005795 if (Subtarget->isThumb1Only())
5796 return isLegalT1AddressImmediate(V, VT);
5797 else if (Subtarget->isThumb2())
5798 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00005799
Evan Chenge6c835f2009-08-14 20:09:37 +00005800 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00005801 if (V < 0)
5802 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00005803 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00005804 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005805 case MVT::i1:
5806 case MVT::i8:
5807 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00005808 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005809 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005810 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00005811 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005812 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005813 case MVT::f32:
5814 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00005815 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00005816 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00005817 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00005818 return false;
5819 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005820 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00005821 }
Evan Chenga8e29892007-01-19 07:51:42 +00005822}
5823
Evan Chenge6c835f2009-08-14 20:09:37 +00005824bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
5825 EVT VT) const {
5826 int Scale = AM.Scale;
5827 if (Scale < 0)
5828 return false;
5829
5830 switch (VT.getSimpleVT().SimpleTy) {
5831 default: return false;
5832 case MVT::i1:
5833 case MVT::i8:
5834 case MVT::i16:
5835 case MVT::i32:
5836 if (Scale == 1)
5837 return true;
5838 // r + r << imm
5839 Scale = Scale & ~1;
5840 return Scale == 2 || Scale == 4 || Scale == 8;
5841 case MVT::i64:
5842 // r + r
5843 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
5844 return true;
5845 return false;
5846 case MVT::isVoid:
5847 // Note, we allow "void" uses (basically, uses that aren't loads or
5848 // stores), because arm allows folding a scale into many arithmetic
5849 // operations. This should be made more precise and revisited later.
5850
5851 // Allow r << imm, but the imm has to be a multiple of two.
5852 if (Scale & 1) return false;
5853 return isPowerOf2_32(Scale);
5854 }
5855}
5856
Chris Lattner37caf8c2007-04-09 23:33:39 +00005857/// isLegalAddressingMode - Return true if the addressing mode represented
5858/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005859bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005860 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005861 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00005862 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00005863 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005864
Chris Lattner37caf8c2007-04-09 23:33:39 +00005865 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005866 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005867 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005868
Chris Lattner37caf8c2007-04-09 23:33:39 +00005869 switch (AM.Scale) {
5870 case 0: // no scale reg, must be "r+i" or "r", or "i".
5871 break;
5872 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00005873 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00005874 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005875 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00005876 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005877 // ARM doesn't support any R+R*scale+imm addr modes.
5878 if (AM.BaseOffs)
5879 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005880
Bob Wilson2c7dab12009-04-08 17:55:28 +00005881 if (!VT.isSimple())
5882 return false;
5883
Evan Chenge6c835f2009-08-14 20:09:37 +00005884 if (Subtarget->isThumb2())
5885 return isLegalT2ScaledAddressingMode(AM, VT);
5886
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005887 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00005888 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00005889 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 case MVT::i1:
5891 case MVT::i8:
5892 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005893 if (Scale < 0) Scale = -Scale;
5894 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005895 return true;
5896 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00005897 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00005899 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005900 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005901 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005902 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00005903 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005904
Owen Anderson825b72b2009-08-11 20:47:22 +00005905 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005906 // Note, we allow "void" uses (basically, uses that aren't loads or
5907 // stores), because arm allows folding a scale into many arithmetic
5908 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005909
Chris Lattner37caf8c2007-04-09 23:33:39 +00005910 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00005911 if (Scale & 1) return false;
5912 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00005913 }
5914 break;
Evan Chengb01fad62007-03-12 23:30:29 +00005915 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00005916 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00005917}
5918
Evan Cheng77e47512009-11-11 19:05:52 +00005919/// isLegalICmpImmediate - Return true if the specified immediate is legal
5920/// icmp immediate, that is the target has icmp instructions which can compare
5921/// a register against the immediate without having to materialize the
5922/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00005923bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00005924 if (!Subtarget->isThumb())
5925 return ARM_AM::getSOImmVal(Imm) != -1;
5926 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00005927 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00005928 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00005929}
5930
Owen Andersone50ed302009-08-10 22:56:29 +00005931static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005932 bool isSEXTLoad, SDValue &Base,
5933 SDValue &Offset, bool &isInc,
5934 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00005935 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5936 return false;
5937
Owen Anderson825b72b2009-08-11 20:47:22 +00005938 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005939 // AddressingMode 3
5940 Base = Ptr->getOperand(0);
5941 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005942 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005943 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005944 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005945 isInc = false;
5946 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5947 return true;
5948 }
5949 }
5950 isInc = (Ptr->getOpcode() == ISD::ADD);
5951 Offset = Ptr->getOperand(1);
5952 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005953 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005954 // AddressingMode 2
5955 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005956 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005957 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005958 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005959 isInc = false;
5960 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5961 Base = Ptr->getOperand(0);
5962 return true;
5963 }
5964 }
5965
5966 if (Ptr->getOpcode() == ISD::ADD) {
5967 isInc = true;
5968 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5969 if (ShOpcVal != ARM_AM::no_shift) {
5970 Base = Ptr->getOperand(1);
5971 Offset = Ptr->getOperand(0);
5972 } else {
5973 Base = Ptr->getOperand(0);
5974 Offset = Ptr->getOperand(1);
5975 }
5976 return true;
5977 }
5978
5979 isInc = (Ptr->getOpcode() == ISD::ADD);
5980 Base = Ptr->getOperand(0);
5981 Offset = Ptr->getOperand(1);
5982 return true;
5983 }
5984
Jim Grosbache5165492009-11-09 00:11:35 +00005985 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005986 return false;
5987}
5988
Owen Andersone50ed302009-08-10 22:56:29 +00005989static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005990 bool isSEXTLoad, SDValue &Base,
5991 SDValue &Offset, bool &isInc,
5992 SelectionDAG &DAG) {
5993 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5994 return false;
5995
5996 Base = Ptr->getOperand(0);
5997 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5998 int RHSC = (int)RHS->getZExtValue();
5999 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6000 assert(Ptr->getOpcode() == ISD::ADD);
6001 isInc = false;
6002 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6003 return true;
6004 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6005 isInc = Ptr->getOpcode() == ISD::ADD;
6006 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6007 return true;
6008 }
6009 }
6010
6011 return false;
6012}
6013
Evan Chenga8e29892007-01-19 07:51:42 +00006014/// getPreIndexedAddressParts - returns true by value, base pointer and
6015/// offset pointer and addressing mode by reference if the node's address
6016/// can be legally represented as pre-indexed load / store address.
6017bool
Dan Gohman475871a2008-07-27 21:46:04 +00006018ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6019 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006020 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006021 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006022 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006023 return false;
6024
Owen Andersone50ed302009-08-10 22:56:29 +00006025 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006026 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006027 bool isSEXTLoad = false;
6028 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6029 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006030 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006031 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6032 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6033 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006034 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006035 } else
6036 return false;
6037
6038 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006039 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006040 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006041 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6042 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006043 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006044 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006045 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006046 if (!isLegal)
6047 return false;
6048
6049 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6050 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006051}
6052
6053/// getPostIndexedAddressParts - returns true by value, base pointer and
6054/// offset pointer and addressing mode by reference if this node can be
6055/// combined with a load / store to form a post-indexed load / store.
6056bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006057 SDValue &Base,
6058 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006059 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006060 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006061 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006062 return false;
6063
Owen Andersone50ed302009-08-10 22:56:29 +00006064 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006065 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006066 bool isSEXTLoad = false;
6067 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006068 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006069 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006070 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6071 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006072 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006073 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006074 } else
6075 return false;
6076
6077 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006078 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006079 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006080 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006081 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006082 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006083 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6084 isInc, DAG);
6085 if (!isLegal)
6086 return false;
6087
Evan Cheng28dad2a2010-05-18 21:31:17 +00006088 if (Ptr != Base) {
6089 // Swap base ptr and offset to catch more post-index load / store when
6090 // it's legal. In Thumb2 mode, offset must be an immediate.
6091 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6092 !Subtarget->isThumb2())
6093 std::swap(Base, Offset);
6094
6095 // Post-indexed load / store update the base pointer.
6096 if (Ptr != Base)
6097 return false;
6098 }
6099
Evan Chenge88d5ce2009-07-02 07:28:31 +00006100 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6101 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006102}
6103
Dan Gohman475871a2008-07-27 21:46:04 +00006104void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006105 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006106 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006107 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006108 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006109 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006110 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006111 switch (Op.getOpcode()) {
6112 default: break;
6113 case ARMISD::CMOV: {
6114 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006115 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006116 if (KnownZero == 0 && KnownOne == 0) return;
6117
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006118 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006119 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6120 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006121 KnownZero &= KnownZeroRHS;
6122 KnownOne &= KnownOneRHS;
6123 return;
6124 }
6125 }
6126}
6127
6128//===----------------------------------------------------------------------===//
6129// ARM Inline Assembly Support
6130//===----------------------------------------------------------------------===//
6131
Evan Cheng55d42002011-01-08 01:24:27 +00006132bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6133 // Looking for "rev" which is V6+.
6134 if (!Subtarget->hasV6Ops())
6135 return false;
6136
6137 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6138 std::string AsmStr = IA->getAsmString();
6139 SmallVector<StringRef, 4> AsmPieces;
6140 SplitString(AsmStr, AsmPieces, ";\n");
6141
6142 switch (AsmPieces.size()) {
6143 default: return false;
6144 case 1:
6145 AsmStr = AsmPieces[0];
6146 AsmPieces.clear();
6147 SplitString(AsmStr, AsmPieces, " \t,");
6148
6149 // rev $0, $1
6150 if (AsmPieces.size() == 3 &&
6151 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6152 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6153 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6154 if (Ty && Ty->getBitWidth() == 32)
6155 return IntrinsicLowering::LowerToByteSwap(CI);
6156 }
6157 break;
6158 }
6159
6160 return false;
6161}
6162
Evan Chenga8e29892007-01-19 07:51:42 +00006163/// getConstraintType - Given a constraint letter, return the type of
6164/// constraint it is for this target.
6165ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006166ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6167 if (Constraint.size() == 1) {
6168 switch (Constraint[0]) {
6169 default: break;
6170 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006171 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006172 }
Evan Chenga8e29892007-01-19 07:51:42 +00006173 }
Chris Lattner4234f572007-03-25 02:14:49 +00006174 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006175}
6176
John Thompson44ab89e2010-10-29 17:29:13 +00006177/// Examine constraint type and operand type and determine a weight value.
6178/// This object must already have been set up with the operand type
6179/// and the current alternative constraint selected.
6180TargetLowering::ConstraintWeight
6181ARMTargetLowering::getSingleConstraintMatchWeight(
6182 AsmOperandInfo &info, const char *constraint) const {
6183 ConstraintWeight weight = CW_Invalid;
6184 Value *CallOperandVal = info.CallOperandVal;
6185 // If we don't have a value, we can't do a match,
6186 // but allow it at the lowest weight.
6187 if (CallOperandVal == NULL)
6188 return CW_Default;
6189 const Type *type = CallOperandVal->getType();
6190 // Look at the constraint type.
6191 switch (*constraint) {
6192 default:
6193 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6194 break;
6195 case 'l':
6196 if (type->isIntegerTy()) {
6197 if (Subtarget->isThumb())
6198 weight = CW_SpecificReg;
6199 else
6200 weight = CW_Register;
6201 }
6202 break;
6203 case 'w':
6204 if (type->isFloatingPointTy())
6205 weight = CW_Register;
6206 break;
6207 }
6208 return weight;
6209}
6210
Bob Wilson2dc4f542009-03-20 22:42:55 +00006211std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006212ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006213 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006214 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006215 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006216 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006217 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006218 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006219 return std::make_pair(0U, ARM::tGPRRegisterClass);
6220 else
6221 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006222 case 'r':
6223 return std::make_pair(0U, ARM::GPRRegisterClass);
6224 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006225 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006226 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006227 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006228 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006229 if (VT.getSizeInBits() == 128)
6230 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006231 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006232 }
6233 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006234 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006235 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006236
Evan Chenga8e29892007-01-19 07:51:42 +00006237 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6238}
6239
6240std::vector<unsigned> ARMTargetLowering::
6241getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006242 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006243 if (Constraint.size() != 1)
6244 return std::vector<unsigned>();
6245
6246 switch (Constraint[0]) { // GCC ARM Constraint Letters
6247 default: break;
6248 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006249 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6250 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6251 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006252 case 'r':
6253 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6254 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6255 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6256 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006257 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006258 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006259 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6260 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6261 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6262 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6263 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6264 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6265 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6266 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006267 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006268 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6269 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6270 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6271 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006272 if (VT.getSizeInBits() == 128)
6273 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6274 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006275 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006276 }
6277
6278 return std::vector<unsigned>();
6279}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006280
6281/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6282/// vector. If it is invalid, don't add anything to Ops.
6283void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6284 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006285 std::vector<SDValue>&Ops,
6286 SelectionDAG &DAG) const {
6287 SDValue Result(0, 0);
6288
6289 switch (Constraint) {
6290 default: break;
6291 case 'I': case 'J': case 'K': case 'L':
6292 case 'M': case 'N': case 'O':
6293 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6294 if (!C)
6295 return;
6296
6297 int64_t CVal64 = C->getSExtValue();
6298 int CVal = (int) CVal64;
6299 // None of these constraints allow values larger than 32 bits. Check
6300 // that the value fits in an int.
6301 if (CVal != CVal64)
6302 return;
6303
6304 switch (Constraint) {
6305 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006306 if (Subtarget->isThumb1Only()) {
6307 // This must be a constant between 0 and 255, for ADD
6308 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006309 if (CVal >= 0 && CVal <= 255)
6310 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006311 } else if (Subtarget->isThumb2()) {
6312 // A constant that can be used as an immediate value in a
6313 // data-processing instruction.
6314 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6315 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006316 } else {
6317 // A constant that can be used as an immediate value in a
6318 // data-processing instruction.
6319 if (ARM_AM::getSOImmVal(CVal) != -1)
6320 break;
6321 }
6322 return;
6323
6324 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006325 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006326 // This must be a constant between -255 and -1, for negated ADD
6327 // immediates. This can be used in GCC with an "n" modifier that
6328 // prints the negated value, for use with SUB instructions. It is
6329 // not useful otherwise but is implemented for compatibility.
6330 if (CVal >= -255 && CVal <= -1)
6331 break;
6332 } else {
6333 // This must be a constant between -4095 and 4095. It is not clear
6334 // what this constraint is intended for. Implemented for
6335 // compatibility with GCC.
6336 if (CVal >= -4095 && CVal <= 4095)
6337 break;
6338 }
6339 return;
6340
6341 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006342 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006343 // A 32-bit value where only one byte has a nonzero value. Exclude
6344 // zero to match GCC. This constraint is used by GCC internally for
6345 // constants that can be loaded with a move/shift combination.
6346 // It is not useful otherwise but is implemented for compatibility.
6347 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
6348 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006349 } else if (Subtarget->isThumb2()) {
6350 // A constant whose bitwise inverse can be used as an immediate
6351 // value in a data-processing instruction. This can be used in GCC
6352 // with a "B" modifier that prints the inverted value, for use with
6353 // BIC and MVN instructions. It is not useful otherwise but is
6354 // implemented for compatibility.
6355 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
6356 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006357 } else {
6358 // A constant whose bitwise inverse can be used as an immediate
6359 // value in a data-processing instruction. This can be used in GCC
6360 // with a "B" modifier that prints the inverted value, for use with
6361 // BIC and MVN instructions. It is not useful otherwise but is
6362 // implemented for compatibility.
6363 if (ARM_AM::getSOImmVal(~CVal) != -1)
6364 break;
6365 }
6366 return;
6367
6368 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006369 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006370 // This must be a constant between -7 and 7,
6371 // for 3-operand ADD/SUB immediate instructions.
6372 if (CVal >= -7 && CVal < 7)
6373 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006374 } else if (Subtarget->isThumb2()) {
6375 // A constant whose negation can be used as an immediate value in a
6376 // data-processing instruction. This can be used in GCC with an "n"
6377 // modifier that prints the negated value, for use with SUB
6378 // instructions. It is not useful otherwise but is implemented for
6379 // compatibility.
6380 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
6381 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006382 } else {
6383 // A constant whose negation can be used as an immediate value in a
6384 // data-processing instruction. This can be used in GCC with an "n"
6385 // modifier that prints the negated value, for use with SUB
6386 // instructions. It is not useful otherwise but is implemented for
6387 // compatibility.
6388 if (ARM_AM::getSOImmVal(-CVal) != -1)
6389 break;
6390 }
6391 return;
6392
6393 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006394 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006395 // This must be a multiple of 4 between 0 and 1020, for
6396 // ADD sp + immediate.
6397 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
6398 break;
6399 } else {
6400 // A power of two or a constant between 0 and 32. This is used in
6401 // GCC for the shift amount on shifted register operands, but it is
6402 // useful in general for any shift amounts.
6403 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
6404 break;
6405 }
6406 return;
6407
6408 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006409 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006410 // This must be a constant between 0 and 31, for shift amounts.
6411 if (CVal >= 0 && CVal <= 31)
6412 break;
6413 }
6414 return;
6415
6416 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006417 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006418 // This must be a multiple of 4 between -508 and 508, for
6419 // ADD/SUB sp = sp + immediate.
6420 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
6421 break;
6422 }
6423 return;
6424 }
6425 Result = DAG.getTargetConstant(CVal, Op.getValueType());
6426 break;
6427 }
6428
6429 if (Result.getNode()) {
6430 Ops.push_back(Result);
6431 return;
6432 }
Dale Johannesen1784d162010-06-25 21:55:36 +00006433 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006434}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00006435
6436bool
6437ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
6438 // The ARM target isn't yet aware of offsets.
6439 return false;
6440}
Evan Cheng39382422009-10-28 01:44:26 +00006441
6442int ARM::getVFPf32Imm(const APFloat &FPImm) {
6443 APInt Imm = FPImm.bitcastToAPInt();
6444 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
6445 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
6446 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
6447
6448 // We can handle 4 bits of mantissa.
6449 // mantissa = (16+UInt(e:f:g:h))/16.
6450 if (Mantissa & 0x7ffff)
6451 return -1;
6452 Mantissa >>= 19;
6453 if ((Mantissa & 0xf) != Mantissa)
6454 return -1;
6455
6456 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6457 if (Exp < -3 || Exp > 4)
6458 return -1;
6459 Exp = ((Exp+3) & 0x7) ^ 4;
6460
6461 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6462}
6463
6464int ARM::getVFPf64Imm(const APFloat &FPImm) {
6465 APInt Imm = FPImm.bitcastToAPInt();
6466 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
6467 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
6468 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
6469
6470 // We can handle 4 bits of mantissa.
6471 // mantissa = (16+UInt(e:f:g:h))/16.
6472 if (Mantissa & 0xffffffffffffLL)
6473 return -1;
6474 Mantissa >>= 48;
6475 if ((Mantissa & 0xf) != Mantissa)
6476 return -1;
6477
6478 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6479 if (Exp < -3 || Exp > 4)
6480 return -1;
6481 Exp = ((Exp+3) & 0x7) ^ 4;
6482
6483 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6484}
6485
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006486bool ARM::isBitFieldInvertedMask(unsigned v) {
6487 if (v == 0xffffffff)
6488 return 0;
6489 // there can be 1's on either or both "outsides", all the "inside"
6490 // bits must be 0's
6491 unsigned int lsb = 0, msb = 31;
6492 while (v & (1 << msb)) --msb;
6493 while (v & (1 << lsb)) ++lsb;
6494 for (unsigned int i = lsb; i <= msb; ++i) {
6495 if (v & (1 << i))
6496 return 0;
6497 }
6498 return 1;
6499}
6500
Evan Cheng39382422009-10-28 01:44:26 +00006501/// isFPImmLegal - Returns true if the target can instruction select the
6502/// specified FP immediate natively. If false, the legalizer will
6503/// materialize the FP immediate as a load from a constant pool.
6504bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
6505 if (!Subtarget->hasVFP3())
6506 return false;
6507 if (VT == MVT::f32)
6508 return ARM::getVFPf32Imm(Imm) != -1;
6509 if (VT == MVT::f64)
6510 return ARM::getVFPf64Imm(Imm) != -1;
6511 return false;
6512}
Bob Wilson65ffec42010-09-21 17:56:22 +00006513
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006514/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00006515/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
6516/// specified in the intrinsic calls.
6517bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
6518 const CallInst &I,
6519 unsigned Intrinsic) const {
6520 switch (Intrinsic) {
6521 case Intrinsic::arm_neon_vld1:
6522 case Intrinsic::arm_neon_vld2:
6523 case Intrinsic::arm_neon_vld3:
6524 case Intrinsic::arm_neon_vld4:
6525 case Intrinsic::arm_neon_vld2lane:
6526 case Intrinsic::arm_neon_vld3lane:
6527 case Intrinsic::arm_neon_vld4lane: {
6528 Info.opc = ISD::INTRINSIC_W_CHAIN;
6529 // Conservatively set memVT to the entire set of vectors loaded.
6530 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
6531 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6532 Info.ptrVal = I.getArgOperand(0);
6533 Info.offset = 0;
6534 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6535 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6536 Info.vol = false; // volatile loads with NEON intrinsics not supported
6537 Info.readMem = true;
6538 Info.writeMem = false;
6539 return true;
6540 }
6541 case Intrinsic::arm_neon_vst1:
6542 case Intrinsic::arm_neon_vst2:
6543 case Intrinsic::arm_neon_vst3:
6544 case Intrinsic::arm_neon_vst4:
6545 case Intrinsic::arm_neon_vst2lane:
6546 case Intrinsic::arm_neon_vst3lane:
6547 case Intrinsic::arm_neon_vst4lane: {
6548 Info.opc = ISD::INTRINSIC_VOID;
6549 // Conservatively set memVT to the entire set of vectors stored.
6550 unsigned NumElts = 0;
6551 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
6552 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
6553 if (!ArgTy->isVectorTy())
6554 break;
6555 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
6556 }
6557 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6558 Info.ptrVal = I.getArgOperand(0);
6559 Info.offset = 0;
6560 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6561 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6562 Info.vol = false; // volatile stores with NEON intrinsics not supported
6563 Info.readMem = false;
6564 Info.writeMem = true;
6565 return true;
6566 }
6567 default:
6568 break;
6569 }
6570
6571 return false;
6572}