blob: 11ff6bf9654c49098dfbb90ff311549dfdf7ab90 [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);
Nate Begeman7973f352011-02-11 20:53:29 +0000457 // Custom handling for some vector types to avoid expensive expansions
458 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
459 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
460 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
461 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000462 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
463 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000464 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
465 // a destination type that is wider than the source.
466 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
467 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000468
Bob Wilson1c3ef902011-02-07 17:43:21 +0000469 setTargetDAGCombine(ISD::INTRINSIC_VOID);
470 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000471 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
472 setTargetDAGCombine(ISD::SHL);
473 setTargetDAGCombine(ISD::SRL);
474 setTargetDAGCombine(ISD::SRA);
475 setTargetDAGCombine(ISD::SIGN_EXTEND);
476 setTargetDAGCombine(ISD::ZERO_EXTEND);
477 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000478 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000479 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000480 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000481 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
482 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000483 }
484
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000485 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000486
487 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000489
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000490 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000492
Evan Chenga8e29892007-01-19 07:51:42 +0000493 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000494 if (!Subtarget->isThumb1Only()) {
495 for (unsigned im = (unsigned)ISD::PRE_INC;
496 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 setIndexedLoadAction(im, MVT::i1, Legal);
498 setIndexedLoadAction(im, MVT::i8, Legal);
499 setIndexedLoadAction(im, MVT::i16, Legal);
500 setIndexedLoadAction(im, MVT::i32, Legal);
501 setIndexedStoreAction(im, MVT::i1, Legal);
502 setIndexedStoreAction(im, MVT::i8, Legal);
503 setIndexedStoreAction(im, MVT::i16, Legal);
504 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000505 }
Evan Chenga8e29892007-01-19 07:51:42 +0000506 }
507
508 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000509 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::MUL, MVT::i64, Expand);
511 setOperationAction(ISD::MULHU, MVT::i32, Expand);
512 setOperationAction(ISD::MULHS, MVT::i32, Expand);
513 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
514 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000515 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::MUL, MVT::i64, Expand);
517 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000518 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000520 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000521 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000522 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000523 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::SRL, MVT::i64, Custom);
525 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000526
527 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000529 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000531 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000533
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000534 // Only ARMv6 has BSWAP.
535 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000537
Evan Chenga8e29892007-01-19 07:51:42 +0000538 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000539 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000540 // v7M has a hardware divider
541 setOperationAction(ISD::SDIV, MVT::i32, Expand);
542 setOperationAction(ISD::UDIV, MVT::i32, Expand);
543 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::SREM, MVT::i32, Expand);
545 setOperationAction(ISD::UREM, MVT::i32, Expand);
546 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
547 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000548
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
550 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
551 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
552 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000553 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000554
Evan Chengfb3611d2010-05-11 07:26:32 +0000555 setOperationAction(ISD::TRAP, MVT::Other, Legal);
556
Evan Chenga8e29892007-01-19 07:51:42 +0000557 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::VASTART, MVT::Other, Custom);
559 setOperationAction(ISD::VAARG, MVT::Other, Expand);
560 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
561 setOperationAction(ISD::VAEND, MVT::Other, Expand);
562 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
563 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000564 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000565 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
566 setExceptionPointerRegister(ARM::R0);
567 setExceptionSelectorRegister(ARM::R1);
568
Evan Cheng3a1588a2010-04-15 22:20:34 +0000569 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000570 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
571 // the default expansion.
572 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000573 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000574 // membarrier needs custom lowering; the rest are legal and handled
575 // normally.
576 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
577 } else {
578 // Set them all for expansion, which will force libcalls.
579 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
580 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
581 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
582 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000583 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
584 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
585 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000586 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
587 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
588 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
589 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
590 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
591 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
592 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
593 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
594 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
595 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
596 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
597 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000604 // Since the libcalls include locking, fold in the fences
605 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000606 }
607 // 64-bit versions are always libcalls (for now)
608 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000609 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000610 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
612 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
613 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000616
Evan Cheng416941d2010-11-04 05:19:35 +0000617 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000618
Eli Friedmana2c6f452010-06-26 04:36:50 +0000619 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
620 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
622 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000623 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000625
Nate Begemand1fb5832010-08-03 21:31:55 +0000626 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000627 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
628 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000629 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000630 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
631 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000632
633 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000635 if (Subtarget->isTargetDarwin()) {
636 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
637 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000638 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000639 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000640
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 setOperationAction(ISD::SETCC, MVT::i32, Expand);
642 setOperationAction(ISD::SETCC, MVT::f32, Expand);
643 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000644 setOperationAction(ISD::SELECT, MVT::i32, Custom);
645 setOperationAction(ISD::SELECT, MVT::f32, Custom);
646 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
648 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
649 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000650
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
652 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
653 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
654 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
655 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000656
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000657 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::FSIN, MVT::f64, Expand);
659 setOperationAction(ISD::FSIN, MVT::f32, Expand);
660 setOperationAction(ISD::FCOS, MVT::f32, Expand);
661 setOperationAction(ISD::FCOS, MVT::f64, Expand);
662 setOperationAction(ISD::FREM, MVT::f64, Expand);
663 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000664 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
666 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000667 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setOperationAction(ISD::FPOW, MVT::f64, Expand);
669 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000670
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000671 // Various VFP goodness
672 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000673 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
674 if (Subtarget->hasVFP2()) {
675 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
676 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
677 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
678 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
679 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000680 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000681 if (!Subtarget->hasFP16()) {
682 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
683 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000684 }
Evan Cheng110cf482008-04-01 01:50:16 +0000685 }
Evan Chenga8e29892007-01-19 07:51:42 +0000686
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000687 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000688 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000689 setTargetDAGCombine(ISD::ADD);
690 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000691 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000692
Owen Anderson080c0922010-11-05 19:27:46 +0000693 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000694 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000695 if (Subtarget->hasNEON())
696 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000697
Evan Chenga8e29892007-01-19 07:51:42 +0000698 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000699
Evan Chengf7d87ee2010-05-21 00:43:17 +0000700 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
701 setSchedulingPreference(Sched::RegPressure);
702 else
703 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000704
Evan Cheng05219282011-01-06 06:52:41 +0000705 //// temporary - rewrite interface to use type
706 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000707
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000708 // On ARM arguments smaller than 4 bytes are extended, so all arguments
709 // are at least 4 bytes aligned.
710 setMinStackArgumentAlignment(4);
711
Evan Chengfff606d2010-09-24 19:07:23 +0000712 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000713}
714
Andrew Trick32cec0a2011-01-19 02:35:27 +0000715// FIXME: It might make sense to define the representative register class as the
716// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
717// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
718// SPR's representative would be DPR_VFP2. This should work well if register
719// pressure tracking were modified such that a register use would increment the
720// pressure of the register class's representative and all of it's super
721// classes' representatives transitively. We have not implemented this because
722// of the difficulty prior to coalescing of modeling operand register classes
723// due to the common occurence of cross class copies and subregister insertions
724// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000725std::pair<const TargetRegisterClass*, uint8_t>
726ARMTargetLowering::findRepresentativeClass(EVT VT) const{
727 const TargetRegisterClass *RRC = 0;
728 uint8_t Cost = 1;
729 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000730 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000731 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000732 // Use DPR as representative register class for all floating point
733 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
734 // the cost is 1 for both f32 and f64.
735 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000736 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000737 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000738 // When NEON is used for SP, only half of the register file is available
739 // because operations that define both SP and DP results will be constrained
740 // to the VFP2 class (D0-D15). We currently model this constraint prior to
741 // coalescing by double-counting the SP regs. See the FIXME above.
742 if (Subtarget->useNEONForSinglePrecisionFP())
743 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000744 break;
745 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
746 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000747 RRC = ARM::DPRRegisterClass;
748 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000749 break;
750 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000751 RRC = ARM::DPRRegisterClass;
752 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000753 break;
754 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000755 RRC = ARM::DPRRegisterClass;
756 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000757 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000758 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000759 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000760}
761
Evan Chenga8e29892007-01-19 07:51:42 +0000762const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
763 switch (Opcode) {
764 default: return 0;
765 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000766 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000767 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000768 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
769 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000770 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000771 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
772 case ARMISD::tCALL: return "ARMISD::tCALL";
773 case ARMISD::BRCOND: return "ARMISD::BRCOND";
774 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000775 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000776 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
777 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
778 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000779 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000780 case ARMISD::CMPFP: return "ARMISD::CMPFP";
781 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000782 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000783 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
784 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000785
Jim Grosbach3482c802010-01-18 19:58:49 +0000786 case ARMISD::RBIT: return "ARMISD::RBIT";
787
Bob Wilson76a312b2010-03-19 22:51:32 +0000788 case ARMISD::FTOSI: return "ARMISD::FTOSI";
789 case ARMISD::FTOUI: return "ARMISD::FTOUI";
790 case ARMISD::SITOF: return "ARMISD::SITOF";
791 case ARMISD::UITOF: return "ARMISD::UITOF";
792
Evan Chenga8e29892007-01-19 07:51:42 +0000793 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
794 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
795 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000796
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000797 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
798 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000799
Evan Chengc5942082009-10-28 06:55:03 +0000800 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
801 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000802 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000803
Dale Johannesen51e28e62010-06-03 21:09:53 +0000804 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000805
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000806 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000807
Evan Cheng86198642009-08-07 00:34:42 +0000808 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
809
Jim Grosbach3728e962009-12-10 00:11:09 +0000810 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000811 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000812
Evan Chengdfed19f2010-11-03 06:34:55 +0000813 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
814
Bob Wilson5bafff32009-06-22 23:27:02 +0000815 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000816 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000817 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000818 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
819 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000820 case ARMISD::VCGEU: return "ARMISD::VCGEU";
821 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000822 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
823 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000824 case ARMISD::VCGTU: return "ARMISD::VCGTU";
825 case ARMISD::VTST: return "ARMISD::VTST";
826
827 case ARMISD::VSHL: return "ARMISD::VSHL";
828 case ARMISD::VSHRs: return "ARMISD::VSHRs";
829 case ARMISD::VSHRu: return "ARMISD::VSHRu";
830 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
831 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
832 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
833 case ARMISD::VSHRN: return "ARMISD::VSHRN";
834 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
835 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
836 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
837 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
838 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
839 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
840 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
841 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
842 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
843 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
844 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
845 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
846 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
847 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000848 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000849 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000850 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000851 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000852 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000853 case ARMISD::VREV64: return "ARMISD::VREV64";
854 case ARMISD::VREV32: return "ARMISD::VREV32";
855 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000856 case ARMISD::VZIP: return "ARMISD::VZIP";
857 case ARMISD::VUZP: return "ARMISD::VUZP";
858 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000859 case ARMISD::VTBL1: return "ARMISD::VTBL1";
860 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000861 case ARMISD::VMULLs: return "ARMISD::VMULLs";
862 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000863 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000864 case ARMISD::FMAX: return "ARMISD::FMAX";
865 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000866 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000867 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
868 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000869 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000870 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
871 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
872 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000873 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
874 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
875 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
876 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
877 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
878 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
879 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
880 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
881 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
882 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
883 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
884 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
885 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
886 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
887 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
888 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
889 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000890 }
891}
892
Evan Cheng06b666c2010-05-15 02:18:07 +0000893/// getRegClassFor - Return the register class that should be used for the
894/// specified value type.
895TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
896 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
897 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
898 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000899 if (Subtarget->hasNEON()) {
900 if (VT == MVT::v4i64)
901 return ARM::QQPRRegisterClass;
902 else if (VT == MVT::v8i64)
903 return ARM::QQQQPRRegisterClass;
904 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000905 return TargetLowering::getRegClassFor(VT);
906}
907
Eric Christopherab695882010-07-21 22:26:11 +0000908// Create a fast isel object.
909FastISel *
910ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
911 return ARM::createFastISel(funcInfo);
912}
913
Bill Wendlingb4202b82009-07-01 18:50:55 +0000914/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000915unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000916 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000917}
918
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000919/// getMaximalGlobalOffset - Returns the maximal possible offset which can
920/// be used for loads / stores from the global.
921unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
922 return (Subtarget->isThumb1Only() ? 127 : 4095);
923}
924
Evan Cheng1cc39842010-05-20 23:26:43 +0000925Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000926 unsigned NumVals = N->getNumValues();
927 if (!NumVals)
928 return Sched::RegPressure;
929
930 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000931 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000932 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000933 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000934 if (VT.isFloatingPoint() || VT.isVector())
935 return Sched::Latency;
936 }
Evan Chengc10f5432010-05-28 23:25:23 +0000937
938 if (!N->isMachineOpcode())
939 return Sched::RegPressure;
940
941 // Load are scheduled for latency even if there instruction itinerary
942 // is not available.
943 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
944 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000945
946 if (TID.getNumDefs() == 0)
947 return Sched::RegPressure;
948 if (!Itins->isEmpty() &&
949 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000950 return Sched::Latency;
951
Evan Cheng1cc39842010-05-20 23:26:43 +0000952 return Sched::RegPressure;
953}
954
Evan Chenga8e29892007-01-19 07:51:42 +0000955//===----------------------------------------------------------------------===//
956// Lowering Code
957//===----------------------------------------------------------------------===//
958
Evan Chenga8e29892007-01-19 07:51:42 +0000959/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
960static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
961 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000962 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000963 case ISD::SETNE: return ARMCC::NE;
964 case ISD::SETEQ: return ARMCC::EQ;
965 case ISD::SETGT: return ARMCC::GT;
966 case ISD::SETGE: return ARMCC::GE;
967 case ISD::SETLT: return ARMCC::LT;
968 case ISD::SETLE: return ARMCC::LE;
969 case ISD::SETUGT: return ARMCC::HI;
970 case ISD::SETUGE: return ARMCC::HS;
971 case ISD::SETULT: return ARMCC::LO;
972 case ISD::SETULE: return ARMCC::LS;
973 }
974}
975
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000976/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
977static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000978 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000979 CondCode2 = ARMCC::AL;
980 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000981 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000982 case ISD::SETEQ:
983 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
984 case ISD::SETGT:
985 case ISD::SETOGT: CondCode = ARMCC::GT; break;
986 case ISD::SETGE:
987 case ISD::SETOGE: CondCode = ARMCC::GE; break;
988 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000989 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000990 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
991 case ISD::SETO: CondCode = ARMCC::VC; break;
992 case ISD::SETUO: CondCode = ARMCC::VS; break;
993 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
994 case ISD::SETUGT: CondCode = ARMCC::HI; break;
995 case ISD::SETUGE: CondCode = ARMCC::PL; break;
996 case ISD::SETLT:
997 case ISD::SETULT: CondCode = ARMCC::LT; break;
998 case ISD::SETLE:
999 case ISD::SETULE: CondCode = ARMCC::LE; break;
1000 case ISD::SETNE:
1001 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1002 }
Evan Chenga8e29892007-01-19 07:51:42 +00001003}
1004
Bob Wilson1f595bb2009-04-17 19:07:39 +00001005//===----------------------------------------------------------------------===//
1006// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001007//===----------------------------------------------------------------------===//
1008
1009#include "ARMGenCallingConv.inc"
1010
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001011/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1012/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001013CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001014 bool Return,
1015 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001016 switch (CC) {
1017 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001018 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001019 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001020 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001021 if (!Subtarget->isAAPCS_ABI())
1022 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1023 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1024 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1025 }
1026 // Fallthrough
1027 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001028 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001029 if (!Subtarget->isAAPCS_ABI())
1030 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1031 else if (Subtarget->hasVFP2() &&
1032 FloatABIType == FloatABI::Hard && !isVarArg)
1033 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1034 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1035 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001036 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001037 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001038 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001039 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001040 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001041 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001042 }
1043}
1044
Dan Gohman98ca4f22009-08-05 01:29:28 +00001045/// LowerCallResult - Lower the result values of a call into the
1046/// appropriate copies out of appropriate physical registers.
1047SDValue
1048ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001049 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001050 const SmallVectorImpl<ISD::InputArg> &Ins,
1051 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001052 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001053
Bob Wilson1f595bb2009-04-17 19:07:39 +00001054 // Assign locations to each value returned by this call.
1055 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001056 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001057 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001058 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001059 CCAssignFnForNode(CallConv, /* Return*/ true,
1060 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001061
1062 // Copy all of the result registers out of their specified physreg.
1063 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1064 CCValAssign VA = RVLocs[i];
1065
Bob Wilson80915242009-04-25 00:33:20 +00001066 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001067 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001068 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001069 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001070 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001071 Chain = Lo.getValue(1);
1072 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001073 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001074 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001075 InFlag);
1076 Chain = Hi.getValue(1);
1077 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001078 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001079
Owen Anderson825b72b2009-08-11 20:47:22 +00001080 if (VA.getLocVT() == MVT::v2f64) {
1081 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1082 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1083 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001084
1085 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001086 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001087 Chain = Lo.getValue(1);
1088 InFlag = Lo.getValue(2);
1089 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001090 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001091 Chain = Hi.getValue(1);
1092 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001093 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001094 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1095 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001096 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001097 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001098 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1099 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001100 Chain = Val.getValue(1);
1101 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001102 }
Bob Wilson80915242009-04-25 00:33:20 +00001103
1104 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001105 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001106 case CCValAssign::Full: break;
1107 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001108 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001109 break;
1110 }
1111
Dan Gohman98ca4f22009-08-05 01:29:28 +00001112 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001113 }
1114
Dan Gohman98ca4f22009-08-05 01:29:28 +00001115 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001116}
1117
1118/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1119/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001120/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001121/// a byval function parameter.
1122/// Sometimes what we are copying is the end of a larger object, the part that
1123/// does not fit in registers.
1124static SDValue
1125CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1126 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1127 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001128 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001130 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001131 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132}
1133
Bob Wilsondee46d72009-04-17 20:35:10 +00001134/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001136ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1137 SDValue StackPtr, SDValue Arg,
1138 DebugLoc dl, SelectionDAG &DAG,
1139 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001140 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 unsigned LocMemOffset = VA.getLocMemOffset();
1142 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1143 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001144 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001145 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001146
Bob Wilson1f595bb2009-04-17 19:07:39 +00001147 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001148 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001149 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001150}
1151
Dan Gohman98ca4f22009-08-05 01:29:28 +00001152void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001153 SDValue Chain, SDValue &Arg,
1154 RegsToPassVector &RegsToPass,
1155 CCValAssign &VA, CCValAssign &NextVA,
1156 SDValue &StackPtr,
1157 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001158 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001159
Jim Grosbache5165492009-11-09 00:11:35 +00001160 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001161 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001162 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1163
1164 if (NextVA.isRegLoc())
1165 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1166 else {
1167 assert(NextVA.isMemLoc());
1168 if (StackPtr.getNode() == 0)
1169 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1170
Dan Gohman98ca4f22009-08-05 01:29:28 +00001171 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1172 dl, DAG, NextVA,
1173 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001174 }
1175}
1176
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001178/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1179/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001181ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001182 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001183 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001184 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001185 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001186 const SmallVectorImpl<ISD::InputArg> &Ins,
1187 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001188 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001189 MachineFunction &MF = DAG.getMachineFunction();
1190 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1191 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001192 // Temporarily disable tail calls so things don't break.
1193 if (!EnableARMTailCalls)
1194 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001195 if (isTailCall) {
1196 // Check if it's really possible to do a tail call.
1197 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1198 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001199 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001200 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1201 // detected sibcalls.
1202 if (isTailCall) {
1203 ++NumTailCalls;
1204 IsSibCall = true;
1205 }
1206 }
Evan Chenga8e29892007-01-19 07:51:42 +00001207
Bob Wilson1f595bb2009-04-17 19:07:39 +00001208 // Analyze operands of the call, assigning locations to each operand.
1209 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001210 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1211 *DAG.getContext());
1212 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001213 CCAssignFnForNode(CallConv, /* Return*/ false,
1214 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001215
Bob Wilson1f595bb2009-04-17 19:07:39 +00001216 // Get a count of how many bytes are to be pushed on the stack.
1217 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001218
Dale Johannesen51e28e62010-06-03 21:09:53 +00001219 // For tail calls, memory operands are available in our caller's stack.
1220 if (IsSibCall)
1221 NumBytes = 0;
1222
Evan Chenga8e29892007-01-19 07:51:42 +00001223 // Adjust the stack pointer for the new arguments...
1224 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001225 if (!IsSibCall)
1226 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001227
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001228 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001229
Bob Wilson5bafff32009-06-22 23:27:02 +00001230 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001232
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001234 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001235 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1236 i != e;
1237 ++i, ++realArgIdx) {
1238 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001239 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001241 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001242
Bob Wilson1f595bb2009-04-17 19:07:39 +00001243 // Promote the value if needed.
1244 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001245 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001246 case CCValAssign::Full: break;
1247 case CCValAssign::SExt:
1248 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1249 break;
1250 case CCValAssign::ZExt:
1251 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1252 break;
1253 case CCValAssign::AExt:
1254 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1255 break;
1256 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001257 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001259 }
1260
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001261 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 if (VA.getLocVT() == MVT::v2f64) {
1264 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1265 DAG.getConstant(0, MVT::i32));
1266 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1267 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268
Dan Gohman98ca4f22009-08-05 01:29:28 +00001269 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001270 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1271
1272 VA = ArgLocs[++i]; // skip ahead to next loc
1273 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001274 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001275 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1276 } else {
1277 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001278
Dan Gohman98ca4f22009-08-05 01:29:28 +00001279 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1280 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001281 }
1282 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001284 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001285 }
1286 } else if (VA.isRegLoc()) {
1287 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsf222e592011-02-28 17:17:53 +00001288 } else if (!IsSibCall || isByVal) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001289 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001290
Dan Gohman98ca4f22009-08-05 01:29:28 +00001291 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1292 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001293 }
Evan Chenga8e29892007-01-19 07:51:42 +00001294 }
1295
1296 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001297 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001298 &MemOpChains[0], MemOpChains.size());
1299
1300 // Build a sequence of copy-to-reg nodes chained together with token chain
1301 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001302 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001303 // Tail call byval lowering might overwrite argument registers so in case of
1304 // tail call optimization the copies to registers are lowered later.
1305 if (!isTailCall)
1306 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1307 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1308 RegsToPass[i].second, InFlag);
1309 InFlag = Chain.getValue(1);
1310 }
Evan Chenga8e29892007-01-19 07:51:42 +00001311
Dale Johannesen51e28e62010-06-03 21:09:53 +00001312 // For tail calls lower the arguments to the 'real' stack slot.
1313 if (isTailCall) {
1314 // Force all the incoming stack arguments to be loaded from the stack
1315 // before any new outgoing arguments are stored to the stack, because the
1316 // outgoing stack slots may alias the incoming argument stack slots, and
1317 // the alias isn't otherwise explicit. This is slightly more conservative
1318 // than necessary, because it means that each store effectively depends
1319 // on every argument instead of just those arguments it would clobber.
1320
1321 // Do not flag preceeding copytoreg stuff together with the following stuff.
1322 InFlag = SDValue();
1323 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1324 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1325 RegsToPass[i].second, InFlag);
1326 InFlag = Chain.getValue(1);
1327 }
1328 InFlag =SDValue();
1329 }
1330
Bill Wendling056292f2008-09-16 21:48:12 +00001331 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1332 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1333 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001334 bool isDirect = false;
1335 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001336 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001337 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001338
1339 if (EnableARMLongCalls) {
1340 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1341 && "long-calls with non-static relocation model!");
1342 // Handle a global address or an external symbol. If it's not one of
1343 // those, the target's already in a register, so we don't need to do
1344 // anything extra.
1345 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001346 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001347 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001348 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001349 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1350 ARMPCLabelIndex,
1351 ARMCP::CPValue, 0);
1352 // Get the address of the callee into a register
1353 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1354 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1355 Callee = DAG.getLoad(getPointerTy(), dl,
1356 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001357 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001358 false, false, 0);
1359 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1360 const char *Sym = S->getSymbol();
1361
1362 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001363 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001364 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1365 Sym, ARMPCLabelIndex, 0);
1366 // Get the address of the callee into a register
1367 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1368 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1369 Callee = DAG.getLoad(getPointerTy(), dl,
1370 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001371 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001372 false, false, 0);
1373 }
1374 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001375 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001376 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001377 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001378 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001379 getTargetMachine().getRelocationModel() != Reloc::Static;
1380 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001381 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001382 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001383 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001384 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001385 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001386 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001387 ARMPCLabelIndex,
1388 ARMCP::CPValue, 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);
Bob Wilson2dc4f542009-03-20 22:42:55 +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 // On ELF targets for PIC code, direct calls should go through the PLT
1400 unsigned OpFlags = 0;
1401 if (Subtarget->isTargetELF() &&
1402 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1403 OpFlags = ARMII::MO_PLT;
1404 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1405 }
Bill Wendling056292f2008-09-16 21:48:12 +00001406 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001407 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001408 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001409 getTargetMachine().getRelocationModel() != Reloc::Static;
1410 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001411 // tBX takes a register source operand.
1412 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001413 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001414 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001415 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001416 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001417 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001418 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001419 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001420 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001421 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001422 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001423 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001424 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001425 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001426 } else {
1427 unsigned OpFlags = 0;
1428 // On ELF targets for PIC code, direct calls should go through the PLT
1429 if (Subtarget->isTargetELF() &&
1430 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1431 OpFlags = ARMII::MO_PLT;
1432 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1433 }
Evan Chenga8e29892007-01-19 07:51:42 +00001434 }
1435
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001436 // FIXME: handle tail calls differently.
1437 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001438 if (Subtarget->isThumb()) {
1439 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001440 CallOpc = ARMISD::CALL_NOLINK;
1441 else
1442 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1443 } else {
1444 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001445 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1446 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001447 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001448
Dan Gohman475871a2008-07-27 21:46:04 +00001449 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001450 Ops.push_back(Chain);
1451 Ops.push_back(Callee);
1452
1453 // Add argument registers to the end of the list so that they are known live
1454 // into the call.
1455 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1456 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1457 RegsToPass[i].second.getValueType()));
1458
Gabor Greifba36cb52008-08-28 21:40:38 +00001459 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001460 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001461
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001462 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001463 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001464 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001465
Duncan Sands4bdcb612008-07-02 17:40:58 +00001466 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001467 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001468 InFlag = Chain.getValue(1);
1469
Chris Lattnere563bbc2008-10-11 22:08:30 +00001470 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1471 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001472 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001473 InFlag = Chain.getValue(1);
1474
Bob Wilson1f595bb2009-04-17 19:07:39 +00001475 // Handle result values, copying them out of physregs into vregs that we
1476 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001477 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1478 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001479}
1480
Stuart Hastingsf222e592011-02-28 17:17:53 +00001481/// HandleByVal - Every parameter *after* a byval parameter is passed
1482/// on the stack. Confiscate all the parameter registers to insure
1483/// this.
1484void
1485llvm::ARMTargetLowering::HandleByVal(CCState *State) const {
1486 static const unsigned RegList1[] = {
1487 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1488 };
1489 do {} while (State->AllocateReg(RegList1, 4));
1490}
1491
Dale Johannesen51e28e62010-06-03 21:09:53 +00001492/// MatchingStackOffset - Return true if the given stack call argument is
1493/// already available in the same position (relatively) of the caller's
1494/// incoming argument stack.
1495static
1496bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1497 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1498 const ARMInstrInfo *TII) {
1499 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1500 int FI = INT_MAX;
1501 if (Arg.getOpcode() == ISD::CopyFromReg) {
1502 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001503 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001504 return false;
1505 MachineInstr *Def = MRI->getVRegDef(VR);
1506 if (!Def)
1507 return false;
1508 if (!Flags.isByVal()) {
1509 if (!TII->isLoadFromStackSlot(Def, FI))
1510 return false;
1511 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001512 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001513 }
1514 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1515 if (Flags.isByVal())
1516 // ByVal argument is passed in as a pointer but it's now being
1517 // dereferenced. e.g.
1518 // define @foo(%struct.X* %A) {
1519 // tail call @bar(%struct.X* byval %A)
1520 // }
1521 return false;
1522 SDValue Ptr = Ld->getBasePtr();
1523 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1524 if (!FINode)
1525 return false;
1526 FI = FINode->getIndex();
1527 } else
1528 return false;
1529
1530 assert(FI != INT_MAX);
1531 if (!MFI->isFixedObjectIndex(FI))
1532 return false;
1533 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1534}
1535
1536/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1537/// for tail call optimization. Targets which want to do tail call
1538/// optimization should implement this function.
1539bool
1540ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1541 CallingConv::ID CalleeCC,
1542 bool isVarArg,
1543 bool isCalleeStructRet,
1544 bool isCallerStructRet,
1545 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001546 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001547 const SmallVectorImpl<ISD::InputArg> &Ins,
1548 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001549 const Function *CallerF = DAG.getMachineFunction().getFunction();
1550 CallingConv::ID CallerCC = CallerF->getCallingConv();
1551 bool CCMatch = CallerCC == CalleeCC;
1552
1553 // Look for obvious safe cases to perform tail call optimization that do not
1554 // require ABI changes. This is what gcc calls sibcall.
1555
Jim Grosbach7616b642010-06-16 23:45:49 +00001556 // Do not sibcall optimize vararg calls unless the call site is not passing
1557 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001558 if (isVarArg && !Outs.empty())
1559 return false;
1560
1561 // Also avoid sibcall optimization if either caller or callee uses struct
1562 // return semantics.
1563 if (isCalleeStructRet || isCallerStructRet)
1564 return false;
1565
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001566 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001567 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001568 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1569 // LR. This means if we need to reload LR, it takes an extra instructions,
1570 // which outweighs the value of the tail call; but here we don't know yet
1571 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001572 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001573 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001574
1575 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1576 // but we need to make sure there are enough registers; the only valid
1577 // registers are the 4 used for parameters. We don't currently do this
1578 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001579 if (Subtarget->isThumb1Only())
1580 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001581
Dale Johannesen51e28e62010-06-03 21:09:53 +00001582 // If the calling conventions do not match, then we'd better make sure the
1583 // results are returned in the same way as what the caller expects.
1584 if (!CCMatch) {
1585 SmallVector<CCValAssign, 16> RVLocs1;
1586 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1587 RVLocs1, *DAG.getContext());
1588 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1589
1590 SmallVector<CCValAssign, 16> RVLocs2;
1591 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1592 RVLocs2, *DAG.getContext());
1593 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1594
1595 if (RVLocs1.size() != RVLocs2.size())
1596 return false;
1597 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1598 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1599 return false;
1600 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1601 return false;
1602 if (RVLocs1[i].isRegLoc()) {
1603 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1604 return false;
1605 } else {
1606 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1607 return false;
1608 }
1609 }
1610 }
1611
1612 // If the callee takes no arguments then go on to check the results of the
1613 // call.
1614 if (!Outs.empty()) {
1615 // Check if stack adjustment is needed. For now, do not do this if any
1616 // argument is passed on the stack.
1617 SmallVector<CCValAssign, 16> ArgLocs;
1618 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1619 ArgLocs, *DAG.getContext());
1620 CCInfo.AnalyzeCallOperands(Outs,
1621 CCAssignFnForNode(CalleeCC, false, isVarArg));
1622 if (CCInfo.getNextStackOffset()) {
1623 MachineFunction &MF = DAG.getMachineFunction();
1624
1625 // Check if the arguments are already laid out in the right way as
1626 // the caller's fixed stack objects.
1627 MachineFrameInfo *MFI = MF.getFrameInfo();
1628 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1629 const ARMInstrInfo *TII =
1630 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001631 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1632 i != e;
1633 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001634 CCValAssign &VA = ArgLocs[i];
1635 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001636 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001637 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001638 if (VA.getLocInfo() == CCValAssign::Indirect)
1639 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001640 if (VA.needsCustom()) {
1641 // f64 and vector types are split into multiple registers or
1642 // register/stack-slot combinations. The types will not match
1643 // the registers; give up on memory f64 refs until we figure
1644 // out what to do about this.
1645 if (!VA.isRegLoc())
1646 return false;
1647 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001648 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001649 if (RegVT == MVT::v2f64) {
1650 if (!ArgLocs[++i].isRegLoc())
1651 return false;
1652 if (!ArgLocs[++i].isRegLoc())
1653 return false;
1654 }
1655 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001656 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1657 MFI, MRI, TII))
1658 return false;
1659 }
1660 }
1661 }
1662 }
1663
1664 return true;
1665}
1666
Dan Gohman98ca4f22009-08-05 01:29:28 +00001667SDValue
1668ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001669 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001670 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001671 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001672 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001673
Bob Wilsondee46d72009-04-17 20:35:10 +00001674 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001675 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001676
Bob Wilsondee46d72009-04-17 20:35:10 +00001677 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001678 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1679 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001680
Dan Gohman98ca4f22009-08-05 01:29:28 +00001681 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001682 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1683 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001684
1685 // If this is the first return lowered for this function, add
1686 // the regs to the liveout set for the function.
1687 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1688 for (unsigned i = 0; i != RVLocs.size(); ++i)
1689 if (RVLocs[i].isRegLoc())
1690 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001691 }
1692
Bob Wilson1f595bb2009-04-17 19:07:39 +00001693 SDValue Flag;
1694
1695 // Copy the result values into the output registers.
1696 for (unsigned i = 0, realRVLocIdx = 0;
1697 i != RVLocs.size();
1698 ++i, ++realRVLocIdx) {
1699 CCValAssign &VA = RVLocs[i];
1700 assert(VA.isRegLoc() && "Can only return in registers!");
1701
Dan Gohmanc9403652010-07-07 15:54:55 +00001702 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001703
1704 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001705 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001706 case CCValAssign::Full: break;
1707 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001708 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001709 break;
1710 }
1711
Bob Wilson1f595bb2009-04-17 19:07:39 +00001712 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001714 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001715 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1716 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001717 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001718 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001719
1720 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1721 Flag = Chain.getValue(1);
1722 VA = RVLocs[++i]; // skip ahead to next loc
1723 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1724 HalfGPRs.getValue(1), Flag);
1725 Flag = Chain.getValue(1);
1726 VA = RVLocs[++i]; // skip ahead to next loc
1727
1728 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001729 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1730 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001731 }
1732 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1733 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001734 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001735 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001736 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001737 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001738 VA = RVLocs[++i]; // skip ahead to next loc
1739 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1740 Flag);
1741 } else
1742 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1743
Bob Wilsondee46d72009-04-17 20:35:10 +00001744 // Guarantee that all emitted copies are
1745 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001746 Flag = Chain.getValue(1);
1747 }
1748
1749 SDValue result;
1750 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001751 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001752 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001753 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001754
1755 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001756}
1757
Evan Cheng3d2125c2010-11-30 23:55:39 +00001758bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1759 if (N->getNumValues() != 1)
1760 return false;
1761 if (!N->hasNUsesOfValue(1, 0))
1762 return false;
1763
1764 unsigned NumCopies = 0;
1765 SDNode* Copies[2];
1766 SDNode *Use = *N->use_begin();
1767 if (Use->getOpcode() == ISD::CopyToReg) {
1768 Copies[NumCopies++] = Use;
1769 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1770 // f64 returned in a pair of GPRs.
1771 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1772 UI != UE; ++UI) {
1773 if (UI->getOpcode() != ISD::CopyToReg)
1774 return false;
1775 Copies[UI.getUse().getResNo()] = *UI;
1776 ++NumCopies;
1777 }
1778 } else if (Use->getOpcode() == ISD::BITCAST) {
1779 // f32 returned in a single GPR.
1780 if (!Use->hasNUsesOfValue(1, 0))
1781 return false;
1782 Use = *Use->use_begin();
1783 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1784 return false;
1785 Copies[NumCopies++] = Use;
1786 } else {
1787 return false;
1788 }
1789
1790 if (NumCopies != 1 && NumCopies != 2)
1791 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001792
1793 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001794 for (unsigned i = 0; i < NumCopies; ++i) {
1795 SDNode *Copy = Copies[i];
1796 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1797 UI != UE; ++UI) {
1798 if (UI->getOpcode() == ISD::CopyToReg) {
1799 SDNode *Use = *UI;
1800 if (Use == Copies[0] || Use == Copies[1])
1801 continue;
1802 return false;
1803 }
1804 if (UI->getOpcode() != ARMISD::RET_FLAG)
1805 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001806 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001807 }
1808 }
1809
Evan Cheng1bf891a2010-12-01 22:59:46 +00001810 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001811}
1812
Evan Cheng485fafc2011-03-21 01:19:09 +00001813bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1814 if (!EnableARMTailCalls)
1815 return false;
1816
1817 if (!CI->isTailCall())
1818 return false;
1819
1820 return !Subtarget->isThumb1Only();
1821}
1822
Bob Wilsonb62d2572009-11-03 00:02:05 +00001823// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1824// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1825// one of the above mentioned nodes. It has to be wrapped because otherwise
1826// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1827// be used to form addressing mode. These wrapped nodes will be selected
1828// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001829static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001830 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001831 // FIXME there is no actual debug info here
1832 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001833 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001834 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001835 if (CP->isMachineConstantPoolEntry())
1836 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1837 CP->getAlignment());
1838 else
1839 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1840 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001842}
1843
Jim Grosbache1102ca2010-07-19 17:20:38 +00001844unsigned ARMTargetLowering::getJumpTableEncoding() const {
1845 return MachineJumpTableInfo::EK_Inline;
1846}
1847
Dan Gohmand858e902010-04-17 15:26:15 +00001848SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1849 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001850 MachineFunction &MF = DAG.getMachineFunction();
1851 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1852 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001853 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001854 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001855 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001856 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1857 SDValue CPAddr;
1858 if (RelocM == Reloc::Static) {
1859 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1860 } else {
1861 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001862 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001863 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1864 ARMCP::CPBlockAddress,
1865 PCAdj);
1866 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1867 }
1868 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1869 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001870 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001871 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001872 if (RelocM == Reloc::Static)
1873 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001874 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001875 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001876}
1877
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001878// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001879SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001880ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001881 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001882 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001883 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001884 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001885 MachineFunction &MF = DAG.getMachineFunction();
1886 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001887 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001888 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001889 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001890 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001891 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001893 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001894 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001895 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001896 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001897
Evan Chenge7e0d622009-11-06 22:24:13 +00001898 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001899 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001900
1901 // call __tls_get_addr.
1902 ArgListTy Args;
1903 ArgListEntry Entry;
1904 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001905 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001906 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001907 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001908 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001909 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1910 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001911 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001912 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001913 return CallResult.first;
1914}
1915
1916// Lower ISD::GlobalTLSAddress using the "initial exec" or
1917// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001918SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001919ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001920 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001921 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001922 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001923 SDValue Offset;
1924 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001925 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001926 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001927 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001928
Chris Lattner4fb63d02009-07-15 04:12:33 +00001929 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001930 MachineFunction &MF = DAG.getMachineFunction();
1931 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001932 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001933 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001934 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1935 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001936 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001937 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001938 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001939 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001940 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001941 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001942 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001943 Chain = Offset.getValue(1);
1944
Evan Chenge7e0d622009-11-06 22:24:13 +00001945 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001946 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001947
Evan Cheng9eda6892009-10-31 03:39:36 +00001948 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001949 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001950 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001951 } else {
1952 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001953 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001954 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001955 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001956 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001957 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001958 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001959 }
1960
1961 // The address of the thread local variable is the add of the thread
1962 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001963 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001964}
1965
Dan Gohman475871a2008-07-27 21:46:04 +00001966SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001967ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001968 // TODO: implement the "local dynamic" model
1969 assert(Subtarget->isTargetELF() &&
1970 "TLS not implemented for non-ELF targets");
1971 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1972 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1973 // otherwise use the "Local Exec" TLS Model
1974 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1975 return LowerToTLSGeneralDynamicModel(GA, DAG);
1976 else
1977 return LowerToTLSExecModels(GA, DAG);
1978}
1979
Dan Gohman475871a2008-07-27 21:46:04 +00001980SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001981 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001982 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001983 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001984 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001985 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1986 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001987 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001988 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001989 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001990 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001992 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001993 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001994 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001995 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001996 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001997 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001998 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001999 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002000 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002001 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002002 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002003 }
2004
2005 // If we have T2 ops, we can materialize the address directly via movt/movw
2006 // pair. This is always cheaper.
2007 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002008 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002009 // FIXME: Once remat is capable of dealing with instructions with register
2010 // operands, expand this into two nodes.
2011 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2012 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002013 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002014 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2015 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2016 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2017 MachinePointerInfo::getConstantPool(),
2018 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002019 }
2020}
2021
Dan Gohman475871a2008-07-27 21:46:04 +00002022SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002023 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002024 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002025 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002026 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002027 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002028 MachineFunction &MF = DAG.getMachineFunction();
2029 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2030
2031 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002032 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002033 // FIXME: Once remat is capable of dealing with instructions with register
2034 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002035 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002036 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2037 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2038
Evan Cheng53519f02011-01-21 18:55:51 +00002039 unsigned Wrapper = (RelocM == Reloc::PIC_)
2040 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2041 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002042 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002043 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2044 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2045 MachinePointerInfo::getGOT(), false, false, 0);
2046 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002047 }
2048
2049 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002050 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002051 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002052 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002053 } else {
2054 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002055 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2056 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002057 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002058 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002059 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002060 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002061
Evan Cheng9eda6892009-10-31 03:39:36 +00002062 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002063 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002064 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002065 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002066
2067 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002068 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002069 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002070 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002071
Evan Cheng63476a82009-09-03 07:04:02 +00002072 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002073 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002074 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002075
2076 return Result;
2077}
2078
Dan Gohman475871a2008-07-27 21:46:04 +00002079SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002080 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002081 assert(Subtarget->isTargetELF() &&
2082 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002083 MachineFunction &MF = DAG.getMachineFunction();
2084 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002085 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002086 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002087 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002088 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002089 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2090 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002091 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002092 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002094 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002095 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002096 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002097 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002098 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002099}
2100
Jim Grosbach0e0da732009-05-12 23:59:14 +00002101SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002102ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2103 const {
2104 DebugLoc dl = Op.getDebugLoc();
2105 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
2106 Op.getOperand(0), Op.getOperand(1));
2107}
2108
2109SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002110ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2111 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002112 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002113 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2114 Op.getOperand(1), Val);
2115}
2116
2117SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002118ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2119 DebugLoc dl = Op.getDebugLoc();
2120 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2121 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2122}
2123
2124SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002125ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002126 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002127 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002128 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002129 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002130 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002131 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002132 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002133 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2134 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002135 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002136 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002137 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002138 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002139 EVT PtrVT = getPointerTy();
2140 DebugLoc dl = Op.getDebugLoc();
2141 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2142 SDValue CPAddr;
2143 unsigned PCAdj = (RelocM != Reloc::PIC_)
2144 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002145 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002146 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2147 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002148 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002149 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002150 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002151 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002152 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002153 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002154
2155 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002156 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002157 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2158 }
2159 return Result;
2160 }
Evan Cheng92e39162011-03-29 23:06:19 +00002161 case Intrinsic::arm_neon_vmulls:
2162 case Intrinsic::arm_neon_vmullu: {
2163 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2164 ? ARMISD::VMULLs : ARMISD::VMULLu;
2165 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2166 Op.getOperand(1), Op.getOperand(2));
2167 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002168 }
2169}
2170
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002171static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002172 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002173 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002174 if (!Subtarget->hasDataBarrier()) {
2175 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2176 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2177 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002178 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002179 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002180 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002181 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002182 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002183
2184 SDValue Op5 = Op.getOperand(5);
2185 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2186 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2187 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2188 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2189
2190 ARM_MB::MemBOpt DMBOpt;
2191 if (isDeviceBarrier)
2192 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2193 else
2194 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2195 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2196 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002197}
2198
Evan Chengdfed19f2010-11-03 06:34:55 +00002199static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2200 const ARMSubtarget *Subtarget) {
2201 // ARM pre v5TE and Thumb1 does not have preload instructions.
2202 if (!(Subtarget->isThumb2() ||
2203 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2204 // Just preserve the chain.
2205 return Op.getOperand(0);
2206
2207 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002208 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2209 if (!isRead &&
2210 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2211 // ARMv7 with MP extension has PLDW.
2212 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002213
2214 if (Subtarget->isThumb())
2215 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002216 isRead = ~isRead & 1;
2217 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002218
Evan Cheng416941d2010-11-04 05:19:35 +00002219 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002220 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002221 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2222 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002223}
2224
Dan Gohman1e93df62010-04-17 14:41:14 +00002225static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2226 MachineFunction &MF = DAG.getMachineFunction();
2227 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2228
Evan Chenga8e29892007-01-19 07:51:42 +00002229 // vastart just stores the address of the VarArgsFrameIndex slot into the
2230 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002231 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002232 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002233 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002234 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002235 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2236 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002237}
2238
Dan Gohman475871a2008-07-27 21:46:04 +00002239SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002240ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2241 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002242 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002243 MachineFunction &MF = DAG.getMachineFunction();
2244 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2245
2246 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002247 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002248 RC = ARM::tGPRRegisterClass;
2249 else
2250 RC = ARM::GPRRegisterClass;
2251
2252 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002253 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002254 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002255
2256 SDValue ArgValue2;
2257 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002258 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002259 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002260
2261 // Create load node to retrieve arguments from the stack.
2262 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002263 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002264 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002265 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002266 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002267 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002268 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002269 }
2270
Jim Grosbache5165492009-11-09 00:11:35 +00002271 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002272}
2273
2274SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002275ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002276 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002277 const SmallVectorImpl<ISD::InputArg>
2278 &Ins,
2279 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002280 SmallVectorImpl<SDValue> &InVals)
2281 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002282
Bob Wilson1f595bb2009-04-17 19:07:39 +00002283 MachineFunction &MF = DAG.getMachineFunction();
2284 MachineFrameInfo *MFI = MF.getFrameInfo();
2285
Bob Wilson1f595bb2009-04-17 19:07:39 +00002286 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2287
2288 // Assign locations to all of the incoming arguments.
2289 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002290 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2291 *DAG.getContext());
2292 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002293 CCAssignFnForNode(CallConv, /* Return*/ false,
2294 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002295
2296 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002297 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002298
Stuart Hastingsf222e592011-02-28 17:17:53 +00002299 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002300 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2301 CCValAssign &VA = ArgLocs[i];
2302
Bob Wilsondee46d72009-04-17 20:35:10 +00002303 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002304 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002305 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002306
Bob Wilson1f595bb2009-04-17 19:07:39 +00002307 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002308 // f64 and vector types are split up into multiple registers or
2309 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002310 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002311 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002312 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002313 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002314 SDValue ArgValue2;
2315 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002316 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002317 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2318 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002319 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002320 false, false, 0);
2321 } else {
2322 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2323 Chain, DAG, dl);
2324 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002325 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2326 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002327 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002328 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002329 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2330 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002331 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002332
Bob Wilson5bafff32009-06-22 23:27:02 +00002333 } else {
2334 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002335
Owen Anderson825b72b2009-08-11 20:47:22 +00002336 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002337 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002338 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002339 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002340 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002341 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002343 RC = (AFI->isThumb1OnlyFunction() ?
2344 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002345 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002346 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002347
2348 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002349 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002350 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002351 }
2352
2353 // If this is an 8 or 16-bit value, it is really passed promoted
2354 // to 32 bits. Insert an assert[sz]ext to capture this, then
2355 // truncate to the right size.
2356 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002357 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002358 case CCValAssign::Full: break;
2359 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002360 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002361 break;
2362 case CCValAssign::SExt:
2363 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2364 DAG.getValueType(VA.getValVT()));
2365 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2366 break;
2367 case CCValAssign::ZExt:
2368 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2369 DAG.getValueType(VA.getValVT()));
2370 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2371 break;
2372 }
2373
Dan Gohman98ca4f22009-08-05 01:29:28 +00002374 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002375
2376 } else { // VA.isRegLoc()
2377
2378 // sanity check
2379 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002380 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002381
Stuart Hastingsf222e592011-02-28 17:17:53 +00002382 int index = ArgLocs[i].getValNo();
2383
2384 // Some Ins[] entries become multiple ArgLoc[] entries.
2385 // Process them only once.
2386 if (index != lastInsIndex)
2387 {
2388 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2389 // FIXME: For now, all byval parameter objects are marked mutable. This can be
2390 // changed with more analysis.
2391 // In case of tail call optimization mark all arguments mutable. Since they
2392 // could be overwritten by lowering of arguments in case of a tail call.
2393 if (Flags.isByVal()) {
2394 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2395 VA.getLocMemOffset(), false);
2396 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2397 } else {
2398 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2399 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002400
Stuart Hastingsf222e592011-02-28 17:17:53 +00002401 // Create load nodes to retrieve arguments from the stack.
2402 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2403 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2404 MachinePointerInfo::getFixedStack(FI),
2405 false, false, 0));
2406 }
2407 lastInsIndex = index;
2408 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002409 }
2410 }
2411
2412 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002413 if (isVarArg) {
2414 static const unsigned GPRArgRegs[] = {
2415 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2416 };
2417
Bob Wilsondee46d72009-04-17 20:35:10 +00002418 unsigned NumGPRs = CCInfo.getFirstUnallocated
2419 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002420
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002421 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002422 unsigned VARegSize = (4 - NumGPRs) * 4;
2423 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002424 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002425 if (VARegSaveSize) {
2426 // If this function is vararg, store any remaining integer argument regs
2427 // to their spots on the stack so that they may be loaded by deferencing
2428 // the result of va_next.
2429 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002430 AFI->setVarArgsFrameIndex(
2431 MFI->CreateFixedObject(VARegSaveSize,
2432 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002433 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002434 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2435 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002436
Dan Gohman475871a2008-07-27 21:46:04 +00002437 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002438 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002439 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002440 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002441 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002442 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002443 RC = ARM::GPRRegisterClass;
2444
Devang Patel68e6bee2011-02-21 23:21:26 +00002445 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002446 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002447 SDValue Store =
2448 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002449 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2450 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002451 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002452 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002453 DAG.getConstant(4, getPointerTy()));
2454 }
2455 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002456 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002457 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002458 } else
2459 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002460 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002461 }
2462
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002464}
2465
2466/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002467static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002468 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002469 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002470 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002471 // Maybe this has already been legalized into the constant pool?
2472 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002473 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002474 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002475 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002476 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002477 }
2478 }
2479 return false;
2480}
2481
Evan Chenga8e29892007-01-19 07:51:42 +00002482/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2483/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002484SDValue
2485ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002486 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002487 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002488 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002489 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002490 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002491 // Constant does not fit, try adjusting it by one?
2492 switch (CC) {
2493 default: break;
2494 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002495 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002496 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002497 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002498 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002499 }
2500 break;
2501 case ISD::SETULT:
2502 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002503 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002504 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002505 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002506 }
2507 break;
2508 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002509 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002510 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002511 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002512 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002513 }
2514 break;
2515 case ISD::SETULE:
2516 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002517 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002518 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002519 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002520 }
2521 break;
2522 }
2523 }
2524 }
2525
2526 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002527 ARMISD::NodeType CompareType;
2528 switch (CondCode) {
2529 default:
2530 CompareType = ARMISD::CMP;
2531 break;
2532 case ARMCC::EQ:
2533 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002534 // Uses only Z Flag
2535 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002536 break;
2537 }
Evan Cheng218977b2010-07-13 19:27:42 +00002538 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002539 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002540}
2541
2542/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002543SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002544ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002545 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002546 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002547 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002548 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002549 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002550 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2551 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002552}
2553
Bob Wilson79f56c92011-03-08 01:17:20 +00002554/// duplicateCmp - Glue values can have only one use, so this function
2555/// duplicates a comparison node.
2556SDValue
2557ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2558 unsigned Opc = Cmp.getOpcode();
2559 DebugLoc DL = Cmp.getDebugLoc();
2560 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2561 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2562
2563 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2564 Cmp = Cmp.getOperand(0);
2565 Opc = Cmp.getOpcode();
2566 if (Opc == ARMISD::CMPFP)
2567 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2568 else {
2569 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2570 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2571 }
2572 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2573}
2574
Bill Wendlingde2b1512010-08-11 08:43:16 +00002575SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2576 SDValue Cond = Op.getOperand(0);
2577 SDValue SelectTrue = Op.getOperand(1);
2578 SDValue SelectFalse = Op.getOperand(2);
2579 DebugLoc dl = Op.getDebugLoc();
2580
2581 // Convert:
2582 //
2583 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2584 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2585 //
2586 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2587 const ConstantSDNode *CMOVTrue =
2588 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2589 const ConstantSDNode *CMOVFalse =
2590 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2591
2592 if (CMOVTrue && CMOVFalse) {
2593 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2594 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2595
2596 SDValue True;
2597 SDValue False;
2598 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2599 True = SelectTrue;
2600 False = SelectFalse;
2601 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2602 True = SelectFalse;
2603 False = SelectTrue;
2604 }
2605
2606 if (True.getNode() && False.getNode()) {
2607 EVT VT = Cond.getValueType();
2608 SDValue ARMcc = Cond.getOperand(2);
2609 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002610 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002611 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2612 }
2613 }
2614 }
2615
2616 return DAG.getSelectCC(dl, Cond,
2617 DAG.getConstant(0, Cond.getValueType()),
2618 SelectTrue, SelectFalse, ISD::SETNE);
2619}
2620
Dan Gohmand858e902010-04-17 15:26:15 +00002621SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002622 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002623 SDValue LHS = Op.getOperand(0);
2624 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002625 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002626 SDValue TrueVal = Op.getOperand(2);
2627 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002628 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002629
Owen Anderson825b72b2009-08-11 20:47:22 +00002630 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002631 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002632 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002633 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2634 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002635 }
2636
2637 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002638 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002639
Evan Cheng218977b2010-07-13 19:27:42 +00002640 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2641 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002642 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002643 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002644 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002645 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002646 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002647 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002648 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002649 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002650 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002651 }
2652 return Result;
2653}
2654
Evan Cheng218977b2010-07-13 19:27:42 +00002655/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2656/// to morph to an integer compare sequence.
2657static bool canChangeToInt(SDValue Op, bool &SeenZero,
2658 const ARMSubtarget *Subtarget) {
2659 SDNode *N = Op.getNode();
2660 if (!N->hasOneUse())
2661 // Otherwise it requires moving the value from fp to integer registers.
2662 return false;
2663 if (!N->getNumValues())
2664 return false;
2665 EVT VT = Op.getValueType();
2666 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2667 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2668 // vmrs are very slow, e.g. cortex-a8.
2669 return false;
2670
2671 if (isFloatingPointZero(Op)) {
2672 SeenZero = true;
2673 return true;
2674 }
2675 return ISD::isNormalLoad(N);
2676}
2677
2678static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2679 if (isFloatingPointZero(Op))
2680 return DAG.getConstant(0, MVT::i32);
2681
2682 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2683 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002684 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002685 Ld->isVolatile(), Ld->isNonTemporal(),
2686 Ld->getAlignment());
2687
2688 llvm_unreachable("Unknown VFP cmp argument!");
2689}
2690
2691static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2692 SDValue &RetVal1, SDValue &RetVal2) {
2693 if (isFloatingPointZero(Op)) {
2694 RetVal1 = DAG.getConstant(0, MVT::i32);
2695 RetVal2 = DAG.getConstant(0, MVT::i32);
2696 return;
2697 }
2698
2699 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2700 SDValue Ptr = Ld->getBasePtr();
2701 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2702 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002703 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002704 Ld->isVolatile(), Ld->isNonTemporal(),
2705 Ld->getAlignment());
2706
2707 EVT PtrType = Ptr.getValueType();
2708 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2709 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2710 PtrType, Ptr, DAG.getConstant(4, PtrType));
2711 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2712 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002713 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002714 Ld->isVolatile(), Ld->isNonTemporal(),
2715 NewAlign);
2716 return;
2717 }
2718
2719 llvm_unreachable("Unknown VFP cmp argument!");
2720}
2721
2722/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2723/// f32 and even f64 comparisons to integer ones.
2724SDValue
2725ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2726 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002727 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002728 SDValue LHS = Op.getOperand(2);
2729 SDValue RHS = Op.getOperand(3);
2730 SDValue Dest = Op.getOperand(4);
2731 DebugLoc dl = Op.getDebugLoc();
2732
2733 bool SeenZero = false;
2734 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2735 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002736 // If one of the operand is zero, it's safe to ignore the NaN case since
2737 // we only care about equality comparisons.
2738 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002739 // If unsafe fp math optimization is enabled and there are no other uses of
2740 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002741 // to an integer comparison.
2742 if (CC == ISD::SETOEQ)
2743 CC = ISD::SETEQ;
2744 else if (CC == ISD::SETUNE)
2745 CC = ISD::SETNE;
2746
2747 SDValue ARMcc;
2748 if (LHS.getValueType() == MVT::f32) {
2749 LHS = bitcastf32Toi32(LHS, DAG);
2750 RHS = bitcastf32Toi32(RHS, DAG);
2751 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2752 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2753 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2754 Chain, Dest, ARMcc, CCR, Cmp);
2755 }
2756
2757 SDValue LHS1, LHS2;
2758 SDValue RHS1, RHS2;
2759 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2760 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2761 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2762 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002763 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002764 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2765 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2766 }
2767
2768 return SDValue();
2769}
2770
2771SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2772 SDValue Chain = Op.getOperand(0);
2773 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2774 SDValue LHS = Op.getOperand(2);
2775 SDValue RHS = Op.getOperand(3);
2776 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002777 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002778
Owen Anderson825b72b2009-08-11 20:47:22 +00002779 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002780 SDValue ARMcc;
2781 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002782 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002783 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002784 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002785 }
2786
Owen Anderson825b72b2009-08-11 20:47:22 +00002787 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002788
2789 if (UnsafeFPMath &&
2790 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2791 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2792 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2793 if (Result.getNode())
2794 return Result;
2795 }
2796
Evan Chenga8e29892007-01-19 07:51:42 +00002797 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002798 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002799
Evan Cheng218977b2010-07-13 19:27:42 +00002800 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2801 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002802 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002803 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002804 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002805 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002806 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002807 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2808 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002809 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002810 }
2811 return Res;
2812}
2813
Dan Gohmand858e902010-04-17 15:26:15 +00002814SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002815 SDValue Chain = Op.getOperand(0);
2816 SDValue Table = Op.getOperand(1);
2817 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002818 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002819
Owen Andersone50ed302009-08-10 22:56:29 +00002820 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002821 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2822 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002823 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002824 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002825 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002826 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2827 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002828 if (Subtarget->isThumb2()) {
2829 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2830 // which does another jump to the destination. This also makes it easier
2831 // to translate it to TBB / TBH later.
2832 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002833 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002834 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002835 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002836 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002837 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002838 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002839 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002840 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002841 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002842 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002843 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002844 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002845 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002846 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002847 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002848 }
Evan Chenga8e29892007-01-19 07:51:42 +00002849}
2850
Bob Wilson76a312b2010-03-19 22:51:32 +00002851static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2852 DebugLoc dl = Op.getDebugLoc();
2853 unsigned Opc;
2854
2855 switch (Op.getOpcode()) {
2856 default:
2857 assert(0 && "Invalid opcode!");
2858 case ISD::FP_TO_SINT:
2859 Opc = ARMISD::FTOSI;
2860 break;
2861 case ISD::FP_TO_UINT:
2862 Opc = ARMISD::FTOUI;
2863 break;
2864 }
2865 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002866 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002867}
2868
Cameron Zwarich3007d332011-03-29 21:41:55 +00002869static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2870 EVT VT = Op.getValueType();
2871 DebugLoc dl = Op.getDebugLoc();
2872
2873 EVT OperandVT = Op.getOperand(0).getValueType();
2874 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2875 if (VT != MVT::v4f32)
2876 return DAG.UnrollVectorOp(Op.getNode());
2877
2878 unsigned CastOpc;
2879 unsigned Opc;
2880 switch (Op.getOpcode()) {
2881 default:
2882 assert(0 && "Invalid opcode!");
2883 case ISD::SINT_TO_FP:
2884 CastOpc = ISD::SIGN_EXTEND;
2885 Opc = ISD::SINT_TO_FP;
2886 break;
2887 case ISD::UINT_TO_FP:
2888 CastOpc = ISD::ZERO_EXTEND;
2889 Opc = ISD::UINT_TO_FP;
2890 break;
2891 }
2892
2893 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2894 return DAG.getNode(Opc, dl, VT, Op);
2895}
2896
Bob Wilson76a312b2010-03-19 22:51:32 +00002897static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2898 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002899 if (VT.isVector())
2900 return LowerVectorINT_TO_FP(Op, DAG);
2901
Bob Wilson76a312b2010-03-19 22:51:32 +00002902 DebugLoc dl = Op.getDebugLoc();
2903 unsigned Opc;
2904
2905 switch (Op.getOpcode()) {
2906 default:
2907 assert(0 && "Invalid opcode!");
2908 case ISD::SINT_TO_FP:
2909 Opc = ARMISD::SITOF;
2910 break;
2911 case ISD::UINT_TO_FP:
2912 Opc = ARMISD::UITOF;
2913 break;
2914 }
2915
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002916 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002917 return DAG.getNode(Opc, dl, VT, Op);
2918}
2919
Evan Cheng515fe3a2010-07-08 02:08:50 +00002920SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002921 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue Tmp0 = Op.getOperand(0);
2923 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002924 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002925 EVT VT = Op.getValueType();
2926 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00002927 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
2928 Tmp0.getOpcode() == ARMISD::VMOVDRR;
2929 bool UseNEON = !InGPR && Subtarget->hasNEON();
2930
2931 if (UseNEON) {
2932 // Use VBSL to copy the sign bit.
2933 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
2934 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
2935 DAG.getTargetConstant(EncodedVal, MVT::i32));
2936 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
2937 if (VT == MVT::f64)
2938 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
2939 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
2940 DAG.getConstant(32, MVT::i32));
2941 else /*if (VT == MVT::f32)*/
2942 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
2943 if (SrcVT == MVT::f32) {
2944 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
2945 if (VT == MVT::f64)
2946 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
2947 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
2948 DAG.getConstant(32, MVT::i32));
2949 }
2950 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
2951 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
2952
2953 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
2954 MVT::i32);
2955 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
2956 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
2957 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
2958
2959 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
2960 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
2961 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00002962 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00002963 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
2964 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
2965 DAG.getConstant(0, MVT::i32));
2966 } else {
2967 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
2968 }
2969
2970 return Res;
2971 }
Evan Chengc143dd42011-02-11 02:28:55 +00002972
2973 // Bitcast operand 1 to i32.
2974 if (SrcVT == MVT::f64)
2975 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
2976 &Tmp1, 1).getValue(1);
2977 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
2978
Evan Chenge573fb32011-02-23 02:24:55 +00002979 // Or in the signbit with integer operations.
2980 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
2981 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
2982 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
2983 if (VT == MVT::f32) {
2984 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
2985 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
2986 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
2987 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00002988 }
2989
Evan Chenge573fb32011-02-23 02:24:55 +00002990 // f64: Or the high part with signbit and then combine two parts.
2991 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
2992 &Tmp0, 1);
2993 SDValue Lo = Tmp0.getValue(0);
2994 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
2995 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
2996 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00002997}
2998
Evan Cheng2457f2c2010-05-22 01:47:14 +00002999SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3000 MachineFunction &MF = DAG.getMachineFunction();
3001 MachineFrameInfo *MFI = MF.getFrameInfo();
3002 MFI->setReturnAddressIsTaken(true);
3003
3004 EVT VT = Op.getValueType();
3005 DebugLoc dl = Op.getDebugLoc();
3006 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3007 if (Depth) {
3008 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3009 SDValue Offset = DAG.getConstant(4, MVT::i32);
3010 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3011 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003012 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003013 }
3014
3015 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003016 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003017 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3018}
3019
Dan Gohmand858e902010-04-17 15:26:15 +00003020SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003021 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3022 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003023
Owen Andersone50ed302009-08-10 22:56:29 +00003024 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003025 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3026 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003027 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003028 ? ARM::R7 : ARM::R11;
3029 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3030 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003031 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3032 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003033 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003034 return FrameAddr;
3035}
3036
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003037/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003038/// expand a bit convert where either the source or destination type is i64 to
3039/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3040/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3041/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003042static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003043 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3044 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003045 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003046
Bob Wilson9f3f0612010-04-17 05:30:19 +00003047 // This function is only supposed to be called for i64 types, either as the
3048 // source or destination of the bit convert.
3049 EVT SrcVT = Op.getValueType();
3050 EVT DstVT = N->getValueType(0);
3051 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003052 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003053
Bob Wilson9f3f0612010-04-17 05:30:19 +00003054 // Turn i64->f64 into VMOVDRR.
3055 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003056 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3057 DAG.getConstant(0, MVT::i32));
3058 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3059 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003060 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003061 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003062 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003063
Jim Grosbache5165492009-11-09 00:11:35 +00003064 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003065 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3066 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3067 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3068 // Merge the pieces into a single i64 value.
3069 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3070 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003071
Bob Wilson9f3f0612010-04-17 05:30:19 +00003072 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003073}
3074
Bob Wilson5bafff32009-06-22 23:27:02 +00003075/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003076/// Zero vectors are used to represent vector negation and in those cases
3077/// will be implemented with the NEON VNEG instruction. However, VNEG does
3078/// not support i64 elements, so sometimes the zero vectors will need to be
3079/// explicitly constructed. Regardless, use a canonical VMOV to create the
3080/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003081static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003082 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003083 // The canonical modified immediate encoding of a zero vector is....0!
3084 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3085 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3086 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003087 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003088}
3089
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003090/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3091/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003092SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3093 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003094 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3095 EVT VT = Op.getValueType();
3096 unsigned VTBits = VT.getSizeInBits();
3097 DebugLoc dl = Op.getDebugLoc();
3098 SDValue ShOpLo = Op.getOperand(0);
3099 SDValue ShOpHi = Op.getOperand(1);
3100 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003101 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003102 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003103
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003104 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3105
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003106 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3107 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3108 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3109 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3110 DAG.getConstant(VTBits, MVT::i32));
3111 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3112 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003113 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003114
3115 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3116 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003117 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003118 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003119 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003120 CCR, Cmp);
3121
3122 SDValue Ops[2] = { Lo, Hi };
3123 return DAG.getMergeValues(Ops, 2, dl);
3124}
3125
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003126/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3127/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003128SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3129 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003130 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3131 EVT VT = Op.getValueType();
3132 unsigned VTBits = VT.getSizeInBits();
3133 DebugLoc dl = Op.getDebugLoc();
3134 SDValue ShOpLo = Op.getOperand(0);
3135 SDValue ShOpHi = Op.getOperand(1);
3136 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003137 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003138
3139 assert(Op.getOpcode() == ISD::SHL_PARTS);
3140 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3141 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3142 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3143 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3144 DAG.getConstant(VTBits, MVT::i32));
3145 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3146 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3147
3148 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3149 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3150 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003151 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003152 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003153 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003154 CCR, Cmp);
3155
3156 SDValue Ops[2] = { Lo, Hi };
3157 return DAG.getMergeValues(Ops, 2, dl);
3158}
3159
Jim Grosbach4725ca72010-09-08 03:54:02 +00003160SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003161 SelectionDAG &DAG) const {
3162 // The rounding mode is in bits 23:22 of the FPSCR.
3163 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3164 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3165 // so that the shift + and get folded into a bitfield extract.
3166 DebugLoc dl = Op.getDebugLoc();
3167 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3168 DAG.getConstant(Intrinsic::arm_get_fpscr,
3169 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003170 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003171 DAG.getConstant(1U << 22, MVT::i32));
3172 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3173 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003174 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003175 DAG.getConstant(3, MVT::i32));
3176}
3177
Jim Grosbach3482c802010-01-18 19:58:49 +00003178static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3179 const ARMSubtarget *ST) {
3180 EVT VT = N->getValueType(0);
3181 DebugLoc dl = N->getDebugLoc();
3182
3183 if (!ST->hasV6T2Ops())
3184 return SDValue();
3185
3186 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3187 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3188}
3189
Bob Wilson5bafff32009-06-22 23:27:02 +00003190static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3191 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003192 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003193 DebugLoc dl = N->getDebugLoc();
3194
Bob Wilsond5448bb2010-11-18 21:16:28 +00003195 if (!VT.isVector())
3196 return SDValue();
3197
Bob Wilson5bafff32009-06-22 23:27:02 +00003198 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003199 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003200
Bob Wilsond5448bb2010-11-18 21:16:28 +00003201 // Left shifts translate directly to the vshiftu intrinsic.
3202 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003203 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003204 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3205 N->getOperand(0), N->getOperand(1));
3206
3207 assert((N->getOpcode() == ISD::SRA ||
3208 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3209
3210 // NEON uses the same intrinsics for both left and right shifts. For
3211 // right shifts, the shift amounts are negative, so negate the vector of
3212 // shift amounts.
3213 EVT ShiftVT = N->getOperand(1).getValueType();
3214 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3215 getZeroVector(ShiftVT, DAG, dl),
3216 N->getOperand(1));
3217 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3218 Intrinsic::arm_neon_vshifts :
3219 Intrinsic::arm_neon_vshiftu);
3220 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3221 DAG.getConstant(vshiftInt, MVT::i32),
3222 N->getOperand(0), NegatedCount);
3223}
3224
3225static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3226 const ARMSubtarget *ST) {
3227 EVT VT = N->getValueType(0);
3228 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003229
Eli Friedmance392eb2009-08-22 03:13:10 +00003230 // We can get here for a node like i32 = ISD::SHL i32, i64
3231 if (VT != MVT::i64)
3232 return SDValue();
3233
3234 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003235 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003236
Chris Lattner27a6c732007-11-24 07:07:01 +00003237 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3238 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003239 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003240 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003241
Chris Lattner27a6c732007-11-24 07:07:01 +00003242 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003243 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003244
Chris Lattner27a6c732007-11-24 07:07:01 +00003245 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003247 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003249 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003250
Chris Lattner27a6c732007-11-24 07:07:01 +00003251 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3252 // captures the result into a carry flag.
3253 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003254 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003255
Chris Lattner27a6c732007-11-24 07:07:01 +00003256 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003258
Chris Lattner27a6c732007-11-24 07:07:01 +00003259 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003260 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003261}
3262
Bob Wilson5bafff32009-06-22 23:27:02 +00003263static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3264 SDValue TmpOp0, TmpOp1;
3265 bool Invert = false;
3266 bool Swap = false;
3267 unsigned Opc = 0;
3268
3269 SDValue Op0 = Op.getOperand(0);
3270 SDValue Op1 = Op.getOperand(1);
3271 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003272 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003273 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3274 DebugLoc dl = Op.getDebugLoc();
3275
3276 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3277 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003278 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003279 case ISD::SETUNE:
3280 case ISD::SETNE: Invert = true; // Fallthrough
3281 case ISD::SETOEQ:
3282 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3283 case ISD::SETOLT:
3284 case ISD::SETLT: Swap = true; // Fallthrough
3285 case ISD::SETOGT:
3286 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3287 case ISD::SETOLE:
3288 case ISD::SETLE: Swap = true; // Fallthrough
3289 case ISD::SETOGE:
3290 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3291 case ISD::SETUGE: Swap = true; // Fallthrough
3292 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3293 case ISD::SETUGT: Swap = true; // Fallthrough
3294 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3295 case ISD::SETUEQ: Invert = true; // Fallthrough
3296 case ISD::SETONE:
3297 // Expand this to (OLT | OGT).
3298 TmpOp0 = Op0;
3299 TmpOp1 = Op1;
3300 Opc = ISD::OR;
3301 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3302 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3303 break;
3304 case ISD::SETUO: Invert = true; // Fallthrough
3305 case ISD::SETO:
3306 // Expand this to (OLT | OGE).
3307 TmpOp0 = Op0;
3308 TmpOp1 = Op1;
3309 Opc = ISD::OR;
3310 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3311 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3312 break;
3313 }
3314 } else {
3315 // Integer comparisons.
3316 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003317 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003318 case ISD::SETNE: Invert = true;
3319 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3320 case ISD::SETLT: Swap = true;
3321 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3322 case ISD::SETLE: Swap = true;
3323 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3324 case ISD::SETULT: Swap = true;
3325 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3326 case ISD::SETULE: Swap = true;
3327 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3328 }
3329
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003330 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003331 if (Opc == ARMISD::VCEQ) {
3332
3333 SDValue AndOp;
3334 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3335 AndOp = Op0;
3336 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3337 AndOp = Op1;
3338
3339 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003340 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003341 AndOp = AndOp.getOperand(0);
3342
3343 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3344 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003345 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3346 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003347 Invert = !Invert;
3348 }
3349 }
3350 }
3351
3352 if (Swap)
3353 std::swap(Op0, Op1);
3354
Owen Andersonc24cb352010-11-08 23:21:22 +00003355 // If one of the operands is a constant vector zero, attempt to fold the
3356 // comparison to a specialized compare-against-zero form.
3357 SDValue SingleOp;
3358 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3359 SingleOp = Op0;
3360 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3361 if (Opc == ARMISD::VCGE)
3362 Opc = ARMISD::VCLEZ;
3363 else if (Opc == ARMISD::VCGT)
3364 Opc = ARMISD::VCLTZ;
3365 SingleOp = Op1;
3366 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003367
Owen Andersonc24cb352010-11-08 23:21:22 +00003368 SDValue Result;
3369 if (SingleOp.getNode()) {
3370 switch (Opc) {
3371 case ARMISD::VCEQ:
3372 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3373 case ARMISD::VCGE:
3374 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3375 case ARMISD::VCLEZ:
3376 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3377 case ARMISD::VCGT:
3378 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3379 case ARMISD::VCLTZ:
3380 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3381 default:
3382 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3383 }
3384 } else {
3385 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3386 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003387
3388 if (Invert)
3389 Result = DAG.getNOT(dl, Result, VT);
3390
3391 return Result;
3392}
3393
Bob Wilsond3c42842010-06-14 22:19:57 +00003394/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3395/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003396/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003397static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3398 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003399 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003400 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003401
Bob Wilson827b2102010-06-15 19:05:35 +00003402 // SplatBitSize is set to the smallest size that splats the vector, so a
3403 // zero vector will always have SplatBitSize == 8. However, NEON modified
3404 // immediate instructions others than VMOV do not support the 8-bit encoding
3405 // of a zero vector, and the default encoding of zero is supposed to be the
3406 // 32-bit version.
3407 if (SplatBits == 0)
3408 SplatBitSize = 32;
3409
Bob Wilson5bafff32009-06-22 23:27:02 +00003410 switch (SplatBitSize) {
3411 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003412 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003413 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003414 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003415 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003416 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003417 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003418 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003419 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003420
3421 case 16:
3422 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003423 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003424 if ((SplatBits & ~0xff) == 0) {
3425 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003426 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003427 Imm = SplatBits;
3428 break;
3429 }
3430 if ((SplatBits & ~0xff00) == 0) {
3431 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003432 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003433 Imm = SplatBits >> 8;
3434 break;
3435 }
3436 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003437
3438 case 32:
3439 // NEON's 32-bit VMOV supports splat values where:
3440 // * only one byte is nonzero, or
3441 // * the least significant byte is 0xff and the second byte is nonzero, or
3442 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003443 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003444 if ((SplatBits & ~0xff) == 0) {
3445 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003446 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003447 Imm = SplatBits;
3448 break;
3449 }
3450 if ((SplatBits & ~0xff00) == 0) {
3451 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003452 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003453 Imm = SplatBits >> 8;
3454 break;
3455 }
3456 if ((SplatBits & ~0xff0000) == 0) {
3457 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003458 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003459 Imm = SplatBits >> 16;
3460 break;
3461 }
3462 if ((SplatBits & ~0xff000000) == 0) {
3463 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003464 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003465 Imm = SplatBits >> 24;
3466 break;
3467 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003468
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003469 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3470 if (type == OtherModImm) return SDValue();
3471
Bob Wilson5bafff32009-06-22 23:27:02 +00003472 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003473 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3474 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003475 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003476 Imm = SplatBits >> 8;
3477 SplatBits |= 0xff;
3478 break;
3479 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003480
3481 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003482 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3483 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003484 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003485 Imm = SplatBits >> 16;
3486 SplatBits |= 0xffff;
3487 break;
3488 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003489
3490 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3491 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3492 // VMOV.I32. A (very) minor optimization would be to replicate the value
3493 // and fall through here to test for a valid 64-bit splat. But, then the
3494 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003495 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003496
3497 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003498 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003499 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003500 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003501 uint64_t BitMask = 0xff;
3502 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003503 unsigned ImmMask = 1;
3504 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003505 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003506 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003507 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003508 Imm |= ImmMask;
3509 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003510 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003511 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003512 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003513 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003514 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003515 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003516 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003517 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003518 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003519 break;
3520 }
3521
Bob Wilson1a913ed2010-06-11 21:34:50 +00003522 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003523 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003524 return SDValue();
3525 }
3526
Bob Wilsoncba270d2010-07-13 21:16:48 +00003527 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3528 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003529}
3530
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003531static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3532 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003533 unsigned NumElts = VT.getVectorNumElements();
3534 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003535
3536 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3537 if (M[0] < 0)
3538 return false;
3539
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003540 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003541
3542 // If this is a VEXT shuffle, the immediate value is the index of the first
3543 // element. The other shuffle indices must be the successive elements after
3544 // the first one.
3545 unsigned ExpectedElt = Imm;
3546 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003547 // Increment the expected index. If it wraps around, it may still be
3548 // a VEXT but the source vectors must be swapped.
3549 ExpectedElt += 1;
3550 if (ExpectedElt == NumElts * 2) {
3551 ExpectedElt = 0;
3552 ReverseVEXT = true;
3553 }
3554
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003555 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003556 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003557 return false;
3558 }
3559
3560 // Adjust the index value if the source operands will be swapped.
3561 if (ReverseVEXT)
3562 Imm -= NumElts;
3563
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003564 return true;
3565}
3566
Bob Wilson8bb9e482009-07-26 00:39:34 +00003567/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3568/// instruction with the specified blocksize. (The order of the elements
3569/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003570static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3571 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003572 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3573 "Only possible block sizes for VREV are: 16, 32, 64");
3574
Bob Wilson8bb9e482009-07-26 00:39:34 +00003575 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003576 if (EltSz == 64)
3577 return false;
3578
3579 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003580 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003581 // If the first shuffle index is UNDEF, be optimistic.
3582 if (M[0] < 0)
3583 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003584
3585 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3586 return false;
3587
3588 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003589 if (M[i] < 0) continue; // ignore UNDEF indices
3590 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003591 return false;
3592 }
3593
3594 return true;
3595}
3596
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003597static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3598 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3599 // range, then 0 is placed into the resulting vector. So pretty much any mask
3600 // of 8 elements can work here.
3601 return VT == MVT::v8i8 && M.size() == 8;
3602}
3603
Bob Wilsonc692cb72009-08-21 20:54:19 +00003604static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3605 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003606 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3607 if (EltSz == 64)
3608 return false;
3609
Bob Wilsonc692cb72009-08-21 20:54:19 +00003610 unsigned NumElts = VT.getVectorNumElements();
3611 WhichResult = (M[0] == 0 ? 0 : 1);
3612 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003613 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3614 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003615 return false;
3616 }
3617 return true;
3618}
3619
Bob Wilson324f4f12009-12-03 06:40:55 +00003620/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3621/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3622/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3623static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3624 unsigned &WhichResult) {
3625 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3626 if (EltSz == 64)
3627 return false;
3628
3629 unsigned NumElts = VT.getVectorNumElements();
3630 WhichResult = (M[0] == 0 ? 0 : 1);
3631 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003632 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3633 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003634 return false;
3635 }
3636 return true;
3637}
3638
Bob Wilsonc692cb72009-08-21 20:54:19 +00003639static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3640 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003641 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3642 if (EltSz == 64)
3643 return false;
3644
Bob Wilsonc692cb72009-08-21 20:54:19 +00003645 unsigned NumElts = VT.getVectorNumElements();
3646 WhichResult = (M[0] == 0 ? 0 : 1);
3647 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003648 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003649 if ((unsigned) M[i] != 2 * i + WhichResult)
3650 return false;
3651 }
3652
3653 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003654 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003655 return false;
3656
3657 return true;
3658}
3659
Bob Wilson324f4f12009-12-03 06:40:55 +00003660/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3661/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3662/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3663static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3664 unsigned &WhichResult) {
3665 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3666 if (EltSz == 64)
3667 return false;
3668
3669 unsigned Half = VT.getVectorNumElements() / 2;
3670 WhichResult = (M[0] == 0 ? 0 : 1);
3671 for (unsigned j = 0; j != 2; ++j) {
3672 unsigned Idx = WhichResult;
3673 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003674 int MIdx = M[i + j * Half];
3675 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003676 return false;
3677 Idx += 2;
3678 }
3679 }
3680
3681 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3682 if (VT.is64BitVector() && EltSz == 32)
3683 return false;
3684
3685 return true;
3686}
3687
Bob Wilsonc692cb72009-08-21 20:54:19 +00003688static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3689 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003690 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3691 if (EltSz == 64)
3692 return false;
3693
Bob Wilsonc692cb72009-08-21 20:54:19 +00003694 unsigned NumElts = VT.getVectorNumElements();
3695 WhichResult = (M[0] == 0 ? 0 : 1);
3696 unsigned Idx = WhichResult * NumElts / 2;
3697 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003698 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3699 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003700 return false;
3701 Idx += 1;
3702 }
3703
3704 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003705 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003706 return false;
3707
3708 return true;
3709}
3710
Bob Wilson324f4f12009-12-03 06:40:55 +00003711/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3712/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3713/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3714static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3715 unsigned &WhichResult) {
3716 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3717 if (EltSz == 64)
3718 return false;
3719
3720 unsigned NumElts = VT.getVectorNumElements();
3721 WhichResult = (M[0] == 0 ? 0 : 1);
3722 unsigned Idx = WhichResult * NumElts / 2;
3723 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003724 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3725 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003726 return false;
3727 Idx += 1;
3728 }
3729
3730 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3731 if (VT.is64BitVector() && EltSz == 32)
3732 return false;
3733
3734 return true;
3735}
3736
Dale Johannesenf630c712010-07-29 20:10:08 +00003737// If N is an integer constant that can be moved into a register in one
3738// instruction, return an SDValue of such a constant (will become a MOV
3739// instruction). Otherwise return null.
3740static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3741 const ARMSubtarget *ST, DebugLoc dl) {
3742 uint64_t Val;
3743 if (!isa<ConstantSDNode>(N))
3744 return SDValue();
3745 Val = cast<ConstantSDNode>(N)->getZExtValue();
3746
3747 if (ST->isThumb1Only()) {
3748 if (Val <= 255 || ~Val <= 255)
3749 return DAG.getConstant(Val, MVT::i32);
3750 } else {
3751 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3752 return DAG.getConstant(Val, MVT::i32);
3753 }
3754 return SDValue();
3755}
3756
Bob Wilson5bafff32009-06-22 23:27:02 +00003757// If this is a case we can't handle, return null and let the default
3758// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003759SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3760 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003761 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003762 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003763 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003764
3765 APInt SplatBits, SplatUndef;
3766 unsigned SplatBitSize;
3767 bool HasAnyUndefs;
3768 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003769 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003770 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003771 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003772 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003773 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003774 DAG, VmovVT, VT.is128BitVector(),
3775 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003776 if (Val.getNode()) {
3777 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003778 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003779 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003780
3781 // Try an immediate VMVN.
3782 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3783 ((1LL << SplatBitSize) - 1));
3784 Val = isNEONModifiedImm(NegatedImm,
3785 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003786 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003787 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003788 if (Val.getNode()) {
3789 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003790 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003791 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003792 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003793 }
3794
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003795 // Scan through the operands to see if only one value is used.
3796 unsigned NumElts = VT.getVectorNumElements();
3797 bool isOnlyLowElement = true;
3798 bool usesOnlyOneValue = true;
3799 bool isConstant = true;
3800 SDValue Value;
3801 for (unsigned i = 0; i < NumElts; ++i) {
3802 SDValue V = Op.getOperand(i);
3803 if (V.getOpcode() == ISD::UNDEF)
3804 continue;
3805 if (i > 0)
3806 isOnlyLowElement = false;
3807 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3808 isConstant = false;
3809
3810 if (!Value.getNode())
3811 Value = V;
3812 else if (V != Value)
3813 usesOnlyOneValue = false;
3814 }
3815
3816 if (!Value.getNode())
3817 return DAG.getUNDEF(VT);
3818
3819 if (isOnlyLowElement)
3820 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3821
Dale Johannesenf630c712010-07-29 20:10:08 +00003822 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3823
Dale Johannesen575cd142010-10-19 20:00:17 +00003824 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3825 // i32 and try again.
3826 if (usesOnlyOneValue && EltSize <= 32) {
3827 if (!isConstant)
3828 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3829 if (VT.getVectorElementType().isFloatingPoint()) {
3830 SmallVector<SDValue, 8> Ops;
3831 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003832 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003833 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003834 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3835 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003836 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3837 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003838 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003839 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003840 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3841 if (Val.getNode())
3842 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003843 }
3844
3845 // If all elements are constants and the case above didn't get hit, fall back
3846 // to the default expansion, which will generate a load from the constant
3847 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003848 if (isConstant)
3849 return SDValue();
3850
Bob Wilson11a1dff2011-01-07 21:37:30 +00003851 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3852 if (NumElts >= 4) {
3853 SDValue shuffle = ReconstructShuffle(Op, DAG);
3854 if (shuffle != SDValue())
3855 return shuffle;
3856 }
3857
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003858 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003859 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3860 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003861 if (EltSize >= 32) {
3862 // Do the expansion with floating-point types, since that is what the VFP
3863 // registers are defined to use, and since i64 is not legal.
3864 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3865 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003866 SmallVector<SDValue, 8> Ops;
3867 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003868 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003869 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003870 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003871 }
3872
3873 return SDValue();
3874}
3875
Bob Wilson11a1dff2011-01-07 21:37:30 +00003876// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003877// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003878SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3879 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003880 DebugLoc dl = Op.getDebugLoc();
3881 EVT VT = Op.getValueType();
3882 unsigned NumElts = VT.getVectorNumElements();
3883
3884 SmallVector<SDValue, 2> SourceVecs;
3885 SmallVector<unsigned, 2> MinElts;
3886 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003887
Bob Wilson11a1dff2011-01-07 21:37:30 +00003888 for (unsigned i = 0; i < NumElts; ++i) {
3889 SDValue V = Op.getOperand(i);
3890 if (V.getOpcode() == ISD::UNDEF)
3891 continue;
3892 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3893 // A shuffle can only come from building a vector from various
3894 // elements of other vectors.
3895 return SDValue();
3896 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003897
Bob Wilson11a1dff2011-01-07 21:37:30 +00003898 // Record this extraction against the appropriate vector if possible...
3899 SDValue SourceVec = V.getOperand(0);
3900 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3901 bool FoundSource = false;
3902 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3903 if (SourceVecs[j] == SourceVec) {
3904 if (MinElts[j] > EltNo)
3905 MinElts[j] = EltNo;
3906 if (MaxElts[j] < EltNo)
3907 MaxElts[j] = EltNo;
3908 FoundSource = true;
3909 break;
3910 }
3911 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003912
Bob Wilson11a1dff2011-01-07 21:37:30 +00003913 // Or record a new source if not...
3914 if (!FoundSource) {
3915 SourceVecs.push_back(SourceVec);
3916 MinElts.push_back(EltNo);
3917 MaxElts.push_back(EltNo);
3918 }
3919 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003920
Bob Wilson11a1dff2011-01-07 21:37:30 +00003921 // Currently only do something sane when at most two source vectors
3922 // involved.
3923 if (SourceVecs.size() > 2)
3924 return SDValue();
3925
3926 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3927 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003928
Bob Wilson11a1dff2011-01-07 21:37:30 +00003929 // This loop extracts the usage patterns of the source vectors
3930 // and prepares appropriate SDValues for a shuffle if possible.
3931 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3932 if (SourceVecs[i].getValueType() == VT) {
3933 // No VEXT necessary
3934 ShuffleSrcs[i] = SourceVecs[i];
3935 VEXTOffsets[i] = 0;
3936 continue;
3937 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3938 // It probably isn't worth padding out a smaller vector just to
3939 // break it down again in a shuffle.
3940 return SDValue();
3941 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003942
Bob Wilson11a1dff2011-01-07 21:37:30 +00003943 // Since only 64-bit and 128-bit vectors are legal on ARM and
3944 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003945 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3946 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003947
Bob Wilson11a1dff2011-01-07 21:37:30 +00003948 if (MaxElts[i] - MinElts[i] >= NumElts) {
3949 // Span too large for a VEXT to cope
3950 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003951 }
3952
Bob Wilson11a1dff2011-01-07 21:37:30 +00003953 if (MinElts[i] >= NumElts) {
3954 // The extraction can just take the second half
3955 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003956 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3957 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003958 DAG.getIntPtrConstant(NumElts));
3959 } else if (MaxElts[i] < NumElts) {
3960 // The extraction can just take the first half
3961 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003962 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3963 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003964 DAG.getIntPtrConstant(0));
3965 } else {
3966 // An actual VEXT is needed
3967 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003968 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3969 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003970 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003971 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3972 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003973 DAG.getIntPtrConstant(NumElts));
3974 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3975 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3976 }
3977 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003978
Bob Wilson11a1dff2011-01-07 21:37:30 +00003979 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003980
Bob Wilson11a1dff2011-01-07 21:37:30 +00003981 for (unsigned i = 0; i < NumElts; ++i) {
3982 SDValue Entry = Op.getOperand(i);
3983 if (Entry.getOpcode() == ISD::UNDEF) {
3984 Mask.push_back(-1);
3985 continue;
3986 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003987
Bob Wilson11a1dff2011-01-07 21:37:30 +00003988 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00003989 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
3990 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00003991 if (ExtractVec == SourceVecs[0]) {
3992 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3993 } else {
3994 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3995 }
3996 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003997
Bob Wilson11a1dff2011-01-07 21:37:30 +00003998 // Final check before we try to produce nonsense...
3999 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004000 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4001 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004002
Bob Wilson11a1dff2011-01-07 21:37:30 +00004003 return SDValue();
4004}
4005
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004006/// isShuffleMaskLegal - Targets can use this to indicate that they only
4007/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4008/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4009/// are assumed to be legal.
4010bool
4011ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4012 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004013 if (VT.getVectorNumElements() == 4 &&
4014 (VT.is128BitVector() || VT.is64BitVector())) {
4015 unsigned PFIndexes[4];
4016 for (unsigned i = 0; i != 4; ++i) {
4017 if (M[i] < 0)
4018 PFIndexes[i] = 8;
4019 else
4020 PFIndexes[i] = M[i];
4021 }
4022
4023 // Compute the index in the perfect shuffle table.
4024 unsigned PFTableIndex =
4025 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4026 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4027 unsigned Cost = (PFEntry >> 30);
4028
4029 if (Cost <= 4)
4030 return true;
4031 }
4032
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004033 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004034 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004035
Bob Wilson53dd2452010-06-07 23:53:38 +00004036 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4037 return (EltSize >= 32 ||
4038 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004039 isVREVMask(M, VT, 64) ||
4040 isVREVMask(M, VT, 32) ||
4041 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004042 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004043 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004044 isVTRNMask(M, VT, WhichResult) ||
4045 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004046 isVZIPMask(M, VT, WhichResult) ||
4047 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4048 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4049 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004050}
4051
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004052/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4053/// the specified operations to build the shuffle.
4054static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4055 SDValue RHS, SelectionDAG &DAG,
4056 DebugLoc dl) {
4057 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4058 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4059 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4060
4061 enum {
4062 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4063 OP_VREV,
4064 OP_VDUP0,
4065 OP_VDUP1,
4066 OP_VDUP2,
4067 OP_VDUP3,
4068 OP_VEXT1,
4069 OP_VEXT2,
4070 OP_VEXT3,
4071 OP_VUZPL, // VUZP, left result
4072 OP_VUZPR, // VUZP, right result
4073 OP_VZIPL, // VZIP, left result
4074 OP_VZIPR, // VZIP, right result
4075 OP_VTRNL, // VTRN, left result
4076 OP_VTRNR // VTRN, right result
4077 };
4078
4079 if (OpNum == OP_COPY) {
4080 if (LHSID == (1*9+2)*9+3) return LHS;
4081 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4082 return RHS;
4083 }
4084
4085 SDValue OpLHS, OpRHS;
4086 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4087 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4088 EVT VT = OpLHS.getValueType();
4089
4090 switch (OpNum) {
4091 default: llvm_unreachable("Unknown shuffle opcode!");
4092 case OP_VREV:
4093 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4094 case OP_VDUP0:
4095 case OP_VDUP1:
4096 case OP_VDUP2:
4097 case OP_VDUP3:
4098 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004099 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004100 case OP_VEXT1:
4101 case OP_VEXT2:
4102 case OP_VEXT3:
4103 return DAG.getNode(ARMISD::VEXT, dl, VT,
4104 OpLHS, OpRHS,
4105 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4106 case OP_VUZPL:
4107 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004108 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004109 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4110 case OP_VZIPL:
4111 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004112 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004113 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4114 case OP_VTRNL:
4115 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004116 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4117 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004118 }
4119}
4120
Bill Wendling69a05a72011-03-14 23:02:38 +00004121static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4122 SmallVectorImpl<int> &ShuffleMask,
4123 SelectionDAG &DAG) {
4124 // Check to see if we can use the VTBL instruction.
4125 SDValue V1 = Op.getOperand(0);
4126 SDValue V2 = Op.getOperand(1);
4127 DebugLoc DL = Op.getDebugLoc();
4128
4129 SmallVector<SDValue, 8> VTBLMask;
4130 for (SmallVectorImpl<int>::iterator
4131 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4132 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4133
4134 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4135 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4136 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4137 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004138
4139 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
4140 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4141 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004142}
4143
Bob Wilson5bafff32009-06-22 23:27:02 +00004144static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004145 SDValue V1 = Op.getOperand(0);
4146 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004147 DebugLoc dl = Op.getDebugLoc();
4148 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004149 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004150 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004151
Bob Wilson28865062009-08-13 02:13:04 +00004152 // Convert shuffles that are directly supported on NEON to target-specific
4153 // DAG nodes, instead of keeping them as shuffles and matching them again
4154 // during code selection. This is more efficient and avoids the possibility
4155 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004156 // FIXME: floating-point vectors should be canonicalized to integer vectors
4157 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004158 SVN->getMask(ShuffleMask);
4159
Bob Wilson53dd2452010-06-07 23:53:38 +00004160 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4161 if (EltSize <= 32) {
4162 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4163 int Lane = SVN->getSplatIndex();
4164 // If this is undef splat, generate it via "just" vdup, if possible.
4165 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004166
Bob Wilson53dd2452010-06-07 23:53:38 +00004167 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4168 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4169 }
4170 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4171 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004172 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004173
4174 bool ReverseVEXT;
4175 unsigned Imm;
4176 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4177 if (ReverseVEXT)
4178 std::swap(V1, V2);
4179 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4180 DAG.getConstant(Imm, MVT::i32));
4181 }
4182
4183 if (isVREVMask(ShuffleMask, VT, 64))
4184 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4185 if (isVREVMask(ShuffleMask, VT, 32))
4186 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4187 if (isVREVMask(ShuffleMask, VT, 16))
4188 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4189
4190 // Check for Neon shuffles that modify both input vectors in place.
4191 // If both results are used, i.e., if there are two shuffles with the same
4192 // source operands and with masks corresponding to both results of one of
4193 // these operations, DAG memoization will ensure that a single node is
4194 // used for both shuffles.
4195 unsigned WhichResult;
4196 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4197 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4198 V1, V2).getValue(WhichResult);
4199 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4200 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4201 V1, V2).getValue(WhichResult);
4202 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4203 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4204 V1, V2).getValue(WhichResult);
4205
4206 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4207 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4208 V1, V1).getValue(WhichResult);
4209 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4210 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4211 V1, V1).getValue(WhichResult);
4212 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4213 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4214 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004215 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004216
Bob Wilsonc692cb72009-08-21 20:54:19 +00004217 // If the shuffle is not directly supported and it has 4 elements, use
4218 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004219 unsigned NumElts = VT.getVectorNumElements();
4220 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004221 unsigned PFIndexes[4];
4222 for (unsigned i = 0; i != 4; ++i) {
4223 if (ShuffleMask[i] < 0)
4224 PFIndexes[i] = 8;
4225 else
4226 PFIndexes[i] = ShuffleMask[i];
4227 }
4228
4229 // Compute the index in the perfect shuffle table.
4230 unsigned PFTableIndex =
4231 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004232 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4233 unsigned Cost = (PFEntry >> 30);
4234
4235 if (Cost <= 4)
4236 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4237 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004238
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004239 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004240 if (EltSize >= 32) {
4241 // Do the expansion with floating-point types, since that is what the VFP
4242 // registers are defined to use, and since i64 is not legal.
4243 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4244 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004245 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4246 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004247 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004248 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004249 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004250 Ops.push_back(DAG.getUNDEF(EltVT));
4251 else
4252 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4253 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4254 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4255 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004256 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004257 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004258 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004259 }
4260
Bill Wendling69a05a72011-03-14 23:02:38 +00004261 if (VT == MVT::v8i8) {
4262 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4263 if (NewOp.getNode())
4264 return NewOp;
4265 }
4266
Bob Wilson22cac0d2009-08-14 05:16:33 +00004267 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004268}
4269
Bob Wilson5bafff32009-06-22 23:27:02 +00004270static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004271 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004272 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004273 if (!isa<ConstantSDNode>(Lane))
4274 return SDValue();
4275
4276 SDValue Vec = Op.getOperand(0);
4277 if (Op.getValueType() == MVT::i32 &&
4278 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4279 DebugLoc dl = Op.getDebugLoc();
4280 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4281 }
4282
4283 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004284}
4285
Bob Wilsona6d65862009-08-03 20:36:38 +00004286static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4287 // The only time a CONCAT_VECTORS operation can have legal types is when
4288 // two 64-bit vectors are concatenated to a 128-bit vector.
4289 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4290 "unexpected CONCAT_VECTORS");
4291 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004292 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004293 SDValue Op0 = Op.getOperand(0);
4294 SDValue Op1 = Op.getOperand(1);
4295 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004296 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004297 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004298 DAG.getIntPtrConstant(0));
4299 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004300 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004301 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004302 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004303 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004304}
4305
Bob Wilson626613d2010-11-23 19:38:38 +00004306/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4307/// element has been zero/sign-extended, depending on the isSigned parameter,
4308/// from an integer type half its size.
4309static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4310 bool isSigned) {
4311 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4312 EVT VT = N->getValueType(0);
4313 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4314 SDNode *BVN = N->getOperand(0).getNode();
4315 if (BVN->getValueType(0) != MVT::v4i32 ||
4316 BVN->getOpcode() != ISD::BUILD_VECTOR)
4317 return false;
4318 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4319 unsigned HiElt = 1 - LoElt;
4320 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4321 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4322 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4323 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4324 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4325 return false;
4326 if (isSigned) {
4327 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4328 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4329 return true;
4330 } else {
4331 if (Hi0->isNullValue() && Hi1->isNullValue())
4332 return true;
4333 }
4334 return false;
4335 }
4336
4337 if (N->getOpcode() != ISD::BUILD_VECTOR)
4338 return false;
4339
4340 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4341 SDNode *Elt = N->getOperand(i).getNode();
4342 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4343 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4344 unsigned HalfSize = EltSize / 2;
4345 if (isSigned) {
4346 int64_t SExtVal = C->getSExtValue();
4347 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4348 return false;
4349 } else {
4350 if ((C->getZExtValue() >> HalfSize) != 0)
4351 return false;
4352 }
4353 continue;
4354 }
4355 return false;
4356 }
4357
4358 return true;
4359}
4360
4361/// isSignExtended - Check if a node is a vector value that is sign-extended
4362/// or a constant BUILD_VECTOR with sign-extended elements.
4363static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4364 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4365 return true;
4366 if (isExtendedBUILD_VECTOR(N, DAG, true))
4367 return true;
4368 return false;
4369}
4370
4371/// isZeroExtended - Check if a node is a vector value that is zero-extended
4372/// or a constant BUILD_VECTOR with zero-extended elements.
4373static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4374 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4375 return true;
4376 if (isExtendedBUILD_VECTOR(N, DAG, false))
4377 return true;
4378 return false;
4379}
4380
4381/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4382/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004383static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4384 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4385 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004386 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4387 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4388 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4389 LD->isNonTemporal(), LD->getAlignment());
4390 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4391 // have been legalized as a BITCAST from v4i32.
4392 if (N->getOpcode() == ISD::BITCAST) {
4393 SDNode *BVN = N->getOperand(0).getNode();
4394 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4395 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4396 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4397 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4398 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4399 }
4400 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4401 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4402 EVT VT = N->getValueType(0);
4403 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4404 unsigned NumElts = VT.getVectorNumElements();
4405 MVT TruncVT = MVT::getIntegerVT(EltSize);
4406 SmallVector<SDValue, 8> Ops;
4407 for (unsigned i = 0; i != NumElts; ++i) {
4408 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4409 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004410 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004411 }
4412 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4413 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004414}
4415
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004416static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4417 unsigned Opcode = N->getOpcode();
4418 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4419 SDNode *N0 = N->getOperand(0).getNode();
4420 SDNode *N1 = N->getOperand(1).getNode();
4421 return N0->hasOneUse() && N1->hasOneUse() &&
4422 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4423 }
4424 return false;
4425}
4426
4427static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4428 unsigned Opcode = N->getOpcode();
4429 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4430 SDNode *N0 = N->getOperand(0).getNode();
4431 SDNode *N1 = N->getOperand(1).getNode();
4432 return N0->hasOneUse() && N1->hasOneUse() &&
4433 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4434 }
4435 return false;
4436}
4437
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004438static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4439 // Multiplications are only custom-lowered for 128-bit vectors so that
4440 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4441 EVT VT = Op.getValueType();
4442 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4443 SDNode *N0 = Op.getOperand(0).getNode();
4444 SDNode *N1 = Op.getOperand(1).getNode();
4445 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004446 bool isMLA = false;
4447 bool isN0SExt = isSignExtended(N0, DAG);
4448 bool isN1SExt = isSignExtended(N1, DAG);
4449 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004450 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004451 else {
4452 bool isN0ZExt = isZeroExtended(N0, DAG);
4453 bool isN1ZExt = isZeroExtended(N1, DAG);
4454 if (isN0ZExt && isN1ZExt)
4455 NewOpc = ARMISD::VMULLu;
4456 else if (isN1SExt || isN1ZExt) {
4457 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4458 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4459 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4460 NewOpc = ARMISD::VMULLs;
4461 isMLA = true;
4462 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4463 NewOpc = ARMISD::VMULLu;
4464 isMLA = true;
4465 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4466 std::swap(N0, N1);
4467 NewOpc = ARMISD::VMULLu;
4468 isMLA = true;
4469 }
4470 }
4471
4472 if (!NewOpc) {
4473 if (VT == MVT::v2i64)
4474 // Fall through to expand this. It is not legal.
4475 return SDValue();
4476 else
4477 // Other vector multiplications are legal.
4478 return Op;
4479 }
4480 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004481
4482 // Legalize to a VMULL instruction.
4483 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004484 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004485 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004486 if (!isMLA) {
4487 Op0 = SkipExtension(N0, DAG);
4488 assert(Op0.getValueType().is64BitVector() &&
4489 Op1.getValueType().is64BitVector() &&
4490 "unexpected types for extended operands to VMULL");
4491 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4492 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004493
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004494 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4495 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4496 // vmull q0, d4, d6
4497 // vmlal q0, d5, d6
4498 // is faster than
4499 // vaddl q0, d4, d5
4500 // vmovl q1, d6
4501 // vmul q0, q0, q1
4502 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4503 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4504 EVT Op1VT = Op1.getValueType();
4505 return DAG.getNode(N0->getOpcode(), DL, VT,
4506 DAG.getNode(NewOpc, DL, VT,
4507 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4508 DAG.getNode(NewOpc, DL, VT,
4509 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004510}
4511
Nate Begeman7973f352011-02-11 20:53:29 +00004512static SDValue
4513LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4514 // Convert to float
4515 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4516 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4517 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4518 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4519 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4520 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4521 // Get reciprocal estimate.
4522 // float4 recip = vrecpeq_f32(yf);
4523 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4524 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4525 // Because char has a smaller range than uchar, we can actually get away
4526 // without any newton steps. This requires that we use a weird bias
4527 // of 0xb000, however (again, this has been exhaustively tested).
4528 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4529 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4530 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4531 Y = DAG.getConstant(0xb000, MVT::i32);
4532 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4533 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4534 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4535 // Convert back to short.
4536 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4537 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4538 return X;
4539}
4540
4541static SDValue
4542LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4543 SDValue N2;
4544 // Convert to float.
4545 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4546 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4547 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4548 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4549 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4550 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4551
4552 // Use reciprocal estimate and one refinement step.
4553 // float4 recip = vrecpeq_f32(yf);
4554 // recip *= vrecpsq_f32(yf, recip);
4555 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4556 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
4557 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4558 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4559 N1, N2);
4560 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4561 // Because short has a smaller range than ushort, we can actually get away
4562 // with only a single newton step. This requires that we use a weird bias
4563 // of 89, however (again, this has been exhaustively tested).
4564 // float4 result = as_float4(as_int4(xf*recip) + 89);
4565 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4566 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4567 N1 = DAG.getConstant(89, MVT::i32);
4568 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4569 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4570 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4571 // Convert back to integer and return.
4572 // return vmovn_s32(vcvt_s32_f32(result));
4573 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4574 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4575 return N0;
4576}
4577
4578static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4579 EVT VT = Op.getValueType();
4580 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4581 "unexpected type for custom-lowering ISD::SDIV");
4582
4583 DebugLoc dl = Op.getDebugLoc();
4584 SDValue N0 = Op.getOperand(0);
4585 SDValue N1 = Op.getOperand(1);
4586 SDValue N2, N3;
4587
4588 if (VT == MVT::v8i8) {
4589 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4590 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
4591
4592 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4593 DAG.getIntPtrConstant(4));
4594 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4595 DAG.getIntPtrConstant(4));
4596 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4597 DAG.getIntPtrConstant(0));
4598 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4599 DAG.getIntPtrConstant(0));
4600
4601 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4602 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4603
4604 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4605 N0 = LowerCONCAT_VECTORS(N0, DAG);
4606
4607 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4608 return N0;
4609 }
4610 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4611}
4612
4613static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4614 EVT VT = Op.getValueType();
4615 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4616 "unexpected type for custom-lowering ISD::UDIV");
4617
4618 DebugLoc dl = Op.getDebugLoc();
4619 SDValue N0 = Op.getOperand(0);
4620 SDValue N1 = Op.getOperand(1);
4621 SDValue N2, N3;
4622
4623 if (VT == MVT::v8i8) {
4624 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4625 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
4626
4627 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4628 DAG.getIntPtrConstant(4));
4629 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4630 DAG.getIntPtrConstant(4));
4631 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4632 DAG.getIntPtrConstant(0));
4633 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4634 DAG.getIntPtrConstant(0));
4635
4636 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4637 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
4638
4639 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4640 N0 = LowerCONCAT_VECTORS(N0, DAG);
4641
4642 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
4643 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4644 N0);
4645 return N0;
4646 }
4647
4648 // v4i16 sdiv ... Convert to float.
4649 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4650 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4651 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4652 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4653 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4654 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4655
4656 // Use reciprocal estimate and two refinement steps.
4657 // float4 recip = vrecpeq_f32(yf);
4658 // recip *= vrecpsq_f32(yf, recip);
4659 // recip *= vrecpsq_f32(yf, recip);
4660 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4661 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
4662 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4663 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4664 N1, N2);
4665 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4666 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4667 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4668 N1, N2);
4669 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4670 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4671 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4672 // and that it will never cause us to return an answer too large).
4673 // float4 result = as_float4(as_int4(xf*recip) + 89);
4674 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4675 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4676 N1 = DAG.getConstant(2, MVT::i32);
4677 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4678 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4679 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4680 // Convert back to integer and return.
4681 // return vmovn_u32(vcvt_s32_f32(result));
4682 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4683 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4684 return N0;
4685}
4686
Dan Gohmand858e902010-04-17 15:26:15 +00004687SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004688 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004689 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004690 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004691 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004692 case ISD::GlobalAddress:
4693 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4694 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004695 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004696 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004697 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4698 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004699 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004700 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004701 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004702 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004703 case ISD::SINT_TO_FP:
4704 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4705 case ISD::FP_TO_SINT:
4706 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004707 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004708 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004709 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004710 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004711 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004712 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004713 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004714 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4715 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004716 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004717 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004718 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004719 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004720 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004721 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004722 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004723 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004724 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004725 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004726 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004727 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004728 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004729 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004730 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004731 case ISD::SDIV: return LowerSDIV(Op, DAG);
4732 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004733 }
Dan Gohman475871a2008-07-27 21:46:04 +00004734 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004735}
4736
Duncan Sands1607f052008-12-01 11:39:25 +00004737/// ReplaceNodeResults - Replace the results of node with an illegal result
4738/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004739void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4740 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004741 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004742 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004743 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004744 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004745 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004746 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004747 case ISD::BITCAST:
4748 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004749 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004750 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004751 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004752 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004753 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004754 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004755 if (Res.getNode())
4756 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004757}
Chris Lattner27a6c732007-11-24 07:07:01 +00004758
Evan Chenga8e29892007-01-19 07:51:42 +00004759//===----------------------------------------------------------------------===//
4760// ARM Scheduler Hooks
4761//===----------------------------------------------------------------------===//
4762
4763MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004764ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4765 MachineBasicBlock *BB,
4766 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004767 unsigned dest = MI->getOperand(0).getReg();
4768 unsigned ptr = MI->getOperand(1).getReg();
4769 unsigned oldval = MI->getOperand(2).getReg();
4770 unsigned newval = MI->getOperand(3).getReg();
4771 unsigned scratch = BB->getParent()->getRegInfo()
4772 .createVirtualRegister(ARM::GPRRegisterClass);
4773 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4774 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004775 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004776
4777 unsigned ldrOpc, strOpc;
4778 switch (Size) {
4779 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004780 case 1:
4781 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004782 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004783 break;
4784 case 2:
4785 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4786 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4787 break;
4788 case 4:
4789 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4790 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4791 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004792 }
4793
4794 MachineFunction *MF = BB->getParent();
4795 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4796 MachineFunction::iterator It = BB;
4797 ++It; // insert the new blocks after the current block
4798
4799 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4800 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4801 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4802 MF->insert(It, loop1MBB);
4803 MF->insert(It, loop2MBB);
4804 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004805
4806 // Transfer the remainder of BB and its successor edges to exitMBB.
4807 exitMBB->splice(exitMBB->begin(), BB,
4808 llvm::next(MachineBasicBlock::iterator(MI)),
4809 BB->end());
4810 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004811
4812 // thisMBB:
4813 // ...
4814 // fallthrough --> loop1MBB
4815 BB->addSuccessor(loop1MBB);
4816
4817 // loop1MBB:
4818 // ldrex dest, [ptr]
4819 // cmp dest, oldval
4820 // bne exitMBB
4821 BB = loop1MBB;
4822 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004823 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004824 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004825 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4826 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004827 BB->addSuccessor(loop2MBB);
4828 BB->addSuccessor(exitMBB);
4829
4830 // loop2MBB:
4831 // strex scratch, newval, [ptr]
4832 // cmp scratch, #0
4833 // bne loop1MBB
4834 BB = loop2MBB;
4835 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4836 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004837 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004838 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004839 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4840 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004841 BB->addSuccessor(loop1MBB);
4842 BB->addSuccessor(exitMBB);
4843
4844 // exitMBB:
4845 // ...
4846 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004847
Dan Gohman14152b42010-07-06 20:24:04 +00004848 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004849
Jim Grosbach5278eb82009-12-11 01:42:04 +00004850 return BB;
4851}
4852
4853MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004854ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4855 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004856 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4857 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4858
4859 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004860 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004861 MachineFunction::iterator It = BB;
4862 ++It;
4863
4864 unsigned dest = MI->getOperand(0).getReg();
4865 unsigned ptr = MI->getOperand(1).getReg();
4866 unsigned incr = MI->getOperand(2).getReg();
4867 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004868
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004869 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004870 unsigned ldrOpc, strOpc;
4871 switch (Size) {
4872 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004873 case 1:
4874 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004875 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004876 break;
4877 case 2:
4878 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4879 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4880 break;
4881 case 4:
4882 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4883 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4884 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004885 }
4886
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004887 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4888 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4889 MF->insert(It, loopMBB);
4890 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004891
4892 // Transfer the remainder of BB and its successor edges to exitMBB.
4893 exitMBB->splice(exitMBB->begin(), BB,
4894 llvm::next(MachineBasicBlock::iterator(MI)),
4895 BB->end());
4896 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004897
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004898 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004899 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4900 unsigned scratch2 = (!BinOpcode) ? incr :
4901 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4902
4903 // thisMBB:
4904 // ...
4905 // fallthrough --> loopMBB
4906 BB->addSuccessor(loopMBB);
4907
4908 // loopMBB:
4909 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004910 // <binop> scratch2, dest, incr
4911 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004912 // cmp scratch, #0
4913 // bne- loopMBB
4914 // fallthrough --> exitMBB
4915 BB = loopMBB;
4916 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004917 if (BinOpcode) {
4918 // operand order needs to go the other way for NAND
4919 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4920 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4921 addReg(incr).addReg(dest)).addReg(0);
4922 else
4923 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4924 addReg(dest).addReg(incr)).addReg(0);
4925 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004926
4927 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4928 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004929 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004930 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004931 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4932 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004933
4934 BB->addSuccessor(loopMBB);
4935 BB->addSuccessor(exitMBB);
4936
4937 // exitMBB:
4938 // ...
4939 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004940
Dan Gohman14152b42010-07-06 20:24:04 +00004941 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004942
Jim Grosbachc3c23542009-12-14 04:22:04 +00004943 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004944}
4945
Evan Cheng218977b2010-07-13 19:27:42 +00004946static
4947MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4948 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4949 E = MBB->succ_end(); I != E; ++I)
4950 if (*I != Succ)
4951 return *I;
4952 llvm_unreachable("Expecting a BB with two successors!");
4953}
4954
Jim Grosbache801dc42009-12-12 01:40:06 +00004955MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004956ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004957 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004958 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004959 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004960 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004961 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004962 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004963 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004964 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004965
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004966 case ARM::ATOMIC_LOAD_ADD_I8:
4967 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4968 case ARM::ATOMIC_LOAD_ADD_I16:
4969 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4970 case ARM::ATOMIC_LOAD_ADD_I32:
4971 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004972
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004973 case ARM::ATOMIC_LOAD_AND_I8:
4974 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4975 case ARM::ATOMIC_LOAD_AND_I16:
4976 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4977 case ARM::ATOMIC_LOAD_AND_I32:
4978 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004979
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004980 case ARM::ATOMIC_LOAD_OR_I8:
4981 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4982 case ARM::ATOMIC_LOAD_OR_I16:
4983 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4984 case ARM::ATOMIC_LOAD_OR_I32:
4985 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004986
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004987 case ARM::ATOMIC_LOAD_XOR_I8:
4988 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4989 case ARM::ATOMIC_LOAD_XOR_I16:
4990 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4991 case ARM::ATOMIC_LOAD_XOR_I32:
4992 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004993
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004994 case ARM::ATOMIC_LOAD_NAND_I8:
4995 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4996 case ARM::ATOMIC_LOAD_NAND_I16:
4997 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4998 case ARM::ATOMIC_LOAD_NAND_I32:
4999 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005000
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005001 case ARM::ATOMIC_LOAD_SUB_I8:
5002 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5003 case ARM::ATOMIC_LOAD_SUB_I16:
5004 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5005 case ARM::ATOMIC_LOAD_SUB_I32:
5006 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005007
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005008 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5009 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5010 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005011
5012 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5013 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5014 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005015
Evan Cheng007ea272009-08-12 05:17:19 +00005016 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005017 // To "insert" a SELECT_CC instruction, we actually have to insert the
5018 // diamond control-flow pattern. The incoming instruction knows the
5019 // destination vreg to set, the condition code register to branch on, the
5020 // true/false values to select between, and a branch opcode to use.
5021 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005022 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005023 ++It;
5024
5025 // thisMBB:
5026 // ...
5027 // TrueVal = ...
5028 // cmpTY ccX, r1, r2
5029 // bCC copy1MBB
5030 // fallthrough --> copy0MBB
5031 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005032 MachineFunction *F = BB->getParent();
5033 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5034 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005035 F->insert(It, copy0MBB);
5036 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005037
5038 // Transfer the remainder of BB and its successor edges to sinkMBB.
5039 sinkMBB->splice(sinkMBB->begin(), BB,
5040 llvm::next(MachineBasicBlock::iterator(MI)),
5041 BB->end());
5042 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5043
Dan Gohman258c58c2010-07-06 15:49:48 +00005044 BB->addSuccessor(copy0MBB);
5045 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005046
Dan Gohman14152b42010-07-06 20:24:04 +00005047 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5048 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5049
Evan Chenga8e29892007-01-19 07:51:42 +00005050 // copy0MBB:
5051 // %FalseValue = ...
5052 // # fallthrough to sinkMBB
5053 BB = copy0MBB;
5054
5055 // Update machine-CFG edges
5056 BB->addSuccessor(sinkMBB);
5057
5058 // sinkMBB:
5059 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5060 // ...
5061 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005062 BuildMI(*BB, BB->begin(), dl,
5063 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005064 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5065 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5066
Dan Gohman14152b42010-07-06 20:24:04 +00005067 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005068 return BB;
5069 }
Evan Cheng86198642009-08-07 00:34:42 +00005070
Evan Cheng218977b2010-07-13 19:27:42 +00005071 case ARM::BCCi64:
5072 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005073 // If there is an unconditional branch to the other successor, remove it.
5074 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005075
Evan Cheng218977b2010-07-13 19:27:42 +00005076 // Compare both parts that make up the double comparison separately for
5077 // equality.
5078 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5079
5080 unsigned LHS1 = MI->getOperand(1).getReg();
5081 unsigned LHS2 = MI->getOperand(2).getReg();
5082 if (RHSisZero) {
5083 AddDefaultPred(BuildMI(BB, dl,
5084 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5085 .addReg(LHS1).addImm(0));
5086 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5087 .addReg(LHS2).addImm(0)
5088 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5089 } else {
5090 unsigned RHS1 = MI->getOperand(3).getReg();
5091 unsigned RHS2 = MI->getOperand(4).getReg();
5092 AddDefaultPred(BuildMI(BB, dl,
5093 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5094 .addReg(LHS1).addReg(RHS1));
5095 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5096 .addReg(LHS2).addReg(RHS2)
5097 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5098 }
5099
5100 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5101 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5102 if (MI->getOperand(0).getImm() == ARMCC::NE)
5103 std::swap(destMBB, exitMBB);
5104
5105 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5106 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5107 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5108 .addMBB(exitMBB);
5109
5110 MI->eraseFromParent(); // The pseudo instruction is gone now.
5111 return BB;
5112 }
Evan Chenga8e29892007-01-19 07:51:42 +00005113 }
5114}
5115
5116//===----------------------------------------------------------------------===//
5117// ARM Optimization Hooks
5118//===----------------------------------------------------------------------===//
5119
Chris Lattnerd1980a52009-03-12 06:52:53 +00005120static
5121SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5122 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005123 SelectionDAG &DAG = DCI.DAG;
5124 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005125 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005126 unsigned Opc = N->getOpcode();
5127 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5128 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5129 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5130 ISD::CondCode CC = ISD::SETCC_INVALID;
5131
5132 if (isSlctCC) {
5133 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5134 } else {
5135 SDValue CCOp = Slct.getOperand(0);
5136 if (CCOp.getOpcode() == ISD::SETCC)
5137 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5138 }
5139
5140 bool DoXform = false;
5141 bool InvCC = false;
5142 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5143 "Bad input!");
5144
5145 if (LHS.getOpcode() == ISD::Constant &&
5146 cast<ConstantSDNode>(LHS)->isNullValue()) {
5147 DoXform = true;
5148 } else if (CC != ISD::SETCC_INVALID &&
5149 RHS.getOpcode() == ISD::Constant &&
5150 cast<ConstantSDNode>(RHS)->isNullValue()) {
5151 std::swap(LHS, RHS);
5152 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005153 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005154 Op0.getOperand(0).getValueType();
5155 bool isInt = OpVT.isInteger();
5156 CC = ISD::getSetCCInverse(CC, isInt);
5157
5158 if (!TLI.isCondCodeLegal(CC, OpVT))
5159 return SDValue(); // Inverse operator isn't legal.
5160
5161 DoXform = true;
5162 InvCC = true;
5163 }
5164
5165 if (DoXform) {
5166 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5167 if (isSlctCC)
5168 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5169 Slct.getOperand(0), Slct.getOperand(1), CC);
5170 SDValue CCOp = Slct.getOperand(0);
5171 if (InvCC)
5172 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5173 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5174 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5175 CCOp, OtherOp, Result);
5176 }
5177 return SDValue();
5178}
5179
Bob Wilson3d5792a2010-07-29 20:34:14 +00005180/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5181/// operands N0 and N1. This is a helper for PerformADDCombine that is
5182/// called with the default operands, and if that fails, with commuted
5183/// operands.
5184static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5185 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005186 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5187 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5188 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5189 if (Result.getNode()) return Result;
5190 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005191 return SDValue();
5192}
5193
Bob Wilson3d5792a2010-07-29 20:34:14 +00005194/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5195///
5196static SDValue PerformADDCombine(SDNode *N,
5197 TargetLowering::DAGCombinerInfo &DCI) {
5198 SDValue N0 = N->getOperand(0);
5199 SDValue N1 = N->getOperand(1);
5200
5201 // First try with the default operand order.
5202 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5203 if (Result.getNode())
5204 return Result;
5205
5206 // If that didn't work, try again with the operands commuted.
5207 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5208}
5209
Chris Lattnerd1980a52009-03-12 06:52:53 +00005210/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005211///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005212static SDValue PerformSUBCombine(SDNode *N,
5213 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005214 SDValue N0 = N->getOperand(0);
5215 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005216
Chris Lattnerd1980a52009-03-12 06:52:53 +00005217 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5218 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5219 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5220 if (Result.getNode()) return Result;
5221 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005222
Chris Lattnerd1980a52009-03-12 06:52:53 +00005223 return SDValue();
5224}
5225
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005226static SDValue PerformMULCombine(SDNode *N,
5227 TargetLowering::DAGCombinerInfo &DCI,
5228 const ARMSubtarget *Subtarget) {
5229 SelectionDAG &DAG = DCI.DAG;
5230
5231 if (Subtarget->isThumb1Only())
5232 return SDValue();
5233
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005234 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5235 return SDValue();
5236
5237 EVT VT = N->getValueType(0);
5238 if (VT != MVT::i32)
5239 return SDValue();
5240
5241 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5242 if (!C)
5243 return SDValue();
5244
5245 uint64_t MulAmt = C->getZExtValue();
5246 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5247 ShiftAmt = ShiftAmt & (32 - 1);
5248 SDValue V = N->getOperand(0);
5249 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005250
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005251 SDValue Res;
5252 MulAmt >>= ShiftAmt;
5253 if (isPowerOf2_32(MulAmt - 1)) {
5254 // (mul x, 2^N + 1) => (add (shl x, N), x)
5255 Res = DAG.getNode(ISD::ADD, DL, VT,
5256 V, DAG.getNode(ISD::SHL, DL, VT,
5257 V, DAG.getConstant(Log2_32(MulAmt-1),
5258 MVT::i32)));
5259 } else if (isPowerOf2_32(MulAmt + 1)) {
5260 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5261 Res = DAG.getNode(ISD::SUB, DL, VT,
5262 DAG.getNode(ISD::SHL, DL, VT,
5263 V, DAG.getConstant(Log2_32(MulAmt+1),
5264 MVT::i32)),
5265 V);
5266 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005267 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005268
5269 if (ShiftAmt != 0)
5270 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5271 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005272
5273 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005274 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005275 return SDValue();
5276}
5277
Owen Anderson080c0922010-11-05 19:27:46 +00005278static SDValue PerformANDCombine(SDNode *N,
5279 TargetLowering::DAGCombinerInfo &DCI) {
Eric Christopher29aeed12011-03-26 01:21:03 +00005280
Owen Anderson080c0922010-11-05 19:27:46 +00005281 // Attempt to use immediate-form VBIC
5282 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5283 DebugLoc dl = N->getDebugLoc();
5284 EVT VT = N->getValueType(0);
5285 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005286
Owen Anderson080c0922010-11-05 19:27:46 +00005287 APInt SplatBits, SplatUndef;
5288 unsigned SplatBitSize;
5289 bool HasAnyUndefs;
5290 if (BVN &&
5291 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5292 if (SplatBitSize <= 64) {
5293 EVT VbicVT;
5294 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5295 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005296 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005297 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005298 if (Val.getNode()) {
5299 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005300 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005301 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005302 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005303 }
5304 }
5305 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005306
Owen Anderson080c0922010-11-05 19:27:46 +00005307 return SDValue();
5308}
5309
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005310/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5311static SDValue PerformORCombine(SDNode *N,
5312 TargetLowering::DAGCombinerInfo &DCI,
5313 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005314 // Attempt to use immediate-form VORR
5315 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5316 DebugLoc dl = N->getDebugLoc();
5317 EVT VT = N->getValueType(0);
5318 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005319
Owen Anderson60f48702010-11-03 23:15:26 +00005320 APInt SplatBits, SplatUndef;
5321 unsigned SplatBitSize;
5322 bool HasAnyUndefs;
5323 if (BVN && Subtarget->hasNEON() &&
5324 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5325 if (SplatBitSize <= 64) {
5326 EVT VorrVT;
5327 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5328 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005329 DAG, VorrVT, VT.is128BitVector(),
5330 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005331 if (Val.getNode()) {
5332 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005333 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005334 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005335 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005336 }
5337 }
5338 }
5339
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005340 SDValue N0 = N->getOperand(0);
5341 if (N0.getOpcode() != ISD::AND)
5342 return SDValue();
5343 SDValue N1 = N->getOperand(1);
5344
5345 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5346 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5347 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5348 APInt SplatUndef;
5349 unsigned SplatBitSize;
5350 bool HasAnyUndefs;
5351
5352 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5353 APInt SplatBits0;
5354 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5355 HasAnyUndefs) && !HasAnyUndefs) {
5356 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5357 APInt SplatBits1;
5358 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5359 HasAnyUndefs) && !HasAnyUndefs &&
5360 SplatBits0 == ~SplatBits1) {
5361 // Canonicalize the vector type to make instruction selection simpler.
5362 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5363 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5364 N0->getOperand(1), N0->getOperand(0),
5365 N1->getOperand(1));
5366 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5367 }
5368 }
5369 }
5370
Jim Grosbach54238562010-07-17 03:30:54 +00005371 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5372 // reasonable.
5373
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005374 // BFI is only available on V6T2+
5375 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5376 return SDValue();
5377
Jim Grosbach54238562010-07-17 03:30:54 +00005378 DebugLoc DL = N->getDebugLoc();
5379 // 1) or (and A, mask), val => ARMbfi A, val, mask
5380 // iff (val & mask) == val
5381 //
5382 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5383 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005384 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005385 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005386 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005387 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005388
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005389 if (VT != MVT::i32)
5390 return SDValue();
5391
Evan Cheng30fb13f2010-12-13 20:32:54 +00005392 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005393
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005394 // The value and the mask need to be constants so we can verify this is
5395 // actually a bitfield set. If the mask is 0xffff, we can do better
5396 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005397 SDValue MaskOp = N0.getOperand(1);
5398 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5399 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005400 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005401 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005402 if (Mask == 0xffff)
5403 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005404 SDValue Res;
5405 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005406 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5407 if (N1C) {
5408 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005409 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005410 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005411
Evan Chenga9688c42010-12-11 04:11:38 +00005412 if (ARM::isBitFieldInvertedMask(Mask)) {
5413 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005414
Evan Cheng30fb13f2010-12-13 20:32:54 +00005415 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005416 DAG.getConstant(Val, MVT::i32),
5417 DAG.getConstant(Mask, MVT::i32));
5418
5419 // Do not add new nodes to DAG combiner worklist.
5420 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005421 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005422 }
Jim Grosbach54238562010-07-17 03:30:54 +00005423 } else if (N1.getOpcode() == ISD::AND) {
5424 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005425 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5426 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005427 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005428 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005429
Eric Christopher29aeed12011-03-26 01:21:03 +00005430 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5431 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005432 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005433 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005434 // The pack halfword instruction works better for masks that fit it,
5435 // so use that when it's available.
5436 if (Subtarget->hasT2ExtractPack() &&
5437 (Mask == 0xffff || Mask == 0xffff0000))
5438 return SDValue();
5439 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005440 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005441 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005442 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005443 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005444 DAG.getConstant(Mask, MVT::i32));
5445 // Do not add new nodes to DAG combiner worklist.
5446 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005447 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005448 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005449 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005450 // The pack halfword instruction works better for masks that fit it,
5451 // so use that when it's available.
5452 if (Subtarget->hasT2ExtractPack() &&
5453 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5454 return SDValue();
5455 // 2b
5456 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005457 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005458 DAG.getConstant(lsb, MVT::i32));
5459 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005460 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005461 // Do not add new nodes to DAG combiner worklist.
5462 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005463 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005464 }
5465 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005466
Evan Cheng30fb13f2010-12-13 20:32:54 +00005467 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5468 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5469 ARM::isBitFieldInvertedMask(~Mask)) {
5470 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5471 // where lsb(mask) == #shamt and masked bits of B are known zero.
5472 SDValue ShAmt = N00.getOperand(1);
5473 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5474 unsigned LSB = CountTrailingZeros_32(Mask);
5475 if (ShAmtC != LSB)
5476 return SDValue();
5477
5478 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5479 DAG.getConstant(~Mask, MVT::i32));
5480
5481 // Do not add new nodes to DAG combiner worklist.
5482 DCI.CombineTo(N, Res, false);
5483 }
5484
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005485 return SDValue();
5486}
5487
Evan Cheng0c1aec12010-12-14 03:22:07 +00005488/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5489/// C1 & C2 == C1.
5490static SDValue PerformBFICombine(SDNode *N,
5491 TargetLowering::DAGCombinerInfo &DCI) {
5492 SDValue N1 = N->getOperand(1);
5493 if (N1.getOpcode() == ISD::AND) {
5494 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5495 if (!N11C)
5496 return SDValue();
5497 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5498 unsigned Mask2 = N11C->getZExtValue();
5499 if ((Mask & Mask2) == Mask2)
5500 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5501 N->getOperand(0), N1.getOperand(0),
5502 N->getOperand(2));
5503 }
5504 return SDValue();
5505}
5506
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005507/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5508/// ARMISD::VMOVRRD.
5509static SDValue PerformVMOVRRDCombine(SDNode *N,
5510 TargetLowering::DAGCombinerInfo &DCI) {
5511 // vmovrrd(vmovdrr x, y) -> x,y
5512 SDValue InDouble = N->getOperand(0);
5513 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5514 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
5515 return SDValue();
5516}
5517
5518/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5519/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5520static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5521 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5522 SDValue Op0 = N->getOperand(0);
5523 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005524 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005525 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005526 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005527 Op1 = Op1.getOperand(0);
5528 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5529 Op0.getNode() == Op1.getNode() &&
5530 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005531 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005532 N->getValueType(0), Op0.getOperand(0));
5533 return SDValue();
5534}
5535
Bob Wilson31600902010-12-21 06:43:19 +00005536/// PerformSTORECombine - Target-specific dag combine xforms for
5537/// ISD::STORE.
5538static SDValue PerformSTORECombine(SDNode *N,
5539 TargetLowering::DAGCombinerInfo &DCI) {
5540 // Bitcast an i64 store extracted from a vector to f64.
5541 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5542 StoreSDNode *St = cast<StoreSDNode>(N);
5543 SDValue StVal = St->getValue();
5544 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5545 StVal.getValueType() != MVT::i64 ||
5546 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5547 return SDValue();
5548
5549 SelectionDAG &DAG = DCI.DAG;
5550 DebugLoc dl = StVal.getDebugLoc();
5551 SDValue IntVec = StVal.getOperand(0);
5552 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5553 IntVec.getValueType().getVectorNumElements());
5554 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5555 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5556 Vec, StVal.getOperand(1));
5557 dl = N->getDebugLoc();
5558 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5559 // Make the DAGCombiner fold the bitcasts.
5560 DCI.AddToWorklist(Vec.getNode());
5561 DCI.AddToWorklist(ExtElt.getNode());
5562 DCI.AddToWorklist(V.getNode());
5563 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5564 St->getPointerInfo(), St->isVolatile(),
5565 St->isNonTemporal(), St->getAlignment(),
5566 St->getTBAAInfo());
5567}
5568
5569/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5570/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5571/// i64 vector to have f64 elements, since the value can then be loaded
5572/// directly into a VFP register.
5573static bool hasNormalLoadOperand(SDNode *N) {
5574 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5575 for (unsigned i = 0; i < NumElts; ++i) {
5576 SDNode *Elt = N->getOperand(i).getNode();
5577 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5578 return true;
5579 }
5580 return false;
5581}
5582
Bob Wilson75f02882010-09-17 22:59:05 +00005583/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5584/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005585static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5586 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005587 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5588 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5589 // into a pair of GPRs, which is fine when the value is used as a scalar,
5590 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005591 SelectionDAG &DAG = DCI.DAG;
5592 if (N->getNumOperands() == 2) {
5593 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5594 if (RV.getNode())
5595 return RV;
5596 }
Bob Wilson75f02882010-09-17 22:59:05 +00005597
Bob Wilson31600902010-12-21 06:43:19 +00005598 // Load i64 elements as f64 values so that type legalization does not split
5599 // them up into i32 values.
5600 EVT VT = N->getValueType(0);
5601 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5602 return SDValue();
5603 DebugLoc dl = N->getDebugLoc();
5604 SmallVector<SDValue, 8> Ops;
5605 unsigned NumElts = VT.getVectorNumElements();
5606 for (unsigned i = 0; i < NumElts; ++i) {
5607 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5608 Ops.push_back(V);
5609 // Make the DAGCombiner fold the bitcast.
5610 DCI.AddToWorklist(V.getNode());
5611 }
5612 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5613 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5614 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5615}
5616
5617/// PerformInsertEltCombine - Target-specific dag combine xforms for
5618/// ISD::INSERT_VECTOR_ELT.
5619static SDValue PerformInsertEltCombine(SDNode *N,
5620 TargetLowering::DAGCombinerInfo &DCI) {
5621 // Bitcast an i64 load inserted into a vector to f64.
5622 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5623 EVT VT = N->getValueType(0);
5624 SDNode *Elt = N->getOperand(1).getNode();
5625 if (VT.getVectorElementType() != MVT::i64 ||
5626 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5627 return SDValue();
5628
5629 SelectionDAG &DAG = DCI.DAG;
5630 DebugLoc dl = N->getDebugLoc();
5631 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5632 VT.getVectorNumElements());
5633 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5634 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5635 // Make the DAGCombiner fold the bitcasts.
5636 DCI.AddToWorklist(Vec.getNode());
5637 DCI.AddToWorklist(V.getNode());
5638 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5639 Vec, V, N->getOperand(2));
5640 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005641}
5642
Bob Wilsonf20700c2010-10-27 20:38:28 +00005643/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5644/// ISD::VECTOR_SHUFFLE.
5645static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5646 // The LLVM shufflevector instruction does not require the shuffle mask
5647 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5648 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5649 // operands do not match the mask length, they are extended by concatenating
5650 // them with undef vectors. That is probably the right thing for other
5651 // targets, but for NEON it is better to concatenate two double-register
5652 // size vector operands into a single quad-register size vector. Do that
5653 // transformation here:
5654 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5655 // shuffle(concat(v1, v2), undef)
5656 SDValue Op0 = N->getOperand(0);
5657 SDValue Op1 = N->getOperand(1);
5658 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5659 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5660 Op0.getNumOperands() != 2 ||
5661 Op1.getNumOperands() != 2)
5662 return SDValue();
5663 SDValue Concat0Op1 = Op0.getOperand(1);
5664 SDValue Concat1Op1 = Op1.getOperand(1);
5665 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5666 Concat1Op1.getOpcode() != ISD::UNDEF)
5667 return SDValue();
5668 // Skip the transformation if any of the types are illegal.
5669 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5670 EVT VT = N->getValueType(0);
5671 if (!TLI.isTypeLegal(VT) ||
5672 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5673 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5674 return SDValue();
5675
5676 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5677 Op0.getOperand(0), Op1.getOperand(0));
5678 // Translate the shuffle mask.
5679 SmallVector<int, 16> NewMask;
5680 unsigned NumElts = VT.getVectorNumElements();
5681 unsigned HalfElts = NumElts/2;
5682 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5683 for (unsigned n = 0; n < NumElts; ++n) {
5684 int MaskElt = SVN->getMaskElt(n);
5685 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005686 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005687 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005688 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005689 NewElt = HalfElts + MaskElt - NumElts;
5690 NewMask.push_back(NewElt);
5691 }
5692 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5693 DAG.getUNDEF(VT), NewMask.data());
5694}
5695
Bob Wilson1c3ef902011-02-07 17:43:21 +00005696/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
5697/// NEON load/store intrinsics to merge base address updates.
5698static SDValue CombineBaseUpdate(SDNode *N,
5699 TargetLowering::DAGCombinerInfo &DCI) {
5700 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5701 return SDValue();
5702
5703 SelectionDAG &DAG = DCI.DAG;
5704 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
5705 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
5706 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
5707 SDValue Addr = N->getOperand(AddrOpIdx);
5708
5709 // Search for a use of the address operand that is an increment.
5710 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
5711 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
5712 SDNode *User = *UI;
5713 if (User->getOpcode() != ISD::ADD ||
5714 UI.getUse().getResNo() != Addr.getResNo())
5715 continue;
5716
5717 // Check that the add is independent of the load/store. Otherwise, folding
5718 // it would create a cycle.
5719 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
5720 continue;
5721
5722 // Find the new opcode for the updating load/store.
5723 bool isLoad = true;
5724 bool isLaneOp = false;
5725 unsigned NewOpc = 0;
5726 unsigned NumVecs = 0;
5727 if (isIntrinsic) {
5728 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
5729 switch (IntNo) {
5730 default: assert(0 && "unexpected intrinsic for Neon base update");
5731 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
5732 NumVecs = 1; break;
5733 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
5734 NumVecs = 2; break;
5735 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
5736 NumVecs = 3; break;
5737 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
5738 NumVecs = 4; break;
5739 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
5740 NumVecs = 2; isLaneOp = true; break;
5741 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
5742 NumVecs = 3; isLaneOp = true; break;
5743 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
5744 NumVecs = 4; isLaneOp = true; break;
5745 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
5746 NumVecs = 1; isLoad = false; break;
5747 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
5748 NumVecs = 2; isLoad = false; break;
5749 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
5750 NumVecs = 3; isLoad = false; break;
5751 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
5752 NumVecs = 4; isLoad = false; break;
5753 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
5754 NumVecs = 2; isLoad = false; isLaneOp = true; break;
5755 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
5756 NumVecs = 3; isLoad = false; isLaneOp = true; break;
5757 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
5758 NumVecs = 4; isLoad = false; isLaneOp = true; break;
5759 }
5760 } else {
5761 isLaneOp = true;
5762 switch (N->getOpcode()) {
5763 default: assert(0 && "unexpected opcode for Neon base update");
5764 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
5765 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
5766 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
5767 }
5768 }
5769
5770 // Find the size of memory referenced by the load/store.
5771 EVT VecTy;
5772 if (isLoad)
5773 VecTy = N->getValueType(0);
5774 else
5775 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
5776 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
5777 if (isLaneOp)
5778 NumBytes /= VecTy.getVectorNumElements();
5779
5780 // If the increment is a constant, it must match the memory ref size.
5781 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
5782 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
5783 uint64_t IncVal = CInc->getZExtValue();
5784 if (IncVal != NumBytes)
5785 continue;
5786 } else if (NumBytes >= 3 * 16) {
5787 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
5788 // separate instructions that make it harder to use a non-constant update.
5789 continue;
5790 }
5791
5792 // Create the new updating load/store node.
5793 EVT Tys[6];
5794 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
5795 unsigned n;
5796 for (n = 0; n < NumResultVecs; ++n)
5797 Tys[n] = VecTy;
5798 Tys[n++] = MVT::i32;
5799 Tys[n] = MVT::Other;
5800 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
5801 SmallVector<SDValue, 8> Ops;
5802 Ops.push_back(N->getOperand(0)); // incoming chain
5803 Ops.push_back(N->getOperand(AddrOpIdx));
5804 Ops.push_back(Inc);
5805 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
5806 Ops.push_back(N->getOperand(i));
5807 }
5808 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
5809 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
5810 Ops.data(), Ops.size(),
5811 MemInt->getMemoryVT(),
5812 MemInt->getMemOperand());
5813
5814 // Update the uses.
5815 std::vector<SDValue> NewResults;
5816 for (unsigned i = 0; i < NumResultVecs; ++i) {
5817 NewResults.push_back(SDValue(UpdN.getNode(), i));
5818 }
5819 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
5820 DCI.CombineTo(N, NewResults);
5821 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
5822
5823 break;
5824 }
5825 return SDValue();
5826}
5827
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005828/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5829/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5830/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5831/// return true.
5832static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5833 SelectionDAG &DAG = DCI.DAG;
5834 EVT VT = N->getValueType(0);
5835 // vldN-dup instructions only support 64-bit vectors for N > 1.
5836 if (!VT.is64BitVector())
5837 return false;
5838
5839 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5840 SDNode *VLD = N->getOperand(0).getNode();
5841 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5842 return false;
5843 unsigned NumVecs = 0;
5844 unsigned NewOpc = 0;
5845 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5846 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5847 NumVecs = 2;
5848 NewOpc = ARMISD::VLD2DUP;
5849 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5850 NumVecs = 3;
5851 NewOpc = ARMISD::VLD3DUP;
5852 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5853 NumVecs = 4;
5854 NewOpc = ARMISD::VLD4DUP;
5855 } else {
5856 return false;
5857 }
5858
5859 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5860 // numbers match the load.
5861 unsigned VLDLaneNo =
5862 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5863 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5864 UI != UE; ++UI) {
5865 // Ignore uses of the chain result.
5866 if (UI.getUse().getResNo() == NumVecs)
5867 continue;
5868 SDNode *User = *UI;
5869 if (User->getOpcode() != ARMISD::VDUPLANE ||
5870 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5871 return false;
5872 }
5873
5874 // Create the vldN-dup node.
5875 EVT Tys[5];
5876 unsigned n;
5877 for (n = 0; n < NumVecs; ++n)
5878 Tys[n] = VT;
5879 Tys[n] = MVT::Other;
5880 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5881 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5882 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5883 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5884 Ops, 2, VLDMemInt->getMemoryVT(),
5885 VLDMemInt->getMemOperand());
5886
5887 // Update the uses.
5888 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5889 UI != UE; ++UI) {
5890 unsigned ResNo = UI.getUse().getResNo();
5891 // Ignore uses of the chain result.
5892 if (ResNo == NumVecs)
5893 continue;
5894 SDNode *User = *UI;
5895 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5896 }
5897
5898 // Now the vldN-lane intrinsic is dead except for its chain result.
5899 // Update uses of the chain.
5900 std::vector<SDValue> VLDDupResults;
5901 for (unsigned n = 0; n < NumVecs; ++n)
5902 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5903 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5904 DCI.CombineTo(VLD, VLDDupResults);
5905
5906 return true;
5907}
5908
Bob Wilson9e82bf12010-07-14 01:22:12 +00005909/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5910/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005911static SDValue PerformVDUPLANECombine(SDNode *N,
5912 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005913 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005914
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005915 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5916 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5917 if (CombineVLDDUP(N, DCI))
5918 return SDValue(N, 0);
5919
5920 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5921 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005922 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005923 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005924 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005925 return SDValue();
5926
5927 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5928 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5929 // The canonical VMOV for a zero vector uses a 32-bit element size.
5930 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5931 unsigned EltBits;
5932 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5933 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005934 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005935 if (EltSize > VT.getVectorElementType().getSizeInBits())
5936 return SDValue();
5937
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005938 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005939}
5940
Bob Wilson5bafff32009-06-22 23:27:02 +00005941/// getVShiftImm - Check if this is a valid build_vector for the immediate
5942/// operand of a vector shift operation, where all the elements of the
5943/// build_vector must have the same constant integer value.
5944static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5945 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005946 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005947 Op = Op.getOperand(0);
5948 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5949 APInt SplatBits, SplatUndef;
5950 unsigned SplatBitSize;
5951 bool HasAnyUndefs;
5952 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5953 HasAnyUndefs, ElementBits) ||
5954 SplatBitSize > ElementBits)
5955 return false;
5956 Cnt = SplatBits.getSExtValue();
5957 return true;
5958}
5959
5960/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5961/// operand of a vector shift left operation. That value must be in the range:
5962/// 0 <= Value < ElementBits for a left shift; or
5963/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005964static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005965 assert(VT.isVector() && "vector shift count is not a vector type");
5966 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5967 if (! getVShiftImm(Op, ElementBits, Cnt))
5968 return false;
5969 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5970}
5971
5972/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5973/// operand of a vector shift right operation. For a shift opcode, the value
5974/// is positive, but for an intrinsic the value count must be negative. The
5975/// absolute value must be in the range:
5976/// 1 <= |Value| <= ElementBits for a right shift; or
5977/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005978static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005979 int64_t &Cnt) {
5980 assert(VT.isVector() && "vector shift count is not a vector type");
5981 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5982 if (! getVShiftImm(Op, ElementBits, Cnt))
5983 return false;
5984 if (isIntrinsic)
5985 Cnt = -Cnt;
5986 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5987}
5988
5989/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5990static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5991 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5992 switch (IntNo) {
5993 default:
5994 // Don't do anything for most intrinsics.
5995 break;
5996
5997 // Vector shifts: check for immediate versions and lower them.
5998 // Note: This is done during DAG combining instead of DAG legalizing because
5999 // the build_vectors for 64-bit vector element shift counts are generally
6000 // not legal, and it is hard to see their values after they get legalized to
6001 // loads from a constant pool.
6002 case Intrinsic::arm_neon_vshifts:
6003 case Intrinsic::arm_neon_vshiftu:
6004 case Intrinsic::arm_neon_vshiftls:
6005 case Intrinsic::arm_neon_vshiftlu:
6006 case Intrinsic::arm_neon_vshiftn:
6007 case Intrinsic::arm_neon_vrshifts:
6008 case Intrinsic::arm_neon_vrshiftu:
6009 case Intrinsic::arm_neon_vrshiftn:
6010 case Intrinsic::arm_neon_vqshifts:
6011 case Intrinsic::arm_neon_vqshiftu:
6012 case Intrinsic::arm_neon_vqshiftsu:
6013 case Intrinsic::arm_neon_vqshiftns:
6014 case Intrinsic::arm_neon_vqshiftnu:
6015 case Intrinsic::arm_neon_vqshiftnsu:
6016 case Intrinsic::arm_neon_vqrshiftns:
6017 case Intrinsic::arm_neon_vqrshiftnu:
6018 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006019 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006020 int64_t Cnt;
6021 unsigned VShiftOpc = 0;
6022
6023 switch (IntNo) {
6024 case Intrinsic::arm_neon_vshifts:
6025 case Intrinsic::arm_neon_vshiftu:
6026 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6027 VShiftOpc = ARMISD::VSHL;
6028 break;
6029 }
6030 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6031 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6032 ARMISD::VSHRs : ARMISD::VSHRu);
6033 break;
6034 }
6035 return SDValue();
6036
6037 case Intrinsic::arm_neon_vshiftls:
6038 case Intrinsic::arm_neon_vshiftlu:
6039 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6040 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006041 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006042
6043 case Intrinsic::arm_neon_vrshifts:
6044 case Intrinsic::arm_neon_vrshiftu:
6045 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6046 break;
6047 return SDValue();
6048
6049 case Intrinsic::arm_neon_vqshifts:
6050 case Intrinsic::arm_neon_vqshiftu:
6051 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6052 break;
6053 return SDValue();
6054
6055 case Intrinsic::arm_neon_vqshiftsu:
6056 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6057 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006058 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006059
6060 case Intrinsic::arm_neon_vshiftn:
6061 case Intrinsic::arm_neon_vrshiftn:
6062 case Intrinsic::arm_neon_vqshiftns:
6063 case Intrinsic::arm_neon_vqshiftnu:
6064 case Intrinsic::arm_neon_vqshiftnsu:
6065 case Intrinsic::arm_neon_vqrshiftns:
6066 case Intrinsic::arm_neon_vqrshiftnu:
6067 case Intrinsic::arm_neon_vqrshiftnsu:
6068 // Narrowing shifts require an immediate right shift.
6069 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6070 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006071 llvm_unreachable("invalid shift count for narrowing vector shift "
6072 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006073
6074 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006075 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006076 }
6077
6078 switch (IntNo) {
6079 case Intrinsic::arm_neon_vshifts:
6080 case Intrinsic::arm_neon_vshiftu:
6081 // Opcode already set above.
6082 break;
6083 case Intrinsic::arm_neon_vshiftls:
6084 case Intrinsic::arm_neon_vshiftlu:
6085 if (Cnt == VT.getVectorElementType().getSizeInBits())
6086 VShiftOpc = ARMISD::VSHLLi;
6087 else
6088 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6089 ARMISD::VSHLLs : ARMISD::VSHLLu);
6090 break;
6091 case Intrinsic::arm_neon_vshiftn:
6092 VShiftOpc = ARMISD::VSHRN; break;
6093 case Intrinsic::arm_neon_vrshifts:
6094 VShiftOpc = ARMISD::VRSHRs; break;
6095 case Intrinsic::arm_neon_vrshiftu:
6096 VShiftOpc = ARMISD::VRSHRu; break;
6097 case Intrinsic::arm_neon_vrshiftn:
6098 VShiftOpc = ARMISD::VRSHRN; break;
6099 case Intrinsic::arm_neon_vqshifts:
6100 VShiftOpc = ARMISD::VQSHLs; break;
6101 case Intrinsic::arm_neon_vqshiftu:
6102 VShiftOpc = ARMISD::VQSHLu; break;
6103 case Intrinsic::arm_neon_vqshiftsu:
6104 VShiftOpc = ARMISD::VQSHLsu; break;
6105 case Intrinsic::arm_neon_vqshiftns:
6106 VShiftOpc = ARMISD::VQSHRNs; break;
6107 case Intrinsic::arm_neon_vqshiftnu:
6108 VShiftOpc = ARMISD::VQSHRNu; break;
6109 case Intrinsic::arm_neon_vqshiftnsu:
6110 VShiftOpc = ARMISD::VQSHRNsu; break;
6111 case Intrinsic::arm_neon_vqrshiftns:
6112 VShiftOpc = ARMISD::VQRSHRNs; break;
6113 case Intrinsic::arm_neon_vqrshiftnu:
6114 VShiftOpc = ARMISD::VQRSHRNu; break;
6115 case Intrinsic::arm_neon_vqrshiftnsu:
6116 VShiftOpc = ARMISD::VQRSHRNsu; break;
6117 }
6118
6119 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006120 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006121 }
6122
6123 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006124 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006125 int64_t Cnt;
6126 unsigned VShiftOpc = 0;
6127
6128 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6129 VShiftOpc = ARMISD::VSLI;
6130 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6131 VShiftOpc = ARMISD::VSRI;
6132 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006133 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006134 }
6135
6136 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6137 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006138 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006139 }
6140
6141 case Intrinsic::arm_neon_vqrshifts:
6142 case Intrinsic::arm_neon_vqrshiftu:
6143 // No immediate versions of these to check for.
6144 break;
6145 }
6146
6147 return SDValue();
6148}
6149
6150/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6151/// lowers them. As with the vector shift intrinsics, this is done during DAG
6152/// combining instead of DAG legalizing because the build_vectors for 64-bit
6153/// vector element shift counts are generally not legal, and it is hard to see
6154/// their values after they get legalized to loads from a constant pool.
6155static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6156 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006157 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006158
6159 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006160 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6161 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006162 return SDValue();
6163
6164 assert(ST->hasNEON() && "unexpected vector shift");
6165 int64_t Cnt;
6166
6167 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006168 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006169
6170 case ISD::SHL:
6171 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6172 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006173 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006174 break;
6175
6176 case ISD::SRA:
6177 case ISD::SRL:
6178 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6179 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6180 ARMISD::VSHRs : ARMISD::VSHRu);
6181 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006182 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006183 }
6184 }
6185 return SDValue();
6186}
6187
6188/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6189/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6190static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6191 const ARMSubtarget *ST) {
6192 SDValue N0 = N->getOperand(0);
6193
6194 // Check for sign- and zero-extensions of vector extract operations of 8-
6195 // and 16-bit vector elements. NEON supports these directly. They are
6196 // handled during DAG combining because type legalization will promote them
6197 // to 32-bit types and it is messy to recognize the operations after that.
6198 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6199 SDValue Vec = N0.getOperand(0);
6200 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006201 EVT VT = N->getValueType(0);
6202 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006203 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6204
Owen Anderson825b72b2009-08-11 20:47:22 +00006205 if (VT == MVT::i32 &&
6206 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006207 TLI.isTypeLegal(Vec.getValueType()) &&
6208 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006209
6210 unsigned Opc = 0;
6211 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006212 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006213 case ISD::SIGN_EXTEND:
6214 Opc = ARMISD::VGETLANEs;
6215 break;
6216 case ISD::ZERO_EXTEND:
6217 case ISD::ANY_EXTEND:
6218 Opc = ARMISD::VGETLANEu;
6219 break;
6220 }
6221 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6222 }
6223 }
6224
6225 return SDValue();
6226}
6227
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006228/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6229/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6230static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6231 const ARMSubtarget *ST) {
6232 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006233 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006234 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6235 // a NaN; only do the transformation when it matches that behavior.
6236
6237 // For now only do this when using NEON for FP operations; if using VFP, it
6238 // is not obvious that the benefit outweighs the cost of switching to the
6239 // NEON pipeline.
6240 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6241 N->getValueType(0) != MVT::f32)
6242 return SDValue();
6243
6244 SDValue CondLHS = N->getOperand(0);
6245 SDValue CondRHS = N->getOperand(1);
6246 SDValue LHS = N->getOperand(2);
6247 SDValue RHS = N->getOperand(3);
6248 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6249
6250 unsigned Opcode = 0;
6251 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006252 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006253 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006254 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006255 IsReversed = true ; // x CC y ? y : x
6256 } else {
6257 return SDValue();
6258 }
6259
Bob Wilsone742bb52010-02-24 22:15:53 +00006260 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006261 switch (CC) {
6262 default: break;
6263 case ISD::SETOLT:
6264 case ISD::SETOLE:
6265 case ISD::SETLT:
6266 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006267 case ISD::SETULT:
6268 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006269 // If LHS is NaN, an ordered comparison will be false and the result will
6270 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6271 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6272 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6273 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6274 break;
6275 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6276 // will return -0, so vmin can only be used for unsafe math or if one of
6277 // the operands is known to be nonzero.
6278 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6279 !UnsafeFPMath &&
6280 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6281 break;
6282 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006283 break;
6284
6285 case ISD::SETOGT:
6286 case ISD::SETOGE:
6287 case ISD::SETGT:
6288 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006289 case ISD::SETUGT:
6290 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006291 // If LHS is NaN, an ordered comparison will be false and the result will
6292 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6293 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6294 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6295 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6296 break;
6297 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6298 // will return +0, so vmax can only be used for unsafe math or if one of
6299 // the operands is known to be nonzero.
6300 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6301 !UnsafeFPMath &&
6302 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6303 break;
6304 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006305 break;
6306 }
6307
6308 if (!Opcode)
6309 return SDValue();
6310 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6311}
6312
Dan Gohman475871a2008-07-27 21:46:04 +00006313SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006314 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006315 switch (N->getOpcode()) {
6316 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006317 case ISD::ADD: return PerformADDCombine(N, DCI);
6318 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006319 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006320 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006321 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006322 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006323 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006324 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006325 case ISD::STORE: return PerformSTORECombine(N, DCI);
6326 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6327 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006328 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006329 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006330 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006331 case ISD::SHL:
6332 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006333 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006334 case ISD::SIGN_EXTEND:
6335 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006336 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6337 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006338 case ARMISD::VLD2DUP:
6339 case ARMISD::VLD3DUP:
6340 case ARMISD::VLD4DUP:
6341 return CombineBaseUpdate(N, DCI);
6342 case ISD::INTRINSIC_VOID:
6343 case ISD::INTRINSIC_W_CHAIN:
6344 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6345 case Intrinsic::arm_neon_vld1:
6346 case Intrinsic::arm_neon_vld2:
6347 case Intrinsic::arm_neon_vld3:
6348 case Intrinsic::arm_neon_vld4:
6349 case Intrinsic::arm_neon_vld2lane:
6350 case Intrinsic::arm_neon_vld3lane:
6351 case Intrinsic::arm_neon_vld4lane:
6352 case Intrinsic::arm_neon_vst1:
6353 case Intrinsic::arm_neon_vst2:
6354 case Intrinsic::arm_neon_vst3:
6355 case Intrinsic::arm_neon_vst4:
6356 case Intrinsic::arm_neon_vst2lane:
6357 case Intrinsic::arm_neon_vst3lane:
6358 case Intrinsic::arm_neon_vst4lane:
6359 return CombineBaseUpdate(N, DCI);
6360 default: break;
6361 }
6362 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006363 }
Dan Gohman475871a2008-07-27 21:46:04 +00006364 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006365}
6366
Evan Cheng31959b12011-02-02 01:06:55 +00006367bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6368 EVT VT) const {
6369 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6370}
6371
Bill Wendlingaf566342009-08-15 21:21:19 +00006372bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006373 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006374 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006375
6376 switch (VT.getSimpleVT().SimpleTy) {
6377 default:
6378 return false;
6379 case MVT::i8:
6380 case MVT::i16:
6381 case MVT::i32:
6382 return true;
6383 // FIXME: VLD1 etc with standard alignment is legal.
6384 }
6385}
6386
Evan Chenge6c835f2009-08-14 20:09:37 +00006387static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6388 if (V < 0)
6389 return false;
6390
6391 unsigned Scale = 1;
6392 switch (VT.getSimpleVT().SimpleTy) {
6393 default: return false;
6394 case MVT::i1:
6395 case MVT::i8:
6396 // Scale == 1;
6397 break;
6398 case MVT::i16:
6399 // Scale == 2;
6400 Scale = 2;
6401 break;
6402 case MVT::i32:
6403 // Scale == 4;
6404 Scale = 4;
6405 break;
6406 }
6407
6408 if ((V & (Scale - 1)) != 0)
6409 return false;
6410 V /= Scale;
6411 return V == (V & ((1LL << 5) - 1));
6412}
6413
6414static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6415 const ARMSubtarget *Subtarget) {
6416 bool isNeg = false;
6417 if (V < 0) {
6418 isNeg = true;
6419 V = - V;
6420 }
6421
6422 switch (VT.getSimpleVT().SimpleTy) {
6423 default: return false;
6424 case MVT::i1:
6425 case MVT::i8:
6426 case MVT::i16:
6427 case MVT::i32:
6428 // + imm12 or - imm8
6429 if (isNeg)
6430 return V == (V & ((1LL << 8) - 1));
6431 return V == (V & ((1LL << 12) - 1));
6432 case MVT::f32:
6433 case MVT::f64:
6434 // Same as ARM mode. FIXME: NEON?
6435 if (!Subtarget->hasVFP2())
6436 return false;
6437 if ((V & 3) != 0)
6438 return false;
6439 V >>= 2;
6440 return V == (V & ((1LL << 8) - 1));
6441 }
6442}
6443
Evan Chengb01fad62007-03-12 23:30:29 +00006444/// isLegalAddressImmediate - Return true if the integer value can be used
6445/// as the offset of the target addressing mode for load / store of the
6446/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006447static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006448 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006449 if (V == 0)
6450 return true;
6451
Evan Cheng65011532009-03-09 19:15:00 +00006452 if (!VT.isSimple())
6453 return false;
6454
Evan Chenge6c835f2009-08-14 20:09:37 +00006455 if (Subtarget->isThumb1Only())
6456 return isLegalT1AddressImmediate(V, VT);
6457 else if (Subtarget->isThumb2())
6458 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006459
Evan Chenge6c835f2009-08-14 20:09:37 +00006460 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006461 if (V < 0)
6462 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006463 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006464 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006465 case MVT::i1:
6466 case MVT::i8:
6467 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006468 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006469 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006470 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006471 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006472 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006473 case MVT::f32:
6474 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006475 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006476 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006477 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006478 return false;
6479 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006480 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006481 }
Evan Chenga8e29892007-01-19 07:51:42 +00006482}
6483
Evan Chenge6c835f2009-08-14 20:09:37 +00006484bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6485 EVT VT) const {
6486 int Scale = AM.Scale;
6487 if (Scale < 0)
6488 return false;
6489
6490 switch (VT.getSimpleVT().SimpleTy) {
6491 default: return false;
6492 case MVT::i1:
6493 case MVT::i8:
6494 case MVT::i16:
6495 case MVT::i32:
6496 if (Scale == 1)
6497 return true;
6498 // r + r << imm
6499 Scale = Scale & ~1;
6500 return Scale == 2 || Scale == 4 || Scale == 8;
6501 case MVT::i64:
6502 // r + r
6503 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6504 return true;
6505 return false;
6506 case MVT::isVoid:
6507 // Note, we allow "void" uses (basically, uses that aren't loads or
6508 // stores), because arm allows folding a scale into many arithmetic
6509 // operations. This should be made more precise and revisited later.
6510
6511 // Allow r << imm, but the imm has to be a multiple of two.
6512 if (Scale & 1) return false;
6513 return isPowerOf2_32(Scale);
6514 }
6515}
6516
Chris Lattner37caf8c2007-04-09 23:33:39 +00006517/// isLegalAddressingMode - Return true if the addressing mode represented
6518/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006519bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006520 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006521 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006522 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006523 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006524
Chris Lattner37caf8c2007-04-09 23:33:39 +00006525 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006526 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006527 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006528
Chris Lattner37caf8c2007-04-09 23:33:39 +00006529 switch (AM.Scale) {
6530 case 0: // no scale reg, must be "r+i" or "r", or "i".
6531 break;
6532 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006533 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006534 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006535 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006536 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006537 // ARM doesn't support any R+R*scale+imm addr modes.
6538 if (AM.BaseOffs)
6539 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006540
Bob Wilson2c7dab12009-04-08 17:55:28 +00006541 if (!VT.isSimple())
6542 return false;
6543
Evan Chenge6c835f2009-08-14 20:09:37 +00006544 if (Subtarget->isThumb2())
6545 return isLegalT2ScaledAddressingMode(AM, VT);
6546
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006547 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006548 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006549 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006550 case MVT::i1:
6551 case MVT::i8:
6552 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006553 if (Scale < 0) Scale = -Scale;
6554 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006555 return true;
6556 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006557 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006558 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006559 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006560 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006561 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006562 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006563 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006564
Owen Anderson825b72b2009-08-11 20:47:22 +00006565 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006566 // Note, we allow "void" uses (basically, uses that aren't loads or
6567 // stores), because arm allows folding a scale into many arithmetic
6568 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006569
Chris Lattner37caf8c2007-04-09 23:33:39 +00006570 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006571 if (Scale & 1) return false;
6572 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006573 }
6574 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006575 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006576 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006577}
6578
Evan Cheng77e47512009-11-11 19:05:52 +00006579/// isLegalICmpImmediate - Return true if the specified immediate is legal
6580/// icmp immediate, that is the target has icmp instructions which can compare
6581/// a register against the immediate without having to materialize the
6582/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006583bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006584 if (!Subtarget->isThumb())
6585 return ARM_AM::getSOImmVal(Imm) != -1;
6586 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006587 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006588 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006589}
6590
Owen Andersone50ed302009-08-10 22:56:29 +00006591static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006592 bool isSEXTLoad, SDValue &Base,
6593 SDValue &Offset, bool &isInc,
6594 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00006595 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6596 return false;
6597
Owen Anderson825b72b2009-08-11 20:47:22 +00006598 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00006599 // AddressingMode 3
6600 Base = Ptr->getOperand(0);
6601 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006602 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006603 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006604 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006605 isInc = false;
6606 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6607 return true;
6608 }
6609 }
6610 isInc = (Ptr->getOpcode() == ISD::ADD);
6611 Offset = Ptr->getOperand(1);
6612 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00006613 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00006614 // AddressingMode 2
6615 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006616 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006617 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006618 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006619 isInc = false;
6620 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6621 Base = Ptr->getOperand(0);
6622 return true;
6623 }
6624 }
6625
6626 if (Ptr->getOpcode() == ISD::ADD) {
6627 isInc = true;
6628 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
6629 if (ShOpcVal != ARM_AM::no_shift) {
6630 Base = Ptr->getOperand(1);
6631 Offset = Ptr->getOperand(0);
6632 } else {
6633 Base = Ptr->getOperand(0);
6634 Offset = Ptr->getOperand(1);
6635 }
6636 return true;
6637 }
6638
6639 isInc = (Ptr->getOpcode() == ISD::ADD);
6640 Base = Ptr->getOperand(0);
6641 Offset = Ptr->getOperand(1);
6642 return true;
6643 }
6644
Jim Grosbache5165492009-11-09 00:11:35 +00006645 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006646 return false;
6647}
6648
Owen Andersone50ed302009-08-10 22:56:29 +00006649static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006650 bool isSEXTLoad, SDValue &Base,
6651 SDValue &Offset, bool &isInc,
6652 SelectionDAG &DAG) {
6653 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6654 return false;
6655
6656 Base = Ptr->getOperand(0);
6657 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6658 int RHSC = (int)RHS->getZExtValue();
6659 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6660 assert(Ptr->getOpcode() == ISD::ADD);
6661 isInc = false;
6662 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6663 return true;
6664 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6665 isInc = Ptr->getOpcode() == ISD::ADD;
6666 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6667 return true;
6668 }
6669 }
6670
6671 return false;
6672}
6673
Evan Chenga8e29892007-01-19 07:51:42 +00006674/// getPreIndexedAddressParts - returns true by value, base pointer and
6675/// offset pointer and addressing mode by reference if the node's address
6676/// can be legally represented as pre-indexed load / store address.
6677bool
Dan Gohman475871a2008-07-27 21:46:04 +00006678ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6679 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006680 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006681 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006682 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006683 return false;
6684
Owen Andersone50ed302009-08-10 22:56:29 +00006685 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006686 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006687 bool isSEXTLoad = false;
6688 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6689 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006690 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006691 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6692 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6693 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006694 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006695 } else
6696 return false;
6697
6698 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006699 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006700 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006701 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6702 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006703 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006704 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006705 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006706 if (!isLegal)
6707 return false;
6708
6709 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6710 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006711}
6712
6713/// getPostIndexedAddressParts - returns true by value, base pointer and
6714/// offset pointer and addressing mode by reference if this node can be
6715/// combined with a load / store to form a post-indexed load / store.
6716bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006717 SDValue &Base,
6718 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006719 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006720 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006721 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006722 return false;
6723
Owen Andersone50ed302009-08-10 22:56:29 +00006724 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006725 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006726 bool isSEXTLoad = false;
6727 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006728 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006729 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006730 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6731 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006732 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006733 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006734 } else
6735 return false;
6736
6737 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006738 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006739 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006740 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006741 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006742 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006743 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6744 isInc, DAG);
6745 if (!isLegal)
6746 return false;
6747
Evan Cheng28dad2a2010-05-18 21:31:17 +00006748 if (Ptr != Base) {
6749 // Swap base ptr and offset to catch more post-index load / store when
6750 // it's legal. In Thumb2 mode, offset must be an immediate.
6751 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6752 !Subtarget->isThumb2())
6753 std::swap(Base, Offset);
6754
6755 // Post-indexed load / store update the base pointer.
6756 if (Ptr != Base)
6757 return false;
6758 }
6759
Evan Chenge88d5ce2009-07-02 07:28:31 +00006760 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6761 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006762}
6763
Dan Gohman475871a2008-07-27 21:46:04 +00006764void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006765 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006766 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006767 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006768 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006769 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006770 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006771 switch (Op.getOpcode()) {
6772 default: break;
6773 case ARMISD::CMOV: {
6774 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006775 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006776 if (KnownZero == 0 && KnownOne == 0) return;
6777
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006778 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006779 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6780 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006781 KnownZero &= KnownZeroRHS;
6782 KnownOne &= KnownOneRHS;
6783 return;
6784 }
6785 }
6786}
6787
6788//===----------------------------------------------------------------------===//
6789// ARM Inline Assembly Support
6790//===----------------------------------------------------------------------===//
6791
Evan Cheng55d42002011-01-08 01:24:27 +00006792bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6793 // Looking for "rev" which is V6+.
6794 if (!Subtarget->hasV6Ops())
6795 return false;
6796
6797 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6798 std::string AsmStr = IA->getAsmString();
6799 SmallVector<StringRef, 4> AsmPieces;
6800 SplitString(AsmStr, AsmPieces, ";\n");
6801
6802 switch (AsmPieces.size()) {
6803 default: return false;
6804 case 1:
6805 AsmStr = AsmPieces[0];
6806 AsmPieces.clear();
6807 SplitString(AsmStr, AsmPieces, " \t,");
6808
6809 // rev $0, $1
6810 if (AsmPieces.size() == 3 &&
6811 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6812 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6813 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6814 if (Ty && Ty->getBitWidth() == 32)
6815 return IntrinsicLowering::LowerToByteSwap(CI);
6816 }
6817 break;
6818 }
6819
6820 return false;
6821}
6822
Evan Chenga8e29892007-01-19 07:51:42 +00006823/// getConstraintType - Given a constraint letter, return the type of
6824/// constraint it is for this target.
6825ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006826ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6827 if (Constraint.size() == 1) {
6828 switch (Constraint[0]) {
6829 default: break;
6830 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006831 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006832 }
Evan Chenga8e29892007-01-19 07:51:42 +00006833 }
Chris Lattner4234f572007-03-25 02:14:49 +00006834 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006835}
6836
John Thompson44ab89e2010-10-29 17:29:13 +00006837/// Examine constraint type and operand type and determine a weight value.
6838/// This object must already have been set up with the operand type
6839/// and the current alternative constraint selected.
6840TargetLowering::ConstraintWeight
6841ARMTargetLowering::getSingleConstraintMatchWeight(
6842 AsmOperandInfo &info, const char *constraint) const {
6843 ConstraintWeight weight = CW_Invalid;
6844 Value *CallOperandVal = info.CallOperandVal;
6845 // If we don't have a value, we can't do a match,
6846 // but allow it at the lowest weight.
6847 if (CallOperandVal == NULL)
6848 return CW_Default;
6849 const Type *type = CallOperandVal->getType();
6850 // Look at the constraint type.
6851 switch (*constraint) {
6852 default:
6853 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6854 break;
6855 case 'l':
6856 if (type->isIntegerTy()) {
6857 if (Subtarget->isThumb())
6858 weight = CW_SpecificReg;
6859 else
6860 weight = CW_Register;
6861 }
6862 break;
6863 case 'w':
6864 if (type->isFloatingPointTy())
6865 weight = CW_Register;
6866 break;
6867 }
6868 return weight;
6869}
6870
Bob Wilson2dc4f542009-03-20 22:42:55 +00006871std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006872ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006873 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006874 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006875 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006876 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006877 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006878 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006879 return std::make_pair(0U, ARM::tGPRRegisterClass);
6880 else
6881 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006882 case 'r':
6883 return std::make_pair(0U, ARM::GPRRegisterClass);
6884 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006885 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006886 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006887 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006888 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006889 if (VT.getSizeInBits() == 128)
6890 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006891 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006892 }
6893 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006894 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006895 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006896
Evan Chenga8e29892007-01-19 07:51:42 +00006897 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6898}
6899
6900std::vector<unsigned> ARMTargetLowering::
6901getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006902 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006903 if (Constraint.size() != 1)
6904 return std::vector<unsigned>();
6905
6906 switch (Constraint[0]) { // GCC ARM Constraint Letters
6907 default: break;
6908 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006909 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6910 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6911 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006912 case 'r':
6913 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6914 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6915 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6916 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006917 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006918 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006919 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6920 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6921 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6922 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6923 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6924 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6925 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6926 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006927 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006928 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6929 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6930 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6931 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006932 if (VT.getSizeInBits() == 128)
6933 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6934 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006935 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006936 }
6937
6938 return std::vector<unsigned>();
6939}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006940
6941/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6942/// vector. If it is invalid, don't add anything to Ops.
6943void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6944 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006945 std::vector<SDValue>&Ops,
6946 SelectionDAG &DAG) const {
6947 SDValue Result(0, 0);
6948
6949 switch (Constraint) {
6950 default: break;
6951 case 'I': case 'J': case 'K': case 'L':
6952 case 'M': case 'N': case 'O':
6953 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6954 if (!C)
6955 return;
6956
6957 int64_t CVal64 = C->getSExtValue();
6958 int CVal = (int) CVal64;
6959 // None of these constraints allow values larger than 32 bits. Check
6960 // that the value fits in an int.
6961 if (CVal != CVal64)
6962 return;
6963
6964 switch (Constraint) {
6965 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006966 if (Subtarget->isThumb1Only()) {
6967 // This must be a constant between 0 and 255, for ADD
6968 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006969 if (CVal >= 0 && CVal <= 255)
6970 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006971 } else if (Subtarget->isThumb2()) {
6972 // A constant that can be used as an immediate value in a
6973 // data-processing instruction.
6974 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6975 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006976 } else {
6977 // A constant that can be used as an immediate value in a
6978 // data-processing instruction.
6979 if (ARM_AM::getSOImmVal(CVal) != -1)
6980 break;
6981 }
6982 return;
6983
6984 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006985 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006986 // This must be a constant between -255 and -1, for negated ADD
6987 // immediates. This can be used in GCC with an "n" modifier that
6988 // prints the negated value, for use with SUB instructions. It is
6989 // not useful otherwise but is implemented for compatibility.
6990 if (CVal >= -255 && CVal <= -1)
6991 break;
6992 } else {
6993 // This must be a constant between -4095 and 4095. It is not clear
6994 // what this constraint is intended for. Implemented for
6995 // compatibility with GCC.
6996 if (CVal >= -4095 && CVal <= 4095)
6997 break;
6998 }
6999 return;
7000
7001 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007002 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007003 // A 32-bit value where only one byte has a nonzero value. Exclude
7004 // zero to match GCC. This constraint is used by GCC internally for
7005 // constants that can be loaded with a move/shift combination.
7006 // It is not useful otherwise but is implemented for compatibility.
7007 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7008 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007009 } else if (Subtarget->isThumb2()) {
7010 // A constant whose bitwise inverse can be used as an immediate
7011 // value in a data-processing instruction. This can be used in GCC
7012 // with a "B" modifier that prints the inverted value, for use with
7013 // BIC and MVN instructions. It is not useful otherwise but is
7014 // implemented for compatibility.
7015 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7016 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007017 } else {
7018 // A constant whose bitwise inverse can be used as an immediate
7019 // value in a data-processing instruction. This can be used in GCC
7020 // with a "B" modifier that prints the inverted value, for use with
7021 // BIC and MVN instructions. It is not useful otherwise but is
7022 // implemented for compatibility.
7023 if (ARM_AM::getSOImmVal(~CVal) != -1)
7024 break;
7025 }
7026 return;
7027
7028 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007029 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007030 // This must be a constant between -7 and 7,
7031 // for 3-operand ADD/SUB immediate instructions.
7032 if (CVal >= -7 && CVal < 7)
7033 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007034 } else if (Subtarget->isThumb2()) {
7035 // A constant whose negation can be used as an immediate value in a
7036 // data-processing instruction. This can be used in GCC with an "n"
7037 // modifier that prints the negated value, for use with SUB
7038 // instructions. It is not useful otherwise but is implemented for
7039 // compatibility.
7040 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7041 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007042 } else {
7043 // A constant whose negation can be used as an immediate value in a
7044 // data-processing instruction. This can be used in GCC with an "n"
7045 // modifier that prints the negated value, for use with SUB
7046 // instructions. It is not useful otherwise but is implemented for
7047 // compatibility.
7048 if (ARM_AM::getSOImmVal(-CVal) != -1)
7049 break;
7050 }
7051 return;
7052
7053 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007054 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007055 // This must be a multiple of 4 between 0 and 1020, for
7056 // ADD sp + immediate.
7057 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7058 break;
7059 } else {
7060 // A power of two or a constant between 0 and 32. This is used in
7061 // GCC for the shift amount on shifted register operands, but it is
7062 // useful in general for any shift amounts.
7063 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7064 break;
7065 }
7066 return;
7067
7068 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007069 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007070 // This must be a constant between 0 and 31, for shift amounts.
7071 if (CVal >= 0 && CVal <= 31)
7072 break;
7073 }
7074 return;
7075
7076 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007077 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007078 // This must be a multiple of 4 between -508 and 508, for
7079 // ADD/SUB sp = sp + immediate.
7080 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7081 break;
7082 }
7083 return;
7084 }
7085 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7086 break;
7087 }
7088
7089 if (Result.getNode()) {
7090 Ops.push_back(Result);
7091 return;
7092 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007093 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007094}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007095
7096bool
7097ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7098 // The ARM target isn't yet aware of offsets.
7099 return false;
7100}
Evan Cheng39382422009-10-28 01:44:26 +00007101
7102int ARM::getVFPf32Imm(const APFloat &FPImm) {
7103 APInt Imm = FPImm.bitcastToAPInt();
7104 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7105 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7106 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7107
7108 // We can handle 4 bits of mantissa.
7109 // mantissa = (16+UInt(e:f:g:h))/16.
7110 if (Mantissa & 0x7ffff)
7111 return -1;
7112 Mantissa >>= 19;
7113 if ((Mantissa & 0xf) != Mantissa)
7114 return -1;
7115
7116 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7117 if (Exp < -3 || Exp > 4)
7118 return -1;
7119 Exp = ((Exp+3) & 0x7) ^ 4;
7120
7121 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7122}
7123
7124int ARM::getVFPf64Imm(const APFloat &FPImm) {
7125 APInt Imm = FPImm.bitcastToAPInt();
7126 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7127 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7128 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7129
7130 // We can handle 4 bits of mantissa.
7131 // mantissa = (16+UInt(e:f:g:h))/16.
7132 if (Mantissa & 0xffffffffffffLL)
7133 return -1;
7134 Mantissa >>= 48;
7135 if ((Mantissa & 0xf) != Mantissa)
7136 return -1;
7137
7138 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7139 if (Exp < -3 || Exp > 4)
7140 return -1;
7141 Exp = ((Exp+3) & 0x7) ^ 4;
7142
7143 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7144}
7145
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007146bool ARM::isBitFieldInvertedMask(unsigned v) {
7147 if (v == 0xffffffff)
7148 return 0;
7149 // there can be 1's on either or both "outsides", all the "inside"
7150 // bits must be 0's
7151 unsigned int lsb = 0, msb = 31;
7152 while (v & (1 << msb)) --msb;
7153 while (v & (1 << lsb)) ++lsb;
7154 for (unsigned int i = lsb; i <= msb; ++i) {
7155 if (v & (1 << i))
7156 return 0;
7157 }
7158 return 1;
7159}
7160
Evan Cheng39382422009-10-28 01:44:26 +00007161/// isFPImmLegal - Returns true if the target can instruction select the
7162/// specified FP immediate natively. If false, the legalizer will
7163/// materialize the FP immediate as a load from a constant pool.
7164bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7165 if (!Subtarget->hasVFP3())
7166 return false;
7167 if (VT == MVT::f32)
7168 return ARM::getVFPf32Imm(Imm) != -1;
7169 if (VT == MVT::f64)
7170 return ARM::getVFPf64Imm(Imm) != -1;
7171 return false;
7172}
Bob Wilson65ffec42010-09-21 17:56:22 +00007173
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007174/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007175/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7176/// specified in the intrinsic calls.
7177bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7178 const CallInst &I,
7179 unsigned Intrinsic) const {
7180 switch (Intrinsic) {
7181 case Intrinsic::arm_neon_vld1:
7182 case Intrinsic::arm_neon_vld2:
7183 case Intrinsic::arm_neon_vld3:
7184 case Intrinsic::arm_neon_vld4:
7185 case Intrinsic::arm_neon_vld2lane:
7186 case Intrinsic::arm_neon_vld3lane:
7187 case Intrinsic::arm_neon_vld4lane: {
7188 Info.opc = ISD::INTRINSIC_W_CHAIN;
7189 // Conservatively set memVT to the entire set of vectors loaded.
7190 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7191 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7192 Info.ptrVal = I.getArgOperand(0);
7193 Info.offset = 0;
7194 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7195 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7196 Info.vol = false; // volatile loads with NEON intrinsics not supported
7197 Info.readMem = true;
7198 Info.writeMem = false;
7199 return true;
7200 }
7201 case Intrinsic::arm_neon_vst1:
7202 case Intrinsic::arm_neon_vst2:
7203 case Intrinsic::arm_neon_vst3:
7204 case Intrinsic::arm_neon_vst4:
7205 case Intrinsic::arm_neon_vst2lane:
7206 case Intrinsic::arm_neon_vst3lane:
7207 case Intrinsic::arm_neon_vst4lane: {
7208 Info.opc = ISD::INTRINSIC_VOID;
7209 // Conservatively set memVT to the entire set of vectors stored.
7210 unsigned NumElts = 0;
7211 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7212 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7213 if (!ArgTy->isVectorTy())
7214 break;
7215 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7216 }
7217 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7218 Info.ptrVal = I.getArgOperand(0);
7219 Info.offset = 0;
7220 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7221 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7222 Info.vol = false; // volatile stores with NEON intrinsics not supported
7223 Info.readMem = false;
7224 Info.writeMem = true;
7225 return true;
7226 }
7227 default:
7228 break;
7229 }
7230
7231 return false;
7232}