blob: 370dd7b44bd73981eaf55be0e2ed27b7f9958fdf [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");
57
Bob Wilson703af3a2010-08-13 22:43:33 +000058// This option should go away when tail calls fully work.
59static cl::opt<bool>
60EnableARMTailCalls("arm-tail-calls", cl::Hidden,
61 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
62 cl::init(false));
63
Eric Christopher836c6242010-12-15 23:47:29 +000064cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000065EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000066 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000067 cl::init(false));
68
Evan Cheng46df4eb2010-06-16 07:35:02 +000069static cl::opt<bool>
70ARMInterworking("arm-interworking", cl::Hidden,
71 cl::desc("Enable / disable ARM interworking (for debugging only)"),
72 cl::init(true));
73
Owen Andersone50ed302009-08-10 22:56:29 +000074void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
75 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000076 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000077 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000078 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
79 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000080
Owen Anderson70671842009-08-10 20:18:46 +000081 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000082 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000083 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000084 }
85
Owen Andersone50ed302009-08-10 22:56:29 +000086 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000087 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +000089 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000090 if (ElemTy != MVT::i32) {
91 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
92 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
94 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
95 }
Owen Anderson70671842009-08-10 20:18:46 +000096 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
97 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +000098 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +000099 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000100 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
101 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000102 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000103 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
104 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
105 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000106 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
107 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000108 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
109 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
110 setTruncStoreAction(VT.getSimpleVT(),
111 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000112 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000113 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000114
115 // Promote all bit-wise operations.
116 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000117 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000118 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
119 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000120 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000121 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000122 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000123 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000124 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000125 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000126 }
Bob Wilson16330762009-09-16 00:17:28 +0000127
128 // Neon does not support vector divide/remainder operations.
129 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
130 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
131 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
132 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
133 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
134 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135}
136
Owen Andersone50ed302009-08-10 22:56:29 +0000137void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000138 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140}
141
Owen Andersone50ed302009-08-10 22:56:29 +0000142void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000143 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000145}
146
Chris Lattnerf0144122009-07-28 03:13:23 +0000147static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
148 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000149 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000150
Chris Lattner80ec2792009-08-02 00:34:36 +0000151 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000152}
153
Evan Chenga8e29892007-01-19 07:51:42 +0000154ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000155 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000156 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000157 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000158 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000159
Evan Chengb1df8f22007-04-27 08:15:43 +0000160 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000161 // Uses VFP for Thumb libfuncs if available.
162 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
163 // Single-precision floating-point arithmetic.
164 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
165 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
166 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
167 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000168
Evan Chengb1df8f22007-04-27 08:15:43 +0000169 // Double-precision floating-point arithmetic.
170 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
171 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
172 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
173 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000174
Evan Chengb1df8f22007-04-27 08:15:43 +0000175 // Single-precision comparisons.
176 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
177 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
178 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
179 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
180 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
181 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
182 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
183 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000184
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000193
Evan Chengb1df8f22007-04-27 08:15:43 +0000194 // Double-precision comparisons.
195 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
196 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
197 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
198 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
199 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
200 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
201 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
202 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000203
Evan Chengb1df8f22007-04-27 08:15:43 +0000204 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000212
Evan Chengb1df8f22007-04-27 08:15:43 +0000213 // Floating-point to integer conversions.
214 // i64 conversions are done via library routines even when generating VFP
215 // instructions, so use the same ones.
216 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
217 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
218 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
219 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000220
Evan Chengb1df8f22007-04-27 08:15:43 +0000221 // Conversions between floating types.
222 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
223 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
224
225 // Integer to floating-point conversions.
226 // i64 conversions are done via library routines even when generating VFP
227 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000228 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
229 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000230 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
231 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
232 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
233 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
234 }
Evan Chenga8e29892007-01-19 07:51:42 +0000235 }
236
Bob Wilson2f954612009-05-22 17:38:41 +0000237 // These libcalls are not available in 32-bit.
238 setLibcallName(RTLIB::SHL_I128, 0);
239 setLibcallName(RTLIB::SRL_I128, 0);
240 setLibcallName(RTLIB::SRA_I128, 0);
241
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000242 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000243 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000244 // RTABI chapter 4.1.2, Table 2
245 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
246 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
247 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
248 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
249 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
250 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
251 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
252 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
253
254 // Double-precision floating-point comparison helper functions
255 // RTABI chapter 4.1.2, Table 3
256 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
257 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
258 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
259 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
260 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
261 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
262 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
263 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
264 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
265 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
266 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
267 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
268 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
269 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
270 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
271 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
272 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
276 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
277 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
278 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
279 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
280
281 // Single-precision floating-point arithmetic helper functions
282 // RTABI chapter 4.1.2, Table 4
283 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
284 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
285 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
286 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
287 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
288 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
291
292 // Single-precision floating-point comparison helper functions
293 // RTABI chapter 4.1.2, Table 5
294 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
295 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
296 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
297 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
298 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
299 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
300 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
301 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
302 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
303 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
304 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
305 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
306 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
307 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
308 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
309 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
310 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
314 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
315 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
316 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
317 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
318
319 // Floating-point to integer conversions.
320 // RTABI chapter 4.1.2, Table 6
321 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
322 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
323 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
324 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
325 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
326 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
327 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
328 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
329 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
337
338 // Conversions between floating types.
339 // RTABI chapter 4.1.2, Table 7
340 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
341 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
342 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000343 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000344
345 // Integer to floating-point conversions.
346 // RTABI chapter 4.1.2, Table 8
347 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
348 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
349 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
350 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
351 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
352 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
353 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
354 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
355 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
360 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
361 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
362 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
363
364 // Long long helper functions
365 // RTABI chapter 4.2, Table 9
366 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
367 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
368 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
369 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
370 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
371 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
372 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
378
379 // Integer division functions
380 // RTABI chapter 4.3.1
381 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
382 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
383 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
384 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
385 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
386 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
387 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000392 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000393 }
394
David Goodwinf1daf7d2009-07-08 23:10:31 +0000395 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000397 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000399 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000401 if (!Subtarget->isFPOnlySP())
402 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000403
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000405 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000406
407 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 addDRTypeForNEON(MVT::v2f32);
409 addDRTypeForNEON(MVT::v8i8);
410 addDRTypeForNEON(MVT::v4i16);
411 addDRTypeForNEON(MVT::v2i32);
412 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000413
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 addQRTypeForNEON(MVT::v4f32);
415 addQRTypeForNEON(MVT::v2f64);
416 addQRTypeForNEON(MVT::v16i8);
417 addQRTypeForNEON(MVT::v8i16);
418 addQRTypeForNEON(MVT::v4i32);
419 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000420
Bob Wilson74dc72e2009-09-15 23:55:57 +0000421 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
422 // neither Neon nor VFP support any arithmetic operations on it.
423 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
424 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
425 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
426 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
427 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
428 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
429 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
430 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
431 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
432 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
433 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
434 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
435 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
436 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
437 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
438 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
439 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
440 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
441 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
442 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
443 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
444 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
445 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
446 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
447
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000448 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
449
Bob Wilson642b3292009-09-16 00:32:15 +0000450 // Neon does not support some operations on v1i64 and v2i64 types.
451 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000452 // Custom handling for some quad-vector types to detect VMULL.
453 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
454 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
455 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000456 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
457 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
458
Bob Wilson5bafff32009-06-22 23:27:02 +0000459 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
460 setTargetDAGCombine(ISD::SHL);
461 setTargetDAGCombine(ISD::SRL);
462 setTargetDAGCombine(ISD::SRA);
463 setTargetDAGCombine(ISD::SIGN_EXTEND);
464 setTargetDAGCombine(ISD::ZERO_EXTEND);
465 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000466 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000467 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000468 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000469 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
470 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000471 }
472
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000473 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000474
475 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000477
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000478 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000480
Evan Chenga8e29892007-01-19 07:51:42 +0000481 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000482 if (!Subtarget->isThumb1Only()) {
483 for (unsigned im = (unsigned)ISD::PRE_INC;
484 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 setIndexedLoadAction(im, MVT::i1, Legal);
486 setIndexedLoadAction(im, MVT::i8, Legal);
487 setIndexedLoadAction(im, MVT::i16, Legal);
488 setIndexedLoadAction(im, MVT::i32, Legal);
489 setIndexedStoreAction(im, MVT::i1, Legal);
490 setIndexedStoreAction(im, MVT::i8, Legal);
491 setIndexedStoreAction(im, MVT::i16, Legal);
492 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000493 }
Evan Chenga8e29892007-01-19 07:51:42 +0000494 }
495
496 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000497 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000498 setOperationAction(ISD::MUL, MVT::i64, Expand);
499 setOperationAction(ISD::MULHU, MVT::i32, Expand);
500 setOperationAction(ISD::MULHS, MVT::i32, Expand);
501 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
502 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000503 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 setOperationAction(ISD::MUL, MVT::i64, Expand);
505 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000506 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000508 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000509 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000510 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000511 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::SRL, MVT::i64, Custom);
513 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000514
515 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000517 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000519 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000521
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000522 // Only ARMv6 has BSWAP.
523 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000525
Evan Chenga8e29892007-01-19 07:51:42 +0000526 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000527 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000528 // v7M has a hardware divider
529 setOperationAction(ISD::SDIV, MVT::i32, Expand);
530 setOperationAction(ISD::UDIV, MVT::i32, Expand);
531 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::SREM, MVT::i32, Expand);
533 setOperationAction(ISD::UREM, MVT::i32, Expand);
534 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
535 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000536
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
538 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
539 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
540 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000541 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000542
Evan Chengfb3611d2010-05-11 07:26:32 +0000543 setOperationAction(ISD::TRAP, MVT::Other, Legal);
544
Evan Chenga8e29892007-01-19 07:51:42 +0000545 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::VASTART, MVT::Other, Custom);
547 setOperationAction(ISD::VAARG, MVT::Other, Expand);
548 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
549 setOperationAction(ISD::VAEND, MVT::Other, Expand);
550 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
551 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000552 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
553 // FIXME: Shouldn't need this, since no register is used, but the legalizer
554 // doesn't yet know how to not do that for SjLj.
555 setExceptionSelectorRegister(ARM::R0);
Evan Cheng3a1588a2010-04-15 22:20:34 +0000556 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000557 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
558 // the default expansion.
559 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000560 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000561 // membarrier needs custom lowering; the rest are legal and handled
562 // normally.
563 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
564 } else {
565 // Set them all for expansion, which will force libcalls.
566 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
567 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
568 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
569 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000570 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
571 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
572 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000573 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
574 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
575 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
576 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
577 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
578 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
579 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
580 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
581 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
582 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
583 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
584 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
585 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
586 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
587 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
588 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
589 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
590 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000591 // Since the libcalls include locking, fold in the fences
592 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000593 }
594 // 64-bit versions are always libcalls (for now)
595 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000596 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000597 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000603
Evan Cheng416941d2010-11-04 05:19:35 +0000604 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000605
Eli Friedmana2c6f452010-06-26 04:36:50 +0000606 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
607 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
609 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000610 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000612
Nate Begemand1fb5832010-08-03 21:31:55 +0000613 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000614 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
615 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000616 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000617 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
618 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000619
620 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000622 if (Subtarget->isTargetDarwin()) {
623 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
624 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000625 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000626 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000627
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::SETCC, MVT::i32, Expand);
629 setOperationAction(ISD::SETCC, MVT::f32, Expand);
630 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000631 setOperationAction(ISD::SELECT, MVT::i32, Custom);
632 setOperationAction(ISD::SELECT, MVT::f32, Custom);
633 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
635 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
636 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000637
Owen Anderson825b72b2009-08-11 20:47:22 +0000638 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
639 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
640 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
641 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
642 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000643
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000644 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 setOperationAction(ISD::FSIN, MVT::f64, Expand);
646 setOperationAction(ISD::FSIN, MVT::f32, Expand);
647 setOperationAction(ISD::FCOS, MVT::f32, Expand);
648 setOperationAction(ISD::FCOS, MVT::f64, Expand);
649 setOperationAction(ISD::FREM, MVT::f64, Expand);
650 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000651 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
653 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000654 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::FPOW, MVT::f64, Expand);
656 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000657
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000658 // Various VFP goodness
659 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000660 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
661 if (Subtarget->hasVFP2()) {
662 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
663 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
664 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
665 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
666 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000667 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000668 if (!Subtarget->hasFP16()) {
669 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
670 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000671 }
Evan Cheng110cf482008-04-01 01:50:16 +0000672 }
Evan Chenga8e29892007-01-19 07:51:42 +0000673
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000674 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000675 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000676 setTargetDAGCombine(ISD::ADD);
677 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000678 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000679
Owen Anderson080c0922010-11-05 19:27:46 +0000680 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000681 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000682 if (Subtarget->hasNEON())
683 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000684
Evan Chenga8e29892007-01-19 07:51:42 +0000685 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000686
Evan Chengf7d87ee2010-05-21 00:43:17 +0000687 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
688 setSchedulingPreference(Sched::RegPressure);
689 else
690 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000691
Evan Cheng05219282011-01-06 06:52:41 +0000692 //// temporary - rewrite interface to use type
693 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000694
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000695 // On ARM arguments smaller than 4 bytes are extended, so all arguments
696 // are at least 4 bytes aligned.
697 setMinStackArgumentAlignment(4);
698
Evan Chengfff606d2010-09-24 19:07:23 +0000699 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000700}
701
Evan Cheng4f6b4672010-07-21 06:09:07 +0000702std::pair<const TargetRegisterClass*, uint8_t>
703ARMTargetLowering::findRepresentativeClass(EVT VT) const{
704 const TargetRegisterClass *RRC = 0;
705 uint8_t Cost = 1;
706 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000707 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000708 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000709 // Use DPR as representative register class for all floating point
710 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
711 // the cost is 1 for both f32 and f64.
712 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000713 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000714 RRC = ARM::DPRRegisterClass;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000715 break;
716 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
717 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000718 RRC = ARM::DPRRegisterClass;
719 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000720 break;
721 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000722 RRC = ARM::DPRRegisterClass;
723 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000724 break;
725 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000726 RRC = ARM::DPRRegisterClass;
727 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000728 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000729 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000730 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000731}
732
Evan Chenga8e29892007-01-19 07:51:42 +0000733const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
734 switch (Opcode) {
735 default: return 0;
736 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chenga8e29892007-01-19 07:51:42 +0000737 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
738 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000739 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000740 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
741 case ARMISD::tCALL: return "ARMISD::tCALL";
742 case ARMISD::BRCOND: return "ARMISD::BRCOND";
743 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000744 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000745 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
746 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
747 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000748 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000749 case ARMISD::CMPFP: return "ARMISD::CMPFP";
750 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000751 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000752 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
753 case ARMISD::CMOV: return "ARMISD::CMOV";
754 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000755
Jim Grosbach3482c802010-01-18 19:58:49 +0000756 case ARMISD::RBIT: return "ARMISD::RBIT";
757
Bob Wilson76a312b2010-03-19 22:51:32 +0000758 case ARMISD::FTOSI: return "ARMISD::FTOSI";
759 case ARMISD::FTOUI: return "ARMISD::FTOUI";
760 case ARMISD::SITOF: return "ARMISD::SITOF";
761 case ARMISD::UITOF: return "ARMISD::UITOF";
762
Evan Chenga8e29892007-01-19 07:51:42 +0000763 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
764 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
765 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000766
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000767 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
768 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000769
Evan Chengc5942082009-10-28 06:55:03 +0000770 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
771 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000772 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000773
Dale Johannesen51e28e62010-06-03 21:09:53 +0000774 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000775
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000776 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000777
Evan Cheng86198642009-08-07 00:34:42 +0000778 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
779
Jim Grosbach3728e962009-12-10 00:11:09 +0000780 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000781 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000782
Evan Chengdfed19f2010-11-03 06:34:55 +0000783 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
784
Bob Wilson5bafff32009-06-22 23:27:02 +0000785 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000786 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000787 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000788 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
789 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000790 case ARMISD::VCGEU: return "ARMISD::VCGEU";
791 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000792 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
793 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000794 case ARMISD::VCGTU: return "ARMISD::VCGTU";
795 case ARMISD::VTST: return "ARMISD::VTST";
796
797 case ARMISD::VSHL: return "ARMISD::VSHL";
798 case ARMISD::VSHRs: return "ARMISD::VSHRs";
799 case ARMISD::VSHRu: return "ARMISD::VSHRu";
800 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
801 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
802 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
803 case ARMISD::VSHRN: return "ARMISD::VSHRN";
804 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
805 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
806 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
807 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
808 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
809 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
810 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
811 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
812 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
813 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
814 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
815 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
816 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
817 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000818 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000819 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000820 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000821 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000822 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000823 case ARMISD::VREV64: return "ARMISD::VREV64";
824 case ARMISD::VREV32: return "ARMISD::VREV32";
825 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000826 case ARMISD::VZIP: return "ARMISD::VZIP";
827 case ARMISD::VUZP: return "ARMISD::VUZP";
828 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000829 case ARMISD::VMULLs: return "ARMISD::VMULLs";
830 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000831 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000832 case ARMISD::FMAX: return "ARMISD::FMAX";
833 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000834 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000835 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
836 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000837 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
838 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
839 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Evan Chenga8e29892007-01-19 07:51:42 +0000840 }
841}
842
Evan Cheng06b666c2010-05-15 02:18:07 +0000843/// getRegClassFor - Return the register class that should be used for the
844/// specified value type.
845TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
846 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
847 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
848 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000849 if (Subtarget->hasNEON()) {
850 if (VT == MVT::v4i64)
851 return ARM::QQPRRegisterClass;
852 else if (VT == MVT::v8i64)
853 return ARM::QQQQPRRegisterClass;
854 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000855 return TargetLowering::getRegClassFor(VT);
856}
857
Eric Christopherab695882010-07-21 22:26:11 +0000858// Create a fast isel object.
859FastISel *
860ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
861 return ARM::createFastISel(funcInfo);
862}
863
Bill Wendlingb4202b82009-07-01 18:50:55 +0000864/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000865unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000866 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000867}
868
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000869/// getMaximalGlobalOffset - Returns the maximal possible offset which can
870/// be used for loads / stores from the global.
871unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
872 return (Subtarget->isThumb1Only() ? 127 : 4095);
873}
874
Evan Cheng1cc39842010-05-20 23:26:43 +0000875Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000876 unsigned NumVals = N->getNumValues();
877 if (!NumVals)
878 return Sched::RegPressure;
879
880 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000881 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000882 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000883 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000884 if (VT.isFloatingPoint() || VT.isVector())
885 return Sched::Latency;
886 }
Evan Chengc10f5432010-05-28 23:25:23 +0000887
888 if (!N->isMachineOpcode())
889 return Sched::RegPressure;
890
891 // Load are scheduled for latency even if there instruction itinerary
892 // is not available.
893 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
894 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000895
896 if (TID.getNumDefs() == 0)
897 return Sched::RegPressure;
898 if (!Itins->isEmpty() &&
899 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000900 return Sched::Latency;
901
Evan Cheng1cc39842010-05-20 23:26:43 +0000902 return Sched::RegPressure;
903}
904
Evan Cheng31446872010-07-23 22:39:59 +0000905unsigned
906ARMTargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
907 MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000908 const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
909
Evan Cheng31446872010-07-23 22:39:59 +0000910 switch (RC->getID()) {
911 default:
912 return 0;
913 case ARM::tGPRRegClassID:
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000914 return TFI->hasFP(MF) ? 4 : 5;
Evan Chengac096802010-08-10 19:30:19 +0000915 case ARM::GPRRegClassID: {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000916 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
Evan Chengac096802010-08-10 19:30:19 +0000917 return 10 - FP - (Subtarget->isR9Reserved() ? 1 : 0);
918 }
Evan Cheng31446872010-07-23 22:39:59 +0000919 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
920 case ARM::DPRRegClassID:
921 return 32 - 10;
922 }
923}
924
Evan Chenga8e29892007-01-19 07:51:42 +0000925//===----------------------------------------------------------------------===//
926// Lowering Code
927//===----------------------------------------------------------------------===//
928
Evan Chenga8e29892007-01-19 07:51:42 +0000929/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
930static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
931 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000932 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000933 case ISD::SETNE: return ARMCC::NE;
934 case ISD::SETEQ: return ARMCC::EQ;
935 case ISD::SETGT: return ARMCC::GT;
936 case ISD::SETGE: return ARMCC::GE;
937 case ISD::SETLT: return ARMCC::LT;
938 case ISD::SETLE: return ARMCC::LE;
939 case ISD::SETUGT: return ARMCC::HI;
940 case ISD::SETUGE: return ARMCC::HS;
941 case ISD::SETULT: return ARMCC::LO;
942 case ISD::SETULE: return ARMCC::LS;
943 }
944}
945
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000946/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
947static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000948 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000949 CondCode2 = ARMCC::AL;
950 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000951 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000952 case ISD::SETEQ:
953 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
954 case ISD::SETGT:
955 case ISD::SETOGT: CondCode = ARMCC::GT; break;
956 case ISD::SETGE:
957 case ISD::SETOGE: CondCode = ARMCC::GE; break;
958 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000959 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000960 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
961 case ISD::SETO: CondCode = ARMCC::VC; break;
962 case ISD::SETUO: CondCode = ARMCC::VS; break;
963 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
964 case ISD::SETUGT: CondCode = ARMCC::HI; break;
965 case ISD::SETUGE: CondCode = ARMCC::PL; break;
966 case ISD::SETLT:
967 case ISD::SETULT: CondCode = ARMCC::LT; break;
968 case ISD::SETLE:
969 case ISD::SETULE: CondCode = ARMCC::LE; break;
970 case ISD::SETNE:
971 case ISD::SETUNE: CondCode = ARMCC::NE; break;
972 }
Evan Chenga8e29892007-01-19 07:51:42 +0000973}
974
Bob Wilson1f595bb2009-04-17 19:07:39 +0000975//===----------------------------------------------------------------------===//
976// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000977//===----------------------------------------------------------------------===//
978
979#include "ARMGenCallingConv.inc"
980
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000981/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
982/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000983CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000984 bool Return,
985 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000986 switch (CC) {
987 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000988 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000989 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +0000990 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +0000991 if (!Subtarget->isAAPCS_ABI())
992 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
993 // For AAPCS ABI targets, just use VFP variant of the calling convention.
994 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
995 }
996 // Fallthrough
997 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000998 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +0000999 if (!Subtarget->isAAPCS_ABI())
1000 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1001 else if (Subtarget->hasVFP2() &&
1002 FloatABIType == FloatABI::Hard && !isVarArg)
1003 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1004 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1005 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001006 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001007 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001008 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001009 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001010 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001011 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001012 }
1013}
1014
Dan Gohman98ca4f22009-08-05 01:29:28 +00001015/// LowerCallResult - Lower the result values of a call into the
1016/// appropriate copies out of appropriate physical registers.
1017SDValue
1018ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001019 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001020 const SmallVectorImpl<ISD::InputArg> &Ins,
1021 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001022 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001023
Bob Wilson1f595bb2009-04-17 19:07:39 +00001024 // Assign locations to each value returned by this call.
1025 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001026 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001027 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001028 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001029 CCAssignFnForNode(CallConv, /* Return*/ true,
1030 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001031
1032 // Copy all of the result registers out of their specified physreg.
1033 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1034 CCValAssign VA = RVLocs[i];
1035
Bob Wilson80915242009-04-25 00:33:20 +00001036 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001037 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001038 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001039 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001040 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001041 Chain = Lo.getValue(1);
1042 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001043 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001044 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001045 InFlag);
1046 Chain = Hi.getValue(1);
1047 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001048 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001049
Owen Anderson825b72b2009-08-11 20:47:22 +00001050 if (VA.getLocVT() == MVT::v2f64) {
1051 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1052 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1053 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001054
1055 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001056 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001057 Chain = Lo.getValue(1);
1058 InFlag = Lo.getValue(2);
1059 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001061 Chain = Hi.getValue(1);
1062 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001063 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001064 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1065 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001066 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001067 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001068 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1069 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001070 Chain = Val.getValue(1);
1071 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072 }
Bob Wilson80915242009-04-25 00:33:20 +00001073
1074 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001075 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001076 case CCValAssign::Full: break;
1077 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001078 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001079 break;
1080 }
1081
Dan Gohman98ca4f22009-08-05 01:29:28 +00001082 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001083 }
1084
Dan Gohman98ca4f22009-08-05 01:29:28 +00001085 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001086}
1087
1088/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1089/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001090/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001091/// a byval function parameter.
1092/// Sometimes what we are copying is the end of a larger object, the part that
1093/// does not fit in registers.
1094static SDValue
1095CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1096 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1097 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001098 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001099 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001100 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001101 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001102}
1103
Bob Wilsondee46d72009-04-17 20:35:10 +00001104/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001105SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001106ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1107 SDValue StackPtr, SDValue Arg,
1108 DebugLoc dl, SelectionDAG &DAG,
1109 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001110 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001111 unsigned LocMemOffset = VA.getLocMemOffset();
1112 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1113 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001114 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001116
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001118 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001119 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001120}
1121
Dan Gohman98ca4f22009-08-05 01:29:28 +00001122void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001123 SDValue Chain, SDValue &Arg,
1124 RegsToPassVector &RegsToPass,
1125 CCValAssign &VA, CCValAssign &NextVA,
1126 SDValue &StackPtr,
1127 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001128 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001129
Jim Grosbache5165492009-11-09 00:11:35 +00001130 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001131 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001132 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1133
1134 if (NextVA.isRegLoc())
1135 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1136 else {
1137 assert(NextVA.isMemLoc());
1138 if (StackPtr.getNode() == 0)
1139 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1140
Dan Gohman98ca4f22009-08-05 01:29:28 +00001141 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1142 dl, DAG, NextVA,
1143 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001144 }
1145}
1146
Dan Gohman98ca4f22009-08-05 01:29:28 +00001147/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001148/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1149/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001150SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001151ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001152 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001153 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001154 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001155 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001156 const SmallVectorImpl<ISD::InputArg> &Ins,
1157 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001158 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001159 MachineFunction &MF = DAG.getMachineFunction();
1160 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1161 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001162 // Temporarily disable tail calls so things don't break.
1163 if (!EnableARMTailCalls)
1164 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001165 if (isTailCall) {
1166 // Check if it's really possible to do a tail call.
1167 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1168 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001169 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001170 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1171 // detected sibcalls.
1172 if (isTailCall) {
1173 ++NumTailCalls;
1174 IsSibCall = true;
1175 }
1176 }
Evan Chenga8e29892007-01-19 07:51:42 +00001177
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178 // Analyze operands of the call, assigning locations to each operand.
1179 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1181 *DAG.getContext());
1182 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001183 CCAssignFnForNode(CallConv, /* Return*/ false,
1184 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001185
Bob Wilson1f595bb2009-04-17 19:07:39 +00001186 // Get a count of how many bytes are to be pushed on the stack.
1187 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001188
Dale Johannesen51e28e62010-06-03 21:09:53 +00001189 // For tail calls, memory operands are available in our caller's stack.
1190 if (IsSibCall)
1191 NumBytes = 0;
1192
Evan Chenga8e29892007-01-19 07:51:42 +00001193 // Adjust the stack pointer for the new arguments...
1194 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001195 if (!IsSibCall)
1196 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001197
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001198 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001199
Bob Wilson5bafff32009-06-22 23:27:02 +00001200 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001202
Bob Wilson1f595bb2009-04-17 19:07:39 +00001203 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001204 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001205 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1206 i != e;
1207 ++i, ++realArgIdx) {
1208 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001209 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001210 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001211
Bob Wilson1f595bb2009-04-17 19:07:39 +00001212 // Promote the value if needed.
1213 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001214 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001215 case CCValAssign::Full: break;
1216 case CCValAssign::SExt:
1217 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1218 break;
1219 case CCValAssign::ZExt:
1220 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1221 break;
1222 case CCValAssign::AExt:
1223 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1224 break;
1225 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001226 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001227 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001228 }
1229
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001230 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001232 if (VA.getLocVT() == MVT::v2f64) {
1233 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1234 DAG.getConstant(0, MVT::i32));
1235 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1236 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001237
Dan Gohman98ca4f22009-08-05 01:29:28 +00001238 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001239 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1240
1241 VA = ArgLocs[++i]; // skip ahead to next loc
1242 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001243 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001244 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1245 } else {
1246 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001247
Dan Gohman98ca4f22009-08-05 01:29:28 +00001248 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1249 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001250 }
1251 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001252 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001253 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 }
1255 } else if (VA.isRegLoc()) {
1256 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001257 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001259
Dan Gohman98ca4f22009-08-05 01:29:28 +00001260 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1261 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 }
Evan Chenga8e29892007-01-19 07:51:42 +00001263 }
1264
1265 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001266 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001267 &MemOpChains[0], MemOpChains.size());
1268
1269 // Build a sequence of copy-to-reg nodes chained together with token chain
1270 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001271 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001272 // Tail call byval lowering might overwrite argument registers so in case of
1273 // tail call optimization the copies to registers are lowered later.
1274 if (!isTailCall)
1275 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1276 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1277 RegsToPass[i].second, InFlag);
1278 InFlag = Chain.getValue(1);
1279 }
Evan Chenga8e29892007-01-19 07:51:42 +00001280
Dale Johannesen51e28e62010-06-03 21:09:53 +00001281 // For tail calls lower the arguments to the 'real' stack slot.
1282 if (isTailCall) {
1283 // Force all the incoming stack arguments to be loaded from the stack
1284 // before any new outgoing arguments are stored to the stack, because the
1285 // outgoing stack slots may alias the incoming argument stack slots, and
1286 // the alias isn't otherwise explicit. This is slightly more conservative
1287 // than necessary, because it means that each store effectively depends
1288 // on every argument instead of just those arguments it would clobber.
1289
1290 // Do not flag preceeding copytoreg stuff together with the following stuff.
1291 InFlag = SDValue();
1292 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1293 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1294 RegsToPass[i].second, InFlag);
1295 InFlag = Chain.getValue(1);
1296 }
1297 InFlag =SDValue();
1298 }
1299
Bill Wendling056292f2008-09-16 21:48:12 +00001300 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1301 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1302 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001303 bool isDirect = false;
1304 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001305 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001306 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001307
1308 if (EnableARMLongCalls) {
1309 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1310 && "long-calls with non-static relocation model!");
1311 // Handle a global address or an external symbol. If it's not one of
1312 // those, the target's already in a register, so we don't need to do
1313 // anything extra.
1314 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001315 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001316 // Create a constant pool entry for the callee address
1317 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1318 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1319 ARMPCLabelIndex,
1320 ARMCP::CPValue, 0);
1321 // Get the address of the callee into a register
1322 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1323 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1324 Callee = DAG.getLoad(getPointerTy(), dl,
1325 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001326 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001327 false, false, 0);
1328 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1329 const char *Sym = S->getSymbol();
1330
1331 // Create a constant pool entry for the callee address
1332 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1333 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1334 Sym, ARMPCLabelIndex, 0);
1335 // Get the address of the callee into a register
1336 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1337 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1338 Callee = DAG.getLoad(getPointerTy(), dl,
1339 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001340 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001341 false, false, 0);
1342 }
1343 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001344 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001345 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001346 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001347 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001348 getTargetMachine().getRelocationModel() != Reloc::Static;
1349 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001350 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001351 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001352 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001353 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001354 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001355 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001356 ARMPCLabelIndex,
1357 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001358 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001359 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001360 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001361 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001362 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001363 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001364 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001365 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001366 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001367 } else {
1368 // On ELF targets for PIC code, direct calls should go through the PLT
1369 unsigned OpFlags = 0;
1370 if (Subtarget->isTargetELF() &&
1371 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1372 OpFlags = ARMII::MO_PLT;
1373 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1374 }
Bill Wendling056292f2008-09-16 21:48:12 +00001375 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001376 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001377 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001378 getTargetMachine().getRelocationModel() != Reloc::Static;
1379 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001380 // tBX takes a register source operand.
1381 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001382 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001383 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001384 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001385 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001386 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001387 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001388 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001389 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001390 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001391 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001392 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001393 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001394 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001395 } else {
1396 unsigned OpFlags = 0;
1397 // On ELF targets for PIC code, direct calls should go through the PLT
1398 if (Subtarget->isTargetELF() &&
1399 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1400 OpFlags = ARMII::MO_PLT;
1401 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1402 }
Evan Chenga8e29892007-01-19 07:51:42 +00001403 }
1404
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001405 // FIXME: handle tail calls differently.
1406 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001407 if (Subtarget->isThumb()) {
1408 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001409 CallOpc = ARMISD::CALL_NOLINK;
1410 else
1411 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1412 } else {
1413 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001414 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1415 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001416 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001417
Dan Gohman475871a2008-07-27 21:46:04 +00001418 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001419 Ops.push_back(Chain);
1420 Ops.push_back(Callee);
1421
1422 // Add argument registers to the end of the list so that they are known live
1423 // into the call.
1424 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1425 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1426 RegsToPass[i].second.getValueType()));
1427
Gabor Greifba36cb52008-08-28 21:40:38 +00001428 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001429 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001430
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001431 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001432 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001433 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001434
Duncan Sands4bdcb612008-07-02 17:40:58 +00001435 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001436 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001437 InFlag = Chain.getValue(1);
1438
Chris Lattnere563bbc2008-10-11 22:08:30 +00001439 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1440 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001441 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001442 InFlag = Chain.getValue(1);
1443
Bob Wilson1f595bb2009-04-17 19:07:39 +00001444 // Handle result values, copying them out of physregs into vregs that we
1445 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001446 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1447 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001448}
1449
Dale Johannesen51e28e62010-06-03 21:09:53 +00001450/// MatchingStackOffset - Return true if the given stack call argument is
1451/// already available in the same position (relatively) of the caller's
1452/// incoming argument stack.
1453static
1454bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1455 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1456 const ARMInstrInfo *TII) {
1457 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1458 int FI = INT_MAX;
1459 if (Arg.getOpcode() == ISD::CopyFromReg) {
1460 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001461 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001462 return false;
1463 MachineInstr *Def = MRI->getVRegDef(VR);
1464 if (!Def)
1465 return false;
1466 if (!Flags.isByVal()) {
1467 if (!TII->isLoadFromStackSlot(Def, FI))
1468 return false;
1469 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001470 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001471 }
1472 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1473 if (Flags.isByVal())
1474 // ByVal argument is passed in as a pointer but it's now being
1475 // dereferenced. e.g.
1476 // define @foo(%struct.X* %A) {
1477 // tail call @bar(%struct.X* byval %A)
1478 // }
1479 return false;
1480 SDValue Ptr = Ld->getBasePtr();
1481 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1482 if (!FINode)
1483 return false;
1484 FI = FINode->getIndex();
1485 } else
1486 return false;
1487
1488 assert(FI != INT_MAX);
1489 if (!MFI->isFixedObjectIndex(FI))
1490 return false;
1491 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1492}
1493
1494/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1495/// for tail call optimization. Targets which want to do tail call
1496/// optimization should implement this function.
1497bool
1498ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1499 CallingConv::ID CalleeCC,
1500 bool isVarArg,
1501 bool isCalleeStructRet,
1502 bool isCallerStructRet,
1503 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001504 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001505 const SmallVectorImpl<ISD::InputArg> &Ins,
1506 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001507 const Function *CallerF = DAG.getMachineFunction().getFunction();
1508 CallingConv::ID CallerCC = CallerF->getCallingConv();
1509 bool CCMatch = CallerCC == CalleeCC;
1510
1511 // Look for obvious safe cases to perform tail call optimization that do not
1512 // require ABI changes. This is what gcc calls sibcall.
1513
Jim Grosbach7616b642010-06-16 23:45:49 +00001514 // Do not sibcall optimize vararg calls unless the call site is not passing
1515 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001516 if (isVarArg && !Outs.empty())
1517 return false;
1518
1519 // Also avoid sibcall optimization if either caller or callee uses struct
1520 // return semantics.
1521 if (isCalleeStructRet || isCallerStructRet)
1522 return false;
1523
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001524 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001525 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001526 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1527 // LR. This means if we need to reload LR, it takes an extra instructions,
1528 // which outweighs the value of the tail call; but here we don't know yet
1529 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001530 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001531 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001532
1533 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1534 // but we need to make sure there are enough registers; the only valid
1535 // registers are the 4 used for parameters. We don't currently do this
1536 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001537 if (Subtarget->isThumb1Only())
1538 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001539
Dale Johannesen51e28e62010-06-03 21:09:53 +00001540 // If the calling conventions do not match, then we'd better make sure the
1541 // results are returned in the same way as what the caller expects.
1542 if (!CCMatch) {
1543 SmallVector<CCValAssign, 16> RVLocs1;
1544 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1545 RVLocs1, *DAG.getContext());
1546 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1547
1548 SmallVector<CCValAssign, 16> RVLocs2;
1549 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1550 RVLocs2, *DAG.getContext());
1551 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1552
1553 if (RVLocs1.size() != RVLocs2.size())
1554 return false;
1555 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1556 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1557 return false;
1558 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1559 return false;
1560 if (RVLocs1[i].isRegLoc()) {
1561 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1562 return false;
1563 } else {
1564 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1565 return false;
1566 }
1567 }
1568 }
1569
1570 // If the callee takes no arguments then go on to check the results of the
1571 // call.
1572 if (!Outs.empty()) {
1573 // Check if stack adjustment is needed. For now, do not do this if any
1574 // argument is passed on the stack.
1575 SmallVector<CCValAssign, 16> ArgLocs;
1576 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1577 ArgLocs, *DAG.getContext());
1578 CCInfo.AnalyzeCallOperands(Outs,
1579 CCAssignFnForNode(CalleeCC, false, isVarArg));
1580 if (CCInfo.getNextStackOffset()) {
1581 MachineFunction &MF = DAG.getMachineFunction();
1582
1583 // Check if the arguments are already laid out in the right way as
1584 // the caller's fixed stack objects.
1585 MachineFrameInfo *MFI = MF.getFrameInfo();
1586 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1587 const ARMInstrInfo *TII =
1588 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001589 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1590 i != e;
1591 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001592 CCValAssign &VA = ArgLocs[i];
1593 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001594 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001595 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001596 if (VA.getLocInfo() == CCValAssign::Indirect)
1597 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001598 if (VA.needsCustom()) {
1599 // f64 and vector types are split into multiple registers or
1600 // register/stack-slot combinations. The types will not match
1601 // the registers; give up on memory f64 refs until we figure
1602 // out what to do about this.
1603 if (!VA.isRegLoc())
1604 return false;
1605 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001606 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001607 if (RegVT == MVT::v2f64) {
1608 if (!ArgLocs[++i].isRegLoc())
1609 return false;
1610 if (!ArgLocs[++i].isRegLoc())
1611 return false;
1612 }
1613 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001614 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1615 MFI, MRI, TII))
1616 return false;
1617 }
1618 }
1619 }
1620 }
1621
1622 return true;
1623}
1624
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625SDValue
1626ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001627 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001628 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001629 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001630 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001631
Bob Wilsondee46d72009-04-17 20:35:10 +00001632 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001633 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001634
Bob Wilsondee46d72009-04-17 20:35:10 +00001635 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001636 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1637 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001638
Dan Gohman98ca4f22009-08-05 01:29:28 +00001639 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001640 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1641 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001642
1643 // If this is the first return lowered for this function, add
1644 // the regs to the liveout set for the function.
1645 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1646 for (unsigned i = 0; i != RVLocs.size(); ++i)
1647 if (RVLocs[i].isRegLoc())
1648 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001649 }
1650
Bob Wilson1f595bb2009-04-17 19:07:39 +00001651 SDValue Flag;
1652
1653 // Copy the result values into the output registers.
1654 for (unsigned i = 0, realRVLocIdx = 0;
1655 i != RVLocs.size();
1656 ++i, ++realRVLocIdx) {
1657 CCValAssign &VA = RVLocs[i];
1658 assert(VA.isRegLoc() && "Can only return in registers!");
1659
Dan Gohmanc9403652010-07-07 15:54:55 +00001660 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001661
1662 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001663 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001664 case CCValAssign::Full: break;
1665 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001666 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001667 break;
1668 }
1669
Bob Wilson1f595bb2009-04-17 19:07:39 +00001670 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001671 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001672 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001673 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1674 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001675 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001676 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001677
1678 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1679 Flag = Chain.getValue(1);
1680 VA = RVLocs[++i]; // skip ahead to next loc
1681 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1682 HalfGPRs.getValue(1), Flag);
1683 Flag = Chain.getValue(1);
1684 VA = RVLocs[++i]; // skip ahead to next loc
1685
1686 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001687 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1688 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001689 }
1690 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1691 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001692 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001694 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001695 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001696 VA = RVLocs[++i]; // skip ahead to next loc
1697 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1698 Flag);
1699 } else
1700 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1701
Bob Wilsondee46d72009-04-17 20:35:10 +00001702 // Guarantee that all emitted copies are
1703 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001704 Flag = Chain.getValue(1);
1705 }
1706
1707 SDValue result;
1708 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001709 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001710 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001712
1713 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001714}
1715
Evan Cheng3d2125c2010-11-30 23:55:39 +00001716bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1717 if (N->getNumValues() != 1)
1718 return false;
1719 if (!N->hasNUsesOfValue(1, 0))
1720 return false;
1721
1722 unsigned NumCopies = 0;
1723 SDNode* Copies[2];
1724 SDNode *Use = *N->use_begin();
1725 if (Use->getOpcode() == ISD::CopyToReg) {
1726 Copies[NumCopies++] = Use;
1727 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1728 // f64 returned in a pair of GPRs.
1729 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1730 UI != UE; ++UI) {
1731 if (UI->getOpcode() != ISD::CopyToReg)
1732 return false;
1733 Copies[UI.getUse().getResNo()] = *UI;
1734 ++NumCopies;
1735 }
1736 } else if (Use->getOpcode() == ISD::BITCAST) {
1737 // f32 returned in a single GPR.
1738 if (!Use->hasNUsesOfValue(1, 0))
1739 return false;
1740 Use = *Use->use_begin();
1741 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1742 return false;
1743 Copies[NumCopies++] = Use;
1744 } else {
1745 return false;
1746 }
1747
1748 if (NumCopies != 1 && NumCopies != 2)
1749 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001750
1751 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001752 for (unsigned i = 0; i < NumCopies; ++i) {
1753 SDNode *Copy = Copies[i];
1754 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1755 UI != UE; ++UI) {
1756 if (UI->getOpcode() == ISD::CopyToReg) {
1757 SDNode *Use = *UI;
1758 if (Use == Copies[0] || Use == Copies[1])
1759 continue;
1760 return false;
1761 }
1762 if (UI->getOpcode() != ARMISD::RET_FLAG)
1763 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001764 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001765 }
1766 }
1767
Evan Cheng1bf891a2010-12-01 22:59:46 +00001768 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001769}
1770
Bob Wilsonb62d2572009-11-03 00:02:05 +00001771// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1772// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1773// one of the above mentioned nodes. It has to be wrapped because otherwise
1774// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1775// be used to form addressing mode. These wrapped nodes will be selected
1776// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001777static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001778 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001779 // FIXME there is no actual debug info here
1780 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001781 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001782 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001783 if (CP->isMachineConstantPoolEntry())
1784 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1785 CP->getAlignment());
1786 else
1787 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1788 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001790}
1791
Jim Grosbache1102ca2010-07-19 17:20:38 +00001792unsigned ARMTargetLowering::getJumpTableEncoding() const {
1793 return MachineJumpTableInfo::EK_Inline;
1794}
1795
Dan Gohmand858e902010-04-17 15:26:15 +00001796SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1797 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001798 MachineFunction &MF = DAG.getMachineFunction();
1799 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1800 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001801 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001802 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001803 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001804 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1805 SDValue CPAddr;
1806 if (RelocM == Reloc::Static) {
1807 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1808 } else {
1809 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001810 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001811 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1812 ARMCP::CPBlockAddress,
1813 PCAdj);
1814 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1815 }
1816 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1817 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001818 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001819 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001820 if (RelocM == Reloc::Static)
1821 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001822 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001823 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001824}
1825
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001826// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001827SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001828ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001829 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001830 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001831 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001832 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001833 MachineFunction &MF = DAG.getMachineFunction();
1834 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1835 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001836 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001837 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001838 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001839 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001841 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001842 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001843 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001844 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001845
Evan Chenge7e0d622009-11-06 22:24:13 +00001846 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001847 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001848
1849 // call __tls_get_addr.
1850 ArgListTy Args;
1851 ArgListEntry Entry;
1852 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001853 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001854 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001855 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001856 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001857 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1858 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001859 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001860 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001861 return CallResult.first;
1862}
1863
1864// Lower ISD::GlobalTLSAddress using the "initial exec" or
1865// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001866SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001867ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001868 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001869 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001870 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001871 SDValue Offset;
1872 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001873 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001874 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001875 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001876
Chris Lattner4fb63d02009-07-15 04:12:33 +00001877 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001878 MachineFunction &MF = DAG.getMachineFunction();
1879 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1880 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1881 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001882 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1883 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001884 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001885 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001886 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001888 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001889 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001890 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001891 Chain = Offset.getValue(1);
1892
Evan Chenge7e0d622009-11-06 22:24:13 +00001893 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001894 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001895
Evan Cheng9eda6892009-10-31 03:39:36 +00001896 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001897 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001898 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001899 } else {
1900 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001901 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001902 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001903 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001904 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001905 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001906 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001907 }
1908
1909 // The address of the thread local variable is the add of the thread
1910 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001911 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001912}
1913
Dan Gohman475871a2008-07-27 21:46:04 +00001914SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001915ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001916 // TODO: implement the "local dynamic" model
1917 assert(Subtarget->isTargetELF() &&
1918 "TLS not implemented for non-ELF targets");
1919 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1920 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1921 // otherwise use the "Local Exec" TLS Model
1922 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1923 return LowerToTLSGeneralDynamicModel(GA, DAG);
1924 else
1925 return LowerToTLSExecModels(GA, DAG);
1926}
1927
Dan Gohman475871a2008-07-27 21:46:04 +00001928SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001929 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001930 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001931 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001932 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001933 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1934 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001935 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001936 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001937 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001938 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001939 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001940 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001941 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001942 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001943 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001945 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001946 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001947 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001948 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001949 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001950 return Result;
1951 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001952 // If we have T2 ops, we can materialize the address directly via movt/movw
1953 // pair. This is always cheaper.
1954 if (Subtarget->useMovt()) {
1955 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
Devang Patel0d881da2010-07-06 22:08:15 +00001956 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001957 } else {
1958 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1959 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1960 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001961 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001962 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001963 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001964 }
1965}
1966
Dan Gohman475871a2008-07-27 21:46:04 +00001967SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001968 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001969 MachineFunction &MF = DAG.getMachineFunction();
1970 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1971 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001972 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001973 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001974 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001975 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001977 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001978 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001979 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001980 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001981 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1982 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001983 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001984 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001985 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001986 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001987
Evan Cheng9eda6892009-10-31 03:39:36 +00001988 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001989 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001990 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001991 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001992
1993 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001994 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001995 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001996 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001997
Evan Cheng63476a82009-09-03 07:04:02 +00001998 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001999 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002000 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002001
2002 return Result;
2003}
2004
Dan Gohman475871a2008-07-27 21:46:04 +00002005SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002006 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002007 assert(Subtarget->isTargetELF() &&
2008 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002009 MachineFunction &MF = DAG.getMachineFunction();
2010 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2011 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002012 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002013 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002014 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002015 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2016 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002017 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002018 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002019 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002020 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002021 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002022 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002023 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002024 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002025}
2026
Jim Grosbach0e0da732009-05-12 23:59:14 +00002027SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002028ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2029 const {
2030 DebugLoc dl = Op.getDebugLoc();
2031 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
2032 Op.getOperand(0), Op.getOperand(1));
2033}
2034
2035SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002036ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2037 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002038 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002039 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2040 Op.getOperand(1), Val);
2041}
2042
2043SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002044ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2045 DebugLoc dl = Op.getDebugLoc();
2046 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2047 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2048}
2049
2050SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002051ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002052 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002053 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002054 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002055 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002056 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002057 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002058 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002059 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2060 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002061 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002062 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002063 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2064 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002065 EVT PtrVT = getPointerTy();
2066 DebugLoc dl = Op.getDebugLoc();
2067 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2068 SDValue CPAddr;
2069 unsigned PCAdj = (RelocM != Reloc::PIC_)
2070 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002071 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002072 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2073 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002074 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002075 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002076 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002077 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002078 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002079 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002080
2081 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002082 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002083 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2084 }
2085 return Result;
2086 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002087 }
2088}
2089
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002090static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002091 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002092 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002093 if (!Subtarget->hasDataBarrier()) {
2094 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2095 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2096 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002097 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002098 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002099 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002100 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002101 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002102
2103 SDValue Op5 = Op.getOperand(5);
2104 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2105 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2106 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2107 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2108
2109 ARM_MB::MemBOpt DMBOpt;
2110 if (isDeviceBarrier)
2111 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2112 else
2113 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2114 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2115 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002116}
2117
Evan Chengdfed19f2010-11-03 06:34:55 +00002118static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2119 const ARMSubtarget *Subtarget) {
2120 // ARM pre v5TE and Thumb1 does not have preload instructions.
2121 if (!(Subtarget->isThumb2() ||
2122 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2123 // Just preserve the chain.
2124 return Op.getOperand(0);
2125
2126 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002127 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2128 if (!isRead &&
2129 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2130 // ARMv7 with MP extension has PLDW.
2131 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002132
2133 if (Subtarget->isThumb())
2134 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002135 isRead = ~isRead & 1;
2136 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002137
Evan Cheng416941d2010-11-04 05:19:35 +00002138 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002139 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002140 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2141 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002142}
2143
Dan Gohman1e93df62010-04-17 14:41:14 +00002144static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2145 MachineFunction &MF = DAG.getMachineFunction();
2146 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2147
Evan Chenga8e29892007-01-19 07:51:42 +00002148 // vastart just stores the address of the VarArgsFrameIndex slot into the
2149 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002150 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002151 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002152 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002153 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002154 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2155 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002156}
2157
Dan Gohman475871a2008-07-27 21:46:04 +00002158SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002159ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2160 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002161 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002162 MachineFunction &MF = DAG.getMachineFunction();
2163 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2164
2165 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002166 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002167 RC = ARM::tGPRRegisterClass;
2168 else
2169 RC = ARM::GPRRegisterClass;
2170
2171 // Transform the arguments stored in physical registers into virtual ones.
Jim Grosbach4725ca72010-09-08 03:54:02 +00002172 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002173 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002174
2175 SDValue ArgValue2;
2176 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002177 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002178 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002179
2180 // Create load node to retrieve arguments from the stack.
2181 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002182 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002183 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002184 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002185 } else {
2186 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002187 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002188 }
2189
Jim Grosbache5165492009-11-09 00:11:35 +00002190 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002191}
2192
2193SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002194ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002195 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002196 const SmallVectorImpl<ISD::InputArg>
2197 &Ins,
2198 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002199 SmallVectorImpl<SDValue> &InVals)
2200 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201
Bob Wilson1f595bb2009-04-17 19:07:39 +00002202 MachineFunction &MF = DAG.getMachineFunction();
2203 MachineFrameInfo *MFI = MF.getFrameInfo();
2204
Bob Wilson1f595bb2009-04-17 19:07:39 +00002205 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2206
2207 // Assign locations to all of the incoming arguments.
2208 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002209 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2210 *DAG.getContext());
2211 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002212 CCAssignFnForNode(CallConv, /* Return*/ false,
2213 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002214
2215 SmallVector<SDValue, 16> ArgValues;
2216
2217 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2218 CCValAssign &VA = ArgLocs[i];
2219
Bob Wilsondee46d72009-04-17 20:35:10 +00002220 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002221 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002222 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002223
Bob Wilson5bafff32009-06-22 23:27:02 +00002224 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002225 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002226 // f64 and vector types are split up into multiple registers or
2227 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002228 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002229 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002230 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002231 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002232 SDValue ArgValue2;
2233 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002234 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002235 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2236 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002237 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002238 false, false, 0);
2239 } else {
2240 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2241 Chain, DAG, dl);
2242 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002243 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2244 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002245 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002246 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002247 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2248 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002249 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002250
Bob Wilson5bafff32009-06-22 23:27:02 +00002251 } else {
2252 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002253
Owen Anderson825b72b2009-08-11 20:47:22 +00002254 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002255 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002256 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002257 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002258 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002259 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002260 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002261 RC = (AFI->isThumb1OnlyFunction() ?
2262 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002263 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002264 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002265
2266 // Transform the arguments in physical registers into virtual ones.
2267 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002268 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002269 }
2270
2271 // If this is an 8 or 16-bit value, it is really passed promoted
2272 // to 32 bits. Insert an assert[sz]ext to capture this, then
2273 // truncate to the right size.
2274 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002275 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002276 case CCValAssign::Full: break;
2277 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002278 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002279 break;
2280 case CCValAssign::SExt:
2281 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2282 DAG.getValueType(VA.getValVT()));
2283 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2284 break;
2285 case CCValAssign::ZExt:
2286 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2287 DAG.getValueType(VA.getValVT()));
2288 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2289 break;
2290 }
2291
Dan Gohman98ca4f22009-08-05 01:29:28 +00002292 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002293
2294 } else { // VA.isRegLoc()
2295
2296 // sanity check
2297 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002298 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002299
2300 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002301 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002302
Bob Wilsondee46d72009-04-17 20:35:10 +00002303 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002304 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002305 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002306 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002307 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002308 }
2309 }
2310
2311 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002312 if (isVarArg) {
2313 static const unsigned GPRArgRegs[] = {
2314 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2315 };
2316
Bob Wilsondee46d72009-04-17 20:35:10 +00002317 unsigned NumGPRs = CCInfo.getFirstUnallocated
2318 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002319
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002320 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
2321 unsigned VARegSize = (4 - NumGPRs) * 4;
2322 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002323 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002324 if (VARegSaveSize) {
2325 // If this function is vararg, store any remaining integer argument regs
2326 // to their spots on the stack so that they may be loaded by deferencing
2327 // the result of va_next.
2328 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002329 AFI->setVarArgsFrameIndex(
2330 MFI->CreateFixedObject(VARegSaveSize,
2331 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002332 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002333 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2334 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002335
Dan Gohman475871a2008-07-27 21:46:04 +00002336 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002337 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002338 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002339 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002340 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002341 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002342 RC = ARM::GPRRegisterClass;
2343
Bob Wilson998e1252009-04-20 18:36:57 +00002344 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002345 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002346 SDValue Store =
2347 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002348 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2349 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002350 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002351 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002352 DAG.getConstant(4, getPointerTy()));
2353 }
2354 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002355 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002356 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002357 } else
2358 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002359 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002360 }
2361
Dan Gohman98ca4f22009-08-05 01:29:28 +00002362 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002363}
2364
2365/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002366static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002367 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002368 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002369 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002370 // Maybe this has already been legalized into the constant pool?
2371 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002372 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002373 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002374 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002375 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002376 }
2377 }
2378 return false;
2379}
2380
Evan Chenga8e29892007-01-19 07:51:42 +00002381/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2382/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002383SDValue
2384ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002385 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002386 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002387 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002388 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002389 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002390 // Constant does not fit, try adjusting it by one?
2391 switch (CC) {
2392 default: break;
2393 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002394 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002395 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002396 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002397 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002398 }
2399 break;
2400 case ISD::SETULT:
2401 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002402 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002403 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002404 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002405 }
2406 break;
2407 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002408 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002409 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002410 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002411 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002412 }
2413 break;
2414 case ISD::SETULE:
2415 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002416 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002417 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002418 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002419 }
2420 break;
2421 }
2422 }
2423 }
2424
2425 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002426 ARMISD::NodeType CompareType;
2427 switch (CondCode) {
2428 default:
2429 CompareType = ARMISD::CMP;
2430 break;
2431 case ARMCC::EQ:
2432 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002433 // Uses only Z Flag
2434 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002435 break;
2436 }
Evan Cheng218977b2010-07-13 19:27:42 +00002437 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002438 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002439}
2440
2441/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002442SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002443ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002444 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002445 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002446 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002447 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002448 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002449 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2450 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002451}
2452
Bill Wendlingde2b1512010-08-11 08:43:16 +00002453SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2454 SDValue Cond = Op.getOperand(0);
2455 SDValue SelectTrue = Op.getOperand(1);
2456 SDValue SelectFalse = Op.getOperand(2);
2457 DebugLoc dl = Op.getDebugLoc();
2458
2459 // Convert:
2460 //
2461 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2462 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2463 //
2464 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2465 const ConstantSDNode *CMOVTrue =
2466 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2467 const ConstantSDNode *CMOVFalse =
2468 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2469
2470 if (CMOVTrue && CMOVFalse) {
2471 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2472 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2473
2474 SDValue True;
2475 SDValue False;
2476 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2477 True = SelectTrue;
2478 False = SelectFalse;
2479 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2480 True = SelectFalse;
2481 False = SelectTrue;
2482 }
2483
2484 if (True.getNode() && False.getNode()) {
2485 EVT VT = Cond.getValueType();
2486 SDValue ARMcc = Cond.getOperand(2);
2487 SDValue CCR = Cond.getOperand(3);
2488 SDValue Cmp = Cond.getOperand(4);
2489 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2490 }
2491 }
2492 }
2493
2494 return DAG.getSelectCC(dl, Cond,
2495 DAG.getConstant(0, Cond.getValueType()),
2496 SelectTrue, SelectFalse, ISD::SETNE);
2497}
2498
Dan Gohmand858e902010-04-17 15:26:15 +00002499SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002500 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002501 SDValue LHS = Op.getOperand(0);
2502 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002503 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002504 SDValue TrueVal = Op.getOperand(2);
2505 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002506 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002507
Owen Anderson825b72b2009-08-11 20:47:22 +00002508 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002509 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002510 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002511 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2512 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002513 }
2514
2515 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002516 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002517
Evan Cheng218977b2010-07-13 19:27:42 +00002518 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2519 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002520 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002521 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002522 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002523 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002524 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002525 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002526 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002527 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002528 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002529 }
2530 return Result;
2531}
2532
Evan Cheng218977b2010-07-13 19:27:42 +00002533/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2534/// to morph to an integer compare sequence.
2535static bool canChangeToInt(SDValue Op, bool &SeenZero,
2536 const ARMSubtarget *Subtarget) {
2537 SDNode *N = Op.getNode();
2538 if (!N->hasOneUse())
2539 // Otherwise it requires moving the value from fp to integer registers.
2540 return false;
2541 if (!N->getNumValues())
2542 return false;
2543 EVT VT = Op.getValueType();
2544 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2545 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2546 // vmrs are very slow, e.g. cortex-a8.
2547 return false;
2548
2549 if (isFloatingPointZero(Op)) {
2550 SeenZero = true;
2551 return true;
2552 }
2553 return ISD::isNormalLoad(N);
2554}
2555
2556static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2557 if (isFloatingPointZero(Op))
2558 return DAG.getConstant(0, MVT::i32);
2559
2560 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2561 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002562 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002563 Ld->isVolatile(), Ld->isNonTemporal(),
2564 Ld->getAlignment());
2565
2566 llvm_unreachable("Unknown VFP cmp argument!");
2567}
2568
2569static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2570 SDValue &RetVal1, SDValue &RetVal2) {
2571 if (isFloatingPointZero(Op)) {
2572 RetVal1 = DAG.getConstant(0, MVT::i32);
2573 RetVal2 = DAG.getConstant(0, MVT::i32);
2574 return;
2575 }
2576
2577 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2578 SDValue Ptr = Ld->getBasePtr();
2579 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2580 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002581 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002582 Ld->isVolatile(), Ld->isNonTemporal(),
2583 Ld->getAlignment());
2584
2585 EVT PtrType = Ptr.getValueType();
2586 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2587 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2588 PtrType, Ptr, DAG.getConstant(4, PtrType));
2589 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2590 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002591 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002592 Ld->isVolatile(), Ld->isNonTemporal(),
2593 NewAlign);
2594 return;
2595 }
2596
2597 llvm_unreachable("Unknown VFP cmp argument!");
2598}
2599
2600/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2601/// f32 and even f64 comparisons to integer ones.
2602SDValue
2603ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2604 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002605 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002606 SDValue LHS = Op.getOperand(2);
2607 SDValue RHS = Op.getOperand(3);
2608 SDValue Dest = Op.getOperand(4);
2609 DebugLoc dl = Op.getDebugLoc();
2610
2611 bool SeenZero = false;
2612 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2613 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002614 // If one of the operand is zero, it's safe to ignore the NaN case since
2615 // we only care about equality comparisons.
2616 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002617 // If unsafe fp math optimization is enabled and there are no othter uses of
2618 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2619 // to an integer comparison.
2620 if (CC == ISD::SETOEQ)
2621 CC = ISD::SETEQ;
2622 else if (CC == ISD::SETUNE)
2623 CC = ISD::SETNE;
2624
2625 SDValue ARMcc;
2626 if (LHS.getValueType() == MVT::f32) {
2627 LHS = bitcastf32Toi32(LHS, DAG);
2628 RHS = bitcastf32Toi32(RHS, DAG);
2629 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2630 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2631 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2632 Chain, Dest, ARMcc, CCR, Cmp);
2633 }
2634
2635 SDValue LHS1, LHS2;
2636 SDValue RHS1, RHS2;
2637 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2638 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2639 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2640 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002641 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002642 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2643 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2644 }
2645
2646 return SDValue();
2647}
2648
2649SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2650 SDValue Chain = Op.getOperand(0);
2651 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2652 SDValue LHS = Op.getOperand(2);
2653 SDValue RHS = Op.getOperand(3);
2654 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002655 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002656
Owen Anderson825b72b2009-08-11 20:47:22 +00002657 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002658 SDValue ARMcc;
2659 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002660 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002661 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002662 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002663 }
2664
Owen Anderson825b72b2009-08-11 20:47:22 +00002665 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002666
2667 if (UnsafeFPMath &&
2668 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2669 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2670 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2671 if (Result.getNode())
2672 return Result;
2673 }
2674
Evan Chenga8e29892007-01-19 07:51:42 +00002675 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002676 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002677
Evan Cheng218977b2010-07-13 19:27:42 +00002678 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2679 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002680 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002681 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002682 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002683 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002684 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002685 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2686 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002687 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002688 }
2689 return Res;
2690}
2691
Dan Gohmand858e902010-04-17 15:26:15 +00002692SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002693 SDValue Chain = Op.getOperand(0);
2694 SDValue Table = Op.getOperand(1);
2695 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002696 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002697
Owen Andersone50ed302009-08-10 22:56:29 +00002698 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002699 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2700 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002701 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002702 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002703 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002704 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2705 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002706 if (Subtarget->isThumb2()) {
2707 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2708 // which does another jump to the destination. This also makes it easier
2709 // to translate it to TBB / TBH later.
2710 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002711 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002712 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002713 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002714 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002715 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002716 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002717 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002718 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002719 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002720 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002721 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002722 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002723 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002724 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002725 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002726 }
Evan Chenga8e29892007-01-19 07:51:42 +00002727}
2728
Bob Wilson76a312b2010-03-19 22:51:32 +00002729static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2730 DebugLoc dl = Op.getDebugLoc();
2731 unsigned Opc;
2732
2733 switch (Op.getOpcode()) {
2734 default:
2735 assert(0 && "Invalid opcode!");
2736 case ISD::FP_TO_SINT:
2737 Opc = ARMISD::FTOSI;
2738 break;
2739 case ISD::FP_TO_UINT:
2740 Opc = ARMISD::FTOUI;
2741 break;
2742 }
2743 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002744 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002745}
2746
2747static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2748 EVT VT = Op.getValueType();
2749 DebugLoc dl = Op.getDebugLoc();
2750 unsigned Opc;
2751
2752 switch (Op.getOpcode()) {
2753 default:
2754 assert(0 && "Invalid opcode!");
2755 case ISD::SINT_TO_FP:
2756 Opc = ARMISD::SITOF;
2757 break;
2758 case ISD::UINT_TO_FP:
2759 Opc = ARMISD::UITOF;
2760 break;
2761 }
2762
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002763 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002764 return DAG.getNode(Opc, dl, VT, Op);
2765}
2766
Evan Cheng515fe3a2010-07-08 02:08:50 +00002767SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002768 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002769 SDValue Tmp0 = Op.getOperand(0);
2770 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002771 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002772 EVT VT = Op.getValueType();
2773 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002774 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002775 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002776 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002777 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002778 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002779 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002780}
2781
Evan Cheng2457f2c2010-05-22 01:47:14 +00002782SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2783 MachineFunction &MF = DAG.getMachineFunction();
2784 MachineFrameInfo *MFI = MF.getFrameInfo();
2785 MFI->setReturnAddressIsTaken(true);
2786
2787 EVT VT = Op.getValueType();
2788 DebugLoc dl = Op.getDebugLoc();
2789 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2790 if (Depth) {
2791 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2792 SDValue Offset = DAG.getConstant(4, MVT::i32);
2793 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2794 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002795 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002796 }
2797
2798 // Return LR, which contains the return address. Mark it an implicit live-in.
Jim Grosbachc2723a52010-07-23 23:50:35 +00002799 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00002800 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2801}
2802
Dan Gohmand858e902010-04-17 15:26:15 +00002803SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002804 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2805 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002806
Owen Andersone50ed302009-08-10 22:56:29 +00002807 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002808 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2809 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002810 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002811 ? ARM::R7 : ARM::R11;
2812 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2813 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002814 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
2815 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00002816 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002817 return FrameAddr;
2818}
2819
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002820/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00002821/// expand a bit convert where either the source or destination type is i64 to
2822/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2823/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2824/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002825static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002826 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2827 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002828 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002829
Bob Wilson9f3f0612010-04-17 05:30:19 +00002830 // This function is only supposed to be called for i64 types, either as the
2831 // source or destination of the bit convert.
2832 EVT SrcVT = Op.getValueType();
2833 EVT DstVT = N->getValueType(0);
2834 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002835 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002836
Bob Wilson9f3f0612010-04-17 05:30:19 +00002837 // Turn i64->f64 into VMOVDRR.
2838 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002839 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2840 DAG.getConstant(0, MVT::i32));
2841 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2842 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002843 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00002844 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002845 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002846
Jim Grosbache5165492009-11-09 00:11:35 +00002847 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002848 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2849 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2850 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2851 // Merge the pieces into a single i64 value.
2852 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2853 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002854
Bob Wilson9f3f0612010-04-17 05:30:19 +00002855 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002856}
2857
Bob Wilson5bafff32009-06-22 23:27:02 +00002858/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002859/// Zero vectors are used to represent vector negation and in those cases
2860/// will be implemented with the NEON VNEG instruction. However, VNEG does
2861/// not support i64 elements, so sometimes the zero vectors will need to be
2862/// explicitly constructed. Regardless, use a canonical VMOV to create the
2863/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002864static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002865 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002866 // The canonical modified immediate encoding of a zero vector is....0!
2867 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2868 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2869 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002870 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002871}
2872
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002873/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2874/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002875SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2876 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002877 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2878 EVT VT = Op.getValueType();
2879 unsigned VTBits = VT.getSizeInBits();
2880 DebugLoc dl = Op.getDebugLoc();
2881 SDValue ShOpLo = Op.getOperand(0);
2882 SDValue ShOpHi = Op.getOperand(1);
2883 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002884 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002885 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002886
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002887 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2888
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002889 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2890 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2891 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2892 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2893 DAG.getConstant(VTBits, MVT::i32));
2894 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2895 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002896 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002897
2898 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2899 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002900 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002901 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002902 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002903 CCR, Cmp);
2904
2905 SDValue Ops[2] = { Lo, Hi };
2906 return DAG.getMergeValues(Ops, 2, dl);
2907}
2908
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002909/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2910/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002911SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2912 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002913 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2914 EVT VT = Op.getValueType();
2915 unsigned VTBits = VT.getSizeInBits();
2916 DebugLoc dl = Op.getDebugLoc();
2917 SDValue ShOpLo = Op.getOperand(0);
2918 SDValue ShOpHi = Op.getOperand(1);
2919 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002920 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002921
2922 assert(Op.getOpcode() == ISD::SHL_PARTS);
2923 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2924 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2925 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2926 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2927 DAG.getConstant(VTBits, MVT::i32));
2928 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2929 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2930
2931 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2932 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2933 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002934 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002935 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002936 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002937 CCR, Cmp);
2938
2939 SDValue Ops[2] = { Lo, Hi };
2940 return DAG.getMergeValues(Ops, 2, dl);
2941}
2942
Jim Grosbach4725ca72010-09-08 03:54:02 +00002943SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00002944 SelectionDAG &DAG) const {
2945 // The rounding mode is in bits 23:22 of the FPSCR.
2946 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
2947 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
2948 // so that the shift + and get folded into a bitfield extract.
2949 DebugLoc dl = Op.getDebugLoc();
2950 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
2951 DAG.getConstant(Intrinsic::arm_get_fpscr,
2952 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00002953 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00002954 DAG.getConstant(1U << 22, MVT::i32));
2955 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
2956 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00002957 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00002958 DAG.getConstant(3, MVT::i32));
2959}
2960
Jim Grosbach3482c802010-01-18 19:58:49 +00002961static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2962 const ARMSubtarget *ST) {
2963 EVT VT = N->getValueType(0);
2964 DebugLoc dl = N->getDebugLoc();
2965
2966 if (!ST->hasV6T2Ops())
2967 return SDValue();
2968
2969 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2970 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2971}
2972
Bob Wilson5bafff32009-06-22 23:27:02 +00002973static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2974 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002975 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002976 DebugLoc dl = N->getDebugLoc();
2977
Bob Wilsond5448bb2010-11-18 21:16:28 +00002978 if (!VT.isVector())
2979 return SDValue();
2980
Bob Wilson5bafff32009-06-22 23:27:02 +00002981 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00002982 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00002983
Bob Wilsond5448bb2010-11-18 21:16:28 +00002984 // Left shifts translate directly to the vshiftu intrinsic.
2985 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00002986 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00002987 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
2988 N->getOperand(0), N->getOperand(1));
2989
2990 assert((N->getOpcode() == ISD::SRA ||
2991 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2992
2993 // NEON uses the same intrinsics for both left and right shifts. For
2994 // right shifts, the shift amounts are negative, so negate the vector of
2995 // shift amounts.
2996 EVT ShiftVT = N->getOperand(1).getValueType();
2997 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2998 getZeroVector(ShiftVT, DAG, dl),
2999 N->getOperand(1));
3000 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3001 Intrinsic::arm_neon_vshifts :
3002 Intrinsic::arm_neon_vshiftu);
3003 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3004 DAG.getConstant(vshiftInt, MVT::i32),
3005 N->getOperand(0), NegatedCount);
3006}
3007
3008static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3009 const ARMSubtarget *ST) {
3010 EVT VT = N->getValueType(0);
3011 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003012
Eli Friedmance392eb2009-08-22 03:13:10 +00003013 // We can get here for a node like i32 = ISD::SHL i32, i64
3014 if (VT != MVT::i64)
3015 return SDValue();
3016
3017 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003018 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003019
Chris Lattner27a6c732007-11-24 07:07:01 +00003020 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3021 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003022 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003023 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003024
Chris Lattner27a6c732007-11-24 07:07:01 +00003025 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003026 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003027
Chris Lattner27a6c732007-11-24 07:07:01 +00003028 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003029 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003030 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003031 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003032 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003033
Chris Lattner27a6c732007-11-24 07:07:01 +00003034 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3035 // captures the result into a carry flag.
3036 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003037 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003038
Chris Lattner27a6c732007-11-24 07:07:01 +00003039 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003041
Chris Lattner27a6c732007-11-24 07:07:01 +00003042 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003043 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003044}
3045
Bob Wilson5bafff32009-06-22 23:27:02 +00003046static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3047 SDValue TmpOp0, TmpOp1;
3048 bool Invert = false;
3049 bool Swap = false;
3050 unsigned Opc = 0;
3051
3052 SDValue Op0 = Op.getOperand(0);
3053 SDValue Op1 = Op.getOperand(1);
3054 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003055 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003056 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3057 DebugLoc dl = Op.getDebugLoc();
3058
3059 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3060 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003061 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003062 case ISD::SETUNE:
3063 case ISD::SETNE: Invert = true; // Fallthrough
3064 case ISD::SETOEQ:
3065 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3066 case ISD::SETOLT:
3067 case ISD::SETLT: Swap = true; // Fallthrough
3068 case ISD::SETOGT:
3069 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3070 case ISD::SETOLE:
3071 case ISD::SETLE: Swap = true; // Fallthrough
3072 case ISD::SETOGE:
3073 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3074 case ISD::SETUGE: Swap = true; // Fallthrough
3075 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3076 case ISD::SETUGT: Swap = true; // Fallthrough
3077 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3078 case ISD::SETUEQ: Invert = true; // Fallthrough
3079 case ISD::SETONE:
3080 // Expand this to (OLT | OGT).
3081 TmpOp0 = Op0;
3082 TmpOp1 = Op1;
3083 Opc = ISD::OR;
3084 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3085 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3086 break;
3087 case ISD::SETUO: Invert = true; // Fallthrough
3088 case ISD::SETO:
3089 // Expand this to (OLT | OGE).
3090 TmpOp0 = Op0;
3091 TmpOp1 = Op1;
3092 Opc = ISD::OR;
3093 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3094 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3095 break;
3096 }
3097 } else {
3098 // Integer comparisons.
3099 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003100 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003101 case ISD::SETNE: Invert = true;
3102 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3103 case ISD::SETLT: Swap = true;
3104 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3105 case ISD::SETLE: Swap = true;
3106 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3107 case ISD::SETULT: Swap = true;
3108 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3109 case ISD::SETULE: Swap = true;
3110 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3111 }
3112
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003113 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003114 if (Opc == ARMISD::VCEQ) {
3115
3116 SDValue AndOp;
3117 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3118 AndOp = Op0;
3119 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3120 AndOp = Op1;
3121
3122 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003123 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003124 AndOp = AndOp.getOperand(0);
3125
3126 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3127 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003128 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3129 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003130 Invert = !Invert;
3131 }
3132 }
3133 }
3134
3135 if (Swap)
3136 std::swap(Op0, Op1);
3137
Owen Andersonc24cb352010-11-08 23:21:22 +00003138 // If one of the operands is a constant vector zero, attempt to fold the
3139 // comparison to a specialized compare-against-zero form.
3140 SDValue SingleOp;
3141 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3142 SingleOp = Op0;
3143 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3144 if (Opc == ARMISD::VCGE)
3145 Opc = ARMISD::VCLEZ;
3146 else if (Opc == ARMISD::VCGT)
3147 Opc = ARMISD::VCLTZ;
3148 SingleOp = Op1;
3149 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003150
Owen Andersonc24cb352010-11-08 23:21:22 +00003151 SDValue Result;
3152 if (SingleOp.getNode()) {
3153 switch (Opc) {
3154 case ARMISD::VCEQ:
3155 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3156 case ARMISD::VCGE:
3157 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3158 case ARMISD::VCLEZ:
3159 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3160 case ARMISD::VCGT:
3161 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3162 case ARMISD::VCLTZ:
3163 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3164 default:
3165 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3166 }
3167 } else {
3168 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3169 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003170
3171 if (Invert)
3172 Result = DAG.getNOT(dl, Result, VT);
3173
3174 return Result;
3175}
3176
Bob Wilsond3c42842010-06-14 22:19:57 +00003177/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3178/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003179/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003180static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3181 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003182 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003183 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003184
Bob Wilson827b2102010-06-15 19:05:35 +00003185 // SplatBitSize is set to the smallest size that splats the vector, so a
3186 // zero vector will always have SplatBitSize == 8. However, NEON modified
3187 // immediate instructions others than VMOV do not support the 8-bit encoding
3188 // of a zero vector, and the default encoding of zero is supposed to be the
3189 // 32-bit version.
3190 if (SplatBits == 0)
3191 SplatBitSize = 32;
3192
Bob Wilson5bafff32009-06-22 23:27:02 +00003193 switch (SplatBitSize) {
3194 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003195 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003196 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003197 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003198 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003199 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003200 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003201 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003202 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003203
3204 case 16:
3205 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003206 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003207 if ((SplatBits & ~0xff) == 0) {
3208 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003209 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003210 Imm = SplatBits;
3211 break;
3212 }
3213 if ((SplatBits & ~0xff00) == 0) {
3214 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003215 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003216 Imm = SplatBits >> 8;
3217 break;
3218 }
3219 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003220
3221 case 32:
3222 // NEON's 32-bit VMOV supports splat values where:
3223 // * only one byte is nonzero, or
3224 // * the least significant byte is 0xff and the second byte is nonzero, or
3225 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003226 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003227 if ((SplatBits & ~0xff) == 0) {
3228 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003229 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003230 Imm = SplatBits;
3231 break;
3232 }
3233 if ((SplatBits & ~0xff00) == 0) {
3234 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003235 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003236 Imm = SplatBits >> 8;
3237 break;
3238 }
3239 if ((SplatBits & ~0xff0000) == 0) {
3240 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003241 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003242 Imm = SplatBits >> 16;
3243 break;
3244 }
3245 if ((SplatBits & ~0xff000000) == 0) {
3246 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003247 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003248 Imm = SplatBits >> 24;
3249 break;
3250 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003251
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003252 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3253 if (type == OtherModImm) return SDValue();
3254
Bob Wilson5bafff32009-06-22 23:27:02 +00003255 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003256 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3257 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003258 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003259 Imm = SplatBits >> 8;
3260 SplatBits |= 0xff;
3261 break;
3262 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003263
3264 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003265 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3266 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003267 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003268 Imm = SplatBits >> 16;
3269 SplatBits |= 0xffff;
3270 break;
3271 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003272
3273 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3274 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3275 // VMOV.I32. A (very) minor optimization would be to replicate the value
3276 // and fall through here to test for a valid 64-bit splat. But, then the
3277 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003278 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003279
3280 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003281 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003282 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003283 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003284 uint64_t BitMask = 0xff;
3285 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003286 unsigned ImmMask = 1;
3287 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003288 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003289 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003290 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003291 Imm |= ImmMask;
3292 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003293 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003294 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003295 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003296 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003297 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003298 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003299 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003300 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003301 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003302 break;
3303 }
3304
Bob Wilson1a913ed2010-06-11 21:34:50 +00003305 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003306 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003307 return SDValue();
3308 }
3309
Bob Wilsoncba270d2010-07-13 21:16:48 +00003310 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3311 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003312}
3313
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003314static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3315 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003316 unsigned NumElts = VT.getVectorNumElements();
3317 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003318
3319 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3320 if (M[0] < 0)
3321 return false;
3322
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003323 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003324
3325 // If this is a VEXT shuffle, the immediate value is the index of the first
3326 // element. The other shuffle indices must be the successive elements after
3327 // the first one.
3328 unsigned ExpectedElt = Imm;
3329 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003330 // Increment the expected index. If it wraps around, it may still be
3331 // a VEXT but the source vectors must be swapped.
3332 ExpectedElt += 1;
3333 if (ExpectedElt == NumElts * 2) {
3334 ExpectedElt = 0;
3335 ReverseVEXT = true;
3336 }
3337
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003338 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003339 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003340 return false;
3341 }
3342
3343 // Adjust the index value if the source operands will be swapped.
3344 if (ReverseVEXT)
3345 Imm -= NumElts;
3346
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003347 return true;
3348}
3349
Bob Wilson8bb9e482009-07-26 00:39:34 +00003350/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3351/// instruction with the specified blocksize. (The order of the elements
3352/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003353static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3354 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003355 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3356 "Only possible block sizes for VREV are: 16, 32, 64");
3357
Bob Wilson8bb9e482009-07-26 00:39:34 +00003358 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003359 if (EltSz == 64)
3360 return false;
3361
3362 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003363 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003364 // If the first shuffle index is UNDEF, be optimistic.
3365 if (M[0] < 0)
3366 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003367
3368 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3369 return false;
3370
3371 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003372 if (M[i] < 0) continue; // ignore UNDEF indices
3373 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003374 return false;
3375 }
3376
3377 return true;
3378}
3379
Bob Wilsonc692cb72009-08-21 20:54:19 +00003380static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3381 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003382 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3383 if (EltSz == 64)
3384 return false;
3385
Bob Wilsonc692cb72009-08-21 20:54:19 +00003386 unsigned NumElts = VT.getVectorNumElements();
3387 WhichResult = (M[0] == 0 ? 0 : 1);
3388 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003389 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3390 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003391 return false;
3392 }
3393 return true;
3394}
3395
Bob Wilson324f4f12009-12-03 06:40:55 +00003396/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3397/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3398/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3399static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3400 unsigned &WhichResult) {
3401 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3402 if (EltSz == 64)
3403 return false;
3404
3405 unsigned NumElts = VT.getVectorNumElements();
3406 WhichResult = (M[0] == 0 ? 0 : 1);
3407 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003408 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3409 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003410 return false;
3411 }
3412 return true;
3413}
3414
Bob Wilsonc692cb72009-08-21 20:54:19 +00003415static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3416 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003417 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3418 if (EltSz == 64)
3419 return false;
3420
Bob Wilsonc692cb72009-08-21 20:54:19 +00003421 unsigned NumElts = VT.getVectorNumElements();
3422 WhichResult = (M[0] == 0 ? 0 : 1);
3423 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003424 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003425 if ((unsigned) M[i] != 2 * i + WhichResult)
3426 return false;
3427 }
3428
3429 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003430 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003431 return false;
3432
3433 return true;
3434}
3435
Bob Wilson324f4f12009-12-03 06:40:55 +00003436/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3437/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3438/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3439static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3440 unsigned &WhichResult) {
3441 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3442 if (EltSz == 64)
3443 return false;
3444
3445 unsigned Half = VT.getVectorNumElements() / 2;
3446 WhichResult = (M[0] == 0 ? 0 : 1);
3447 for (unsigned j = 0; j != 2; ++j) {
3448 unsigned Idx = WhichResult;
3449 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003450 int MIdx = M[i + j * Half];
3451 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003452 return false;
3453 Idx += 2;
3454 }
3455 }
3456
3457 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3458 if (VT.is64BitVector() && EltSz == 32)
3459 return false;
3460
3461 return true;
3462}
3463
Bob Wilsonc692cb72009-08-21 20:54:19 +00003464static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3465 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003466 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3467 if (EltSz == 64)
3468 return false;
3469
Bob Wilsonc692cb72009-08-21 20:54:19 +00003470 unsigned NumElts = VT.getVectorNumElements();
3471 WhichResult = (M[0] == 0 ? 0 : 1);
3472 unsigned Idx = WhichResult * NumElts / 2;
3473 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003474 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3475 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003476 return false;
3477 Idx += 1;
3478 }
3479
3480 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003481 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003482 return false;
3483
3484 return true;
3485}
3486
Bob Wilson324f4f12009-12-03 06:40:55 +00003487/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3488/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3489/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3490static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3491 unsigned &WhichResult) {
3492 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3493 if (EltSz == 64)
3494 return false;
3495
3496 unsigned NumElts = VT.getVectorNumElements();
3497 WhichResult = (M[0] == 0 ? 0 : 1);
3498 unsigned Idx = WhichResult * NumElts / 2;
3499 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003500 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3501 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003502 return false;
3503 Idx += 1;
3504 }
3505
3506 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3507 if (VT.is64BitVector() && EltSz == 32)
3508 return false;
3509
3510 return true;
3511}
3512
Dale Johannesenf630c712010-07-29 20:10:08 +00003513// If N is an integer constant that can be moved into a register in one
3514// instruction, return an SDValue of such a constant (will become a MOV
3515// instruction). Otherwise return null.
3516static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3517 const ARMSubtarget *ST, DebugLoc dl) {
3518 uint64_t Val;
3519 if (!isa<ConstantSDNode>(N))
3520 return SDValue();
3521 Val = cast<ConstantSDNode>(N)->getZExtValue();
3522
3523 if (ST->isThumb1Only()) {
3524 if (Val <= 255 || ~Val <= 255)
3525 return DAG.getConstant(Val, MVT::i32);
3526 } else {
3527 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3528 return DAG.getConstant(Val, MVT::i32);
3529 }
3530 return SDValue();
3531}
3532
Bob Wilson5bafff32009-06-22 23:27:02 +00003533// If this is a case we can't handle, return null and let the default
3534// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003535SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3536 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003537 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003538 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003539 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003540
3541 APInt SplatBits, SplatUndef;
3542 unsigned SplatBitSize;
3543 bool HasAnyUndefs;
3544 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003545 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003546 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003547 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003548 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003549 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003550 DAG, VmovVT, VT.is128BitVector(),
3551 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003552 if (Val.getNode()) {
3553 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003554 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003555 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003556
3557 // Try an immediate VMVN.
3558 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3559 ((1LL << SplatBitSize) - 1));
3560 Val = isNEONModifiedImm(NegatedImm,
3561 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003562 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003563 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003564 if (Val.getNode()) {
3565 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003566 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003567 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003568 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003569 }
3570
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003571 // Scan through the operands to see if only one value is used.
3572 unsigned NumElts = VT.getVectorNumElements();
3573 bool isOnlyLowElement = true;
3574 bool usesOnlyOneValue = true;
3575 bool isConstant = true;
3576 SDValue Value;
3577 for (unsigned i = 0; i < NumElts; ++i) {
3578 SDValue V = Op.getOperand(i);
3579 if (V.getOpcode() == ISD::UNDEF)
3580 continue;
3581 if (i > 0)
3582 isOnlyLowElement = false;
3583 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3584 isConstant = false;
3585
3586 if (!Value.getNode())
3587 Value = V;
3588 else if (V != Value)
3589 usesOnlyOneValue = false;
3590 }
3591
3592 if (!Value.getNode())
3593 return DAG.getUNDEF(VT);
3594
3595 if (isOnlyLowElement)
3596 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3597
Dale Johannesenf630c712010-07-29 20:10:08 +00003598 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3599
Dale Johannesen575cd142010-10-19 20:00:17 +00003600 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3601 // i32 and try again.
3602 if (usesOnlyOneValue && EltSize <= 32) {
3603 if (!isConstant)
3604 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3605 if (VT.getVectorElementType().isFloatingPoint()) {
3606 SmallVector<SDValue, 8> Ops;
3607 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003608 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003609 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003610 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3611 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003612 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3613 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003614 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003615 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003616 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3617 if (Val.getNode())
3618 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003619 }
3620
3621 // If all elements are constants and the case above didn't get hit, fall back
3622 // to the default expansion, which will generate a load from the constant
3623 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003624 if (isConstant)
3625 return SDValue();
3626
Bob Wilson11a1dff2011-01-07 21:37:30 +00003627 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3628 if (NumElts >= 4) {
3629 SDValue shuffle = ReconstructShuffle(Op, DAG);
3630 if (shuffle != SDValue())
3631 return shuffle;
3632 }
3633
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003634 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003635 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3636 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003637 if (EltSize >= 32) {
3638 // Do the expansion with floating-point types, since that is what the VFP
3639 // registers are defined to use, and since i64 is not legal.
3640 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3641 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003642 SmallVector<SDValue, 8> Ops;
3643 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003644 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003645 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003646 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003647 }
3648
3649 return SDValue();
3650}
3651
Bob Wilson11a1dff2011-01-07 21:37:30 +00003652// Gather data to see if the operation can be modelled as a
3653// shuffle in combination with VEXTs.
3654SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, SelectionDAG &DAG) const {
3655 DebugLoc dl = Op.getDebugLoc();
3656 EVT VT = Op.getValueType();
3657 unsigned NumElts = VT.getVectorNumElements();
3658
3659 SmallVector<SDValue, 2> SourceVecs;
3660 SmallVector<unsigned, 2> MinElts;
3661 SmallVector<unsigned, 2> MaxElts;
3662
3663 for (unsigned i = 0; i < NumElts; ++i) {
3664 SDValue V = Op.getOperand(i);
3665 if (V.getOpcode() == ISD::UNDEF)
3666 continue;
3667 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3668 // A shuffle can only come from building a vector from various
3669 // elements of other vectors.
3670 return SDValue();
3671 }
3672
3673 // Record this extraction against the appropriate vector if possible...
3674 SDValue SourceVec = V.getOperand(0);
3675 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3676 bool FoundSource = false;
3677 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3678 if (SourceVecs[j] == SourceVec) {
3679 if (MinElts[j] > EltNo)
3680 MinElts[j] = EltNo;
3681 if (MaxElts[j] < EltNo)
3682 MaxElts[j] = EltNo;
3683 FoundSource = true;
3684 break;
3685 }
3686 }
3687
3688 // Or record a new source if not...
3689 if (!FoundSource) {
3690 SourceVecs.push_back(SourceVec);
3691 MinElts.push_back(EltNo);
3692 MaxElts.push_back(EltNo);
3693 }
3694 }
3695
3696 // Currently only do something sane when at most two source vectors
3697 // involved.
3698 if (SourceVecs.size() > 2)
3699 return SDValue();
3700
3701 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3702 int VEXTOffsets[2] = {0, 0};
3703
3704 // This loop extracts the usage patterns of the source vectors
3705 // and prepares appropriate SDValues for a shuffle if possible.
3706 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3707 if (SourceVecs[i].getValueType() == VT) {
3708 // No VEXT necessary
3709 ShuffleSrcs[i] = SourceVecs[i];
3710 VEXTOffsets[i] = 0;
3711 continue;
3712 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3713 // It probably isn't worth padding out a smaller vector just to
3714 // break it down again in a shuffle.
3715 return SDValue();
3716 }
Bob Wilson11a1dff2011-01-07 21:37:30 +00003717
3718 // Since only 64-bit and 128-bit vectors are legal on ARM and
3719 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003720 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3721 "unexpected vector sizes in ReconstructShuffle");
Bob Wilson11a1dff2011-01-07 21:37:30 +00003722
3723 if (MaxElts[i] - MinElts[i] >= NumElts) {
3724 // Span too large for a VEXT to cope
3725 return SDValue();
3726 }
3727
3728 if (MinElts[i] >= NumElts) {
3729 // The extraction can just take the second half
3730 VEXTOffsets[i] = NumElts;
3731 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SourceVecs[i],
3732 DAG.getIntPtrConstant(NumElts));
3733 } else if (MaxElts[i] < NumElts) {
3734 // The extraction can just take the first half
3735 VEXTOffsets[i] = 0;
3736 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SourceVecs[i],
3737 DAG.getIntPtrConstant(0));
3738 } else {
3739 // An actual VEXT is needed
3740 VEXTOffsets[i] = MinElts[i];
3741 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SourceVecs[i],
3742 DAG.getIntPtrConstant(0));
3743 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SourceVecs[i],
3744 DAG.getIntPtrConstant(NumElts));
3745 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3746 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3747 }
3748 }
3749
3750 SmallVector<int, 8> Mask;
3751
3752 for (unsigned i = 0; i < NumElts; ++i) {
3753 SDValue Entry = Op.getOperand(i);
3754 if (Entry.getOpcode() == ISD::UNDEF) {
3755 Mask.push_back(-1);
3756 continue;
3757 }
3758
3759 SDValue ExtractVec = Entry.getOperand(0);
3760 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i).getOperand(1))->getSExtValue();
3761 if (ExtractVec == SourceVecs[0]) {
3762 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3763 } else {
3764 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3765 }
3766 }
3767
3768 // Final check before we try to produce nonsense...
3769 if (isShuffleMaskLegal(Mask, VT))
3770 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1], &Mask[0]);
3771
3772 return SDValue();
3773}
3774
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003775/// isShuffleMaskLegal - Targets can use this to indicate that they only
3776/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3777/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3778/// are assumed to be legal.
3779bool
3780ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3781 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003782 if (VT.getVectorNumElements() == 4 &&
3783 (VT.is128BitVector() || VT.is64BitVector())) {
3784 unsigned PFIndexes[4];
3785 for (unsigned i = 0; i != 4; ++i) {
3786 if (M[i] < 0)
3787 PFIndexes[i] = 8;
3788 else
3789 PFIndexes[i] = M[i];
3790 }
3791
3792 // Compute the index in the perfect shuffle table.
3793 unsigned PFTableIndex =
3794 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3795 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3796 unsigned Cost = (PFEntry >> 30);
3797
3798 if (Cost <= 4)
3799 return true;
3800 }
3801
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003802 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003803 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003804
Bob Wilson53dd2452010-06-07 23:53:38 +00003805 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3806 return (EltSize >= 32 ||
3807 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003808 isVREVMask(M, VT, 64) ||
3809 isVREVMask(M, VT, 32) ||
3810 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003811 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3812 isVTRNMask(M, VT, WhichResult) ||
3813 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003814 isVZIPMask(M, VT, WhichResult) ||
3815 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3816 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3817 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003818}
3819
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003820/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3821/// the specified operations to build the shuffle.
3822static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3823 SDValue RHS, SelectionDAG &DAG,
3824 DebugLoc dl) {
3825 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3826 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3827 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3828
3829 enum {
3830 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3831 OP_VREV,
3832 OP_VDUP0,
3833 OP_VDUP1,
3834 OP_VDUP2,
3835 OP_VDUP3,
3836 OP_VEXT1,
3837 OP_VEXT2,
3838 OP_VEXT3,
3839 OP_VUZPL, // VUZP, left result
3840 OP_VUZPR, // VUZP, right result
3841 OP_VZIPL, // VZIP, left result
3842 OP_VZIPR, // VZIP, right result
3843 OP_VTRNL, // VTRN, left result
3844 OP_VTRNR // VTRN, right result
3845 };
3846
3847 if (OpNum == OP_COPY) {
3848 if (LHSID == (1*9+2)*9+3) return LHS;
3849 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3850 return RHS;
3851 }
3852
3853 SDValue OpLHS, OpRHS;
3854 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3855 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3856 EVT VT = OpLHS.getValueType();
3857
3858 switch (OpNum) {
3859 default: llvm_unreachable("Unknown shuffle opcode!");
3860 case OP_VREV:
3861 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3862 case OP_VDUP0:
3863 case OP_VDUP1:
3864 case OP_VDUP2:
3865 case OP_VDUP3:
3866 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003867 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003868 case OP_VEXT1:
3869 case OP_VEXT2:
3870 case OP_VEXT3:
3871 return DAG.getNode(ARMISD::VEXT, dl, VT,
3872 OpLHS, OpRHS,
3873 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3874 case OP_VUZPL:
3875 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003876 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003877 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3878 case OP_VZIPL:
3879 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003880 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003881 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3882 case OP_VTRNL:
3883 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003884 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3885 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003886 }
3887}
3888
Bob Wilson5bafff32009-06-22 23:27:02 +00003889static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003890 SDValue V1 = Op.getOperand(0);
3891 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003892 DebugLoc dl = Op.getDebugLoc();
3893 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003894 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003895 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003896
Bob Wilson28865062009-08-13 02:13:04 +00003897 // Convert shuffles that are directly supported on NEON to target-specific
3898 // DAG nodes, instead of keeping them as shuffles and matching them again
3899 // during code selection. This is more efficient and avoids the possibility
3900 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003901 // FIXME: floating-point vectors should be canonicalized to integer vectors
3902 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003903 SVN->getMask(ShuffleMask);
3904
Bob Wilson53dd2452010-06-07 23:53:38 +00003905 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3906 if (EltSize <= 32) {
3907 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3908 int Lane = SVN->getSplatIndex();
3909 // If this is undef splat, generate it via "just" vdup, if possible.
3910 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003911
Bob Wilson53dd2452010-06-07 23:53:38 +00003912 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3913 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3914 }
3915 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3916 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003917 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003918
3919 bool ReverseVEXT;
3920 unsigned Imm;
3921 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3922 if (ReverseVEXT)
3923 std::swap(V1, V2);
3924 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3925 DAG.getConstant(Imm, MVT::i32));
3926 }
3927
3928 if (isVREVMask(ShuffleMask, VT, 64))
3929 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3930 if (isVREVMask(ShuffleMask, VT, 32))
3931 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3932 if (isVREVMask(ShuffleMask, VT, 16))
3933 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3934
3935 // Check for Neon shuffles that modify both input vectors in place.
3936 // If both results are used, i.e., if there are two shuffles with the same
3937 // source operands and with masks corresponding to both results of one of
3938 // these operations, DAG memoization will ensure that a single node is
3939 // used for both shuffles.
3940 unsigned WhichResult;
3941 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3942 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3943 V1, V2).getValue(WhichResult);
3944 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3945 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3946 V1, V2).getValue(WhichResult);
3947 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3948 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3949 V1, V2).getValue(WhichResult);
3950
3951 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3952 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3953 V1, V1).getValue(WhichResult);
3954 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3955 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3956 V1, V1).getValue(WhichResult);
3957 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3958 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3959 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003960 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003961
Bob Wilsonc692cb72009-08-21 20:54:19 +00003962 // If the shuffle is not directly supported and it has 4 elements, use
3963 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003964 unsigned NumElts = VT.getVectorNumElements();
3965 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003966 unsigned PFIndexes[4];
3967 for (unsigned i = 0; i != 4; ++i) {
3968 if (ShuffleMask[i] < 0)
3969 PFIndexes[i] = 8;
3970 else
3971 PFIndexes[i] = ShuffleMask[i];
3972 }
3973
3974 // Compute the index in the perfect shuffle table.
3975 unsigned PFTableIndex =
3976 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003977 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3978 unsigned Cost = (PFEntry >> 30);
3979
3980 if (Cost <= 4)
3981 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3982 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003983
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003984 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003985 if (EltSize >= 32) {
3986 // Do the expansion with floating-point types, since that is what the VFP
3987 // registers are defined to use, and since i64 is not legal.
3988 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3989 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003990 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
3991 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003992 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003993 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003994 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003995 Ops.push_back(DAG.getUNDEF(EltVT));
3996 else
3997 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3998 ShuffleMask[i] < (int)NumElts ? V1 : V2,
3999 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4000 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004001 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004002 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004003 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004004 }
4005
Bob Wilson22cac0d2009-08-14 05:16:33 +00004006 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004007}
4008
Bob Wilson5bafff32009-06-22 23:27:02 +00004009static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004010 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004011 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004012 if (!isa<ConstantSDNode>(Lane))
4013 return SDValue();
4014
4015 SDValue Vec = Op.getOperand(0);
4016 if (Op.getValueType() == MVT::i32 &&
4017 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4018 DebugLoc dl = Op.getDebugLoc();
4019 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4020 }
4021
4022 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004023}
4024
Bob Wilsona6d65862009-08-03 20:36:38 +00004025static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4026 // The only time a CONCAT_VECTORS operation can have legal types is when
4027 // two 64-bit vectors are concatenated to a 128-bit vector.
4028 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4029 "unexpected CONCAT_VECTORS");
4030 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004031 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004032 SDValue Op0 = Op.getOperand(0);
4033 SDValue Op1 = Op.getOperand(1);
4034 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004035 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004036 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004037 DAG.getIntPtrConstant(0));
4038 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004040 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004041 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004042 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004043}
4044
Bob Wilson626613d2010-11-23 19:38:38 +00004045/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4046/// element has been zero/sign-extended, depending on the isSigned parameter,
4047/// from an integer type half its size.
4048static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4049 bool isSigned) {
4050 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4051 EVT VT = N->getValueType(0);
4052 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4053 SDNode *BVN = N->getOperand(0).getNode();
4054 if (BVN->getValueType(0) != MVT::v4i32 ||
4055 BVN->getOpcode() != ISD::BUILD_VECTOR)
4056 return false;
4057 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4058 unsigned HiElt = 1 - LoElt;
4059 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4060 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4061 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4062 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4063 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4064 return false;
4065 if (isSigned) {
4066 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4067 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4068 return true;
4069 } else {
4070 if (Hi0->isNullValue() && Hi1->isNullValue())
4071 return true;
4072 }
4073 return false;
4074 }
4075
4076 if (N->getOpcode() != ISD::BUILD_VECTOR)
4077 return false;
4078
4079 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4080 SDNode *Elt = N->getOperand(i).getNode();
4081 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4082 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4083 unsigned HalfSize = EltSize / 2;
4084 if (isSigned) {
4085 int64_t SExtVal = C->getSExtValue();
4086 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4087 return false;
4088 } else {
4089 if ((C->getZExtValue() >> HalfSize) != 0)
4090 return false;
4091 }
4092 continue;
4093 }
4094 return false;
4095 }
4096
4097 return true;
4098}
4099
4100/// isSignExtended - Check if a node is a vector value that is sign-extended
4101/// or a constant BUILD_VECTOR with sign-extended elements.
4102static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4103 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4104 return true;
4105 if (isExtendedBUILD_VECTOR(N, DAG, true))
4106 return true;
4107 return false;
4108}
4109
4110/// isZeroExtended - Check if a node is a vector value that is zero-extended
4111/// or a constant BUILD_VECTOR with zero-extended elements.
4112static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4113 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4114 return true;
4115 if (isExtendedBUILD_VECTOR(N, DAG, false))
4116 return true;
4117 return false;
4118}
4119
4120/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4121/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004122static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4123 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4124 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004125 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4126 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4127 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4128 LD->isNonTemporal(), LD->getAlignment());
4129 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4130 // have been legalized as a BITCAST from v4i32.
4131 if (N->getOpcode() == ISD::BITCAST) {
4132 SDNode *BVN = N->getOperand(0).getNode();
4133 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4134 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4135 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4136 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4137 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4138 }
4139 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4140 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4141 EVT VT = N->getValueType(0);
4142 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4143 unsigned NumElts = VT.getVectorNumElements();
4144 MVT TruncVT = MVT::getIntegerVT(EltSize);
4145 SmallVector<SDValue, 8> Ops;
4146 for (unsigned i = 0; i != NumElts; ++i) {
4147 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4148 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004149 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004150 }
4151 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4152 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004153}
4154
4155static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4156 // Multiplications are only custom-lowered for 128-bit vectors so that
4157 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4158 EVT VT = Op.getValueType();
4159 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4160 SDNode *N0 = Op.getOperand(0).getNode();
4161 SDNode *N1 = Op.getOperand(1).getNode();
4162 unsigned NewOpc = 0;
Bob Wilson626613d2010-11-23 19:38:38 +00004163 if (isSignExtended(N0, DAG) && isSignExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004164 NewOpc = ARMISD::VMULLs;
Bob Wilson626613d2010-11-23 19:38:38 +00004165 else if (isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004166 NewOpc = ARMISD::VMULLu;
Bob Wilson626613d2010-11-23 19:38:38 +00004167 else if (VT == MVT::v2i64)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004168 // Fall through to expand this. It is not legal.
4169 return SDValue();
Bob Wilson626613d2010-11-23 19:38:38 +00004170 else
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004171 // Other vector multiplications are legal.
4172 return Op;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004173
4174 // Legalize to a VMULL instruction.
4175 DebugLoc DL = Op.getDebugLoc();
4176 SDValue Op0 = SkipExtension(N0, DAG);
4177 SDValue Op1 = SkipExtension(N1, DAG);
4178
4179 assert(Op0.getValueType().is64BitVector() &&
4180 Op1.getValueType().is64BitVector() &&
4181 "unexpected types for extended operands to VMULL");
4182 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4183}
4184
Dan Gohmand858e902010-04-17 15:26:15 +00004185SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004186 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004187 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004188 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004189 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004190 case ISD::GlobalAddress:
4191 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4192 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00004193 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004194 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004195 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4196 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004197 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004198 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004199 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004200 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004201 case ISD::SINT_TO_FP:
4202 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4203 case ISD::FP_TO_SINT:
4204 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004205 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004206 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004207 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004208 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004209 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004210 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004211 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004212 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4213 Subtarget);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004214 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004215 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004216 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004217 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004218 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004219 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004220 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004221 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004222 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004223 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004224 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004225 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004226 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004227 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004228 case ISD::MUL: return LowerMUL(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004229 }
Dan Gohman475871a2008-07-27 21:46:04 +00004230 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004231}
4232
Duncan Sands1607f052008-12-01 11:39:25 +00004233/// ReplaceNodeResults - Replace the results of node with an illegal result
4234/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004235void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4236 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004237 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004238 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004239 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004240 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004241 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004242 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004243 case ISD::BITCAST:
4244 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004245 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004246 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004247 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004248 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004249 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004250 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004251 if (Res.getNode())
4252 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004253}
Chris Lattner27a6c732007-11-24 07:07:01 +00004254
Evan Chenga8e29892007-01-19 07:51:42 +00004255//===----------------------------------------------------------------------===//
4256// ARM Scheduler Hooks
4257//===----------------------------------------------------------------------===//
4258
4259MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004260ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4261 MachineBasicBlock *BB,
4262 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004263 unsigned dest = MI->getOperand(0).getReg();
4264 unsigned ptr = MI->getOperand(1).getReg();
4265 unsigned oldval = MI->getOperand(2).getReg();
4266 unsigned newval = MI->getOperand(3).getReg();
4267 unsigned scratch = BB->getParent()->getRegInfo()
4268 .createVirtualRegister(ARM::GPRRegisterClass);
4269 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4270 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004271 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004272
4273 unsigned ldrOpc, strOpc;
4274 switch (Size) {
4275 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004276 case 1:
4277 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
4278 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
4279 break;
4280 case 2:
4281 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4282 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4283 break;
4284 case 4:
4285 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4286 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4287 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004288 }
4289
4290 MachineFunction *MF = BB->getParent();
4291 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4292 MachineFunction::iterator It = BB;
4293 ++It; // insert the new blocks after the current block
4294
4295 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4296 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4297 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4298 MF->insert(It, loop1MBB);
4299 MF->insert(It, loop2MBB);
4300 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004301
4302 // Transfer the remainder of BB and its successor edges to exitMBB.
4303 exitMBB->splice(exitMBB->begin(), BB,
4304 llvm::next(MachineBasicBlock::iterator(MI)),
4305 BB->end());
4306 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004307
4308 // thisMBB:
4309 // ...
4310 // fallthrough --> loop1MBB
4311 BB->addSuccessor(loop1MBB);
4312
4313 // loop1MBB:
4314 // ldrex dest, [ptr]
4315 // cmp dest, oldval
4316 // bne exitMBB
4317 BB = loop1MBB;
4318 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004319 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004320 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004321 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4322 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004323 BB->addSuccessor(loop2MBB);
4324 BB->addSuccessor(exitMBB);
4325
4326 // loop2MBB:
4327 // strex scratch, newval, [ptr]
4328 // cmp scratch, #0
4329 // bne loop1MBB
4330 BB = loop2MBB;
4331 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4332 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004333 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004334 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004335 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4336 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004337 BB->addSuccessor(loop1MBB);
4338 BB->addSuccessor(exitMBB);
4339
4340 // exitMBB:
4341 // ...
4342 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004343
Dan Gohman14152b42010-07-06 20:24:04 +00004344 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004345
Jim Grosbach5278eb82009-12-11 01:42:04 +00004346 return BB;
4347}
4348
4349MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004350ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4351 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004352 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4353 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4354
4355 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004356 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004357 MachineFunction::iterator It = BB;
4358 ++It;
4359
4360 unsigned dest = MI->getOperand(0).getReg();
4361 unsigned ptr = MI->getOperand(1).getReg();
4362 unsigned incr = MI->getOperand(2).getReg();
4363 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004364
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004365 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004366 unsigned ldrOpc, strOpc;
4367 switch (Size) {
4368 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004369 case 1:
4370 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004371 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004372 break;
4373 case 2:
4374 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4375 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4376 break;
4377 case 4:
4378 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4379 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4380 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004381 }
4382
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004383 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4384 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4385 MF->insert(It, loopMBB);
4386 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004387
4388 // Transfer the remainder of BB and its successor edges to exitMBB.
4389 exitMBB->splice(exitMBB->begin(), BB,
4390 llvm::next(MachineBasicBlock::iterator(MI)),
4391 BB->end());
4392 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004393
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004394 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004395 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4396 unsigned scratch2 = (!BinOpcode) ? incr :
4397 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4398
4399 // thisMBB:
4400 // ...
4401 // fallthrough --> loopMBB
4402 BB->addSuccessor(loopMBB);
4403
4404 // loopMBB:
4405 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004406 // <binop> scratch2, dest, incr
4407 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004408 // cmp scratch, #0
4409 // bne- loopMBB
4410 // fallthrough --> exitMBB
4411 BB = loopMBB;
4412 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004413 if (BinOpcode) {
4414 // operand order needs to go the other way for NAND
4415 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4416 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4417 addReg(incr).addReg(dest)).addReg(0);
4418 else
4419 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4420 addReg(dest).addReg(incr)).addReg(0);
4421 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004422
4423 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4424 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004425 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004426 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004427 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4428 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004429
4430 BB->addSuccessor(loopMBB);
4431 BB->addSuccessor(exitMBB);
4432
4433 // exitMBB:
4434 // ...
4435 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004436
Dan Gohman14152b42010-07-06 20:24:04 +00004437 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004438
Jim Grosbachc3c23542009-12-14 04:22:04 +00004439 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004440}
4441
Evan Cheng218977b2010-07-13 19:27:42 +00004442static
4443MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4444 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4445 E = MBB->succ_end(); I != E; ++I)
4446 if (*I != Succ)
4447 return *I;
4448 llvm_unreachable("Expecting a BB with two successors!");
4449}
4450
Jim Grosbache801dc42009-12-12 01:40:06 +00004451MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004452ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004453 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004454 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004455 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004456 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004457 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004458 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004459 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004460 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004461
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004462 case ARM::ATOMIC_LOAD_ADD_I8:
4463 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4464 case ARM::ATOMIC_LOAD_ADD_I16:
4465 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4466 case ARM::ATOMIC_LOAD_ADD_I32:
4467 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004468
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004469 case ARM::ATOMIC_LOAD_AND_I8:
4470 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4471 case ARM::ATOMIC_LOAD_AND_I16:
4472 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4473 case ARM::ATOMIC_LOAD_AND_I32:
4474 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004475
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004476 case ARM::ATOMIC_LOAD_OR_I8:
4477 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4478 case ARM::ATOMIC_LOAD_OR_I16:
4479 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4480 case ARM::ATOMIC_LOAD_OR_I32:
4481 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004482
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004483 case ARM::ATOMIC_LOAD_XOR_I8:
4484 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4485 case ARM::ATOMIC_LOAD_XOR_I16:
4486 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4487 case ARM::ATOMIC_LOAD_XOR_I32:
4488 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004489
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004490 case ARM::ATOMIC_LOAD_NAND_I8:
4491 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4492 case ARM::ATOMIC_LOAD_NAND_I16:
4493 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4494 case ARM::ATOMIC_LOAD_NAND_I32:
4495 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004496
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004497 case ARM::ATOMIC_LOAD_SUB_I8:
4498 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4499 case ARM::ATOMIC_LOAD_SUB_I16:
4500 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4501 case ARM::ATOMIC_LOAD_SUB_I32:
4502 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004503
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004504 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4505 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4506 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004507
4508 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4509 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4510 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004511
Evan Cheng007ea272009-08-12 05:17:19 +00004512 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004513 // To "insert" a SELECT_CC instruction, we actually have to insert the
4514 // diamond control-flow pattern. The incoming instruction knows the
4515 // destination vreg to set, the condition code register to branch on, the
4516 // true/false values to select between, and a branch opcode to use.
4517 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004518 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004519 ++It;
4520
4521 // thisMBB:
4522 // ...
4523 // TrueVal = ...
4524 // cmpTY ccX, r1, r2
4525 // bCC copy1MBB
4526 // fallthrough --> copy0MBB
4527 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004528 MachineFunction *F = BB->getParent();
4529 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4530 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004531 F->insert(It, copy0MBB);
4532 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004533
4534 // Transfer the remainder of BB and its successor edges to sinkMBB.
4535 sinkMBB->splice(sinkMBB->begin(), BB,
4536 llvm::next(MachineBasicBlock::iterator(MI)),
4537 BB->end());
4538 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4539
Dan Gohman258c58c2010-07-06 15:49:48 +00004540 BB->addSuccessor(copy0MBB);
4541 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004542
Dan Gohman14152b42010-07-06 20:24:04 +00004543 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4544 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4545
Evan Chenga8e29892007-01-19 07:51:42 +00004546 // copy0MBB:
4547 // %FalseValue = ...
4548 // # fallthrough to sinkMBB
4549 BB = copy0MBB;
4550
4551 // Update machine-CFG edges
4552 BB->addSuccessor(sinkMBB);
4553
4554 // sinkMBB:
4555 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4556 // ...
4557 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004558 BuildMI(*BB, BB->begin(), dl,
4559 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004560 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4561 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4562
Dan Gohman14152b42010-07-06 20:24:04 +00004563 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004564 return BB;
4565 }
Evan Cheng86198642009-08-07 00:34:42 +00004566
Evan Cheng218977b2010-07-13 19:27:42 +00004567 case ARM::BCCi64:
4568 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00004569 // If there is an unconditional branch to the other successor, remove it.
4570 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
4571
Evan Cheng218977b2010-07-13 19:27:42 +00004572 // Compare both parts that make up the double comparison separately for
4573 // equality.
4574 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4575
4576 unsigned LHS1 = MI->getOperand(1).getReg();
4577 unsigned LHS2 = MI->getOperand(2).getReg();
4578 if (RHSisZero) {
4579 AddDefaultPred(BuildMI(BB, dl,
4580 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4581 .addReg(LHS1).addImm(0));
4582 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4583 .addReg(LHS2).addImm(0)
4584 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4585 } else {
4586 unsigned RHS1 = MI->getOperand(3).getReg();
4587 unsigned RHS2 = MI->getOperand(4).getReg();
4588 AddDefaultPred(BuildMI(BB, dl,
4589 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4590 .addReg(LHS1).addReg(RHS1));
4591 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4592 .addReg(LHS2).addReg(RHS2)
4593 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4594 }
4595
4596 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4597 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4598 if (MI->getOperand(0).getImm() == ARMCC::NE)
4599 std::swap(destMBB, exitMBB);
4600
4601 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4602 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4603 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4604 .addMBB(exitMBB);
4605
4606 MI->eraseFromParent(); // The pseudo instruction is gone now.
4607 return BB;
4608 }
Evan Chenga8e29892007-01-19 07:51:42 +00004609 }
4610}
4611
4612//===----------------------------------------------------------------------===//
4613// ARM Optimization Hooks
4614//===----------------------------------------------------------------------===//
4615
Chris Lattnerd1980a52009-03-12 06:52:53 +00004616static
4617SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4618 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004619 SelectionDAG &DAG = DCI.DAG;
4620 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004621 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004622 unsigned Opc = N->getOpcode();
4623 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4624 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4625 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4626 ISD::CondCode CC = ISD::SETCC_INVALID;
4627
4628 if (isSlctCC) {
4629 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4630 } else {
4631 SDValue CCOp = Slct.getOperand(0);
4632 if (CCOp.getOpcode() == ISD::SETCC)
4633 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4634 }
4635
4636 bool DoXform = false;
4637 bool InvCC = false;
4638 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4639 "Bad input!");
4640
4641 if (LHS.getOpcode() == ISD::Constant &&
4642 cast<ConstantSDNode>(LHS)->isNullValue()) {
4643 DoXform = true;
4644 } else if (CC != ISD::SETCC_INVALID &&
4645 RHS.getOpcode() == ISD::Constant &&
4646 cast<ConstantSDNode>(RHS)->isNullValue()) {
4647 std::swap(LHS, RHS);
4648 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004649 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004650 Op0.getOperand(0).getValueType();
4651 bool isInt = OpVT.isInteger();
4652 CC = ISD::getSetCCInverse(CC, isInt);
4653
4654 if (!TLI.isCondCodeLegal(CC, OpVT))
4655 return SDValue(); // Inverse operator isn't legal.
4656
4657 DoXform = true;
4658 InvCC = true;
4659 }
4660
4661 if (DoXform) {
4662 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4663 if (isSlctCC)
4664 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4665 Slct.getOperand(0), Slct.getOperand(1), CC);
4666 SDValue CCOp = Slct.getOperand(0);
4667 if (InvCC)
4668 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4669 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4670 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4671 CCOp, OtherOp, Result);
4672 }
4673 return SDValue();
4674}
4675
Bob Wilson3d5792a2010-07-29 20:34:14 +00004676/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4677/// operands N0 and N1. This is a helper for PerformADDCombine that is
4678/// called with the default operands, and if that fails, with commuted
4679/// operands.
4680static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4681 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004682 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4683 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4684 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4685 if (Result.getNode()) return Result;
4686 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00004687 return SDValue();
4688}
4689
Bob Wilson3d5792a2010-07-29 20:34:14 +00004690/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4691///
4692static SDValue PerformADDCombine(SDNode *N,
4693 TargetLowering::DAGCombinerInfo &DCI) {
4694 SDValue N0 = N->getOperand(0);
4695 SDValue N1 = N->getOperand(1);
4696
4697 // First try with the default operand order.
4698 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4699 if (Result.getNode())
4700 return Result;
4701
4702 // If that didn't work, try again with the operands commuted.
4703 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4704}
4705
Chris Lattnerd1980a52009-03-12 06:52:53 +00004706/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004707///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004708static SDValue PerformSUBCombine(SDNode *N,
4709 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004710 SDValue N0 = N->getOperand(0);
4711 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004712
Chris Lattnerd1980a52009-03-12 06:52:53 +00004713 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4714 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4715 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4716 if (Result.getNode()) return Result;
4717 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004718
Chris Lattnerd1980a52009-03-12 06:52:53 +00004719 return SDValue();
4720}
4721
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004722static SDValue PerformMULCombine(SDNode *N,
4723 TargetLowering::DAGCombinerInfo &DCI,
4724 const ARMSubtarget *Subtarget) {
4725 SelectionDAG &DAG = DCI.DAG;
4726
4727 if (Subtarget->isThumb1Only())
4728 return SDValue();
4729
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004730 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4731 return SDValue();
4732
4733 EVT VT = N->getValueType(0);
4734 if (VT != MVT::i32)
4735 return SDValue();
4736
4737 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4738 if (!C)
4739 return SDValue();
4740
4741 uint64_t MulAmt = C->getZExtValue();
4742 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4743 ShiftAmt = ShiftAmt & (32 - 1);
4744 SDValue V = N->getOperand(0);
4745 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004746
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004747 SDValue Res;
4748 MulAmt >>= ShiftAmt;
4749 if (isPowerOf2_32(MulAmt - 1)) {
4750 // (mul x, 2^N + 1) => (add (shl x, N), x)
4751 Res = DAG.getNode(ISD::ADD, DL, VT,
4752 V, DAG.getNode(ISD::SHL, DL, VT,
4753 V, DAG.getConstant(Log2_32(MulAmt-1),
4754 MVT::i32)));
4755 } else if (isPowerOf2_32(MulAmt + 1)) {
4756 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4757 Res = DAG.getNode(ISD::SUB, DL, VT,
4758 DAG.getNode(ISD::SHL, DL, VT,
4759 V, DAG.getConstant(Log2_32(MulAmt+1),
4760 MVT::i32)),
4761 V);
4762 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004763 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004764
4765 if (ShiftAmt != 0)
4766 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4767 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004768
4769 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004770 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004771 return SDValue();
4772}
4773
Owen Anderson080c0922010-11-05 19:27:46 +00004774static SDValue PerformANDCombine(SDNode *N,
4775 TargetLowering::DAGCombinerInfo &DCI) {
4776 // Attempt to use immediate-form VBIC
4777 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4778 DebugLoc dl = N->getDebugLoc();
4779 EVT VT = N->getValueType(0);
4780 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004781
Owen Anderson080c0922010-11-05 19:27:46 +00004782 APInt SplatBits, SplatUndef;
4783 unsigned SplatBitSize;
4784 bool HasAnyUndefs;
4785 if (BVN &&
4786 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4787 if (SplatBitSize <= 64) {
4788 EVT VbicVT;
4789 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
4790 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004791 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004792 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00004793 if (Val.getNode()) {
4794 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004795 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00004796 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004797 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00004798 }
4799 }
4800 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004801
Owen Anderson080c0922010-11-05 19:27:46 +00004802 return SDValue();
4803}
4804
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004805/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4806static SDValue PerformORCombine(SDNode *N,
4807 TargetLowering::DAGCombinerInfo &DCI,
4808 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00004809 // Attempt to use immediate-form VORR
4810 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4811 DebugLoc dl = N->getDebugLoc();
4812 EVT VT = N->getValueType(0);
4813 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004814
Owen Anderson60f48702010-11-03 23:15:26 +00004815 APInt SplatBits, SplatUndef;
4816 unsigned SplatBitSize;
4817 bool HasAnyUndefs;
4818 if (BVN && Subtarget->hasNEON() &&
4819 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4820 if (SplatBitSize <= 64) {
4821 EVT VorrVT;
4822 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
4823 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004824 DAG, VorrVT, VT.is128BitVector(),
4825 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00004826 if (Val.getNode()) {
4827 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004828 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00004829 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004830 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00004831 }
4832 }
4833 }
4834
Jim Grosbach54238562010-07-17 03:30:54 +00004835 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4836 // reasonable.
4837
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004838 // BFI is only available on V6T2+
4839 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4840 return SDValue();
4841
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004842 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004843 DebugLoc DL = N->getDebugLoc();
4844 // 1) or (and A, mask), val => ARMbfi A, val, mask
4845 // iff (val & mask) == val
4846 //
4847 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4848 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4849 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4850 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4851 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4852 // (i.e., copy a bitfield value into another bitfield of the same width)
4853 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004854 return SDValue();
4855
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004856 if (VT != MVT::i32)
4857 return SDValue();
4858
Evan Cheng30fb13f2010-12-13 20:32:54 +00004859 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00004860
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004861 // The value and the mask need to be constants so we can verify this is
4862 // actually a bitfield set. If the mask is 0xffff, we can do better
4863 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00004864 SDValue MaskOp = N0.getOperand(1);
4865 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
4866 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004867 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004868 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004869 if (Mask == 0xffff)
4870 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004871 SDValue Res;
4872 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004873 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4874 if (N1C) {
4875 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004876 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00004877 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004878
Evan Chenga9688c42010-12-11 04:11:38 +00004879 if (ARM::isBitFieldInvertedMask(Mask)) {
4880 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004881
Evan Cheng30fb13f2010-12-13 20:32:54 +00004882 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00004883 DAG.getConstant(Val, MVT::i32),
4884 DAG.getConstant(Mask, MVT::i32));
4885
4886 // Do not add new nodes to DAG combiner worklist.
4887 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004888 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004889 }
Jim Grosbach54238562010-07-17 03:30:54 +00004890 } else if (N1.getOpcode() == ISD::AND) {
4891 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004892 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4893 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00004894 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004895 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004896
4897 if (ARM::isBitFieldInvertedMask(Mask) &&
4898 ARM::isBitFieldInvertedMask(~Mask2) &&
4899 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4900 // The pack halfword instruction works better for masks that fit it,
4901 // so use that when it's available.
4902 if (Subtarget->hasT2ExtractPack() &&
4903 (Mask == 0xffff || Mask == 0xffff0000))
4904 return SDValue();
4905 // 2a
4906 unsigned lsb = CountTrailingZeros_32(Mask2);
4907 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4908 DAG.getConstant(lsb, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00004909 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00004910 DAG.getConstant(Mask, MVT::i32));
4911 // Do not add new nodes to DAG combiner worklist.
4912 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004913 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004914 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4915 ARM::isBitFieldInvertedMask(Mask2) &&
4916 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4917 // The pack halfword instruction works better for masks that fit it,
4918 // so use that when it's available.
4919 if (Subtarget->hasT2ExtractPack() &&
4920 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4921 return SDValue();
4922 // 2b
4923 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004924 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00004925 DAG.getConstant(lsb, MVT::i32));
4926 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4927 DAG.getConstant(Mask2, MVT::i32));
4928 // Do not add new nodes to DAG combiner worklist.
4929 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004930 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004931 }
4932 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004933
Evan Cheng30fb13f2010-12-13 20:32:54 +00004934 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
4935 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
4936 ARM::isBitFieldInvertedMask(~Mask)) {
4937 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
4938 // where lsb(mask) == #shamt and masked bits of B are known zero.
4939 SDValue ShAmt = N00.getOperand(1);
4940 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
4941 unsigned LSB = CountTrailingZeros_32(Mask);
4942 if (ShAmtC != LSB)
4943 return SDValue();
4944
4945 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
4946 DAG.getConstant(~Mask, MVT::i32));
4947
4948 // Do not add new nodes to DAG combiner worklist.
4949 DCI.CombineTo(N, Res, false);
4950 }
4951
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004952 return SDValue();
4953}
4954
Evan Cheng0c1aec12010-12-14 03:22:07 +00004955/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
4956/// C1 & C2 == C1.
4957static SDValue PerformBFICombine(SDNode *N,
4958 TargetLowering::DAGCombinerInfo &DCI) {
4959 SDValue N1 = N->getOperand(1);
4960 if (N1.getOpcode() == ISD::AND) {
4961 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4962 if (!N11C)
4963 return SDValue();
4964 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
4965 unsigned Mask2 = N11C->getZExtValue();
4966 if ((Mask & Mask2) == Mask2)
4967 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
4968 N->getOperand(0), N1.getOperand(0),
4969 N->getOperand(2));
4970 }
4971 return SDValue();
4972}
4973
Bob Wilson0b8ccb82010-09-22 22:09:21 +00004974/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4975/// ARMISD::VMOVRRD.
4976static SDValue PerformVMOVRRDCombine(SDNode *N,
4977 TargetLowering::DAGCombinerInfo &DCI) {
4978 // vmovrrd(vmovdrr x, y) -> x,y
4979 SDValue InDouble = N->getOperand(0);
4980 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
4981 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
4982 return SDValue();
4983}
4984
4985/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
4986/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
4987static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
4988 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
4989 SDValue Op0 = N->getOperand(0);
4990 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004991 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00004992 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004993 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00004994 Op1 = Op1.getOperand(0);
4995 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
4996 Op0.getNode() == Op1.getNode() &&
4997 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004998 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00004999 N->getValueType(0), Op0.getOperand(0));
5000 return SDValue();
5001}
5002
Bob Wilson31600902010-12-21 06:43:19 +00005003/// PerformSTORECombine - Target-specific dag combine xforms for
5004/// ISD::STORE.
5005static SDValue PerformSTORECombine(SDNode *N,
5006 TargetLowering::DAGCombinerInfo &DCI) {
5007 // Bitcast an i64 store extracted from a vector to f64.
5008 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5009 StoreSDNode *St = cast<StoreSDNode>(N);
5010 SDValue StVal = St->getValue();
5011 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5012 StVal.getValueType() != MVT::i64 ||
5013 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5014 return SDValue();
5015
5016 SelectionDAG &DAG = DCI.DAG;
5017 DebugLoc dl = StVal.getDebugLoc();
5018 SDValue IntVec = StVal.getOperand(0);
5019 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5020 IntVec.getValueType().getVectorNumElements());
5021 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5022 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5023 Vec, StVal.getOperand(1));
5024 dl = N->getDebugLoc();
5025 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5026 // Make the DAGCombiner fold the bitcasts.
5027 DCI.AddToWorklist(Vec.getNode());
5028 DCI.AddToWorklist(ExtElt.getNode());
5029 DCI.AddToWorklist(V.getNode());
5030 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5031 St->getPointerInfo(), St->isVolatile(),
5032 St->isNonTemporal(), St->getAlignment(),
5033 St->getTBAAInfo());
5034}
5035
5036/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5037/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5038/// i64 vector to have f64 elements, since the value can then be loaded
5039/// directly into a VFP register.
5040static bool hasNormalLoadOperand(SDNode *N) {
5041 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5042 for (unsigned i = 0; i < NumElts; ++i) {
5043 SDNode *Elt = N->getOperand(i).getNode();
5044 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5045 return true;
5046 }
5047 return false;
5048}
5049
Bob Wilson75f02882010-09-17 22:59:05 +00005050/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5051/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005052static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5053 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005054 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5055 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5056 // into a pair of GPRs, which is fine when the value is used as a scalar,
5057 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005058 SelectionDAG &DAG = DCI.DAG;
5059 if (N->getNumOperands() == 2) {
5060 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5061 if (RV.getNode())
5062 return RV;
5063 }
Bob Wilson75f02882010-09-17 22:59:05 +00005064
Bob Wilson31600902010-12-21 06:43:19 +00005065 // Load i64 elements as f64 values so that type legalization does not split
5066 // them up into i32 values.
5067 EVT VT = N->getValueType(0);
5068 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5069 return SDValue();
5070 DebugLoc dl = N->getDebugLoc();
5071 SmallVector<SDValue, 8> Ops;
5072 unsigned NumElts = VT.getVectorNumElements();
5073 for (unsigned i = 0; i < NumElts; ++i) {
5074 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5075 Ops.push_back(V);
5076 // Make the DAGCombiner fold the bitcast.
5077 DCI.AddToWorklist(V.getNode());
5078 }
5079 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5080 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5081 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5082}
5083
5084/// PerformInsertEltCombine - Target-specific dag combine xforms for
5085/// ISD::INSERT_VECTOR_ELT.
5086static SDValue PerformInsertEltCombine(SDNode *N,
5087 TargetLowering::DAGCombinerInfo &DCI) {
5088 // Bitcast an i64 load inserted into a vector to f64.
5089 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5090 EVT VT = N->getValueType(0);
5091 SDNode *Elt = N->getOperand(1).getNode();
5092 if (VT.getVectorElementType() != MVT::i64 ||
5093 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5094 return SDValue();
5095
5096 SelectionDAG &DAG = DCI.DAG;
5097 DebugLoc dl = N->getDebugLoc();
5098 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5099 VT.getVectorNumElements());
5100 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5101 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5102 // Make the DAGCombiner fold the bitcasts.
5103 DCI.AddToWorklist(Vec.getNode());
5104 DCI.AddToWorklist(V.getNode());
5105 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5106 Vec, V, N->getOperand(2));
5107 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005108}
5109
Bob Wilsonf20700c2010-10-27 20:38:28 +00005110/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5111/// ISD::VECTOR_SHUFFLE.
5112static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5113 // The LLVM shufflevector instruction does not require the shuffle mask
5114 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5115 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5116 // operands do not match the mask length, they are extended by concatenating
5117 // them with undef vectors. That is probably the right thing for other
5118 // targets, but for NEON it is better to concatenate two double-register
5119 // size vector operands into a single quad-register size vector. Do that
5120 // transformation here:
5121 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5122 // shuffle(concat(v1, v2), undef)
5123 SDValue Op0 = N->getOperand(0);
5124 SDValue Op1 = N->getOperand(1);
5125 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5126 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5127 Op0.getNumOperands() != 2 ||
5128 Op1.getNumOperands() != 2)
5129 return SDValue();
5130 SDValue Concat0Op1 = Op0.getOperand(1);
5131 SDValue Concat1Op1 = Op1.getOperand(1);
5132 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5133 Concat1Op1.getOpcode() != ISD::UNDEF)
5134 return SDValue();
5135 // Skip the transformation if any of the types are illegal.
5136 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5137 EVT VT = N->getValueType(0);
5138 if (!TLI.isTypeLegal(VT) ||
5139 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5140 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5141 return SDValue();
5142
5143 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5144 Op0.getOperand(0), Op1.getOperand(0));
5145 // Translate the shuffle mask.
5146 SmallVector<int, 16> NewMask;
5147 unsigned NumElts = VT.getVectorNumElements();
5148 unsigned HalfElts = NumElts/2;
5149 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5150 for (unsigned n = 0; n < NumElts; ++n) {
5151 int MaskElt = SVN->getMaskElt(n);
5152 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005153 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005154 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005155 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005156 NewElt = HalfElts + MaskElt - NumElts;
5157 NewMask.push_back(NewElt);
5158 }
5159 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5160 DAG.getUNDEF(VT), NewMask.data());
5161}
5162
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005163/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5164/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5165/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5166/// return true.
5167static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5168 SelectionDAG &DAG = DCI.DAG;
5169 EVT VT = N->getValueType(0);
5170 // vldN-dup instructions only support 64-bit vectors for N > 1.
5171 if (!VT.is64BitVector())
5172 return false;
5173
5174 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5175 SDNode *VLD = N->getOperand(0).getNode();
5176 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5177 return false;
5178 unsigned NumVecs = 0;
5179 unsigned NewOpc = 0;
5180 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5181 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5182 NumVecs = 2;
5183 NewOpc = ARMISD::VLD2DUP;
5184 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5185 NumVecs = 3;
5186 NewOpc = ARMISD::VLD3DUP;
5187 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5188 NumVecs = 4;
5189 NewOpc = ARMISD::VLD4DUP;
5190 } else {
5191 return false;
5192 }
5193
5194 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5195 // numbers match the load.
5196 unsigned VLDLaneNo =
5197 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5198 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5199 UI != UE; ++UI) {
5200 // Ignore uses of the chain result.
5201 if (UI.getUse().getResNo() == NumVecs)
5202 continue;
5203 SDNode *User = *UI;
5204 if (User->getOpcode() != ARMISD::VDUPLANE ||
5205 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5206 return false;
5207 }
5208
5209 // Create the vldN-dup node.
5210 EVT Tys[5];
5211 unsigned n;
5212 for (n = 0; n < NumVecs; ++n)
5213 Tys[n] = VT;
5214 Tys[n] = MVT::Other;
5215 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5216 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5217 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5218 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5219 Ops, 2, VLDMemInt->getMemoryVT(),
5220 VLDMemInt->getMemOperand());
5221
5222 // Update the uses.
5223 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5224 UI != UE; ++UI) {
5225 unsigned ResNo = UI.getUse().getResNo();
5226 // Ignore uses of the chain result.
5227 if (ResNo == NumVecs)
5228 continue;
5229 SDNode *User = *UI;
5230 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5231 }
5232
5233 // Now the vldN-lane intrinsic is dead except for its chain result.
5234 // Update uses of the chain.
5235 std::vector<SDValue> VLDDupResults;
5236 for (unsigned n = 0; n < NumVecs; ++n)
5237 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5238 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5239 DCI.CombineTo(VLD, VLDDupResults);
5240
5241 return true;
5242}
5243
Bob Wilson9e82bf12010-07-14 01:22:12 +00005244/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5245/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005246static SDValue PerformVDUPLANECombine(SDNode *N,
5247 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005248 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005249
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005250 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5251 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5252 if (CombineVLDDUP(N, DCI))
5253 return SDValue(N, 0);
5254
5255 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5256 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005257 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005258 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005259 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005260 return SDValue();
5261
5262 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5263 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5264 // The canonical VMOV for a zero vector uses a 32-bit element size.
5265 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5266 unsigned EltBits;
5267 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5268 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005269 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005270 if (EltSize > VT.getVectorElementType().getSizeInBits())
5271 return SDValue();
5272
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005273 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005274}
5275
Bob Wilson5bafff32009-06-22 23:27:02 +00005276/// getVShiftImm - Check if this is a valid build_vector for the immediate
5277/// operand of a vector shift operation, where all the elements of the
5278/// build_vector must have the same constant integer value.
5279static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5280 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005281 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005282 Op = Op.getOperand(0);
5283 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5284 APInt SplatBits, SplatUndef;
5285 unsigned SplatBitSize;
5286 bool HasAnyUndefs;
5287 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5288 HasAnyUndefs, ElementBits) ||
5289 SplatBitSize > ElementBits)
5290 return false;
5291 Cnt = SplatBits.getSExtValue();
5292 return true;
5293}
5294
5295/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5296/// operand of a vector shift left operation. That value must be in the range:
5297/// 0 <= Value < ElementBits for a left shift; or
5298/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005299static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005300 assert(VT.isVector() && "vector shift count is not a vector type");
5301 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5302 if (! getVShiftImm(Op, ElementBits, Cnt))
5303 return false;
5304 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5305}
5306
5307/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5308/// operand of a vector shift right operation. For a shift opcode, the value
5309/// is positive, but for an intrinsic the value count must be negative. The
5310/// absolute value must be in the range:
5311/// 1 <= |Value| <= ElementBits for a right shift; or
5312/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005313static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005314 int64_t &Cnt) {
5315 assert(VT.isVector() && "vector shift count is not a vector type");
5316 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5317 if (! getVShiftImm(Op, ElementBits, Cnt))
5318 return false;
5319 if (isIntrinsic)
5320 Cnt = -Cnt;
5321 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5322}
5323
5324/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5325static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5326 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5327 switch (IntNo) {
5328 default:
5329 // Don't do anything for most intrinsics.
5330 break;
5331
5332 // Vector shifts: check for immediate versions and lower them.
5333 // Note: This is done during DAG combining instead of DAG legalizing because
5334 // the build_vectors for 64-bit vector element shift counts are generally
5335 // not legal, and it is hard to see their values after they get legalized to
5336 // loads from a constant pool.
5337 case Intrinsic::arm_neon_vshifts:
5338 case Intrinsic::arm_neon_vshiftu:
5339 case Intrinsic::arm_neon_vshiftls:
5340 case Intrinsic::arm_neon_vshiftlu:
5341 case Intrinsic::arm_neon_vshiftn:
5342 case Intrinsic::arm_neon_vrshifts:
5343 case Intrinsic::arm_neon_vrshiftu:
5344 case Intrinsic::arm_neon_vrshiftn:
5345 case Intrinsic::arm_neon_vqshifts:
5346 case Intrinsic::arm_neon_vqshiftu:
5347 case Intrinsic::arm_neon_vqshiftsu:
5348 case Intrinsic::arm_neon_vqshiftns:
5349 case Intrinsic::arm_neon_vqshiftnu:
5350 case Intrinsic::arm_neon_vqshiftnsu:
5351 case Intrinsic::arm_neon_vqrshiftns:
5352 case Intrinsic::arm_neon_vqrshiftnu:
5353 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00005354 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005355 int64_t Cnt;
5356 unsigned VShiftOpc = 0;
5357
5358 switch (IntNo) {
5359 case Intrinsic::arm_neon_vshifts:
5360 case Intrinsic::arm_neon_vshiftu:
5361 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
5362 VShiftOpc = ARMISD::VSHL;
5363 break;
5364 }
5365 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
5366 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
5367 ARMISD::VSHRs : ARMISD::VSHRu);
5368 break;
5369 }
5370 return SDValue();
5371
5372 case Intrinsic::arm_neon_vshiftls:
5373 case Intrinsic::arm_neon_vshiftlu:
5374 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
5375 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005376 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005377
5378 case Intrinsic::arm_neon_vrshifts:
5379 case Intrinsic::arm_neon_vrshiftu:
5380 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
5381 break;
5382 return SDValue();
5383
5384 case Intrinsic::arm_neon_vqshifts:
5385 case Intrinsic::arm_neon_vqshiftu:
5386 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5387 break;
5388 return SDValue();
5389
5390 case Intrinsic::arm_neon_vqshiftsu:
5391 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5392 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005393 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005394
5395 case Intrinsic::arm_neon_vshiftn:
5396 case Intrinsic::arm_neon_vrshiftn:
5397 case Intrinsic::arm_neon_vqshiftns:
5398 case Intrinsic::arm_neon_vqshiftnu:
5399 case Intrinsic::arm_neon_vqshiftnsu:
5400 case Intrinsic::arm_neon_vqrshiftns:
5401 case Intrinsic::arm_neon_vqrshiftnu:
5402 case Intrinsic::arm_neon_vqrshiftnsu:
5403 // Narrowing shifts require an immediate right shift.
5404 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
5405 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00005406 llvm_unreachable("invalid shift count for narrowing vector shift "
5407 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005408
5409 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005410 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00005411 }
5412
5413 switch (IntNo) {
5414 case Intrinsic::arm_neon_vshifts:
5415 case Intrinsic::arm_neon_vshiftu:
5416 // Opcode already set above.
5417 break;
5418 case Intrinsic::arm_neon_vshiftls:
5419 case Intrinsic::arm_neon_vshiftlu:
5420 if (Cnt == VT.getVectorElementType().getSizeInBits())
5421 VShiftOpc = ARMISD::VSHLLi;
5422 else
5423 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
5424 ARMISD::VSHLLs : ARMISD::VSHLLu);
5425 break;
5426 case Intrinsic::arm_neon_vshiftn:
5427 VShiftOpc = ARMISD::VSHRN; break;
5428 case Intrinsic::arm_neon_vrshifts:
5429 VShiftOpc = ARMISD::VRSHRs; break;
5430 case Intrinsic::arm_neon_vrshiftu:
5431 VShiftOpc = ARMISD::VRSHRu; break;
5432 case Intrinsic::arm_neon_vrshiftn:
5433 VShiftOpc = ARMISD::VRSHRN; break;
5434 case Intrinsic::arm_neon_vqshifts:
5435 VShiftOpc = ARMISD::VQSHLs; break;
5436 case Intrinsic::arm_neon_vqshiftu:
5437 VShiftOpc = ARMISD::VQSHLu; break;
5438 case Intrinsic::arm_neon_vqshiftsu:
5439 VShiftOpc = ARMISD::VQSHLsu; break;
5440 case Intrinsic::arm_neon_vqshiftns:
5441 VShiftOpc = ARMISD::VQSHRNs; break;
5442 case Intrinsic::arm_neon_vqshiftnu:
5443 VShiftOpc = ARMISD::VQSHRNu; break;
5444 case Intrinsic::arm_neon_vqshiftnsu:
5445 VShiftOpc = ARMISD::VQSHRNsu; break;
5446 case Intrinsic::arm_neon_vqrshiftns:
5447 VShiftOpc = ARMISD::VQRSHRNs; break;
5448 case Intrinsic::arm_neon_vqrshiftnu:
5449 VShiftOpc = ARMISD::VQRSHRNu; break;
5450 case Intrinsic::arm_neon_vqrshiftnsu:
5451 VShiftOpc = ARMISD::VQRSHRNsu; break;
5452 }
5453
5454 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005455 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005456 }
5457
5458 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00005459 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005460 int64_t Cnt;
5461 unsigned VShiftOpc = 0;
5462
5463 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
5464 VShiftOpc = ARMISD::VSLI;
5465 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
5466 VShiftOpc = ARMISD::VSRI;
5467 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005468 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005469 }
5470
5471 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
5472 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00005473 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005474 }
5475
5476 case Intrinsic::arm_neon_vqrshifts:
5477 case Intrinsic::arm_neon_vqrshiftu:
5478 // No immediate versions of these to check for.
5479 break;
5480 }
5481
5482 return SDValue();
5483}
5484
5485/// PerformShiftCombine - Checks for immediate versions of vector shifts and
5486/// lowers them. As with the vector shift intrinsics, this is done during DAG
5487/// combining instead of DAG legalizing because the build_vectors for 64-bit
5488/// vector element shift counts are generally not legal, and it is hard to see
5489/// their values after they get legalized to loads from a constant pool.
5490static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
5491 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00005492 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00005493
5494 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00005495 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5496 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00005497 return SDValue();
5498
5499 assert(ST->hasNEON() && "unexpected vector shift");
5500 int64_t Cnt;
5501
5502 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005503 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005504
5505 case ISD::SHL:
5506 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
5507 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005508 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005509 break;
5510
5511 case ISD::SRA:
5512 case ISD::SRL:
5513 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
5514 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
5515 ARMISD::VSHRs : ARMISD::VSHRu);
5516 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005517 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005518 }
5519 }
5520 return SDValue();
5521}
5522
5523/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
5524/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
5525static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
5526 const ARMSubtarget *ST) {
5527 SDValue N0 = N->getOperand(0);
5528
5529 // Check for sign- and zero-extensions of vector extract operations of 8-
5530 // and 16-bit vector elements. NEON supports these directly. They are
5531 // handled during DAG combining because type legalization will promote them
5532 // to 32-bit types and it is messy to recognize the operations after that.
5533 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5534 SDValue Vec = N0.getOperand(0);
5535 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005536 EVT VT = N->getValueType(0);
5537 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005538 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5539
Owen Anderson825b72b2009-08-11 20:47:22 +00005540 if (VT == MVT::i32 &&
5541 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00005542 TLI.isTypeLegal(Vec.getValueType()) &&
5543 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005544
5545 unsigned Opc = 0;
5546 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005547 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005548 case ISD::SIGN_EXTEND:
5549 Opc = ARMISD::VGETLANEs;
5550 break;
5551 case ISD::ZERO_EXTEND:
5552 case ISD::ANY_EXTEND:
5553 Opc = ARMISD::VGETLANEu;
5554 break;
5555 }
5556 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
5557 }
5558 }
5559
5560 return SDValue();
5561}
5562
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005563/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
5564/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
5565static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
5566 const ARMSubtarget *ST) {
5567 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00005568 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005569 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
5570 // a NaN; only do the transformation when it matches that behavior.
5571
5572 // For now only do this when using NEON for FP operations; if using VFP, it
5573 // is not obvious that the benefit outweighs the cost of switching to the
5574 // NEON pipeline.
5575 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
5576 N->getValueType(0) != MVT::f32)
5577 return SDValue();
5578
5579 SDValue CondLHS = N->getOperand(0);
5580 SDValue CondRHS = N->getOperand(1);
5581 SDValue LHS = N->getOperand(2);
5582 SDValue RHS = N->getOperand(3);
5583 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
5584
5585 unsigned Opcode = 0;
5586 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00005587 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005588 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00005589 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005590 IsReversed = true ; // x CC y ? y : x
5591 } else {
5592 return SDValue();
5593 }
5594
Bob Wilsone742bb52010-02-24 22:15:53 +00005595 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005596 switch (CC) {
5597 default: break;
5598 case ISD::SETOLT:
5599 case ISD::SETOLE:
5600 case ISD::SETLT:
5601 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005602 case ISD::SETULT:
5603 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005604 // If LHS is NaN, an ordered comparison will be false and the result will
5605 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
5606 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5607 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
5608 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5609 break;
5610 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
5611 // will return -0, so vmin can only be used for unsafe math or if one of
5612 // the operands is known to be nonzero.
5613 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
5614 !UnsafeFPMath &&
5615 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5616 break;
5617 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005618 break;
5619
5620 case ISD::SETOGT:
5621 case ISD::SETOGE:
5622 case ISD::SETGT:
5623 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005624 case ISD::SETUGT:
5625 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005626 // If LHS is NaN, an ordered comparison will be false and the result will
5627 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
5628 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5629 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
5630 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5631 break;
5632 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
5633 // will return +0, so vmax can only be used for unsafe math or if one of
5634 // the operands is known to be nonzero.
5635 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
5636 !UnsafeFPMath &&
5637 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5638 break;
5639 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005640 break;
5641 }
5642
5643 if (!Opcode)
5644 return SDValue();
5645 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
5646}
5647
Dan Gohman475871a2008-07-27 21:46:04 +00005648SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005649 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005650 switch (N->getOpcode()) {
5651 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005652 case ISD::ADD: return PerformADDCombine(N, DCI);
5653 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005654 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005655 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00005656 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00005657 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00005658 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005659 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00005660 case ISD::STORE: return PerformSTORECombine(N, DCI);
5661 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
5662 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00005663 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005664 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005665 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005666 case ISD::SHL:
5667 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005668 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005669 case ISD::SIGN_EXTEND:
5670 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005671 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
5672 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005673 }
Dan Gohman475871a2008-07-27 21:46:04 +00005674 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005675}
5676
Bill Wendlingaf566342009-08-15 21:21:19 +00005677bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00005678 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00005679 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00005680
5681 switch (VT.getSimpleVT().SimpleTy) {
5682 default:
5683 return false;
5684 case MVT::i8:
5685 case MVT::i16:
5686 case MVT::i32:
5687 return true;
5688 // FIXME: VLD1 etc with standard alignment is legal.
5689 }
5690}
5691
Evan Chenge6c835f2009-08-14 20:09:37 +00005692static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
5693 if (V < 0)
5694 return false;
5695
5696 unsigned Scale = 1;
5697 switch (VT.getSimpleVT().SimpleTy) {
5698 default: return false;
5699 case MVT::i1:
5700 case MVT::i8:
5701 // Scale == 1;
5702 break;
5703 case MVT::i16:
5704 // Scale == 2;
5705 Scale = 2;
5706 break;
5707 case MVT::i32:
5708 // Scale == 4;
5709 Scale = 4;
5710 break;
5711 }
5712
5713 if ((V & (Scale - 1)) != 0)
5714 return false;
5715 V /= Scale;
5716 return V == (V & ((1LL << 5) - 1));
5717}
5718
5719static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
5720 const ARMSubtarget *Subtarget) {
5721 bool isNeg = false;
5722 if (V < 0) {
5723 isNeg = true;
5724 V = - V;
5725 }
5726
5727 switch (VT.getSimpleVT().SimpleTy) {
5728 default: return false;
5729 case MVT::i1:
5730 case MVT::i8:
5731 case MVT::i16:
5732 case MVT::i32:
5733 // + imm12 or - imm8
5734 if (isNeg)
5735 return V == (V & ((1LL << 8) - 1));
5736 return V == (V & ((1LL << 12) - 1));
5737 case MVT::f32:
5738 case MVT::f64:
5739 // Same as ARM mode. FIXME: NEON?
5740 if (!Subtarget->hasVFP2())
5741 return false;
5742 if ((V & 3) != 0)
5743 return false;
5744 V >>= 2;
5745 return V == (V & ((1LL << 8) - 1));
5746 }
5747}
5748
Evan Chengb01fad62007-03-12 23:30:29 +00005749/// isLegalAddressImmediate - Return true if the integer value can be used
5750/// as the offset of the target addressing mode for load / store of the
5751/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00005752static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005753 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00005754 if (V == 0)
5755 return true;
5756
Evan Cheng65011532009-03-09 19:15:00 +00005757 if (!VT.isSimple())
5758 return false;
5759
Evan Chenge6c835f2009-08-14 20:09:37 +00005760 if (Subtarget->isThumb1Only())
5761 return isLegalT1AddressImmediate(V, VT);
5762 else if (Subtarget->isThumb2())
5763 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00005764
Evan Chenge6c835f2009-08-14 20:09:37 +00005765 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00005766 if (V < 0)
5767 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00005768 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00005769 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005770 case MVT::i1:
5771 case MVT::i8:
5772 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00005773 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005774 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005775 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00005776 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005777 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 case MVT::f32:
5779 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00005780 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00005781 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00005782 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00005783 return false;
5784 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005785 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00005786 }
Evan Chenga8e29892007-01-19 07:51:42 +00005787}
5788
Evan Chenge6c835f2009-08-14 20:09:37 +00005789bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
5790 EVT VT) const {
5791 int Scale = AM.Scale;
5792 if (Scale < 0)
5793 return false;
5794
5795 switch (VT.getSimpleVT().SimpleTy) {
5796 default: return false;
5797 case MVT::i1:
5798 case MVT::i8:
5799 case MVT::i16:
5800 case MVT::i32:
5801 if (Scale == 1)
5802 return true;
5803 // r + r << imm
5804 Scale = Scale & ~1;
5805 return Scale == 2 || Scale == 4 || Scale == 8;
5806 case MVT::i64:
5807 // r + r
5808 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
5809 return true;
5810 return false;
5811 case MVT::isVoid:
5812 // Note, we allow "void" uses (basically, uses that aren't loads or
5813 // stores), because arm allows folding a scale into many arithmetic
5814 // operations. This should be made more precise and revisited later.
5815
5816 // Allow r << imm, but the imm has to be a multiple of two.
5817 if (Scale & 1) return false;
5818 return isPowerOf2_32(Scale);
5819 }
5820}
5821
Chris Lattner37caf8c2007-04-09 23:33:39 +00005822/// isLegalAddressingMode - Return true if the addressing mode represented
5823/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005824bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005825 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005826 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00005827 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00005828 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005829
Chris Lattner37caf8c2007-04-09 23:33:39 +00005830 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005831 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005832 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005833
Chris Lattner37caf8c2007-04-09 23:33:39 +00005834 switch (AM.Scale) {
5835 case 0: // no scale reg, must be "r+i" or "r", or "i".
5836 break;
5837 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00005838 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00005839 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005840 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00005841 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005842 // ARM doesn't support any R+R*scale+imm addr modes.
5843 if (AM.BaseOffs)
5844 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005845
Bob Wilson2c7dab12009-04-08 17:55:28 +00005846 if (!VT.isSimple())
5847 return false;
5848
Evan Chenge6c835f2009-08-14 20:09:37 +00005849 if (Subtarget->isThumb2())
5850 return isLegalT2ScaledAddressingMode(AM, VT);
5851
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005852 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00005854 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005855 case MVT::i1:
5856 case MVT::i8:
5857 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005858 if (Scale < 0) Scale = -Scale;
5859 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005860 return true;
5861 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00005862 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00005864 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005865 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005866 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005867 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00005868 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005869
Owen Anderson825b72b2009-08-11 20:47:22 +00005870 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005871 // Note, we allow "void" uses (basically, uses that aren't loads or
5872 // stores), because arm allows folding a scale into many arithmetic
5873 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005874
Chris Lattner37caf8c2007-04-09 23:33:39 +00005875 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00005876 if (Scale & 1) return false;
5877 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00005878 }
5879 break;
Evan Chengb01fad62007-03-12 23:30:29 +00005880 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00005881 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00005882}
5883
Evan Cheng77e47512009-11-11 19:05:52 +00005884/// isLegalICmpImmediate - Return true if the specified immediate is legal
5885/// icmp immediate, that is the target has icmp instructions which can compare
5886/// a register against the immediate without having to materialize the
5887/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00005888bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00005889 if (!Subtarget->isThumb())
5890 return ARM_AM::getSOImmVal(Imm) != -1;
5891 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00005892 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00005893 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00005894}
5895
Owen Andersone50ed302009-08-10 22:56:29 +00005896static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005897 bool isSEXTLoad, SDValue &Base,
5898 SDValue &Offset, bool &isInc,
5899 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00005900 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5901 return false;
5902
Owen Anderson825b72b2009-08-11 20:47:22 +00005903 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005904 // AddressingMode 3
5905 Base = Ptr->getOperand(0);
5906 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005907 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005908 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005909 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005910 isInc = false;
5911 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5912 return true;
5913 }
5914 }
5915 isInc = (Ptr->getOpcode() == ISD::ADD);
5916 Offset = Ptr->getOperand(1);
5917 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005919 // AddressingMode 2
5920 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005921 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005922 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005923 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005924 isInc = false;
5925 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5926 Base = Ptr->getOperand(0);
5927 return true;
5928 }
5929 }
5930
5931 if (Ptr->getOpcode() == ISD::ADD) {
5932 isInc = true;
5933 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5934 if (ShOpcVal != ARM_AM::no_shift) {
5935 Base = Ptr->getOperand(1);
5936 Offset = Ptr->getOperand(0);
5937 } else {
5938 Base = Ptr->getOperand(0);
5939 Offset = Ptr->getOperand(1);
5940 }
5941 return true;
5942 }
5943
5944 isInc = (Ptr->getOpcode() == ISD::ADD);
5945 Base = Ptr->getOperand(0);
5946 Offset = Ptr->getOperand(1);
5947 return true;
5948 }
5949
Jim Grosbache5165492009-11-09 00:11:35 +00005950 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005951 return false;
5952}
5953
Owen Andersone50ed302009-08-10 22:56:29 +00005954static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005955 bool isSEXTLoad, SDValue &Base,
5956 SDValue &Offset, bool &isInc,
5957 SelectionDAG &DAG) {
5958 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5959 return false;
5960
5961 Base = Ptr->getOperand(0);
5962 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5963 int RHSC = (int)RHS->getZExtValue();
5964 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
5965 assert(Ptr->getOpcode() == ISD::ADD);
5966 isInc = false;
5967 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5968 return true;
5969 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
5970 isInc = Ptr->getOpcode() == ISD::ADD;
5971 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
5972 return true;
5973 }
5974 }
5975
5976 return false;
5977}
5978
Evan Chenga8e29892007-01-19 07:51:42 +00005979/// getPreIndexedAddressParts - returns true by value, base pointer and
5980/// offset pointer and addressing mode by reference if the node's address
5981/// can be legally represented as pre-indexed load / store address.
5982bool
Dan Gohman475871a2008-07-27 21:46:04 +00005983ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
5984 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005985 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005986 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005987 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005988 return false;
5989
Owen Andersone50ed302009-08-10 22:56:29 +00005990 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005991 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005992 bool isSEXTLoad = false;
5993 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5994 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005995 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005996 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5997 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5998 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005999 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006000 } else
6001 return false;
6002
6003 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006004 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006005 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006006 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6007 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006008 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006009 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006010 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006011 if (!isLegal)
6012 return false;
6013
6014 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6015 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006016}
6017
6018/// getPostIndexedAddressParts - returns true by value, base pointer and
6019/// offset pointer and addressing mode by reference if this node can be
6020/// combined with a load / store to form a post-indexed load / store.
6021bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006022 SDValue &Base,
6023 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006024 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006025 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006026 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006027 return false;
6028
Owen Andersone50ed302009-08-10 22:56:29 +00006029 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006030 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006031 bool isSEXTLoad = false;
6032 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006033 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006034 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006035 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6036 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006037 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006038 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006039 } else
6040 return false;
6041
6042 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006043 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006044 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006045 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006046 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006047 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006048 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6049 isInc, DAG);
6050 if (!isLegal)
6051 return false;
6052
Evan Cheng28dad2a2010-05-18 21:31:17 +00006053 if (Ptr != Base) {
6054 // Swap base ptr and offset to catch more post-index load / store when
6055 // it's legal. In Thumb2 mode, offset must be an immediate.
6056 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6057 !Subtarget->isThumb2())
6058 std::swap(Base, Offset);
6059
6060 // Post-indexed load / store update the base pointer.
6061 if (Ptr != Base)
6062 return false;
6063 }
6064
Evan Chenge88d5ce2009-07-02 07:28:31 +00006065 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6066 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006067}
6068
Dan Gohman475871a2008-07-27 21:46:04 +00006069void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006070 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006071 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006072 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006073 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006074 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006075 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006076 switch (Op.getOpcode()) {
6077 default: break;
6078 case ARMISD::CMOV: {
6079 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006080 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006081 if (KnownZero == 0 && KnownOne == 0) return;
6082
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006083 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006084 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6085 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006086 KnownZero &= KnownZeroRHS;
6087 KnownOne &= KnownOneRHS;
6088 return;
6089 }
6090 }
6091}
6092
6093//===----------------------------------------------------------------------===//
6094// ARM Inline Assembly Support
6095//===----------------------------------------------------------------------===//
6096
Evan Cheng55d42002011-01-08 01:24:27 +00006097bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6098 // Looking for "rev" which is V6+.
6099 if (!Subtarget->hasV6Ops())
6100 return false;
6101
6102 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6103 std::string AsmStr = IA->getAsmString();
6104 SmallVector<StringRef, 4> AsmPieces;
6105 SplitString(AsmStr, AsmPieces, ";\n");
6106
6107 switch (AsmPieces.size()) {
6108 default: return false;
6109 case 1:
6110 AsmStr = AsmPieces[0];
6111 AsmPieces.clear();
6112 SplitString(AsmStr, AsmPieces, " \t,");
6113
6114 // rev $0, $1
6115 if (AsmPieces.size() == 3 &&
6116 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6117 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6118 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6119 if (Ty && Ty->getBitWidth() == 32)
6120 return IntrinsicLowering::LowerToByteSwap(CI);
6121 }
6122 break;
6123 }
6124
6125 return false;
6126}
6127
Evan Chenga8e29892007-01-19 07:51:42 +00006128/// getConstraintType - Given a constraint letter, return the type of
6129/// constraint it is for this target.
6130ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006131ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6132 if (Constraint.size() == 1) {
6133 switch (Constraint[0]) {
6134 default: break;
6135 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006136 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006137 }
Evan Chenga8e29892007-01-19 07:51:42 +00006138 }
Chris Lattner4234f572007-03-25 02:14:49 +00006139 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006140}
6141
John Thompson44ab89e2010-10-29 17:29:13 +00006142/// Examine constraint type and operand type and determine a weight value.
6143/// This object must already have been set up with the operand type
6144/// and the current alternative constraint selected.
6145TargetLowering::ConstraintWeight
6146ARMTargetLowering::getSingleConstraintMatchWeight(
6147 AsmOperandInfo &info, const char *constraint) const {
6148 ConstraintWeight weight = CW_Invalid;
6149 Value *CallOperandVal = info.CallOperandVal;
6150 // If we don't have a value, we can't do a match,
6151 // but allow it at the lowest weight.
6152 if (CallOperandVal == NULL)
6153 return CW_Default;
6154 const Type *type = CallOperandVal->getType();
6155 // Look at the constraint type.
6156 switch (*constraint) {
6157 default:
6158 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6159 break;
6160 case 'l':
6161 if (type->isIntegerTy()) {
6162 if (Subtarget->isThumb())
6163 weight = CW_SpecificReg;
6164 else
6165 weight = CW_Register;
6166 }
6167 break;
6168 case 'w':
6169 if (type->isFloatingPointTy())
6170 weight = CW_Register;
6171 break;
6172 }
6173 return weight;
6174}
6175
Bob Wilson2dc4f542009-03-20 22:42:55 +00006176std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006177ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006178 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006179 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006180 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006181 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006182 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006183 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006184 return std::make_pair(0U, ARM::tGPRRegisterClass);
6185 else
6186 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006187 case 'r':
6188 return std::make_pair(0U, ARM::GPRRegisterClass);
6189 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006190 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006191 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006192 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006193 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006194 if (VT.getSizeInBits() == 128)
6195 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006196 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006197 }
6198 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006199 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006200 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006201
Evan Chenga8e29892007-01-19 07:51:42 +00006202 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6203}
6204
6205std::vector<unsigned> ARMTargetLowering::
6206getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006207 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006208 if (Constraint.size() != 1)
6209 return std::vector<unsigned>();
6210
6211 switch (Constraint[0]) { // GCC ARM Constraint Letters
6212 default: break;
6213 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006214 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6215 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6216 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006217 case 'r':
6218 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6219 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6220 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6221 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006222 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006223 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006224 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6225 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6226 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6227 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6228 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6229 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6230 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6231 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006232 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006233 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6234 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6235 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6236 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006237 if (VT.getSizeInBits() == 128)
6238 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6239 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006240 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006241 }
6242
6243 return std::vector<unsigned>();
6244}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006245
6246/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6247/// vector. If it is invalid, don't add anything to Ops.
6248void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6249 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006250 std::vector<SDValue>&Ops,
6251 SelectionDAG &DAG) const {
6252 SDValue Result(0, 0);
6253
6254 switch (Constraint) {
6255 default: break;
6256 case 'I': case 'J': case 'K': case 'L':
6257 case 'M': case 'N': case 'O':
6258 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6259 if (!C)
6260 return;
6261
6262 int64_t CVal64 = C->getSExtValue();
6263 int CVal = (int) CVal64;
6264 // None of these constraints allow values larger than 32 bits. Check
6265 // that the value fits in an int.
6266 if (CVal != CVal64)
6267 return;
6268
6269 switch (Constraint) {
6270 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006271 if (Subtarget->isThumb1Only()) {
6272 // This must be a constant between 0 and 255, for ADD
6273 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006274 if (CVal >= 0 && CVal <= 255)
6275 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006276 } else if (Subtarget->isThumb2()) {
6277 // A constant that can be used as an immediate value in a
6278 // data-processing instruction.
6279 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6280 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006281 } else {
6282 // A constant that can be used as an immediate value in a
6283 // data-processing instruction.
6284 if (ARM_AM::getSOImmVal(CVal) != -1)
6285 break;
6286 }
6287 return;
6288
6289 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006290 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006291 // This must be a constant between -255 and -1, for negated ADD
6292 // immediates. This can be used in GCC with an "n" modifier that
6293 // prints the negated value, for use with SUB instructions. It is
6294 // not useful otherwise but is implemented for compatibility.
6295 if (CVal >= -255 && CVal <= -1)
6296 break;
6297 } else {
6298 // This must be a constant between -4095 and 4095. It is not clear
6299 // what this constraint is intended for. Implemented for
6300 // compatibility with GCC.
6301 if (CVal >= -4095 && CVal <= 4095)
6302 break;
6303 }
6304 return;
6305
6306 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006307 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006308 // A 32-bit value where only one byte has a nonzero value. Exclude
6309 // zero to match GCC. This constraint is used by GCC internally for
6310 // constants that can be loaded with a move/shift combination.
6311 // It is not useful otherwise but is implemented for compatibility.
6312 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
6313 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006314 } else if (Subtarget->isThumb2()) {
6315 // A constant whose bitwise inverse can be used as an immediate
6316 // value in a data-processing instruction. This can be used in GCC
6317 // with a "B" modifier that prints the inverted value, for use with
6318 // BIC and MVN instructions. It is not useful otherwise but is
6319 // implemented for compatibility.
6320 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
6321 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006322 } else {
6323 // A constant whose bitwise inverse can be used as an immediate
6324 // value in a data-processing instruction. This can be used in GCC
6325 // with a "B" modifier that prints the inverted value, for use with
6326 // BIC and MVN instructions. It is not useful otherwise but is
6327 // implemented for compatibility.
6328 if (ARM_AM::getSOImmVal(~CVal) != -1)
6329 break;
6330 }
6331 return;
6332
6333 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006334 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006335 // This must be a constant between -7 and 7,
6336 // for 3-operand ADD/SUB immediate instructions.
6337 if (CVal >= -7 && CVal < 7)
6338 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006339 } else if (Subtarget->isThumb2()) {
6340 // A constant whose negation can be used as an immediate value in a
6341 // data-processing instruction. This can be used in GCC with an "n"
6342 // modifier that prints the negated value, for use with SUB
6343 // instructions. It is not useful otherwise but is implemented for
6344 // compatibility.
6345 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
6346 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006347 } else {
6348 // A constant whose negation can be used as an immediate value in a
6349 // data-processing instruction. This can be used in GCC with an "n"
6350 // modifier that prints the negated value, for use with SUB
6351 // instructions. It is not useful otherwise but is implemented for
6352 // compatibility.
6353 if (ARM_AM::getSOImmVal(-CVal) != -1)
6354 break;
6355 }
6356 return;
6357
6358 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006359 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006360 // This must be a multiple of 4 between 0 and 1020, for
6361 // ADD sp + immediate.
6362 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
6363 break;
6364 } else {
6365 // A power of two or a constant between 0 and 32. This is used in
6366 // GCC for the shift amount on shifted register operands, but it is
6367 // useful in general for any shift amounts.
6368 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
6369 break;
6370 }
6371 return;
6372
6373 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006374 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006375 // This must be a constant between 0 and 31, for shift amounts.
6376 if (CVal >= 0 && CVal <= 31)
6377 break;
6378 }
6379 return;
6380
6381 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006382 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006383 // This must be a multiple of 4 between -508 and 508, for
6384 // ADD/SUB sp = sp + immediate.
6385 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
6386 break;
6387 }
6388 return;
6389 }
6390 Result = DAG.getTargetConstant(CVal, Op.getValueType());
6391 break;
6392 }
6393
6394 if (Result.getNode()) {
6395 Ops.push_back(Result);
6396 return;
6397 }
Dale Johannesen1784d162010-06-25 21:55:36 +00006398 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006399}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00006400
6401bool
6402ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
6403 // The ARM target isn't yet aware of offsets.
6404 return false;
6405}
Evan Cheng39382422009-10-28 01:44:26 +00006406
6407int ARM::getVFPf32Imm(const APFloat &FPImm) {
6408 APInt Imm = FPImm.bitcastToAPInt();
6409 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
6410 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
6411 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
6412
6413 // We can handle 4 bits of mantissa.
6414 // mantissa = (16+UInt(e:f:g:h))/16.
6415 if (Mantissa & 0x7ffff)
6416 return -1;
6417 Mantissa >>= 19;
6418 if ((Mantissa & 0xf) != Mantissa)
6419 return -1;
6420
6421 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6422 if (Exp < -3 || Exp > 4)
6423 return -1;
6424 Exp = ((Exp+3) & 0x7) ^ 4;
6425
6426 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6427}
6428
6429int ARM::getVFPf64Imm(const APFloat &FPImm) {
6430 APInt Imm = FPImm.bitcastToAPInt();
6431 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
6432 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
6433 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
6434
6435 // We can handle 4 bits of mantissa.
6436 // mantissa = (16+UInt(e:f:g:h))/16.
6437 if (Mantissa & 0xffffffffffffLL)
6438 return -1;
6439 Mantissa >>= 48;
6440 if ((Mantissa & 0xf) != Mantissa)
6441 return -1;
6442
6443 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6444 if (Exp < -3 || Exp > 4)
6445 return -1;
6446 Exp = ((Exp+3) & 0x7) ^ 4;
6447
6448 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6449}
6450
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006451bool ARM::isBitFieldInvertedMask(unsigned v) {
6452 if (v == 0xffffffff)
6453 return 0;
6454 // there can be 1's on either or both "outsides", all the "inside"
6455 // bits must be 0's
6456 unsigned int lsb = 0, msb = 31;
6457 while (v & (1 << msb)) --msb;
6458 while (v & (1 << lsb)) ++lsb;
6459 for (unsigned int i = lsb; i <= msb; ++i) {
6460 if (v & (1 << i))
6461 return 0;
6462 }
6463 return 1;
6464}
6465
Evan Cheng39382422009-10-28 01:44:26 +00006466/// isFPImmLegal - Returns true if the target can instruction select the
6467/// specified FP immediate natively. If false, the legalizer will
6468/// materialize the FP immediate as a load from a constant pool.
6469bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
6470 if (!Subtarget->hasVFP3())
6471 return false;
6472 if (VT == MVT::f32)
6473 return ARM::getVFPf32Imm(Imm) != -1;
6474 if (VT == MVT::f64)
6475 return ARM::getVFPf64Imm(Imm) != -1;
6476 return false;
6477}
Bob Wilson65ffec42010-09-21 17:56:22 +00006478
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006479/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00006480/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
6481/// specified in the intrinsic calls.
6482bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
6483 const CallInst &I,
6484 unsigned Intrinsic) const {
6485 switch (Intrinsic) {
6486 case Intrinsic::arm_neon_vld1:
6487 case Intrinsic::arm_neon_vld2:
6488 case Intrinsic::arm_neon_vld3:
6489 case Intrinsic::arm_neon_vld4:
6490 case Intrinsic::arm_neon_vld2lane:
6491 case Intrinsic::arm_neon_vld3lane:
6492 case Intrinsic::arm_neon_vld4lane: {
6493 Info.opc = ISD::INTRINSIC_W_CHAIN;
6494 // Conservatively set memVT to the entire set of vectors loaded.
6495 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
6496 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6497 Info.ptrVal = I.getArgOperand(0);
6498 Info.offset = 0;
6499 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6500 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6501 Info.vol = false; // volatile loads with NEON intrinsics not supported
6502 Info.readMem = true;
6503 Info.writeMem = false;
6504 return true;
6505 }
6506 case Intrinsic::arm_neon_vst1:
6507 case Intrinsic::arm_neon_vst2:
6508 case Intrinsic::arm_neon_vst3:
6509 case Intrinsic::arm_neon_vst4:
6510 case Intrinsic::arm_neon_vst2lane:
6511 case Intrinsic::arm_neon_vst3lane:
6512 case Intrinsic::arm_neon_vst4lane: {
6513 Info.opc = ISD::INTRINSIC_VOID;
6514 // Conservatively set memVT to the entire set of vectors stored.
6515 unsigned NumElts = 0;
6516 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
6517 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
6518 if (!ArgTy->isVectorTy())
6519 break;
6520 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
6521 }
6522 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6523 Info.ptrVal = I.getArgOperand(0);
6524 Info.offset = 0;
6525 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6526 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6527 Info.vol = false; // volatile stores with NEON intrinsics not supported
6528 Info.readMem = false;
6529 Info.writeMem = true;
6530 return true;
6531 }
6532 default:
6533 break;
6534 }
6535
6536 return false;
6537}