blob: 0167bd7c6286fc5e7a098232cd715ff180e779d1 [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
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000905// FIXME: Move to RegInfo
Evan Cheng31446872010-07-23 22:39:59 +0000906unsigned
907ARMTargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
908 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000909 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000910
Evan Cheng31446872010-07-23 22:39:59 +0000911 switch (RC->getID()) {
912 default:
913 return 0;
914 case ARM::tGPRRegClassID:
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000915 return TFI->hasFP(MF) ? 4 : 5;
Evan Chengac096802010-08-10 19:30:19 +0000916 case ARM::GPRRegClassID: {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000917 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
Evan Chengac096802010-08-10 19:30:19 +0000918 return 10 - FP - (Subtarget->isR9Reserved() ? 1 : 0);
919 }
Evan Cheng31446872010-07-23 22:39:59 +0000920 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
921 case ARM::DPRRegClassID:
922 return 32 - 10;
923 }
924}
925
Evan Chenga8e29892007-01-19 07:51:42 +0000926//===----------------------------------------------------------------------===//
927// Lowering Code
928//===----------------------------------------------------------------------===//
929
Evan Chenga8e29892007-01-19 07:51:42 +0000930/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
931static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
932 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000933 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000934 case ISD::SETNE: return ARMCC::NE;
935 case ISD::SETEQ: return ARMCC::EQ;
936 case ISD::SETGT: return ARMCC::GT;
937 case ISD::SETGE: return ARMCC::GE;
938 case ISD::SETLT: return ARMCC::LT;
939 case ISD::SETLE: return ARMCC::LE;
940 case ISD::SETUGT: return ARMCC::HI;
941 case ISD::SETUGE: return ARMCC::HS;
942 case ISD::SETULT: return ARMCC::LO;
943 case ISD::SETULE: return ARMCC::LS;
944 }
945}
946
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000947/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
948static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000949 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000950 CondCode2 = ARMCC::AL;
951 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000952 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000953 case ISD::SETEQ:
954 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
955 case ISD::SETGT:
956 case ISD::SETOGT: CondCode = ARMCC::GT; break;
957 case ISD::SETGE:
958 case ISD::SETOGE: CondCode = ARMCC::GE; break;
959 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000960 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000961 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
962 case ISD::SETO: CondCode = ARMCC::VC; break;
963 case ISD::SETUO: CondCode = ARMCC::VS; break;
964 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
965 case ISD::SETUGT: CondCode = ARMCC::HI; break;
966 case ISD::SETUGE: CondCode = ARMCC::PL; break;
967 case ISD::SETLT:
968 case ISD::SETULT: CondCode = ARMCC::LT; break;
969 case ISD::SETLE:
970 case ISD::SETULE: CondCode = ARMCC::LE; break;
971 case ISD::SETNE:
972 case ISD::SETUNE: CondCode = ARMCC::NE; break;
973 }
Evan Chenga8e29892007-01-19 07:51:42 +0000974}
975
Bob Wilson1f595bb2009-04-17 19:07:39 +0000976//===----------------------------------------------------------------------===//
977// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000978//===----------------------------------------------------------------------===//
979
980#include "ARMGenCallingConv.inc"
981
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000982/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
983/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000984CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000985 bool Return,
986 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000987 switch (CC) {
988 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000989 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000990 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +0000991 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +0000992 if (!Subtarget->isAAPCS_ABI())
993 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
994 // For AAPCS ABI targets, just use VFP variant of the calling convention.
995 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
996 }
997 // Fallthrough
998 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000999 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001000 if (!Subtarget->isAAPCS_ABI())
1001 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1002 else if (Subtarget->hasVFP2() &&
1003 FloatABIType == FloatABI::Hard && !isVarArg)
1004 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1005 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1006 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001007 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001008 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001009 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001010 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001011 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001012 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001013 }
1014}
1015
Dan Gohman98ca4f22009-08-05 01:29:28 +00001016/// LowerCallResult - Lower the result values of a call into the
1017/// appropriate copies out of appropriate physical registers.
1018SDValue
1019ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001020 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001021 const SmallVectorImpl<ISD::InputArg> &Ins,
1022 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001023 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001024
Bob Wilson1f595bb2009-04-17 19:07:39 +00001025 // Assign locations to each value returned by this call.
1026 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001027 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001028 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001029 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001030 CCAssignFnForNode(CallConv, /* Return*/ true,
1031 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001032
1033 // Copy all of the result registers out of their specified physreg.
1034 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1035 CCValAssign VA = RVLocs[i];
1036
Bob Wilson80915242009-04-25 00:33:20 +00001037 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001038 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001039 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001040 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001041 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001042 Chain = Lo.getValue(1);
1043 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001044 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001045 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001046 InFlag);
1047 Chain = Hi.getValue(1);
1048 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001049 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001050
Owen Anderson825b72b2009-08-11 20:47:22 +00001051 if (VA.getLocVT() == MVT::v2f64) {
1052 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1053 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1054 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001055
1056 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001057 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001058 Chain = Lo.getValue(1);
1059 InFlag = Lo.getValue(2);
1060 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001061 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001062 Chain = Hi.getValue(1);
1063 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001064 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001065 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1066 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001067 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001068 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001069 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1070 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001071 Chain = Val.getValue(1);
1072 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001073 }
Bob Wilson80915242009-04-25 00:33:20 +00001074
1075 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001076 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001077 case CCValAssign::Full: break;
1078 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001079 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001080 break;
1081 }
1082
Dan Gohman98ca4f22009-08-05 01:29:28 +00001083 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001084 }
1085
Dan Gohman98ca4f22009-08-05 01:29:28 +00001086 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001087}
1088
1089/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1090/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001091/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001092/// a byval function parameter.
1093/// Sometimes what we are copying is the end of a larger object, the part that
1094/// does not fit in registers.
1095static SDValue
1096CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1097 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1098 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001099 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001100 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001101 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001102 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001103}
1104
Bob Wilsondee46d72009-04-17 20:35:10 +00001105/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001106SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001107ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1108 SDValue StackPtr, SDValue Arg,
1109 DebugLoc dl, SelectionDAG &DAG,
1110 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001111 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112 unsigned LocMemOffset = VA.getLocMemOffset();
1113 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1114 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001115 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001116 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001117
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001119 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001120 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001121}
1122
Dan Gohman98ca4f22009-08-05 01:29:28 +00001123void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001124 SDValue Chain, SDValue &Arg,
1125 RegsToPassVector &RegsToPass,
1126 CCValAssign &VA, CCValAssign &NextVA,
1127 SDValue &StackPtr,
1128 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001129 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001130
Jim Grosbache5165492009-11-09 00:11:35 +00001131 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001133 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1134
1135 if (NextVA.isRegLoc())
1136 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1137 else {
1138 assert(NextVA.isMemLoc());
1139 if (StackPtr.getNode() == 0)
1140 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1141
Dan Gohman98ca4f22009-08-05 01:29:28 +00001142 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1143 dl, DAG, NextVA,
1144 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001145 }
1146}
1147
Dan Gohman98ca4f22009-08-05 01:29:28 +00001148/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001149/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1150/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001151SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001152ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001153 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001154 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001155 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001156 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001157 const SmallVectorImpl<ISD::InputArg> &Ins,
1158 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001159 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001160 MachineFunction &MF = DAG.getMachineFunction();
1161 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1162 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001163 // Temporarily disable tail calls so things don't break.
1164 if (!EnableARMTailCalls)
1165 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001166 if (isTailCall) {
1167 // Check if it's really possible to do a tail call.
1168 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1169 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001170 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001171 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1172 // detected sibcalls.
1173 if (isTailCall) {
1174 ++NumTailCalls;
1175 IsSibCall = true;
1176 }
1177 }
Evan Chenga8e29892007-01-19 07:51:42 +00001178
Bob Wilson1f595bb2009-04-17 19:07:39 +00001179 // Analyze operands of the call, assigning locations to each operand.
1180 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001181 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1182 *DAG.getContext());
1183 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001184 CCAssignFnForNode(CallConv, /* Return*/ false,
1185 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001186
Bob Wilson1f595bb2009-04-17 19:07:39 +00001187 // Get a count of how many bytes are to be pushed on the stack.
1188 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001189
Dale Johannesen51e28e62010-06-03 21:09:53 +00001190 // For tail calls, memory operands are available in our caller's stack.
1191 if (IsSibCall)
1192 NumBytes = 0;
1193
Evan Chenga8e29892007-01-19 07:51:42 +00001194 // Adjust the stack pointer for the new arguments...
1195 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001196 if (!IsSibCall)
1197 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001198
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001199 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001200
Bob Wilson5bafff32009-06-22 23:27:02 +00001201 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001202 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001203
Bob Wilson1f595bb2009-04-17 19:07:39 +00001204 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001205 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001206 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1207 i != e;
1208 ++i, ++realArgIdx) {
1209 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001210 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001211 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001212
Bob Wilson1f595bb2009-04-17 19:07:39 +00001213 // Promote the value if needed.
1214 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001215 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001216 case CCValAssign::Full: break;
1217 case CCValAssign::SExt:
1218 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1219 break;
1220 case CCValAssign::ZExt:
1221 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1222 break;
1223 case CCValAssign::AExt:
1224 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1225 break;
1226 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001227 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001229 }
1230
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001231 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001233 if (VA.getLocVT() == MVT::v2f64) {
1234 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1235 DAG.getConstant(0, MVT::i32));
1236 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1237 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001238
Dan Gohman98ca4f22009-08-05 01:29:28 +00001239 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001240 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1241
1242 VA = ArgLocs[++i]; // skip ahead to next loc
1243 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001244 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001245 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1246 } else {
1247 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001248
Dan Gohman98ca4f22009-08-05 01:29:28 +00001249 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1250 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001251 }
1252 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001253 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001254 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001255 }
1256 } else if (VA.isRegLoc()) {
1257 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001258 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001259 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260
Dan Gohman98ca4f22009-08-05 01:29:28 +00001261 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1262 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001263 }
Evan Chenga8e29892007-01-19 07:51:42 +00001264 }
1265
1266 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001268 &MemOpChains[0], MemOpChains.size());
1269
1270 // Build a sequence of copy-to-reg nodes chained together with token chain
1271 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001272 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001273 // Tail call byval lowering might overwrite argument registers so in case of
1274 // tail call optimization the copies to registers are lowered later.
1275 if (!isTailCall)
1276 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1277 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1278 RegsToPass[i].second, InFlag);
1279 InFlag = Chain.getValue(1);
1280 }
Evan Chenga8e29892007-01-19 07:51:42 +00001281
Dale Johannesen51e28e62010-06-03 21:09:53 +00001282 // For tail calls lower the arguments to the 'real' stack slot.
1283 if (isTailCall) {
1284 // Force all the incoming stack arguments to be loaded from the stack
1285 // before any new outgoing arguments are stored to the stack, because the
1286 // outgoing stack slots may alias the incoming argument stack slots, and
1287 // the alias isn't otherwise explicit. This is slightly more conservative
1288 // than necessary, because it means that each store effectively depends
1289 // on every argument instead of just those arguments it would clobber.
1290
1291 // Do not flag preceeding copytoreg stuff together with the following stuff.
1292 InFlag = SDValue();
1293 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1294 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1295 RegsToPass[i].second, InFlag);
1296 InFlag = Chain.getValue(1);
1297 }
1298 InFlag =SDValue();
1299 }
1300
Bill Wendling056292f2008-09-16 21:48:12 +00001301 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1302 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1303 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001304 bool isDirect = false;
1305 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001306 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001307 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001308
1309 if (EnableARMLongCalls) {
1310 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1311 && "long-calls with non-static relocation model!");
1312 // Handle a global address or an external symbol. If it's not one of
1313 // those, the target's already in a register, so we don't need to do
1314 // anything extra.
1315 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001316 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001317 // Create a constant pool entry for the callee address
1318 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1319 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1320 ARMPCLabelIndex,
1321 ARMCP::CPValue, 0);
1322 // Get the address of the callee into a register
1323 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1324 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1325 Callee = DAG.getLoad(getPointerTy(), dl,
1326 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001327 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001328 false, false, 0);
1329 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1330 const char *Sym = S->getSymbol();
1331
1332 // Create a constant pool entry for the callee address
1333 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1334 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1335 Sym, ARMPCLabelIndex, 0);
1336 // Get the address of the callee into a register
1337 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1338 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1339 Callee = DAG.getLoad(getPointerTy(), dl,
1340 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001341 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001342 false, false, 0);
1343 }
1344 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001345 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001346 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001347 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001348 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001349 getTargetMachine().getRelocationModel() != Reloc::Static;
1350 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001351 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001352 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001353 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001354 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001355 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001356 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001357 ARMPCLabelIndex,
1358 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001359 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001360 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001361 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001362 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001363 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001364 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001365 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001366 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001367 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001368 } else {
1369 // On ELF targets for PIC code, direct calls should go through the PLT
1370 unsigned OpFlags = 0;
1371 if (Subtarget->isTargetELF() &&
1372 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1373 OpFlags = ARMII::MO_PLT;
1374 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1375 }
Bill Wendling056292f2008-09-16 21:48:12 +00001376 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001377 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001378 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001379 getTargetMachine().getRelocationModel() != Reloc::Static;
1380 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001381 // tBX takes a register source operand.
1382 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001383 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001384 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001385 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001386 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001387 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001388 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001389 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001390 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001391 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001392 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001393 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001394 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001395 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001396 } else {
1397 unsigned OpFlags = 0;
1398 // On ELF targets for PIC code, direct calls should go through the PLT
1399 if (Subtarget->isTargetELF() &&
1400 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1401 OpFlags = ARMII::MO_PLT;
1402 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1403 }
Evan Chenga8e29892007-01-19 07:51:42 +00001404 }
1405
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001406 // FIXME: handle tail calls differently.
1407 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001408 if (Subtarget->isThumb()) {
1409 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001410 CallOpc = ARMISD::CALL_NOLINK;
1411 else
1412 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1413 } else {
1414 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001415 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1416 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001417 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001418
Dan Gohman475871a2008-07-27 21:46:04 +00001419 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001420 Ops.push_back(Chain);
1421 Ops.push_back(Callee);
1422
1423 // Add argument registers to the end of the list so that they are known live
1424 // into the call.
1425 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1426 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1427 RegsToPass[i].second.getValueType()));
1428
Gabor Greifba36cb52008-08-28 21:40:38 +00001429 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001430 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001431
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001432 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001433 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001434 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001435
Duncan Sands4bdcb612008-07-02 17:40:58 +00001436 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001437 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001438 InFlag = Chain.getValue(1);
1439
Chris Lattnere563bbc2008-10-11 22:08:30 +00001440 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1441 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001442 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001443 InFlag = Chain.getValue(1);
1444
Bob Wilson1f595bb2009-04-17 19:07:39 +00001445 // Handle result values, copying them out of physregs into vregs that we
1446 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001447 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1448 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001449}
1450
Dale Johannesen51e28e62010-06-03 21:09:53 +00001451/// MatchingStackOffset - Return true if the given stack call argument is
1452/// already available in the same position (relatively) of the caller's
1453/// incoming argument stack.
1454static
1455bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1456 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1457 const ARMInstrInfo *TII) {
1458 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1459 int FI = INT_MAX;
1460 if (Arg.getOpcode() == ISD::CopyFromReg) {
1461 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001462 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001463 return false;
1464 MachineInstr *Def = MRI->getVRegDef(VR);
1465 if (!Def)
1466 return false;
1467 if (!Flags.isByVal()) {
1468 if (!TII->isLoadFromStackSlot(Def, FI))
1469 return false;
1470 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001471 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001472 }
1473 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1474 if (Flags.isByVal())
1475 // ByVal argument is passed in as a pointer but it's now being
1476 // dereferenced. e.g.
1477 // define @foo(%struct.X* %A) {
1478 // tail call @bar(%struct.X* byval %A)
1479 // }
1480 return false;
1481 SDValue Ptr = Ld->getBasePtr();
1482 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1483 if (!FINode)
1484 return false;
1485 FI = FINode->getIndex();
1486 } else
1487 return false;
1488
1489 assert(FI != INT_MAX);
1490 if (!MFI->isFixedObjectIndex(FI))
1491 return false;
1492 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1493}
1494
1495/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1496/// for tail call optimization. Targets which want to do tail call
1497/// optimization should implement this function.
1498bool
1499ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1500 CallingConv::ID CalleeCC,
1501 bool isVarArg,
1502 bool isCalleeStructRet,
1503 bool isCallerStructRet,
1504 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001505 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001506 const SmallVectorImpl<ISD::InputArg> &Ins,
1507 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001508 const Function *CallerF = DAG.getMachineFunction().getFunction();
1509 CallingConv::ID CallerCC = CallerF->getCallingConv();
1510 bool CCMatch = CallerCC == CalleeCC;
1511
1512 // Look for obvious safe cases to perform tail call optimization that do not
1513 // require ABI changes. This is what gcc calls sibcall.
1514
Jim Grosbach7616b642010-06-16 23:45:49 +00001515 // Do not sibcall optimize vararg calls unless the call site is not passing
1516 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001517 if (isVarArg && !Outs.empty())
1518 return false;
1519
1520 // Also avoid sibcall optimization if either caller or callee uses struct
1521 // return semantics.
1522 if (isCalleeStructRet || isCallerStructRet)
1523 return false;
1524
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001525 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001526 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001527 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1528 // LR. This means if we need to reload LR, it takes an extra instructions,
1529 // which outweighs the value of the tail call; but here we don't know yet
1530 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001531 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001532 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001533
1534 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1535 // but we need to make sure there are enough registers; the only valid
1536 // registers are the 4 used for parameters. We don't currently do this
1537 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001538 if (Subtarget->isThumb1Only())
1539 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001540
Dale Johannesen51e28e62010-06-03 21:09:53 +00001541 // If the calling conventions do not match, then we'd better make sure the
1542 // results are returned in the same way as what the caller expects.
1543 if (!CCMatch) {
1544 SmallVector<CCValAssign, 16> RVLocs1;
1545 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1546 RVLocs1, *DAG.getContext());
1547 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1548
1549 SmallVector<CCValAssign, 16> RVLocs2;
1550 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1551 RVLocs2, *DAG.getContext());
1552 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1553
1554 if (RVLocs1.size() != RVLocs2.size())
1555 return false;
1556 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1557 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1558 return false;
1559 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1560 return false;
1561 if (RVLocs1[i].isRegLoc()) {
1562 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1563 return false;
1564 } else {
1565 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1566 return false;
1567 }
1568 }
1569 }
1570
1571 // If the callee takes no arguments then go on to check the results of the
1572 // call.
1573 if (!Outs.empty()) {
1574 // Check if stack adjustment is needed. For now, do not do this if any
1575 // argument is passed on the stack.
1576 SmallVector<CCValAssign, 16> ArgLocs;
1577 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1578 ArgLocs, *DAG.getContext());
1579 CCInfo.AnalyzeCallOperands(Outs,
1580 CCAssignFnForNode(CalleeCC, false, isVarArg));
1581 if (CCInfo.getNextStackOffset()) {
1582 MachineFunction &MF = DAG.getMachineFunction();
1583
1584 // Check if the arguments are already laid out in the right way as
1585 // the caller's fixed stack objects.
1586 MachineFrameInfo *MFI = MF.getFrameInfo();
1587 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1588 const ARMInstrInfo *TII =
1589 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001590 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1591 i != e;
1592 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001593 CCValAssign &VA = ArgLocs[i];
1594 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001595 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001596 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001597 if (VA.getLocInfo() == CCValAssign::Indirect)
1598 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001599 if (VA.needsCustom()) {
1600 // f64 and vector types are split into multiple registers or
1601 // register/stack-slot combinations. The types will not match
1602 // the registers; give up on memory f64 refs until we figure
1603 // out what to do about this.
1604 if (!VA.isRegLoc())
1605 return false;
1606 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001607 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001608 if (RegVT == MVT::v2f64) {
1609 if (!ArgLocs[++i].isRegLoc())
1610 return false;
1611 if (!ArgLocs[++i].isRegLoc())
1612 return false;
1613 }
1614 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001615 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1616 MFI, MRI, TII))
1617 return false;
1618 }
1619 }
1620 }
1621 }
1622
1623 return true;
1624}
1625
Dan Gohman98ca4f22009-08-05 01:29:28 +00001626SDValue
1627ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001628 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001629 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001630 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001631 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001632
Bob Wilsondee46d72009-04-17 20:35:10 +00001633 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001634 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001635
Bob Wilsondee46d72009-04-17 20:35:10 +00001636 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001637 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1638 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001639
Dan Gohman98ca4f22009-08-05 01:29:28 +00001640 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001641 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1642 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001643
1644 // If this is the first return lowered for this function, add
1645 // the regs to the liveout set for the function.
1646 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1647 for (unsigned i = 0; i != RVLocs.size(); ++i)
1648 if (RVLocs[i].isRegLoc())
1649 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001650 }
1651
Bob Wilson1f595bb2009-04-17 19:07:39 +00001652 SDValue Flag;
1653
1654 // Copy the result values into the output registers.
1655 for (unsigned i = 0, realRVLocIdx = 0;
1656 i != RVLocs.size();
1657 ++i, ++realRVLocIdx) {
1658 CCValAssign &VA = RVLocs[i];
1659 assert(VA.isRegLoc() && "Can only return in registers!");
1660
Dan Gohmanc9403652010-07-07 15:54:55 +00001661 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001662
1663 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001664 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001665 case CCValAssign::Full: break;
1666 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001667 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001668 break;
1669 }
1670
Bob Wilson1f595bb2009-04-17 19:07:39 +00001671 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001672 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001673 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001674 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1675 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001676 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001677 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001678
1679 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1680 Flag = Chain.getValue(1);
1681 VA = RVLocs[++i]; // skip ahead to next loc
1682 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1683 HalfGPRs.getValue(1), Flag);
1684 Flag = Chain.getValue(1);
1685 VA = RVLocs[++i]; // skip ahead to next loc
1686
1687 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001688 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1689 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001690 }
1691 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1692 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001693 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001695 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001696 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001697 VA = RVLocs[++i]; // skip ahead to next loc
1698 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1699 Flag);
1700 } else
1701 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1702
Bob Wilsondee46d72009-04-17 20:35:10 +00001703 // Guarantee that all emitted copies are
1704 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001705 Flag = Chain.getValue(1);
1706 }
1707
1708 SDValue result;
1709 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001710 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001711 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001713
1714 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001715}
1716
Evan Cheng3d2125c2010-11-30 23:55:39 +00001717bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1718 if (N->getNumValues() != 1)
1719 return false;
1720 if (!N->hasNUsesOfValue(1, 0))
1721 return false;
1722
1723 unsigned NumCopies = 0;
1724 SDNode* Copies[2];
1725 SDNode *Use = *N->use_begin();
1726 if (Use->getOpcode() == ISD::CopyToReg) {
1727 Copies[NumCopies++] = Use;
1728 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1729 // f64 returned in a pair of GPRs.
1730 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1731 UI != UE; ++UI) {
1732 if (UI->getOpcode() != ISD::CopyToReg)
1733 return false;
1734 Copies[UI.getUse().getResNo()] = *UI;
1735 ++NumCopies;
1736 }
1737 } else if (Use->getOpcode() == ISD::BITCAST) {
1738 // f32 returned in a single GPR.
1739 if (!Use->hasNUsesOfValue(1, 0))
1740 return false;
1741 Use = *Use->use_begin();
1742 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1743 return false;
1744 Copies[NumCopies++] = Use;
1745 } else {
1746 return false;
1747 }
1748
1749 if (NumCopies != 1 && NumCopies != 2)
1750 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001751
1752 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001753 for (unsigned i = 0; i < NumCopies; ++i) {
1754 SDNode *Copy = Copies[i];
1755 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1756 UI != UE; ++UI) {
1757 if (UI->getOpcode() == ISD::CopyToReg) {
1758 SDNode *Use = *UI;
1759 if (Use == Copies[0] || Use == Copies[1])
1760 continue;
1761 return false;
1762 }
1763 if (UI->getOpcode() != ARMISD::RET_FLAG)
1764 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001765 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001766 }
1767 }
1768
Evan Cheng1bf891a2010-12-01 22:59:46 +00001769 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001770}
1771
Bob Wilsonb62d2572009-11-03 00:02:05 +00001772// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1773// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1774// one of the above mentioned nodes. It has to be wrapped because otherwise
1775// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1776// be used to form addressing mode. These wrapped nodes will be selected
1777// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001778static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001779 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001780 // FIXME there is no actual debug info here
1781 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001782 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001783 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001784 if (CP->isMachineConstantPoolEntry())
1785 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1786 CP->getAlignment());
1787 else
1788 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1789 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001790 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001791}
1792
Jim Grosbache1102ca2010-07-19 17:20:38 +00001793unsigned ARMTargetLowering::getJumpTableEncoding() const {
1794 return MachineJumpTableInfo::EK_Inline;
1795}
1796
Dan Gohmand858e902010-04-17 15:26:15 +00001797SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1798 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001799 MachineFunction &MF = DAG.getMachineFunction();
1800 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1801 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001802 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001803 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001804 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001805 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1806 SDValue CPAddr;
1807 if (RelocM == Reloc::Static) {
1808 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1809 } else {
1810 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001811 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001812 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1813 ARMCP::CPBlockAddress,
1814 PCAdj);
1815 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1816 }
1817 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1818 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001819 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001820 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001821 if (RelocM == Reloc::Static)
1822 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001823 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001824 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001825}
1826
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001827// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001828SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001829ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001830 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001831 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001832 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001833 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001834 MachineFunction &MF = DAG.getMachineFunction();
1835 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1836 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001837 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001838 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001839 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001840 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001842 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001843 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001844 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001845 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001846
Evan Chenge7e0d622009-11-06 22:24:13 +00001847 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001848 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001849
1850 // call __tls_get_addr.
1851 ArgListTy Args;
1852 ArgListEntry Entry;
1853 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001854 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001855 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001856 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001857 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001858 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1859 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001860 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001861 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001862 return CallResult.first;
1863}
1864
1865// Lower ISD::GlobalTLSAddress using the "initial exec" or
1866// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001867SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001868ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001869 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001870 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001871 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001872 SDValue Offset;
1873 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001874 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001875 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001876 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001877
Chris Lattner4fb63d02009-07-15 04:12:33 +00001878 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001879 MachineFunction &MF = DAG.getMachineFunction();
1880 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1881 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1882 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001883 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1884 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001885 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001886 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001887 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001889 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001890 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001891 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001892 Chain = Offset.getValue(1);
1893
Evan Chenge7e0d622009-11-06 22:24:13 +00001894 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001895 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001896
Evan Cheng9eda6892009-10-31 03:39:36 +00001897 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001898 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001899 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001900 } else {
1901 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001902 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001903 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001905 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001906 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001907 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001908 }
1909
1910 // The address of the thread local variable is the add of the thread
1911 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001912 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001913}
1914
Dan Gohman475871a2008-07-27 21:46:04 +00001915SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001916ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001917 // TODO: implement the "local dynamic" model
1918 assert(Subtarget->isTargetELF() &&
1919 "TLS not implemented for non-ELF targets");
1920 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1921 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1922 // otherwise use the "Local Exec" TLS Model
1923 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1924 return LowerToTLSGeneralDynamicModel(GA, DAG);
1925 else
1926 return LowerToTLSExecModels(GA, DAG);
1927}
1928
Dan Gohman475871a2008-07-27 21:46:04 +00001929SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001930 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001931 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001932 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001933 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001934 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1935 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001936 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001937 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001938 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001939 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001941 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001942 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001943 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001944 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001945 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001946 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001947 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001948 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001949 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001950 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001951 return Result;
1952 } else {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001953 // If we have T2 ops, we can materialize the address directly via movt/movw
1954 // pair. This is always cheaper.
1955 if (Subtarget->useMovt()) {
1956 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
Devang Patel0d881da2010-07-06 22:08:15 +00001957 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001958 } else {
1959 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1960 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1961 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001962 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001963 false, false, 0);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +00001964 }
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001965 }
1966}
1967
Dan Gohman475871a2008-07-27 21:46:04 +00001968SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001969 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001970 MachineFunction &MF = DAG.getMachineFunction();
1971 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1972 unsigned ARMPCLabelIndex = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00001973 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001974 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001975 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001976 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Dan Gohman475871a2008-07-27 21:46:04 +00001977 SDValue CPAddr;
Evan Chenga8e29892007-01-19 07:51:42 +00001978 if (RelocM == Reloc::Static)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001979 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001980 else {
Evan Chenge7e0d622009-11-06 22:24:13 +00001981 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001982 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1983 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001984 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001985 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00001986 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00001988
Evan Cheng9eda6892009-10-31 03:39:36 +00001989 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001990 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001991 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001993
1994 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001995 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001996 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00001997 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00001998
Evan Cheng63476a82009-09-03 07:04:02 +00001999 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002000 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002001 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002002
2003 return Result;
2004}
2005
Dan Gohman475871a2008-07-27 21:46:04 +00002006SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002007 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002008 assert(Subtarget->isTargetELF() &&
2009 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002010 MachineFunction &MF = DAG.getMachineFunction();
2011 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2012 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002013 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002014 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002015 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002016 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2017 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002018 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002019 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002020 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002021 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002022 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002023 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002024 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002025 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002026}
2027
Jim Grosbach0e0da732009-05-12 23:59:14 +00002028SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002029ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2030 const {
2031 DebugLoc dl = Op.getDebugLoc();
2032 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
2033 Op.getOperand(0), Op.getOperand(1));
2034}
2035
2036SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002037ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2038 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002039 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002040 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2041 Op.getOperand(1), Val);
2042}
2043
2044SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002045ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2046 DebugLoc dl = Op.getDebugLoc();
2047 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2048 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2049}
2050
2051SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002052ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002053 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002054 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002055 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002056 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002057 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002058 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002059 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002060 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2061 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002062 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002063 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002064 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2065 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002066 EVT PtrVT = getPointerTy();
2067 DebugLoc dl = Op.getDebugLoc();
2068 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2069 SDValue CPAddr;
2070 unsigned PCAdj = (RelocM != Reloc::PIC_)
2071 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002072 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002073 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2074 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002075 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002076 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002077 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002078 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002079 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002080 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002081
2082 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002083 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002084 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2085 }
2086 return Result;
2087 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002088 }
2089}
2090
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002091static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002092 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002093 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002094 if (!Subtarget->hasDataBarrier()) {
2095 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2096 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2097 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002098 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002099 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002100 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002101 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002102 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002103
2104 SDValue Op5 = Op.getOperand(5);
2105 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2106 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2107 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2108 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2109
2110 ARM_MB::MemBOpt DMBOpt;
2111 if (isDeviceBarrier)
2112 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2113 else
2114 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2115 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2116 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002117}
2118
Evan Chengdfed19f2010-11-03 06:34:55 +00002119static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2120 const ARMSubtarget *Subtarget) {
2121 // ARM pre v5TE and Thumb1 does not have preload instructions.
2122 if (!(Subtarget->isThumb2() ||
2123 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2124 // Just preserve the chain.
2125 return Op.getOperand(0);
2126
2127 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002128 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2129 if (!isRead &&
2130 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2131 // ARMv7 with MP extension has PLDW.
2132 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002133
2134 if (Subtarget->isThumb())
2135 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002136 isRead = ~isRead & 1;
2137 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002138
Evan Cheng416941d2010-11-04 05:19:35 +00002139 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002140 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002141 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2142 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002143}
2144
Dan Gohman1e93df62010-04-17 14:41:14 +00002145static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2146 MachineFunction &MF = DAG.getMachineFunction();
2147 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2148
Evan Chenga8e29892007-01-19 07:51:42 +00002149 // vastart just stores the address of the VarArgsFrameIndex slot into the
2150 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002151 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002152 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002153 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002154 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002155 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2156 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002157}
2158
Dan Gohman475871a2008-07-27 21:46:04 +00002159SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002160ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2161 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002162 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002163 MachineFunction &MF = DAG.getMachineFunction();
2164 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2165
2166 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002167 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002168 RC = ARM::tGPRRegisterClass;
2169 else
2170 RC = ARM::GPRRegisterClass;
2171
2172 // Transform the arguments stored in physical registers into virtual ones.
Jim Grosbach4725ca72010-09-08 03:54:02 +00002173 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002174 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002175
2176 SDValue ArgValue2;
2177 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002178 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002179 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002180
2181 // Create load node to retrieve arguments from the stack.
2182 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002183 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002184 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002185 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002186 } else {
2187 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002188 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002189 }
2190
Jim Grosbache5165492009-11-09 00:11:35 +00002191 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002192}
2193
2194SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002195ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002196 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197 const SmallVectorImpl<ISD::InputArg>
2198 &Ins,
2199 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002200 SmallVectorImpl<SDValue> &InVals)
2201 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202
Bob Wilson1f595bb2009-04-17 19:07:39 +00002203 MachineFunction &MF = DAG.getMachineFunction();
2204 MachineFrameInfo *MFI = MF.getFrameInfo();
2205
Bob Wilson1f595bb2009-04-17 19:07:39 +00002206 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2207
2208 // Assign locations to all of the incoming arguments.
2209 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002210 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2211 *DAG.getContext());
2212 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002213 CCAssignFnForNode(CallConv, /* Return*/ false,
2214 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002215
2216 SmallVector<SDValue, 16> ArgValues;
2217
2218 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2219 CCValAssign &VA = ArgLocs[i];
2220
Bob Wilsondee46d72009-04-17 20:35:10 +00002221 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002222 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002223 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002224
Bob Wilson5bafff32009-06-22 23:27:02 +00002225 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002226 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002227 // f64 and vector types are split up into multiple registers or
2228 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002229 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002230 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002231 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002232 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002233 SDValue ArgValue2;
2234 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002235 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002236 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2237 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002238 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002239 false, false, 0);
2240 } else {
2241 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2242 Chain, DAG, dl);
2243 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002244 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2245 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002246 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002247 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002248 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2249 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002250 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002251
Bob Wilson5bafff32009-06-22 23:27:02 +00002252 } else {
2253 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002254
Owen Anderson825b72b2009-08-11 20:47:22 +00002255 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002256 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002257 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002258 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002259 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002260 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002261 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002262 RC = (AFI->isThumb1OnlyFunction() ?
2263 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002264 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002265 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002266
2267 // Transform the arguments in physical registers into virtual ones.
2268 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002269 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002270 }
2271
2272 // If this is an 8 or 16-bit value, it is really passed promoted
2273 // to 32 bits. Insert an assert[sz]ext to capture this, then
2274 // truncate to the right size.
2275 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002276 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002277 case CCValAssign::Full: break;
2278 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002279 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002280 break;
2281 case CCValAssign::SExt:
2282 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2283 DAG.getValueType(VA.getValVT()));
2284 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2285 break;
2286 case CCValAssign::ZExt:
2287 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2288 DAG.getValueType(VA.getValVT()));
2289 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2290 break;
2291 }
2292
Dan Gohman98ca4f22009-08-05 01:29:28 +00002293 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002294
2295 } else { // VA.isRegLoc()
2296
2297 // sanity check
2298 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002299 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002300
2301 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002302 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002303
Bob Wilsondee46d72009-04-17 20:35:10 +00002304 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002305 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002306 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002307 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002308 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002309 }
2310 }
2311
2312 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002313 if (isVarArg) {
2314 static const unsigned GPRArgRegs[] = {
2315 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2316 };
2317
Bob Wilsondee46d72009-04-17 20:35:10 +00002318 unsigned NumGPRs = CCInfo.getFirstUnallocated
2319 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002320
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002321 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002322 unsigned VARegSize = (4 - NumGPRs) * 4;
2323 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002324 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002325 if (VARegSaveSize) {
2326 // If this function is vararg, store any remaining integer argument regs
2327 // to their spots on the stack so that they may be loaded by deferencing
2328 // the result of va_next.
2329 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002330 AFI->setVarArgsFrameIndex(
2331 MFI->CreateFixedObject(VARegSaveSize,
2332 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002333 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002334 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2335 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002336
Dan Gohman475871a2008-07-27 21:46:04 +00002337 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002338 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002339 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002340 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002341 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002342 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002343 RC = ARM::GPRRegisterClass;
2344
Bob Wilson998e1252009-04-20 18:36:57 +00002345 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002346 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002347 SDValue Store =
2348 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002349 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2350 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002351 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002352 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002353 DAG.getConstant(4, getPointerTy()));
2354 }
2355 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002357 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002358 } else
2359 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002360 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002361 }
2362
Dan Gohman98ca4f22009-08-05 01:29:28 +00002363 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002364}
2365
2366/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002367static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002368 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002369 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002370 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002371 // Maybe this has already been legalized into the constant pool?
2372 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002373 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002374 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002375 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002376 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002377 }
2378 }
2379 return false;
2380}
2381
Evan Chenga8e29892007-01-19 07:51:42 +00002382/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2383/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002384SDValue
2385ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002386 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002387 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002388 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002389 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002390 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002391 // Constant does not fit, try adjusting it by one?
2392 switch (CC) {
2393 default: break;
2394 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002395 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002396 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002397 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002398 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002399 }
2400 break;
2401 case ISD::SETULT:
2402 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002403 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002404 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002405 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002406 }
2407 break;
2408 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002409 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002410 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002411 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002412 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002413 }
2414 break;
2415 case ISD::SETULE:
2416 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002417 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002418 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002419 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002420 }
2421 break;
2422 }
2423 }
2424 }
2425
2426 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002427 ARMISD::NodeType CompareType;
2428 switch (CondCode) {
2429 default:
2430 CompareType = ARMISD::CMP;
2431 break;
2432 case ARMCC::EQ:
2433 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002434 // Uses only Z Flag
2435 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002436 break;
2437 }
Evan Cheng218977b2010-07-13 19:27:42 +00002438 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002439 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002440}
2441
2442/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002443SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002444ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002445 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002446 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002447 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002448 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002449 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002450 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2451 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002452}
2453
Bill Wendlingde2b1512010-08-11 08:43:16 +00002454SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2455 SDValue Cond = Op.getOperand(0);
2456 SDValue SelectTrue = Op.getOperand(1);
2457 SDValue SelectFalse = Op.getOperand(2);
2458 DebugLoc dl = Op.getDebugLoc();
2459
2460 // Convert:
2461 //
2462 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2463 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2464 //
2465 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2466 const ConstantSDNode *CMOVTrue =
2467 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2468 const ConstantSDNode *CMOVFalse =
2469 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2470
2471 if (CMOVTrue && CMOVFalse) {
2472 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2473 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2474
2475 SDValue True;
2476 SDValue False;
2477 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2478 True = SelectTrue;
2479 False = SelectFalse;
2480 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2481 True = SelectFalse;
2482 False = SelectTrue;
2483 }
2484
2485 if (True.getNode() && False.getNode()) {
2486 EVT VT = Cond.getValueType();
2487 SDValue ARMcc = Cond.getOperand(2);
2488 SDValue CCR = Cond.getOperand(3);
2489 SDValue Cmp = Cond.getOperand(4);
2490 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2491 }
2492 }
2493 }
2494
2495 return DAG.getSelectCC(dl, Cond,
2496 DAG.getConstant(0, Cond.getValueType()),
2497 SelectTrue, SelectFalse, ISD::SETNE);
2498}
2499
Dan Gohmand858e902010-04-17 15:26:15 +00002500SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002501 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002502 SDValue LHS = Op.getOperand(0);
2503 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002504 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002505 SDValue TrueVal = Op.getOperand(2);
2506 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002507 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002508
Owen Anderson825b72b2009-08-11 20:47:22 +00002509 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002510 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002512 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2513 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002514 }
2515
2516 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002517 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002518
Evan Cheng218977b2010-07-13 19:27:42 +00002519 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2520 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002521 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002522 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002523 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002524 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002525 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002526 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002527 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002528 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002529 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002530 }
2531 return Result;
2532}
2533
Evan Cheng218977b2010-07-13 19:27:42 +00002534/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2535/// to morph to an integer compare sequence.
2536static bool canChangeToInt(SDValue Op, bool &SeenZero,
2537 const ARMSubtarget *Subtarget) {
2538 SDNode *N = Op.getNode();
2539 if (!N->hasOneUse())
2540 // Otherwise it requires moving the value from fp to integer registers.
2541 return false;
2542 if (!N->getNumValues())
2543 return false;
2544 EVT VT = Op.getValueType();
2545 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2546 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2547 // vmrs are very slow, e.g. cortex-a8.
2548 return false;
2549
2550 if (isFloatingPointZero(Op)) {
2551 SeenZero = true;
2552 return true;
2553 }
2554 return ISD::isNormalLoad(N);
2555}
2556
2557static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2558 if (isFloatingPointZero(Op))
2559 return DAG.getConstant(0, MVT::i32);
2560
2561 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2562 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002563 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002564 Ld->isVolatile(), Ld->isNonTemporal(),
2565 Ld->getAlignment());
2566
2567 llvm_unreachable("Unknown VFP cmp argument!");
2568}
2569
2570static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2571 SDValue &RetVal1, SDValue &RetVal2) {
2572 if (isFloatingPointZero(Op)) {
2573 RetVal1 = DAG.getConstant(0, MVT::i32);
2574 RetVal2 = DAG.getConstant(0, MVT::i32);
2575 return;
2576 }
2577
2578 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2579 SDValue Ptr = Ld->getBasePtr();
2580 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2581 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002582 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002583 Ld->isVolatile(), Ld->isNonTemporal(),
2584 Ld->getAlignment());
2585
2586 EVT PtrType = Ptr.getValueType();
2587 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2588 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2589 PtrType, Ptr, DAG.getConstant(4, PtrType));
2590 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2591 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002592 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002593 Ld->isVolatile(), Ld->isNonTemporal(),
2594 NewAlign);
2595 return;
2596 }
2597
2598 llvm_unreachable("Unknown VFP cmp argument!");
2599}
2600
2601/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2602/// f32 and even f64 comparisons to integer ones.
2603SDValue
2604ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2605 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002606 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002607 SDValue LHS = Op.getOperand(2);
2608 SDValue RHS = Op.getOperand(3);
2609 SDValue Dest = Op.getOperand(4);
2610 DebugLoc dl = Op.getDebugLoc();
2611
2612 bool SeenZero = false;
2613 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2614 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002615 // If one of the operand is zero, it's safe to ignore the NaN case since
2616 // we only care about equality comparisons.
2617 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002618 // If unsafe fp math optimization is enabled and there are no othter uses of
2619 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2620 // to an integer comparison.
2621 if (CC == ISD::SETOEQ)
2622 CC = ISD::SETEQ;
2623 else if (CC == ISD::SETUNE)
2624 CC = ISD::SETNE;
2625
2626 SDValue ARMcc;
2627 if (LHS.getValueType() == MVT::f32) {
2628 LHS = bitcastf32Toi32(LHS, DAG);
2629 RHS = bitcastf32Toi32(RHS, DAG);
2630 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2631 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2632 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2633 Chain, Dest, ARMcc, CCR, Cmp);
2634 }
2635
2636 SDValue LHS1, LHS2;
2637 SDValue RHS1, RHS2;
2638 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2639 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2640 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2641 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002642 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002643 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2644 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2645 }
2646
2647 return SDValue();
2648}
2649
2650SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2651 SDValue Chain = Op.getOperand(0);
2652 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2653 SDValue LHS = Op.getOperand(2);
2654 SDValue RHS = Op.getOperand(3);
2655 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002656 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002657
Owen Anderson825b72b2009-08-11 20:47:22 +00002658 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002659 SDValue ARMcc;
2660 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002661 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002662 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002663 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002664 }
2665
Owen Anderson825b72b2009-08-11 20:47:22 +00002666 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002667
2668 if (UnsafeFPMath &&
2669 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2670 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2671 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2672 if (Result.getNode())
2673 return Result;
2674 }
2675
Evan Chenga8e29892007-01-19 07:51:42 +00002676 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002677 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002678
Evan Cheng218977b2010-07-13 19:27:42 +00002679 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2680 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002681 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002682 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002683 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002684 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002685 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002686 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2687 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002688 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002689 }
2690 return Res;
2691}
2692
Dan Gohmand858e902010-04-17 15:26:15 +00002693SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002694 SDValue Chain = Op.getOperand(0);
2695 SDValue Table = Op.getOperand(1);
2696 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002697 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002698
Owen Andersone50ed302009-08-10 22:56:29 +00002699 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002700 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2701 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002702 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002703 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002704 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002705 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2706 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002707 if (Subtarget->isThumb2()) {
2708 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2709 // which does another jump to the destination. This also makes it easier
2710 // to translate it to TBB / TBH later.
2711 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002712 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002713 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002714 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002715 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002716 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002717 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002718 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002719 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002720 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002721 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002722 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002723 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002724 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002725 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002726 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002727 }
Evan Chenga8e29892007-01-19 07:51:42 +00002728}
2729
Bob Wilson76a312b2010-03-19 22:51:32 +00002730static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2731 DebugLoc dl = Op.getDebugLoc();
2732 unsigned Opc;
2733
2734 switch (Op.getOpcode()) {
2735 default:
2736 assert(0 && "Invalid opcode!");
2737 case ISD::FP_TO_SINT:
2738 Opc = ARMISD::FTOSI;
2739 break;
2740 case ISD::FP_TO_UINT:
2741 Opc = ARMISD::FTOUI;
2742 break;
2743 }
2744 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002745 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002746}
2747
2748static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2749 EVT VT = Op.getValueType();
2750 DebugLoc dl = Op.getDebugLoc();
2751 unsigned Opc;
2752
2753 switch (Op.getOpcode()) {
2754 default:
2755 assert(0 && "Invalid opcode!");
2756 case ISD::SINT_TO_FP:
2757 Opc = ARMISD::SITOF;
2758 break;
2759 case ISD::UINT_TO_FP:
2760 Opc = ARMISD::UITOF;
2761 break;
2762 }
2763
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002764 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002765 return DAG.getNode(Opc, dl, VT, Op);
2766}
2767
Evan Cheng515fe3a2010-07-08 02:08:50 +00002768SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002769 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002770 SDValue Tmp0 = Op.getOperand(0);
2771 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002772 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002773 EVT VT = Op.getValueType();
2774 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002775 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002776 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002777 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002778 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002779 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002780 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002781}
2782
Evan Cheng2457f2c2010-05-22 01:47:14 +00002783SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2784 MachineFunction &MF = DAG.getMachineFunction();
2785 MachineFrameInfo *MFI = MF.getFrameInfo();
2786 MFI->setReturnAddressIsTaken(true);
2787
2788 EVT VT = Op.getValueType();
2789 DebugLoc dl = Op.getDebugLoc();
2790 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2791 if (Depth) {
2792 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2793 SDValue Offset = DAG.getConstant(4, MVT::i32);
2794 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2795 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002796 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002797 }
2798
2799 // Return LR, which contains the return address. Mark it an implicit live-in.
Jim Grosbachc2723a52010-07-23 23:50:35 +00002800 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00002801 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2802}
2803
Dan Gohmand858e902010-04-17 15:26:15 +00002804SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002805 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2806 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002807
Owen Andersone50ed302009-08-10 22:56:29 +00002808 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002809 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2810 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002811 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002812 ? ARM::R7 : ARM::R11;
2813 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2814 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002815 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
2816 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00002817 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002818 return FrameAddr;
2819}
2820
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002821/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00002822/// expand a bit convert where either the source or destination type is i64 to
2823/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2824/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2825/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002826static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002827 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2828 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002829 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002830
Bob Wilson9f3f0612010-04-17 05:30:19 +00002831 // This function is only supposed to be called for i64 types, either as the
2832 // source or destination of the bit convert.
2833 EVT SrcVT = Op.getValueType();
2834 EVT DstVT = N->getValueType(0);
2835 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002836 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002837
Bob Wilson9f3f0612010-04-17 05:30:19 +00002838 // Turn i64->f64 into VMOVDRR.
2839 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002840 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2841 DAG.getConstant(0, MVT::i32));
2842 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2843 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002844 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00002845 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002846 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002847
Jim Grosbache5165492009-11-09 00:11:35 +00002848 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002849 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2850 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2851 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2852 // Merge the pieces into a single i64 value.
2853 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2854 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002855
Bob Wilson9f3f0612010-04-17 05:30:19 +00002856 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002857}
2858
Bob Wilson5bafff32009-06-22 23:27:02 +00002859/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002860/// Zero vectors are used to represent vector negation and in those cases
2861/// will be implemented with the NEON VNEG instruction. However, VNEG does
2862/// not support i64 elements, so sometimes the zero vectors will need to be
2863/// explicitly constructed. Regardless, use a canonical VMOV to create the
2864/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002865static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002866 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002867 // The canonical modified immediate encoding of a zero vector is....0!
2868 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2869 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2870 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002871 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002872}
2873
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002874/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2875/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002876SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2877 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002878 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2879 EVT VT = Op.getValueType();
2880 unsigned VTBits = VT.getSizeInBits();
2881 DebugLoc dl = Op.getDebugLoc();
2882 SDValue ShOpLo = Op.getOperand(0);
2883 SDValue ShOpHi = Op.getOperand(1);
2884 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002885 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002886 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002887
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002888 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2889
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002890 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2891 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2892 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2893 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2894 DAG.getConstant(VTBits, MVT::i32));
2895 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2896 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002897 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002898
2899 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2900 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002901 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002902 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002903 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002904 CCR, Cmp);
2905
2906 SDValue Ops[2] = { Lo, Hi };
2907 return DAG.getMergeValues(Ops, 2, dl);
2908}
2909
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002910/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2911/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002912SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2913 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002914 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2915 EVT VT = Op.getValueType();
2916 unsigned VTBits = VT.getSizeInBits();
2917 DebugLoc dl = Op.getDebugLoc();
2918 SDValue ShOpLo = Op.getOperand(0);
2919 SDValue ShOpHi = Op.getOperand(1);
2920 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002921 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002922
2923 assert(Op.getOpcode() == ISD::SHL_PARTS);
2924 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2925 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2926 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2927 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2928 DAG.getConstant(VTBits, MVT::i32));
2929 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2930 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2931
2932 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2933 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2934 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002935 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002936 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002937 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002938 CCR, Cmp);
2939
2940 SDValue Ops[2] = { Lo, Hi };
2941 return DAG.getMergeValues(Ops, 2, dl);
2942}
2943
Jim Grosbach4725ca72010-09-08 03:54:02 +00002944SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00002945 SelectionDAG &DAG) const {
2946 // The rounding mode is in bits 23:22 of the FPSCR.
2947 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
2948 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
2949 // so that the shift + and get folded into a bitfield extract.
2950 DebugLoc dl = Op.getDebugLoc();
2951 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
2952 DAG.getConstant(Intrinsic::arm_get_fpscr,
2953 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00002954 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00002955 DAG.getConstant(1U << 22, MVT::i32));
2956 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
2957 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00002958 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00002959 DAG.getConstant(3, MVT::i32));
2960}
2961
Jim Grosbach3482c802010-01-18 19:58:49 +00002962static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2963 const ARMSubtarget *ST) {
2964 EVT VT = N->getValueType(0);
2965 DebugLoc dl = N->getDebugLoc();
2966
2967 if (!ST->hasV6T2Ops())
2968 return SDValue();
2969
2970 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2971 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2972}
2973
Bob Wilson5bafff32009-06-22 23:27:02 +00002974static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2975 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002976 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002977 DebugLoc dl = N->getDebugLoc();
2978
Bob Wilsond5448bb2010-11-18 21:16:28 +00002979 if (!VT.isVector())
2980 return SDValue();
2981
Bob Wilson5bafff32009-06-22 23:27:02 +00002982 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00002983 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00002984
Bob Wilsond5448bb2010-11-18 21:16:28 +00002985 // Left shifts translate directly to the vshiftu intrinsic.
2986 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00002987 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00002988 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
2989 N->getOperand(0), N->getOperand(1));
2990
2991 assert((N->getOpcode() == ISD::SRA ||
2992 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2993
2994 // NEON uses the same intrinsics for both left and right shifts. For
2995 // right shifts, the shift amounts are negative, so negate the vector of
2996 // shift amounts.
2997 EVT ShiftVT = N->getOperand(1).getValueType();
2998 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2999 getZeroVector(ShiftVT, DAG, dl),
3000 N->getOperand(1));
3001 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3002 Intrinsic::arm_neon_vshifts :
3003 Intrinsic::arm_neon_vshiftu);
3004 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3005 DAG.getConstant(vshiftInt, MVT::i32),
3006 N->getOperand(0), NegatedCount);
3007}
3008
3009static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3010 const ARMSubtarget *ST) {
3011 EVT VT = N->getValueType(0);
3012 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003013
Eli Friedmance392eb2009-08-22 03:13:10 +00003014 // We can get here for a node like i32 = ISD::SHL i32, i64
3015 if (VT != MVT::i64)
3016 return SDValue();
3017
3018 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003019 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003020
Chris Lattner27a6c732007-11-24 07:07:01 +00003021 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3022 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003023 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003024 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003025
Chris Lattner27a6c732007-11-24 07:07:01 +00003026 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003027 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003028
Chris Lattner27a6c732007-11-24 07:07:01 +00003029 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003030 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003031 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003032 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003033 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003034
Chris Lattner27a6c732007-11-24 07:07:01 +00003035 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3036 // captures the result into a carry flag.
3037 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003038 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003039
Chris Lattner27a6c732007-11-24 07:07:01 +00003040 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003041 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003042
Chris Lattner27a6c732007-11-24 07:07:01 +00003043 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003044 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003045}
3046
Bob Wilson5bafff32009-06-22 23:27:02 +00003047static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3048 SDValue TmpOp0, TmpOp1;
3049 bool Invert = false;
3050 bool Swap = false;
3051 unsigned Opc = 0;
3052
3053 SDValue Op0 = Op.getOperand(0);
3054 SDValue Op1 = Op.getOperand(1);
3055 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003056 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003057 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3058 DebugLoc dl = Op.getDebugLoc();
3059
3060 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3061 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003062 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003063 case ISD::SETUNE:
3064 case ISD::SETNE: Invert = true; // Fallthrough
3065 case ISD::SETOEQ:
3066 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3067 case ISD::SETOLT:
3068 case ISD::SETLT: Swap = true; // Fallthrough
3069 case ISD::SETOGT:
3070 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3071 case ISD::SETOLE:
3072 case ISD::SETLE: Swap = true; // Fallthrough
3073 case ISD::SETOGE:
3074 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3075 case ISD::SETUGE: Swap = true; // Fallthrough
3076 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3077 case ISD::SETUGT: Swap = true; // Fallthrough
3078 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3079 case ISD::SETUEQ: Invert = true; // Fallthrough
3080 case ISD::SETONE:
3081 // Expand this to (OLT | OGT).
3082 TmpOp0 = Op0;
3083 TmpOp1 = Op1;
3084 Opc = ISD::OR;
3085 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3086 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3087 break;
3088 case ISD::SETUO: Invert = true; // Fallthrough
3089 case ISD::SETO:
3090 // Expand this to (OLT | OGE).
3091 TmpOp0 = Op0;
3092 TmpOp1 = Op1;
3093 Opc = ISD::OR;
3094 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3095 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3096 break;
3097 }
3098 } else {
3099 // Integer comparisons.
3100 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003101 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003102 case ISD::SETNE: Invert = true;
3103 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3104 case ISD::SETLT: Swap = true;
3105 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3106 case ISD::SETLE: Swap = true;
3107 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3108 case ISD::SETULT: Swap = true;
3109 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3110 case ISD::SETULE: Swap = true;
3111 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3112 }
3113
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003114 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003115 if (Opc == ARMISD::VCEQ) {
3116
3117 SDValue AndOp;
3118 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3119 AndOp = Op0;
3120 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3121 AndOp = Op1;
3122
3123 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003124 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003125 AndOp = AndOp.getOperand(0);
3126
3127 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3128 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003129 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3130 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003131 Invert = !Invert;
3132 }
3133 }
3134 }
3135
3136 if (Swap)
3137 std::swap(Op0, Op1);
3138
Owen Andersonc24cb352010-11-08 23:21:22 +00003139 // If one of the operands is a constant vector zero, attempt to fold the
3140 // comparison to a specialized compare-against-zero form.
3141 SDValue SingleOp;
3142 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3143 SingleOp = Op0;
3144 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3145 if (Opc == ARMISD::VCGE)
3146 Opc = ARMISD::VCLEZ;
3147 else if (Opc == ARMISD::VCGT)
3148 Opc = ARMISD::VCLTZ;
3149 SingleOp = Op1;
3150 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003151
Owen Andersonc24cb352010-11-08 23:21:22 +00003152 SDValue Result;
3153 if (SingleOp.getNode()) {
3154 switch (Opc) {
3155 case ARMISD::VCEQ:
3156 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3157 case ARMISD::VCGE:
3158 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3159 case ARMISD::VCLEZ:
3160 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3161 case ARMISD::VCGT:
3162 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3163 case ARMISD::VCLTZ:
3164 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3165 default:
3166 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3167 }
3168 } else {
3169 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3170 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003171
3172 if (Invert)
3173 Result = DAG.getNOT(dl, Result, VT);
3174
3175 return Result;
3176}
3177
Bob Wilsond3c42842010-06-14 22:19:57 +00003178/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3179/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003180/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003181static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3182 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003183 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003184 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003185
Bob Wilson827b2102010-06-15 19:05:35 +00003186 // SplatBitSize is set to the smallest size that splats the vector, so a
3187 // zero vector will always have SplatBitSize == 8. However, NEON modified
3188 // immediate instructions others than VMOV do not support the 8-bit encoding
3189 // of a zero vector, and the default encoding of zero is supposed to be the
3190 // 32-bit version.
3191 if (SplatBits == 0)
3192 SplatBitSize = 32;
3193
Bob Wilson5bafff32009-06-22 23:27:02 +00003194 switch (SplatBitSize) {
3195 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003196 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003197 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003198 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003199 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003200 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003201 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003202 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003203 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003204
3205 case 16:
3206 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003207 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003208 if ((SplatBits & ~0xff) == 0) {
3209 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003210 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003211 Imm = SplatBits;
3212 break;
3213 }
3214 if ((SplatBits & ~0xff00) == 0) {
3215 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003216 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003217 Imm = SplatBits >> 8;
3218 break;
3219 }
3220 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003221
3222 case 32:
3223 // NEON's 32-bit VMOV supports splat values where:
3224 // * only one byte is nonzero, or
3225 // * the least significant byte is 0xff and the second byte is nonzero, or
3226 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003227 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003228 if ((SplatBits & ~0xff) == 0) {
3229 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003230 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003231 Imm = SplatBits;
3232 break;
3233 }
3234 if ((SplatBits & ~0xff00) == 0) {
3235 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003236 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003237 Imm = SplatBits >> 8;
3238 break;
3239 }
3240 if ((SplatBits & ~0xff0000) == 0) {
3241 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003242 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003243 Imm = SplatBits >> 16;
3244 break;
3245 }
3246 if ((SplatBits & ~0xff000000) == 0) {
3247 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003248 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003249 Imm = SplatBits >> 24;
3250 break;
3251 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003252
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003253 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3254 if (type == OtherModImm) return SDValue();
3255
Bob Wilson5bafff32009-06-22 23:27:02 +00003256 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003257 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3258 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003259 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003260 Imm = SplatBits >> 8;
3261 SplatBits |= 0xff;
3262 break;
3263 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003264
3265 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003266 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3267 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003268 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003269 Imm = SplatBits >> 16;
3270 SplatBits |= 0xffff;
3271 break;
3272 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003273
3274 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3275 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3276 // VMOV.I32. A (very) minor optimization would be to replicate the value
3277 // and fall through here to test for a valid 64-bit splat. But, then the
3278 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003279 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003280
3281 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003282 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003283 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003284 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003285 uint64_t BitMask = 0xff;
3286 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003287 unsigned ImmMask = 1;
3288 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003289 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003290 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003291 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003292 Imm |= ImmMask;
3293 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003294 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003295 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003296 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003297 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003298 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003299 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003300 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003301 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003302 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003303 break;
3304 }
3305
Bob Wilson1a913ed2010-06-11 21:34:50 +00003306 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003307 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003308 return SDValue();
3309 }
3310
Bob Wilsoncba270d2010-07-13 21:16:48 +00003311 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3312 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003313}
3314
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003315static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3316 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003317 unsigned NumElts = VT.getVectorNumElements();
3318 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003319
3320 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3321 if (M[0] < 0)
3322 return false;
3323
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003324 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003325
3326 // If this is a VEXT shuffle, the immediate value is the index of the first
3327 // element. The other shuffle indices must be the successive elements after
3328 // the first one.
3329 unsigned ExpectedElt = Imm;
3330 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003331 // Increment the expected index. If it wraps around, it may still be
3332 // a VEXT but the source vectors must be swapped.
3333 ExpectedElt += 1;
3334 if (ExpectedElt == NumElts * 2) {
3335 ExpectedElt = 0;
3336 ReverseVEXT = true;
3337 }
3338
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003339 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003340 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003341 return false;
3342 }
3343
3344 // Adjust the index value if the source operands will be swapped.
3345 if (ReverseVEXT)
3346 Imm -= NumElts;
3347
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003348 return true;
3349}
3350
Bob Wilson8bb9e482009-07-26 00:39:34 +00003351/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3352/// instruction with the specified blocksize. (The order of the elements
3353/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003354static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3355 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003356 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3357 "Only possible block sizes for VREV are: 16, 32, 64");
3358
Bob Wilson8bb9e482009-07-26 00:39:34 +00003359 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003360 if (EltSz == 64)
3361 return false;
3362
3363 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003364 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003365 // If the first shuffle index is UNDEF, be optimistic.
3366 if (M[0] < 0)
3367 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003368
3369 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3370 return false;
3371
3372 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003373 if (M[i] < 0) continue; // ignore UNDEF indices
3374 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003375 return false;
3376 }
3377
3378 return true;
3379}
3380
Bob Wilsonc692cb72009-08-21 20:54:19 +00003381static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3382 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003383 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3384 if (EltSz == 64)
3385 return false;
3386
Bob Wilsonc692cb72009-08-21 20:54:19 +00003387 unsigned NumElts = VT.getVectorNumElements();
3388 WhichResult = (M[0] == 0 ? 0 : 1);
3389 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003390 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3391 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003392 return false;
3393 }
3394 return true;
3395}
3396
Bob Wilson324f4f12009-12-03 06:40:55 +00003397/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3398/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3399/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3400static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3401 unsigned &WhichResult) {
3402 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3403 if (EltSz == 64)
3404 return false;
3405
3406 unsigned NumElts = VT.getVectorNumElements();
3407 WhichResult = (M[0] == 0 ? 0 : 1);
3408 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003409 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3410 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003411 return false;
3412 }
3413 return true;
3414}
3415
Bob Wilsonc692cb72009-08-21 20:54:19 +00003416static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3417 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003418 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3419 if (EltSz == 64)
3420 return false;
3421
Bob Wilsonc692cb72009-08-21 20:54:19 +00003422 unsigned NumElts = VT.getVectorNumElements();
3423 WhichResult = (M[0] == 0 ? 0 : 1);
3424 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003425 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003426 if ((unsigned) M[i] != 2 * i + WhichResult)
3427 return false;
3428 }
3429
3430 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003431 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003432 return false;
3433
3434 return true;
3435}
3436
Bob Wilson324f4f12009-12-03 06:40:55 +00003437/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3438/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3439/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3440static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3441 unsigned &WhichResult) {
3442 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3443 if (EltSz == 64)
3444 return false;
3445
3446 unsigned Half = VT.getVectorNumElements() / 2;
3447 WhichResult = (M[0] == 0 ? 0 : 1);
3448 for (unsigned j = 0; j != 2; ++j) {
3449 unsigned Idx = WhichResult;
3450 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003451 int MIdx = M[i + j * Half];
3452 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003453 return false;
3454 Idx += 2;
3455 }
3456 }
3457
3458 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3459 if (VT.is64BitVector() && EltSz == 32)
3460 return false;
3461
3462 return true;
3463}
3464
Bob Wilsonc692cb72009-08-21 20:54:19 +00003465static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3466 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003467 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3468 if (EltSz == 64)
3469 return false;
3470
Bob Wilsonc692cb72009-08-21 20:54:19 +00003471 unsigned NumElts = VT.getVectorNumElements();
3472 WhichResult = (M[0] == 0 ? 0 : 1);
3473 unsigned Idx = WhichResult * NumElts / 2;
3474 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003475 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3476 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003477 return false;
3478 Idx += 1;
3479 }
3480
3481 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003482 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003483 return false;
3484
3485 return true;
3486}
3487
Bob Wilson324f4f12009-12-03 06:40:55 +00003488/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3489/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3490/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3491static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3492 unsigned &WhichResult) {
3493 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3494 if (EltSz == 64)
3495 return false;
3496
3497 unsigned NumElts = VT.getVectorNumElements();
3498 WhichResult = (M[0] == 0 ? 0 : 1);
3499 unsigned Idx = WhichResult * NumElts / 2;
3500 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003501 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3502 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003503 return false;
3504 Idx += 1;
3505 }
3506
3507 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3508 if (VT.is64BitVector() && EltSz == 32)
3509 return false;
3510
3511 return true;
3512}
3513
Dale Johannesenf630c712010-07-29 20:10:08 +00003514// If N is an integer constant that can be moved into a register in one
3515// instruction, return an SDValue of such a constant (will become a MOV
3516// instruction). Otherwise return null.
3517static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3518 const ARMSubtarget *ST, DebugLoc dl) {
3519 uint64_t Val;
3520 if (!isa<ConstantSDNode>(N))
3521 return SDValue();
3522 Val = cast<ConstantSDNode>(N)->getZExtValue();
3523
3524 if (ST->isThumb1Only()) {
3525 if (Val <= 255 || ~Val <= 255)
3526 return DAG.getConstant(Val, MVT::i32);
3527 } else {
3528 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3529 return DAG.getConstant(Val, MVT::i32);
3530 }
3531 return SDValue();
3532}
3533
Bob Wilson5bafff32009-06-22 23:27:02 +00003534// If this is a case we can't handle, return null and let the default
3535// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003536SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3537 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003538 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003539 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003540 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003541
3542 APInt SplatBits, SplatUndef;
3543 unsigned SplatBitSize;
3544 bool HasAnyUndefs;
3545 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003546 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003547 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003548 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003549 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003550 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003551 DAG, VmovVT, VT.is128BitVector(),
3552 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003553 if (Val.getNode()) {
3554 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003555 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003556 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003557
3558 // Try an immediate VMVN.
3559 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3560 ((1LL << SplatBitSize) - 1));
3561 Val = isNEONModifiedImm(NegatedImm,
3562 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003563 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003564 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003565 if (Val.getNode()) {
3566 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003567 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003568 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003569 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003570 }
3571
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003572 // Scan through the operands to see if only one value is used.
3573 unsigned NumElts = VT.getVectorNumElements();
3574 bool isOnlyLowElement = true;
3575 bool usesOnlyOneValue = true;
3576 bool isConstant = true;
3577 SDValue Value;
3578 for (unsigned i = 0; i < NumElts; ++i) {
3579 SDValue V = Op.getOperand(i);
3580 if (V.getOpcode() == ISD::UNDEF)
3581 continue;
3582 if (i > 0)
3583 isOnlyLowElement = false;
3584 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3585 isConstant = false;
3586
3587 if (!Value.getNode())
3588 Value = V;
3589 else if (V != Value)
3590 usesOnlyOneValue = false;
3591 }
3592
3593 if (!Value.getNode())
3594 return DAG.getUNDEF(VT);
3595
3596 if (isOnlyLowElement)
3597 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3598
Dale Johannesenf630c712010-07-29 20:10:08 +00003599 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3600
Dale Johannesen575cd142010-10-19 20:00:17 +00003601 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3602 // i32 and try again.
3603 if (usesOnlyOneValue && EltSize <= 32) {
3604 if (!isConstant)
3605 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3606 if (VT.getVectorElementType().isFloatingPoint()) {
3607 SmallVector<SDValue, 8> Ops;
3608 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003609 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003610 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003611 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3612 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003613 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3614 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003615 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003616 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003617 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3618 if (Val.getNode())
3619 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003620 }
3621
3622 // If all elements are constants and the case above didn't get hit, fall back
3623 // to the default expansion, which will generate a load from the constant
3624 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003625 if (isConstant)
3626 return SDValue();
3627
Bob Wilson11a1dff2011-01-07 21:37:30 +00003628 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3629 if (NumElts >= 4) {
3630 SDValue shuffle = ReconstructShuffle(Op, DAG);
3631 if (shuffle != SDValue())
3632 return shuffle;
3633 }
3634
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003635 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003636 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3637 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003638 if (EltSize >= 32) {
3639 // Do the expansion with floating-point types, since that is what the VFP
3640 // registers are defined to use, and since i64 is not legal.
3641 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3642 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003643 SmallVector<SDValue, 8> Ops;
3644 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003645 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003646 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003647 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003648 }
3649
3650 return SDValue();
3651}
3652
Bob Wilson11a1dff2011-01-07 21:37:30 +00003653// Gather data to see if the operation can be modelled as a
3654// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003655SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3656 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003657 DebugLoc dl = Op.getDebugLoc();
3658 EVT VT = Op.getValueType();
3659 unsigned NumElts = VT.getVectorNumElements();
3660
3661 SmallVector<SDValue, 2> SourceVecs;
3662 SmallVector<unsigned, 2> MinElts;
3663 SmallVector<unsigned, 2> MaxElts;
3664
3665 for (unsigned i = 0; i < NumElts; ++i) {
3666 SDValue V = Op.getOperand(i);
3667 if (V.getOpcode() == ISD::UNDEF)
3668 continue;
3669 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3670 // A shuffle can only come from building a vector from various
3671 // elements of other vectors.
3672 return SDValue();
3673 }
3674
3675 // Record this extraction against the appropriate vector if possible...
3676 SDValue SourceVec = V.getOperand(0);
3677 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3678 bool FoundSource = false;
3679 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3680 if (SourceVecs[j] == SourceVec) {
3681 if (MinElts[j] > EltNo)
3682 MinElts[j] = EltNo;
3683 if (MaxElts[j] < EltNo)
3684 MaxElts[j] = EltNo;
3685 FoundSource = true;
3686 break;
3687 }
3688 }
3689
3690 // Or record a new source if not...
3691 if (!FoundSource) {
3692 SourceVecs.push_back(SourceVec);
3693 MinElts.push_back(EltNo);
3694 MaxElts.push_back(EltNo);
3695 }
3696 }
3697
3698 // Currently only do something sane when at most two source vectors
3699 // involved.
3700 if (SourceVecs.size() > 2)
3701 return SDValue();
3702
3703 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3704 int VEXTOffsets[2] = {0, 0};
3705
3706 // This loop extracts the usage patterns of the source vectors
3707 // and prepares appropriate SDValues for a shuffle if possible.
3708 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3709 if (SourceVecs[i].getValueType() == VT) {
3710 // No VEXT necessary
3711 ShuffleSrcs[i] = SourceVecs[i];
3712 VEXTOffsets[i] = 0;
3713 continue;
3714 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3715 // It probably isn't worth padding out a smaller vector just to
3716 // break it down again in a shuffle.
3717 return SDValue();
3718 }
Bob Wilson11a1dff2011-01-07 21:37:30 +00003719
3720 // Since only 64-bit and 128-bit vectors are legal on ARM and
3721 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003722 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3723 "unexpected vector sizes in ReconstructShuffle");
Bob Wilson11a1dff2011-01-07 21:37:30 +00003724
3725 if (MaxElts[i] - MinElts[i] >= NumElts) {
3726 // Span too large for a VEXT to cope
3727 return SDValue();
3728 }
3729
3730 if (MinElts[i] >= NumElts) {
3731 // The extraction can just take the second half
3732 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003733 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3734 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003735 DAG.getIntPtrConstant(NumElts));
3736 } else if (MaxElts[i] < NumElts) {
3737 // The extraction can just take the first half
3738 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003739 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3740 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003741 DAG.getIntPtrConstant(0));
3742 } else {
3743 // An actual VEXT is needed
3744 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003745 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3746 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003747 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003748 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3749 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003750 DAG.getIntPtrConstant(NumElts));
3751 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3752 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3753 }
3754 }
3755
3756 SmallVector<int, 8> Mask;
3757
3758 for (unsigned i = 0; i < NumElts; ++i) {
3759 SDValue Entry = Op.getOperand(i);
3760 if (Entry.getOpcode() == ISD::UNDEF) {
3761 Mask.push_back(-1);
3762 continue;
3763 }
3764
3765 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00003766 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
3767 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00003768 if (ExtractVec == SourceVecs[0]) {
3769 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3770 } else {
3771 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3772 }
3773 }
3774
3775 // Final check before we try to produce nonsense...
3776 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00003777 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
3778 &Mask[0]);
Bob Wilson11a1dff2011-01-07 21:37:30 +00003779
3780 return SDValue();
3781}
3782
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003783/// isShuffleMaskLegal - Targets can use this to indicate that they only
3784/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3785/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3786/// are assumed to be legal.
3787bool
3788ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3789 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003790 if (VT.getVectorNumElements() == 4 &&
3791 (VT.is128BitVector() || VT.is64BitVector())) {
3792 unsigned PFIndexes[4];
3793 for (unsigned i = 0; i != 4; ++i) {
3794 if (M[i] < 0)
3795 PFIndexes[i] = 8;
3796 else
3797 PFIndexes[i] = M[i];
3798 }
3799
3800 // Compute the index in the perfect shuffle table.
3801 unsigned PFTableIndex =
3802 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3803 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3804 unsigned Cost = (PFEntry >> 30);
3805
3806 if (Cost <= 4)
3807 return true;
3808 }
3809
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003810 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003811 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003812
Bob Wilson53dd2452010-06-07 23:53:38 +00003813 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3814 return (EltSize >= 32 ||
3815 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003816 isVREVMask(M, VT, 64) ||
3817 isVREVMask(M, VT, 32) ||
3818 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003819 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3820 isVTRNMask(M, VT, WhichResult) ||
3821 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003822 isVZIPMask(M, VT, WhichResult) ||
3823 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3824 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3825 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003826}
3827
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003828/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3829/// the specified operations to build the shuffle.
3830static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3831 SDValue RHS, SelectionDAG &DAG,
3832 DebugLoc dl) {
3833 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3834 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3835 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3836
3837 enum {
3838 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3839 OP_VREV,
3840 OP_VDUP0,
3841 OP_VDUP1,
3842 OP_VDUP2,
3843 OP_VDUP3,
3844 OP_VEXT1,
3845 OP_VEXT2,
3846 OP_VEXT3,
3847 OP_VUZPL, // VUZP, left result
3848 OP_VUZPR, // VUZP, right result
3849 OP_VZIPL, // VZIP, left result
3850 OP_VZIPR, // VZIP, right result
3851 OP_VTRNL, // VTRN, left result
3852 OP_VTRNR // VTRN, right result
3853 };
3854
3855 if (OpNum == OP_COPY) {
3856 if (LHSID == (1*9+2)*9+3) return LHS;
3857 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3858 return RHS;
3859 }
3860
3861 SDValue OpLHS, OpRHS;
3862 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3863 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3864 EVT VT = OpLHS.getValueType();
3865
3866 switch (OpNum) {
3867 default: llvm_unreachable("Unknown shuffle opcode!");
3868 case OP_VREV:
3869 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3870 case OP_VDUP0:
3871 case OP_VDUP1:
3872 case OP_VDUP2:
3873 case OP_VDUP3:
3874 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003875 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003876 case OP_VEXT1:
3877 case OP_VEXT2:
3878 case OP_VEXT3:
3879 return DAG.getNode(ARMISD::VEXT, dl, VT,
3880 OpLHS, OpRHS,
3881 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3882 case OP_VUZPL:
3883 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003884 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003885 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3886 case OP_VZIPL:
3887 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003888 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003889 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3890 case OP_VTRNL:
3891 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003892 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3893 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003894 }
3895}
3896
Bob Wilson5bafff32009-06-22 23:27:02 +00003897static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003898 SDValue V1 = Op.getOperand(0);
3899 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003900 DebugLoc dl = Op.getDebugLoc();
3901 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003902 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003903 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003904
Bob Wilson28865062009-08-13 02:13:04 +00003905 // Convert shuffles that are directly supported on NEON to target-specific
3906 // DAG nodes, instead of keeping them as shuffles and matching them again
3907 // during code selection. This is more efficient and avoids the possibility
3908 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003909 // FIXME: floating-point vectors should be canonicalized to integer vectors
3910 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003911 SVN->getMask(ShuffleMask);
3912
Bob Wilson53dd2452010-06-07 23:53:38 +00003913 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3914 if (EltSize <= 32) {
3915 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3916 int Lane = SVN->getSplatIndex();
3917 // If this is undef splat, generate it via "just" vdup, if possible.
3918 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003919
Bob Wilson53dd2452010-06-07 23:53:38 +00003920 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3921 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3922 }
3923 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3924 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003925 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003926
3927 bool ReverseVEXT;
3928 unsigned Imm;
3929 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3930 if (ReverseVEXT)
3931 std::swap(V1, V2);
3932 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3933 DAG.getConstant(Imm, MVT::i32));
3934 }
3935
3936 if (isVREVMask(ShuffleMask, VT, 64))
3937 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3938 if (isVREVMask(ShuffleMask, VT, 32))
3939 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3940 if (isVREVMask(ShuffleMask, VT, 16))
3941 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3942
3943 // Check for Neon shuffles that modify both input vectors in place.
3944 // If both results are used, i.e., if there are two shuffles with the same
3945 // source operands and with masks corresponding to both results of one of
3946 // these operations, DAG memoization will ensure that a single node is
3947 // used for both shuffles.
3948 unsigned WhichResult;
3949 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3950 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3951 V1, V2).getValue(WhichResult);
3952 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3953 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3954 V1, V2).getValue(WhichResult);
3955 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3956 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3957 V1, V2).getValue(WhichResult);
3958
3959 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3960 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3961 V1, V1).getValue(WhichResult);
3962 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3963 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3964 V1, V1).getValue(WhichResult);
3965 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3966 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3967 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003968 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003969
Bob Wilsonc692cb72009-08-21 20:54:19 +00003970 // If the shuffle is not directly supported and it has 4 elements, use
3971 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003972 unsigned NumElts = VT.getVectorNumElements();
3973 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003974 unsigned PFIndexes[4];
3975 for (unsigned i = 0; i != 4; ++i) {
3976 if (ShuffleMask[i] < 0)
3977 PFIndexes[i] = 8;
3978 else
3979 PFIndexes[i] = ShuffleMask[i];
3980 }
3981
3982 // Compute the index in the perfect shuffle table.
3983 unsigned PFTableIndex =
3984 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003985 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3986 unsigned Cost = (PFEntry >> 30);
3987
3988 if (Cost <= 4)
3989 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3990 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003991
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003992 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003993 if (EltSize >= 32) {
3994 // Do the expansion with floating-point types, since that is what the VFP
3995 // registers are defined to use, and since i64 is not legal.
3996 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3997 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003998 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
3999 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004000 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004001 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004002 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004003 Ops.push_back(DAG.getUNDEF(EltVT));
4004 else
4005 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4006 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4007 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4008 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004009 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004010 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004011 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004012 }
4013
Bob Wilson22cac0d2009-08-14 05:16:33 +00004014 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004015}
4016
Bob Wilson5bafff32009-06-22 23:27:02 +00004017static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004018 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004019 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004020 if (!isa<ConstantSDNode>(Lane))
4021 return SDValue();
4022
4023 SDValue Vec = Op.getOperand(0);
4024 if (Op.getValueType() == MVT::i32 &&
4025 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4026 DebugLoc dl = Op.getDebugLoc();
4027 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4028 }
4029
4030 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004031}
4032
Bob Wilsona6d65862009-08-03 20:36:38 +00004033static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4034 // The only time a CONCAT_VECTORS operation can have legal types is when
4035 // two 64-bit vectors are concatenated to a 128-bit vector.
4036 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4037 "unexpected CONCAT_VECTORS");
4038 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004040 SDValue Op0 = Op.getOperand(0);
4041 SDValue Op1 = Op.getOperand(1);
4042 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004043 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004044 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004045 DAG.getIntPtrConstant(0));
4046 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004047 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004048 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004049 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004050 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004051}
4052
Bob Wilson626613d2010-11-23 19:38:38 +00004053/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4054/// element has been zero/sign-extended, depending on the isSigned parameter,
4055/// from an integer type half its size.
4056static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4057 bool isSigned) {
4058 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4059 EVT VT = N->getValueType(0);
4060 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4061 SDNode *BVN = N->getOperand(0).getNode();
4062 if (BVN->getValueType(0) != MVT::v4i32 ||
4063 BVN->getOpcode() != ISD::BUILD_VECTOR)
4064 return false;
4065 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4066 unsigned HiElt = 1 - LoElt;
4067 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4068 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4069 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4070 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4071 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4072 return false;
4073 if (isSigned) {
4074 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4075 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4076 return true;
4077 } else {
4078 if (Hi0->isNullValue() && Hi1->isNullValue())
4079 return true;
4080 }
4081 return false;
4082 }
4083
4084 if (N->getOpcode() != ISD::BUILD_VECTOR)
4085 return false;
4086
4087 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4088 SDNode *Elt = N->getOperand(i).getNode();
4089 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4090 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4091 unsigned HalfSize = EltSize / 2;
4092 if (isSigned) {
4093 int64_t SExtVal = C->getSExtValue();
4094 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4095 return false;
4096 } else {
4097 if ((C->getZExtValue() >> HalfSize) != 0)
4098 return false;
4099 }
4100 continue;
4101 }
4102 return false;
4103 }
4104
4105 return true;
4106}
4107
4108/// isSignExtended - Check if a node is a vector value that is sign-extended
4109/// or a constant BUILD_VECTOR with sign-extended elements.
4110static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4111 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4112 return true;
4113 if (isExtendedBUILD_VECTOR(N, DAG, true))
4114 return true;
4115 return false;
4116}
4117
4118/// isZeroExtended - Check if a node is a vector value that is zero-extended
4119/// or a constant BUILD_VECTOR with zero-extended elements.
4120static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4121 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4122 return true;
4123 if (isExtendedBUILD_VECTOR(N, DAG, false))
4124 return true;
4125 return false;
4126}
4127
4128/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4129/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004130static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4131 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4132 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004133 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4134 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4135 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4136 LD->isNonTemporal(), LD->getAlignment());
4137 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4138 // have been legalized as a BITCAST from v4i32.
4139 if (N->getOpcode() == ISD::BITCAST) {
4140 SDNode *BVN = N->getOperand(0).getNode();
4141 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4142 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4143 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4144 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4145 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4146 }
4147 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4148 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4149 EVT VT = N->getValueType(0);
4150 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4151 unsigned NumElts = VT.getVectorNumElements();
4152 MVT TruncVT = MVT::getIntegerVT(EltSize);
4153 SmallVector<SDValue, 8> Ops;
4154 for (unsigned i = 0; i != NumElts; ++i) {
4155 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4156 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004157 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004158 }
4159 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4160 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004161}
4162
4163static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4164 // Multiplications are only custom-lowered for 128-bit vectors so that
4165 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4166 EVT VT = Op.getValueType();
4167 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4168 SDNode *N0 = Op.getOperand(0).getNode();
4169 SDNode *N1 = Op.getOperand(1).getNode();
4170 unsigned NewOpc = 0;
Bob Wilson626613d2010-11-23 19:38:38 +00004171 if (isSignExtended(N0, DAG) && isSignExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004172 NewOpc = ARMISD::VMULLs;
Bob Wilson626613d2010-11-23 19:38:38 +00004173 else if (isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004174 NewOpc = ARMISD::VMULLu;
Bob Wilson626613d2010-11-23 19:38:38 +00004175 else if (VT == MVT::v2i64)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004176 // Fall through to expand this. It is not legal.
4177 return SDValue();
Bob Wilson626613d2010-11-23 19:38:38 +00004178 else
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004179 // Other vector multiplications are legal.
4180 return Op;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004181
4182 // Legalize to a VMULL instruction.
4183 DebugLoc DL = Op.getDebugLoc();
4184 SDValue Op0 = SkipExtension(N0, DAG);
4185 SDValue Op1 = SkipExtension(N1, DAG);
4186
4187 assert(Op0.getValueType().is64BitVector() &&
4188 Op1.getValueType().is64BitVector() &&
4189 "unexpected types for extended operands to VMULL");
4190 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4191}
4192
Dan Gohmand858e902010-04-17 15:26:15 +00004193SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004194 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004195 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004196 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004197 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004198 case ISD::GlobalAddress:
4199 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4200 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00004201 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004202 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004203 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4204 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004205 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004206 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004207 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004208 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004209 case ISD::SINT_TO_FP:
4210 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4211 case ISD::FP_TO_SINT:
4212 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004213 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004214 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004215 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004216 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004217 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004218 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004219 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004220 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4221 Subtarget);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004222 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004223 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004224 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004225 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004226 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004227 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004228 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004229 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004230 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004231 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004232 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004233 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004234 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004235 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004236 case ISD::MUL: return LowerMUL(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004237 }
Dan Gohman475871a2008-07-27 21:46:04 +00004238 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004239}
4240
Duncan Sands1607f052008-12-01 11:39:25 +00004241/// ReplaceNodeResults - Replace the results of node with an illegal result
4242/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004243void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4244 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004245 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004246 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004247 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004248 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004249 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004250 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004251 case ISD::BITCAST:
4252 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004253 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004254 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004255 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004256 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004257 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004258 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004259 if (Res.getNode())
4260 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004261}
Chris Lattner27a6c732007-11-24 07:07:01 +00004262
Evan Chenga8e29892007-01-19 07:51:42 +00004263//===----------------------------------------------------------------------===//
4264// ARM Scheduler Hooks
4265//===----------------------------------------------------------------------===//
4266
4267MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004268ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4269 MachineBasicBlock *BB,
4270 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004271 unsigned dest = MI->getOperand(0).getReg();
4272 unsigned ptr = MI->getOperand(1).getReg();
4273 unsigned oldval = MI->getOperand(2).getReg();
4274 unsigned newval = MI->getOperand(3).getReg();
4275 unsigned scratch = BB->getParent()->getRegInfo()
4276 .createVirtualRegister(ARM::GPRRegisterClass);
4277 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4278 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004279 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004280
4281 unsigned ldrOpc, strOpc;
4282 switch (Size) {
4283 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004284 case 1:
4285 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
4286 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
4287 break;
4288 case 2:
4289 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4290 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4291 break;
4292 case 4:
4293 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4294 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4295 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004296 }
4297
4298 MachineFunction *MF = BB->getParent();
4299 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4300 MachineFunction::iterator It = BB;
4301 ++It; // insert the new blocks after the current block
4302
4303 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4304 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4305 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4306 MF->insert(It, loop1MBB);
4307 MF->insert(It, loop2MBB);
4308 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004309
4310 // Transfer the remainder of BB and its successor edges to exitMBB.
4311 exitMBB->splice(exitMBB->begin(), BB,
4312 llvm::next(MachineBasicBlock::iterator(MI)),
4313 BB->end());
4314 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004315
4316 // thisMBB:
4317 // ...
4318 // fallthrough --> loop1MBB
4319 BB->addSuccessor(loop1MBB);
4320
4321 // loop1MBB:
4322 // ldrex dest, [ptr]
4323 // cmp dest, oldval
4324 // bne exitMBB
4325 BB = loop1MBB;
4326 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004327 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004328 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004329 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4330 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004331 BB->addSuccessor(loop2MBB);
4332 BB->addSuccessor(exitMBB);
4333
4334 // loop2MBB:
4335 // strex scratch, newval, [ptr]
4336 // cmp scratch, #0
4337 // bne loop1MBB
4338 BB = loop2MBB;
4339 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4340 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004341 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004342 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004343 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4344 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004345 BB->addSuccessor(loop1MBB);
4346 BB->addSuccessor(exitMBB);
4347
4348 // exitMBB:
4349 // ...
4350 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004351
Dan Gohman14152b42010-07-06 20:24:04 +00004352 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004353
Jim Grosbach5278eb82009-12-11 01:42:04 +00004354 return BB;
4355}
4356
4357MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004358ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4359 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004360 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4361 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4362
4363 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004364 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004365 MachineFunction::iterator It = BB;
4366 ++It;
4367
4368 unsigned dest = MI->getOperand(0).getReg();
4369 unsigned ptr = MI->getOperand(1).getReg();
4370 unsigned incr = MI->getOperand(2).getReg();
4371 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004372
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004373 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004374 unsigned ldrOpc, strOpc;
4375 switch (Size) {
4376 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004377 case 1:
4378 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004379 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004380 break;
4381 case 2:
4382 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4383 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4384 break;
4385 case 4:
4386 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4387 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4388 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004389 }
4390
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004391 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4392 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4393 MF->insert(It, loopMBB);
4394 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004395
4396 // Transfer the remainder of BB and its successor edges to exitMBB.
4397 exitMBB->splice(exitMBB->begin(), BB,
4398 llvm::next(MachineBasicBlock::iterator(MI)),
4399 BB->end());
4400 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004401
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004402 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004403 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4404 unsigned scratch2 = (!BinOpcode) ? incr :
4405 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4406
4407 // thisMBB:
4408 // ...
4409 // fallthrough --> loopMBB
4410 BB->addSuccessor(loopMBB);
4411
4412 // loopMBB:
4413 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004414 // <binop> scratch2, dest, incr
4415 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004416 // cmp scratch, #0
4417 // bne- loopMBB
4418 // fallthrough --> exitMBB
4419 BB = loopMBB;
4420 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004421 if (BinOpcode) {
4422 // operand order needs to go the other way for NAND
4423 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4424 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4425 addReg(incr).addReg(dest)).addReg(0);
4426 else
4427 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4428 addReg(dest).addReg(incr)).addReg(0);
4429 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004430
4431 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4432 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004433 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004434 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004435 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4436 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004437
4438 BB->addSuccessor(loopMBB);
4439 BB->addSuccessor(exitMBB);
4440
4441 // exitMBB:
4442 // ...
4443 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004444
Dan Gohman14152b42010-07-06 20:24:04 +00004445 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004446
Jim Grosbachc3c23542009-12-14 04:22:04 +00004447 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004448}
4449
Evan Cheng218977b2010-07-13 19:27:42 +00004450static
4451MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4452 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4453 E = MBB->succ_end(); I != E; ++I)
4454 if (*I != Succ)
4455 return *I;
4456 llvm_unreachable("Expecting a BB with two successors!");
4457}
4458
Jim Grosbache801dc42009-12-12 01:40:06 +00004459MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004460ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004461 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004462 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004463 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004464 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004465 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004466 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004467 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004468 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004469
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004470 case ARM::ATOMIC_LOAD_ADD_I8:
4471 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4472 case ARM::ATOMIC_LOAD_ADD_I16:
4473 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4474 case ARM::ATOMIC_LOAD_ADD_I32:
4475 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004476
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004477 case ARM::ATOMIC_LOAD_AND_I8:
4478 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4479 case ARM::ATOMIC_LOAD_AND_I16:
4480 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4481 case ARM::ATOMIC_LOAD_AND_I32:
4482 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004483
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004484 case ARM::ATOMIC_LOAD_OR_I8:
4485 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4486 case ARM::ATOMIC_LOAD_OR_I16:
4487 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4488 case ARM::ATOMIC_LOAD_OR_I32:
4489 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004490
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004491 case ARM::ATOMIC_LOAD_XOR_I8:
4492 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4493 case ARM::ATOMIC_LOAD_XOR_I16:
4494 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4495 case ARM::ATOMIC_LOAD_XOR_I32:
4496 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004497
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004498 case ARM::ATOMIC_LOAD_NAND_I8:
4499 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4500 case ARM::ATOMIC_LOAD_NAND_I16:
4501 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4502 case ARM::ATOMIC_LOAD_NAND_I32:
4503 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004504
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004505 case ARM::ATOMIC_LOAD_SUB_I8:
4506 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4507 case ARM::ATOMIC_LOAD_SUB_I16:
4508 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4509 case ARM::ATOMIC_LOAD_SUB_I32:
4510 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004511
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004512 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4513 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4514 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004515
4516 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4517 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4518 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004519
Evan Cheng007ea272009-08-12 05:17:19 +00004520 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004521 // To "insert" a SELECT_CC instruction, we actually have to insert the
4522 // diamond control-flow pattern. The incoming instruction knows the
4523 // destination vreg to set, the condition code register to branch on, the
4524 // true/false values to select between, and a branch opcode to use.
4525 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004526 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004527 ++It;
4528
4529 // thisMBB:
4530 // ...
4531 // TrueVal = ...
4532 // cmpTY ccX, r1, r2
4533 // bCC copy1MBB
4534 // fallthrough --> copy0MBB
4535 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004536 MachineFunction *F = BB->getParent();
4537 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4538 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004539 F->insert(It, copy0MBB);
4540 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004541
4542 // Transfer the remainder of BB and its successor edges to sinkMBB.
4543 sinkMBB->splice(sinkMBB->begin(), BB,
4544 llvm::next(MachineBasicBlock::iterator(MI)),
4545 BB->end());
4546 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4547
Dan Gohman258c58c2010-07-06 15:49:48 +00004548 BB->addSuccessor(copy0MBB);
4549 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004550
Dan Gohman14152b42010-07-06 20:24:04 +00004551 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4552 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4553
Evan Chenga8e29892007-01-19 07:51:42 +00004554 // copy0MBB:
4555 // %FalseValue = ...
4556 // # fallthrough to sinkMBB
4557 BB = copy0MBB;
4558
4559 // Update machine-CFG edges
4560 BB->addSuccessor(sinkMBB);
4561
4562 // sinkMBB:
4563 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4564 // ...
4565 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004566 BuildMI(*BB, BB->begin(), dl,
4567 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004568 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4569 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4570
Dan Gohman14152b42010-07-06 20:24:04 +00004571 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004572 return BB;
4573 }
Evan Cheng86198642009-08-07 00:34:42 +00004574
Evan Cheng218977b2010-07-13 19:27:42 +00004575 case ARM::BCCi64:
4576 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00004577 // If there is an unconditional branch to the other successor, remove it.
4578 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
4579
Evan Cheng218977b2010-07-13 19:27:42 +00004580 // Compare both parts that make up the double comparison separately for
4581 // equality.
4582 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4583
4584 unsigned LHS1 = MI->getOperand(1).getReg();
4585 unsigned LHS2 = MI->getOperand(2).getReg();
4586 if (RHSisZero) {
4587 AddDefaultPred(BuildMI(BB, dl,
4588 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4589 .addReg(LHS1).addImm(0));
4590 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4591 .addReg(LHS2).addImm(0)
4592 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4593 } else {
4594 unsigned RHS1 = MI->getOperand(3).getReg();
4595 unsigned RHS2 = MI->getOperand(4).getReg();
4596 AddDefaultPred(BuildMI(BB, dl,
4597 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4598 .addReg(LHS1).addReg(RHS1));
4599 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4600 .addReg(LHS2).addReg(RHS2)
4601 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4602 }
4603
4604 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4605 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4606 if (MI->getOperand(0).getImm() == ARMCC::NE)
4607 std::swap(destMBB, exitMBB);
4608
4609 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4610 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4611 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4612 .addMBB(exitMBB);
4613
4614 MI->eraseFromParent(); // The pseudo instruction is gone now.
4615 return BB;
4616 }
Evan Chenga8e29892007-01-19 07:51:42 +00004617 }
4618}
4619
4620//===----------------------------------------------------------------------===//
4621// ARM Optimization Hooks
4622//===----------------------------------------------------------------------===//
4623
Chris Lattnerd1980a52009-03-12 06:52:53 +00004624static
4625SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4626 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004627 SelectionDAG &DAG = DCI.DAG;
4628 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004629 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004630 unsigned Opc = N->getOpcode();
4631 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4632 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4633 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4634 ISD::CondCode CC = ISD::SETCC_INVALID;
4635
4636 if (isSlctCC) {
4637 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4638 } else {
4639 SDValue CCOp = Slct.getOperand(0);
4640 if (CCOp.getOpcode() == ISD::SETCC)
4641 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4642 }
4643
4644 bool DoXform = false;
4645 bool InvCC = false;
4646 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4647 "Bad input!");
4648
4649 if (LHS.getOpcode() == ISD::Constant &&
4650 cast<ConstantSDNode>(LHS)->isNullValue()) {
4651 DoXform = true;
4652 } else if (CC != ISD::SETCC_INVALID &&
4653 RHS.getOpcode() == ISD::Constant &&
4654 cast<ConstantSDNode>(RHS)->isNullValue()) {
4655 std::swap(LHS, RHS);
4656 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004657 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004658 Op0.getOperand(0).getValueType();
4659 bool isInt = OpVT.isInteger();
4660 CC = ISD::getSetCCInverse(CC, isInt);
4661
4662 if (!TLI.isCondCodeLegal(CC, OpVT))
4663 return SDValue(); // Inverse operator isn't legal.
4664
4665 DoXform = true;
4666 InvCC = true;
4667 }
4668
4669 if (DoXform) {
4670 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4671 if (isSlctCC)
4672 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4673 Slct.getOperand(0), Slct.getOperand(1), CC);
4674 SDValue CCOp = Slct.getOperand(0);
4675 if (InvCC)
4676 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4677 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4678 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4679 CCOp, OtherOp, Result);
4680 }
4681 return SDValue();
4682}
4683
Bob Wilson3d5792a2010-07-29 20:34:14 +00004684/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4685/// operands N0 and N1. This is a helper for PerformADDCombine that is
4686/// called with the default operands, and if that fails, with commuted
4687/// operands.
4688static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4689 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004690 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4691 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4692 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4693 if (Result.getNode()) return Result;
4694 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00004695 return SDValue();
4696}
4697
Bob Wilson3d5792a2010-07-29 20:34:14 +00004698/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4699///
4700static SDValue PerformADDCombine(SDNode *N,
4701 TargetLowering::DAGCombinerInfo &DCI) {
4702 SDValue N0 = N->getOperand(0);
4703 SDValue N1 = N->getOperand(1);
4704
4705 // First try with the default operand order.
4706 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4707 if (Result.getNode())
4708 return Result;
4709
4710 // If that didn't work, try again with the operands commuted.
4711 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4712}
4713
Chris Lattnerd1980a52009-03-12 06:52:53 +00004714/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004715///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004716static SDValue PerformSUBCombine(SDNode *N,
4717 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004718 SDValue N0 = N->getOperand(0);
4719 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004720
Chris Lattnerd1980a52009-03-12 06:52:53 +00004721 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4722 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4723 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4724 if (Result.getNode()) return Result;
4725 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004726
Chris Lattnerd1980a52009-03-12 06:52:53 +00004727 return SDValue();
4728}
4729
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004730static SDValue PerformMULCombine(SDNode *N,
4731 TargetLowering::DAGCombinerInfo &DCI,
4732 const ARMSubtarget *Subtarget) {
4733 SelectionDAG &DAG = DCI.DAG;
4734
4735 if (Subtarget->isThumb1Only())
4736 return SDValue();
4737
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004738 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4739 return SDValue();
4740
4741 EVT VT = N->getValueType(0);
4742 if (VT != MVT::i32)
4743 return SDValue();
4744
4745 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4746 if (!C)
4747 return SDValue();
4748
4749 uint64_t MulAmt = C->getZExtValue();
4750 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4751 ShiftAmt = ShiftAmt & (32 - 1);
4752 SDValue V = N->getOperand(0);
4753 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004754
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004755 SDValue Res;
4756 MulAmt >>= ShiftAmt;
4757 if (isPowerOf2_32(MulAmt - 1)) {
4758 // (mul x, 2^N + 1) => (add (shl x, N), x)
4759 Res = DAG.getNode(ISD::ADD, DL, VT,
4760 V, DAG.getNode(ISD::SHL, DL, VT,
4761 V, DAG.getConstant(Log2_32(MulAmt-1),
4762 MVT::i32)));
4763 } else if (isPowerOf2_32(MulAmt + 1)) {
4764 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4765 Res = DAG.getNode(ISD::SUB, DL, VT,
4766 DAG.getNode(ISD::SHL, DL, VT,
4767 V, DAG.getConstant(Log2_32(MulAmt+1),
4768 MVT::i32)),
4769 V);
4770 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004771 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004772
4773 if (ShiftAmt != 0)
4774 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4775 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004776
4777 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004778 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004779 return SDValue();
4780}
4781
Owen Anderson080c0922010-11-05 19:27:46 +00004782static SDValue PerformANDCombine(SDNode *N,
4783 TargetLowering::DAGCombinerInfo &DCI) {
4784 // Attempt to use immediate-form VBIC
4785 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4786 DebugLoc dl = N->getDebugLoc();
4787 EVT VT = N->getValueType(0);
4788 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004789
Owen Anderson080c0922010-11-05 19:27:46 +00004790 APInt SplatBits, SplatUndef;
4791 unsigned SplatBitSize;
4792 bool HasAnyUndefs;
4793 if (BVN &&
4794 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4795 if (SplatBitSize <= 64) {
4796 EVT VbicVT;
4797 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
4798 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004799 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004800 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00004801 if (Val.getNode()) {
4802 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004803 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00004804 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004805 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00004806 }
4807 }
4808 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004809
Owen Anderson080c0922010-11-05 19:27:46 +00004810 return SDValue();
4811}
4812
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004813/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4814static SDValue PerformORCombine(SDNode *N,
4815 TargetLowering::DAGCombinerInfo &DCI,
4816 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00004817 // Attempt to use immediate-form VORR
4818 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4819 DebugLoc dl = N->getDebugLoc();
4820 EVT VT = N->getValueType(0);
4821 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004822
Owen Anderson60f48702010-11-03 23:15:26 +00004823 APInt SplatBits, SplatUndef;
4824 unsigned SplatBitSize;
4825 bool HasAnyUndefs;
4826 if (BVN && Subtarget->hasNEON() &&
4827 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4828 if (SplatBitSize <= 64) {
4829 EVT VorrVT;
4830 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
4831 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004832 DAG, VorrVT, VT.is128BitVector(),
4833 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00004834 if (Val.getNode()) {
4835 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004836 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00004837 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004838 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00004839 }
4840 }
4841 }
4842
Jim Grosbach54238562010-07-17 03:30:54 +00004843 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4844 // reasonable.
4845
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004846 // BFI is only available on V6T2+
4847 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4848 return SDValue();
4849
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004850 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004851 DebugLoc DL = N->getDebugLoc();
4852 // 1) or (and A, mask), val => ARMbfi A, val, mask
4853 // iff (val & mask) == val
4854 //
4855 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4856 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4857 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4858 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4859 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4860 // (i.e., copy a bitfield value into another bitfield of the same width)
4861 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004862 return SDValue();
4863
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004864 if (VT != MVT::i32)
4865 return SDValue();
4866
Evan Cheng30fb13f2010-12-13 20:32:54 +00004867 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00004868
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004869 // The value and the mask need to be constants so we can verify this is
4870 // actually a bitfield set. If the mask is 0xffff, we can do better
4871 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00004872 SDValue MaskOp = N0.getOperand(1);
4873 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
4874 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004875 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004876 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004877 if (Mask == 0xffff)
4878 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004879 SDValue Res;
4880 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004881 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4882 if (N1C) {
4883 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004884 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00004885 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004886
Evan Chenga9688c42010-12-11 04:11:38 +00004887 if (ARM::isBitFieldInvertedMask(Mask)) {
4888 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004889
Evan Cheng30fb13f2010-12-13 20:32:54 +00004890 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00004891 DAG.getConstant(Val, MVT::i32),
4892 DAG.getConstant(Mask, MVT::i32));
4893
4894 // Do not add new nodes to DAG combiner worklist.
4895 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004896 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004897 }
Jim Grosbach54238562010-07-17 03:30:54 +00004898 } else if (N1.getOpcode() == ISD::AND) {
4899 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004900 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4901 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00004902 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004903 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004904
4905 if (ARM::isBitFieldInvertedMask(Mask) &&
4906 ARM::isBitFieldInvertedMask(~Mask2) &&
4907 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4908 // The pack halfword instruction works better for masks that fit it,
4909 // so use that when it's available.
4910 if (Subtarget->hasT2ExtractPack() &&
4911 (Mask == 0xffff || Mask == 0xffff0000))
4912 return SDValue();
4913 // 2a
4914 unsigned lsb = CountTrailingZeros_32(Mask2);
4915 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4916 DAG.getConstant(lsb, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00004917 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00004918 DAG.getConstant(Mask, MVT::i32));
4919 // Do not add new nodes to DAG combiner worklist.
4920 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004921 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004922 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4923 ARM::isBitFieldInvertedMask(Mask2) &&
4924 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4925 // The pack halfword instruction works better for masks that fit it,
4926 // so use that when it's available.
4927 if (Subtarget->hasT2ExtractPack() &&
4928 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4929 return SDValue();
4930 // 2b
4931 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004932 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00004933 DAG.getConstant(lsb, MVT::i32));
4934 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4935 DAG.getConstant(Mask2, MVT::i32));
4936 // Do not add new nodes to DAG combiner worklist.
4937 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004938 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004939 }
4940 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004941
Evan Cheng30fb13f2010-12-13 20:32:54 +00004942 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
4943 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
4944 ARM::isBitFieldInvertedMask(~Mask)) {
4945 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
4946 // where lsb(mask) == #shamt and masked bits of B are known zero.
4947 SDValue ShAmt = N00.getOperand(1);
4948 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
4949 unsigned LSB = CountTrailingZeros_32(Mask);
4950 if (ShAmtC != LSB)
4951 return SDValue();
4952
4953 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
4954 DAG.getConstant(~Mask, MVT::i32));
4955
4956 // Do not add new nodes to DAG combiner worklist.
4957 DCI.CombineTo(N, Res, false);
4958 }
4959
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004960 return SDValue();
4961}
4962
Evan Cheng0c1aec12010-12-14 03:22:07 +00004963/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
4964/// C1 & C2 == C1.
4965static SDValue PerformBFICombine(SDNode *N,
4966 TargetLowering::DAGCombinerInfo &DCI) {
4967 SDValue N1 = N->getOperand(1);
4968 if (N1.getOpcode() == ISD::AND) {
4969 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4970 if (!N11C)
4971 return SDValue();
4972 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
4973 unsigned Mask2 = N11C->getZExtValue();
4974 if ((Mask & Mask2) == Mask2)
4975 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
4976 N->getOperand(0), N1.getOperand(0),
4977 N->getOperand(2));
4978 }
4979 return SDValue();
4980}
4981
Bob Wilson0b8ccb82010-09-22 22:09:21 +00004982/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4983/// ARMISD::VMOVRRD.
4984static SDValue PerformVMOVRRDCombine(SDNode *N,
4985 TargetLowering::DAGCombinerInfo &DCI) {
4986 // vmovrrd(vmovdrr x, y) -> x,y
4987 SDValue InDouble = N->getOperand(0);
4988 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
4989 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
4990 return SDValue();
4991}
4992
4993/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
4994/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
4995static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
4996 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
4997 SDValue Op0 = N->getOperand(0);
4998 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004999 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005000 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005001 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005002 Op1 = Op1.getOperand(0);
5003 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5004 Op0.getNode() == Op1.getNode() &&
5005 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005006 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005007 N->getValueType(0), Op0.getOperand(0));
5008 return SDValue();
5009}
5010
Bob Wilson31600902010-12-21 06:43:19 +00005011/// PerformSTORECombine - Target-specific dag combine xforms for
5012/// ISD::STORE.
5013static SDValue PerformSTORECombine(SDNode *N,
5014 TargetLowering::DAGCombinerInfo &DCI) {
5015 // Bitcast an i64 store extracted from a vector to f64.
5016 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5017 StoreSDNode *St = cast<StoreSDNode>(N);
5018 SDValue StVal = St->getValue();
5019 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5020 StVal.getValueType() != MVT::i64 ||
5021 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5022 return SDValue();
5023
5024 SelectionDAG &DAG = DCI.DAG;
5025 DebugLoc dl = StVal.getDebugLoc();
5026 SDValue IntVec = StVal.getOperand(0);
5027 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5028 IntVec.getValueType().getVectorNumElements());
5029 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5030 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5031 Vec, StVal.getOperand(1));
5032 dl = N->getDebugLoc();
5033 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5034 // Make the DAGCombiner fold the bitcasts.
5035 DCI.AddToWorklist(Vec.getNode());
5036 DCI.AddToWorklist(ExtElt.getNode());
5037 DCI.AddToWorklist(V.getNode());
5038 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5039 St->getPointerInfo(), St->isVolatile(),
5040 St->isNonTemporal(), St->getAlignment(),
5041 St->getTBAAInfo());
5042}
5043
5044/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5045/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5046/// i64 vector to have f64 elements, since the value can then be loaded
5047/// directly into a VFP register.
5048static bool hasNormalLoadOperand(SDNode *N) {
5049 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5050 for (unsigned i = 0; i < NumElts; ++i) {
5051 SDNode *Elt = N->getOperand(i).getNode();
5052 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5053 return true;
5054 }
5055 return false;
5056}
5057
Bob Wilson75f02882010-09-17 22:59:05 +00005058/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5059/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005060static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5061 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005062 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5063 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5064 // into a pair of GPRs, which is fine when the value is used as a scalar,
5065 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005066 SelectionDAG &DAG = DCI.DAG;
5067 if (N->getNumOperands() == 2) {
5068 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5069 if (RV.getNode())
5070 return RV;
5071 }
Bob Wilson75f02882010-09-17 22:59:05 +00005072
Bob Wilson31600902010-12-21 06:43:19 +00005073 // Load i64 elements as f64 values so that type legalization does not split
5074 // them up into i32 values.
5075 EVT VT = N->getValueType(0);
5076 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5077 return SDValue();
5078 DebugLoc dl = N->getDebugLoc();
5079 SmallVector<SDValue, 8> Ops;
5080 unsigned NumElts = VT.getVectorNumElements();
5081 for (unsigned i = 0; i < NumElts; ++i) {
5082 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5083 Ops.push_back(V);
5084 // Make the DAGCombiner fold the bitcast.
5085 DCI.AddToWorklist(V.getNode());
5086 }
5087 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5088 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5089 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5090}
5091
5092/// PerformInsertEltCombine - Target-specific dag combine xforms for
5093/// ISD::INSERT_VECTOR_ELT.
5094static SDValue PerformInsertEltCombine(SDNode *N,
5095 TargetLowering::DAGCombinerInfo &DCI) {
5096 // Bitcast an i64 load inserted into a vector to f64.
5097 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5098 EVT VT = N->getValueType(0);
5099 SDNode *Elt = N->getOperand(1).getNode();
5100 if (VT.getVectorElementType() != MVT::i64 ||
5101 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5102 return SDValue();
5103
5104 SelectionDAG &DAG = DCI.DAG;
5105 DebugLoc dl = N->getDebugLoc();
5106 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5107 VT.getVectorNumElements());
5108 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5109 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5110 // Make the DAGCombiner fold the bitcasts.
5111 DCI.AddToWorklist(Vec.getNode());
5112 DCI.AddToWorklist(V.getNode());
5113 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5114 Vec, V, N->getOperand(2));
5115 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005116}
5117
Bob Wilsonf20700c2010-10-27 20:38:28 +00005118/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5119/// ISD::VECTOR_SHUFFLE.
5120static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5121 // The LLVM shufflevector instruction does not require the shuffle mask
5122 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5123 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5124 // operands do not match the mask length, they are extended by concatenating
5125 // them with undef vectors. That is probably the right thing for other
5126 // targets, but for NEON it is better to concatenate two double-register
5127 // size vector operands into a single quad-register size vector. Do that
5128 // transformation here:
5129 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5130 // shuffle(concat(v1, v2), undef)
5131 SDValue Op0 = N->getOperand(0);
5132 SDValue Op1 = N->getOperand(1);
5133 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5134 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5135 Op0.getNumOperands() != 2 ||
5136 Op1.getNumOperands() != 2)
5137 return SDValue();
5138 SDValue Concat0Op1 = Op0.getOperand(1);
5139 SDValue Concat1Op1 = Op1.getOperand(1);
5140 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5141 Concat1Op1.getOpcode() != ISD::UNDEF)
5142 return SDValue();
5143 // Skip the transformation if any of the types are illegal.
5144 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5145 EVT VT = N->getValueType(0);
5146 if (!TLI.isTypeLegal(VT) ||
5147 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5148 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5149 return SDValue();
5150
5151 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5152 Op0.getOperand(0), Op1.getOperand(0));
5153 // Translate the shuffle mask.
5154 SmallVector<int, 16> NewMask;
5155 unsigned NumElts = VT.getVectorNumElements();
5156 unsigned HalfElts = NumElts/2;
5157 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5158 for (unsigned n = 0; n < NumElts; ++n) {
5159 int MaskElt = SVN->getMaskElt(n);
5160 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005161 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005162 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005163 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005164 NewElt = HalfElts + MaskElt - NumElts;
5165 NewMask.push_back(NewElt);
5166 }
5167 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5168 DAG.getUNDEF(VT), NewMask.data());
5169}
5170
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005171/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5172/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5173/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5174/// return true.
5175static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5176 SelectionDAG &DAG = DCI.DAG;
5177 EVT VT = N->getValueType(0);
5178 // vldN-dup instructions only support 64-bit vectors for N > 1.
5179 if (!VT.is64BitVector())
5180 return false;
5181
5182 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5183 SDNode *VLD = N->getOperand(0).getNode();
5184 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5185 return false;
5186 unsigned NumVecs = 0;
5187 unsigned NewOpc = 0;
5188 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5189 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5190 NumVecs = 2;
5191 NewOpc = ARMISD::VLD2DUP;
5192 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5193 NumVecs = 3;
5194 NewOpc = ARMISD::VLD3DUP;
5195 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5196 NumVecs = 4;
5197 NewOpc = ARMISD::VLD4DUP;
5198 } else {
5199 return false;
5200 }
5201
5202 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5203 // numbers match the load.
5204 unsigned VLDLaneNo =
5205 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5206 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5207 UI != UE; ++UI) {
5208 // Ignore uses of the chain result.
5209 if (UI.getUse().getResNo() == NumVecs)
5210 continue;
5211 SDNode *User = *UI;
5212 if (User->getOpcode() != ARMISD::VDUPLANE ||
5213 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5214 return false;
5215 }
5216
5217 // Create the vldN-dup node.
5218 EVT Tys[5];
5219 unsigned n;
5220 for (n = 0; n < NumVecs; ++n)
5221 Tys[n] = VT;
5222 Tys[n] = MVT::Other;
5223 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5224 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5225 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5226 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5227 Ops, 2, VLDMemInt->getMemoryVT(),
5228 VLDMemInt->getMemOperand());
5229
5230 // Update the uses.
5231 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5232 UI != UE; ++UI) {
5233 unsigned ResNo = UI.getUse().getResNo();
5234 // Ignore uses of the chain result.
5235 if (ResNo == NumVecs)
5236 continue;
5237 SDNode *User = *UI;
5238 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5239 }
5240
5241 // Now the vldN-lane intrinsic is dead except for its chain result.
5242 // Update uses of the chain.
5243 std::vector<SDValue> VLDDupResults;
5244 for (unsigned n = 0; n < NumVecs; ++n)
5245 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5246 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5247 DCI.CombineTo(VLD, VLDDupResults);
5248
5249 return true;
5250}
5251
Bob Wilson9e82bf12010-07-14 01:22:12 +00005252/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5253/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005254static SDValue PerformVDUPLANECombine(SDNode *N,
5255 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005256 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005257
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005258 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5259 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5260 if (CombineVLDDUP(N, DCI))
5261 return SDValue(N, 0);
5262
5263 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5264 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005265 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005266 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005267 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005268 return SDValue();
5269
5270 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5271 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5272 // The canonical VMOV for a zero vector uses a 32-bit element size.
5273 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5274 unsigned EltBits;
5275 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5276 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005277 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005278 if (EltSize > VT.getVectorElementType().getSizeInBits())
5279 return SDValue();
5280
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005281 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005282}
5283
Bob Wilson5bafff32009-06-22 23:27:02 +00005284/// getVShiftImm - Check if this is a valid build_vector for the immediate
5285/// operand of a vector shift operation, where all the elements of the
5286/// build_vector must have the same constant integer value.
5287static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5288 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005289 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005290 Op = Op.getOperand(0);
5291 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5292 APInt SplatBits, SplatUndef;
5293 unsigned SplatBitSize;
5294 bool HasAnyUndefs;
5295 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5296 HasAnyUndefs, ElementBits) ||
5297 SplatBitSize > ElementBits)
5298 return false;
5299 Cnt = SplatBits.getSExtValue();
5300 return true;
5301}
5302
5303/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5304/// operand of a vector shift left operation. That value must be in the range:
5305/// 0 <= Value < ElementBits for a left shift; or
5306/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005307static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005308 assert(VT.isVector() && "vector shift count is not a vector type");
5309 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5310 if (! getVShiftImm(Op, ElementBits, Cnt))
5311 return false;
5312 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5313}
5314
5315/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5316/// operand of a vector shift right operation. For a shift opcode, the value
5317/// is positive, but for an intrinsic the value count must be negative. The
5318/// absolute value must be in the range:
5319/// 1 <= |Value| <= ElementBits for a right shift; or
5320/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005321static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005322 int64_t &Cnt) {
5323 assert(VT.isVector() && "vector shift count is not a vector type");
5324 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5325 if (! getVShiftImm(Op, ElementBits, Cnt))
5326 return false;
5327 if (isIntrinsic)
5328 Cnt = -Cnt;
5329 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5330}
5331
5332/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5333static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5334 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5335 switch (IntNo) {
5336 default:
5337 // Don't do anything for most intrinsics.
5338 break;
5339
5340 // Vector shifts: check for immediate versions and lower them.
5341 // Note: This is done during DAG combining instead of DAG legalizing because
5342 // the build_vectors for 64-bit vector element shift counts are generally
5343 // not legal, and it is hard to see their values after they get legalized to
5344 // loads from a constant pool.
5345 case Intrinsic::arm_neon_vshifts:
5346 case Intrinsic::arm_neon_vshiftu:
5347 case Intrinsic::arm_neon_vshiftls:
5348 case Intrinsic::arm_neon_vshiftlu:
5349 case Intrinsic::arm_neon_vshiftn:
5350 case Intrinsic::arm_neon_vrshifts:
5351 case Intrinsic::arm_neon_vrshiftu:
5352 case Intrinsic::arm_neon_vrshiftn:
5353 case Intrinsic::arm_neon_vqshifts:
5354 case Intrinsic::arm_neon_vqshiftu:
5355 case Intrinsic::arm_neon_vqshiftsu:
5356 case Intrinsic::arm_neon_vqshiftns:
5357 case Intrinsic::arm_neon_vqshiftnu:
5358 case Intrinsic::arm_neon_vqshiftnsu:
5359 case Intrinsic::arm_neon_vqrshiftns:
5360 case Intrinsic::arm_neon_vqrshiftnu:
5361 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00005362 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005363 int64_t Cnt;
5364 unsigned VShiftOpc = 0;
5365
5366 switch (IntNo) {
5367 case Intrinsic::arm_neon_vshifts:
5368 case Intrinsic::arm_neon_vshiftu:
5369 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
5370 VShiftOpc = ARMISD::VSHL;
5371 break;
5372 }
5373 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
5374 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
5375 ARMISD::VSHRs : ARMISD::VSHRu);
5376 break;
5377 }
5378 return SDValue();
5379
5380 case Intrinsic::arm_neon_vshiftls:
5381 case Intrinsic::arm_neon_vshiftlu:
5382 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
5383 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005384 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005385
5386 case Intrinsic::arm_neon_vrshifts:
5387 case Intrinsic::arm_neon_vrshiftu:
5388 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
5389 break;
5390 return SDValue();
5391
5392 case Intrinsic::arm_neon_vqshifts:
5393 case Intrinsic::arm_neon_vqshiftu:
5394 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5395 break;
5396 return SDValue();
5397
5398 case Intrinsic::arm_neon_vqshiftsu:
5399 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5400 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005401 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005402
5403 case Intrinsic::arm_neon_vshiftn:
5404 case Intrinsic::arm_neon_vrshiftn:
5405 case Intrinsic::arm_neon_vqshiftns:
5406 case Intrinsic::arm_neon_vqshiftnu:
5407 case Intrinsic::arm_neon_vqshiftnsu:
5408 case Intrinsic::arm_neon_vqrshiftns:
5409 case Intrinsic::arm_neon_vqrshiftnu:
5410 case Intrinsic::arm_neon_vqrshiftnsu:
5411 // Narrowing shifts require an immediate right shift.
5412 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
5413 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00005414 llvm_unreachable("invalid shift count for narrowing vector shift "
5415 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005416
5417 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005418 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00005419 }
5420
5421 switch (IntNo) {
5422 case Intrinsic::arm_neon_vshifts:
5423 case Intrinsic::arm_neon_vshiftu:
5424 // Opcode already set above.
5425 break;
5426 case Intrinsic::arm_neon_vshiftls:
5427 case Intrinsic::arm_neon_vshiftlu:
5428 if (Cnt == VT.getVectorElementType().getSizeInBits())
5429 VShiftOpc = ARMISD::VSHLLi;
5430 else
5431 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
5432 ARMISD::VSHLLs : ARMISD::VSHLLu);
5433 break;
5434 case Intrinsic::arm_neon_vshiftn:
5435 VShiftOpc = ARMISD::VSHRN; break;
5436 case Intrinsic::arm_neon_vrshifts:
5437 VShiftOpc = ARMISD::VRSHRs; break;
5438 case Intrinsic::arm_neon_vrshiftu:
5439 VShiftOpc = ARMISD::VRSHRu; break;
5440 case Intrinsic::arm_neon_vrshiftn:
5441 VShiftOpc = ARMISD::VRSHRN; break;
5442 case Intrinsic::arm_neon_vqshifts:
5443 VShiftOpc = ARMISD::VQSHLs; break;
5444 case Intrinsic::arm_neon_vqshiftu:
5445 VShiftOpc = ARMISD::VQSHLu; break;
5446 case Intrinsic::arm_neon_vqshiftsu:
5447 VShiftOpc = ARMISD::VQSHLsu; break;
5448 case Intrinsic::arm_neon_vqshiftns:
5449 VShiftOpc = ARMISD::VQSHRNs; break;
5450 case Intrinsic::arm_neon_vqshiftnu:
5451 VShiftOpc = ARMISD::VQSHRNu; break;
5452 case Intrinsic::arm_neon_vqshiftnsu:
5453 VShiftOpc = ARMISD::VQSHRNsu; break;
5454 case Intrinsic::arm_neon_vqrshiftns:
5455 VShiftOpc = ARMISD::VQRSHRNs; break;
5456 case Intrinsic::arm_neon_vqrshiftnu:
5457 VShiftOpc = ARMISD::VQRSHRNu; break;
5458 case Intrinsic::arm_neon_vqrshiftnsu:
5459 VShiftOpc = ARMISD::VQRSHRNsu; break;
5460 }
5461
5462 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005463 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005464 }
5465
5466 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00005467 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005468 int64_t Cnt;
5469 unsigned VShiftOpc = 0;
5470
5471 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
5472 VShiftOpc = ARMISD::VSLI;
5473 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
5474 VShiftOpc = ARMISD::VSRI;
5475 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005476 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005477 }
5478
5479 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
5480 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00005481 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005482 }
5483
5484 case Intrinsic::arm_neon_vqrshifts:
5485 case Intrinsic::arm_neon_vqrshiftu:
5486 // No immediate versions of these to check for.
5487 break;
5488 }
5489
5490 return SDValue();
5491}
5492
5493/// PerformShiftCombine - Checks for immediate versions of vector shifts and
5494/// lowers them. As with the vector shift intrinsics, this is done during DAG
5495/// combining instead of DAG legalizing because the build_vectors for 64-bit
5496/// vector element shift counts are generally not legal, and it is hard to see
5497/// their values after they get legalized to loads from a constant pool.
5498static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
5499 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00005500 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00005501
5502 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00005503 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5504 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00005505 return SDValue();
5506
5507 assert(ST->hasNEON() && "unexpected vector shift");
5508 int64_t Cnt;
5509
5510 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005511 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005512
5513 case ISD::SHL:
5514 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
5515 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005516 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005517 break;
5518
5519 case ISD::SRA:
5520 case ISD::SRL:
5521 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
5522 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
5523 ARMISD::VSHRs : ARMISD::VSHRu);
5524 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005525 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005526 }
5527 }
5528 return SDValue();
5529}
5530
5531/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
5532/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
5533static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
5534 const ARMSubtarget *ST) {
5535 SDValue N0 = N->getOperand(0);
5536
5537 // Check for sign- and zero-extensions of vector extract operations of 8-
5538 // and 16-bit vector elements. NEON supports these directly. They are
5539 // handled during DAG combining because type legalization will promote them
5540 // to 32-bit types and it is messy to recognize the operations after that.
5541 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5542 SDValue Vec = N0.getOperand(0);
5543 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005544 EVT VT = N->getValueType(0);
5545 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005546 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5547
Owen Anderson825b72b2009-08-11 20:47:22 +00005548 if (VT == MVT::i32 &&
5549 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00005550 TLI.isTypeLegal(Vec.getValueType()) &&
5551 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005552
5553 unsigned Opc = 0;
5554 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005555 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005556 case ISD::SIGN_EXTEND:
5557 Opc = ARMISD::VGETLANEs;
5558 break;
5559 case ISD::ZERO_EXTEND:
5560 case ISD::ANY_EXTEND:
5561 Opc = ARMISD::VGETLANEu;
5562 break;
5563 }
5564 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
5565 }
5566 }
5567
5568 return SDValue();
5569}
5570
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005571/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
5572/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
5573static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
5574 const ARMSubtarget *ST) {
5575 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00005576 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005577 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
5578 // a NaN; only do the transformation when it matches that behavior.
5579
5580 // For now only do this when using NEON for FP operations; if using VFP, it
5581 // is not obvious that the benefit outweighs the cost of switching to the
5582 // NEON pipeline.
5583 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
5584 N->getValueType(0) != MVT::f32)
5585 return SDValue();
5586
5587 SDValue CondLHS = N->getOperand(0);
5588 SDValue CondRHS = N->getOperand(1);
5589 SDValue LHS = N->getOperand(2);
5590 SDValue RHS = N->getOperand(3);
5591 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
5592
5593 unsigned Opcode = 0;
5594 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00005595 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005596 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00005597 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005598 IsReversed = true ; // x CC y ? y : x
5599 } else {
5600 return SDValue();
5601 }
5602
Bob Wilsone742bb52010-02-24 22:15:53 +00005603 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005604 switch (CC) {
5605 default: break;
5606 case ISD::SETOLT:
5607 case ISD::SETOLE:
5608 case ISD::SETLT:
5609 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005610 case ISD::SETULT:
5611 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005612 // If LHS is NaN, an ordered comparison will be false and the result will
5613 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
5614 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5615 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
5616 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5617 break;
5618 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
5619 // will return -0, so vmin can only be used for unsafe math or if one of
5620 // the operands is known to be nonzero.
5621 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
5622 !UnsafeFPMath &&
5623 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5624 break;
5625 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005626 break;
5627
5628 case ISD::SETOGT:
5629 case ISD::SETOGE:
5630 case ISD::SETGT:
5631 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005632 case ISD::SETUGT:
5633 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005634 // If LHS is NaN, an ordered comparison will be false and the result will
5635 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
5636 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5637 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
5638 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5639 break;
5640 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
5641 // will return +0, so vmax can only be used for unsafe math or if one of
5642 // the operands is known to be nonzero.
5643 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
5644 !UnsafeFPMath &&
5645 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5646 break;
5647 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005648 break;
5649 }
5650
5651 if (!Opcode)
5652 return SDValue();
5653 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
5654}
5655
Dan Gohman475871a2008-07-27 21:46:04 +00005656SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005657 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005658 switch (N->getOpcode()) {
5659 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005660 case ISD::ADD: return PerformADDCombine(N, DCI);
5661 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005662 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005663 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00005664 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00005665 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00005666 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005667 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00005668 case ISD::STORE: return PerformSTORECombine(N, DCI);
5669 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
5670 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00005671 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005672 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005673 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005674 case ISD::SHL:
5675 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005676 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005677 case ISD::SIGN_EXTEND:
5678 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005679 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
5680 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005681 }
Dan Gohman475871a2008-07-27 21:46:04 +00005682 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005683}
5684
Bill Wendlingaf566342009-08-15 21:21:19 +00005685bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00005686 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00005687 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00005688
5689 switch (VT.getSimpleVT().SimpleTy) {
5690 default:
5691 return false;
5692 case MVT::i8:
5693 case MVT::i16:
5694 case MVT::i32:
5695 return true;
5696 // FIXME: VLD1 etc with standard alignment is legal.
5697 }
5698}
5699
Evan Chenge6c835f2009-08-14 20:09:37 +00005700static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
5701 if (V < 0)
5702 return false;
5703
5704 unsigned Scale = 1;
5705 switch (VT.getSimpleVT().SimpleTy) {
5706 default: return false;
5707 case MVT::i1:
5708 case MVT::i8:
5709 // Scale == 1;
5710 break;
5711 case MVT::i16:
5712 // Scale == 2;
5713 Scale = 2;
5714 break;
5715 case MVT::i32:
5716 // Scale == 4;
5717 Scale = 4;
5718 break;
5719 }
5720
5721 if ((V & (Scale - 1)) != 0)
5722 return false;
5723 V /= Scale;
5724 return V == (V & ((1LL << 5) - 1));
5725}
5726
5727static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
5728 const ARMSubtarget *Subtarget) {
5729 bool isNeg = false;
5730 if (V < 0) {
5731 isNeg = true;
5732 V = - V;
5733 }
5734
5735 switch (VT.getSimpleVT().SimpleTy) {
5736 default: return false;
5737 case MVT::i1:
5738 case MVT::i8:
5739 case MVT::i16:
5740 case MVT::i32:
5741 // + imm12 or - imm8
5742 if (isNeg)
5743 return V == (V & ((1LL << 8) - 1));
5744 return V == (V & ((1LL << 12) - 1));
5745 case MVT::f32:
5746 case MVT::f64:
5747 // Same as ARM mode. FIXME: NEON?
5748 if (!Subtarget->hasVFP2())
5749 return false;
5750 if ((V & 3) != 0)
5751 return false;
5752 V >>= 2;
5753 return V == (V & ((1LL << 8) - 1));
5754 }
5755}
5756
Evan Chengb01fad62007-03-12 23:30:29 +00005757/// isLegalAddressImmediate - Return true if the integer value can be used
5758/// as the offset of the target addressing mode for load / store of the
5759/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00005760static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005761 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00005762 if (V == 0)
5763 return true;
5764
Evan Cheng65011532009-03-09 19:15:00 +00005765 if (!VT.isSimple())
5766 return false;
5767
Evan Chenge6c835f2009-08-14 20:09:37 +00005768 if (Subtarget->isThumb1Only())
5769 return isLegalT1AddressImmediate(V, VT);
5770 else if (Subtarget->isThumb2())
5771 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00005772
Evan Chenge6c835f2009-08-14 20:09:37 +00005773 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00005774 if (V < 0)
5775 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00005776 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00005777 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 case MVT::i1:
5779 case MVT::i8:
5780 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00005781 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005782 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005783 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00005784 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005785 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005786 case MVT::f32:
5787 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00005788 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00005789 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00005790 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00005791 return false;
5792 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005793 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00005794 }
Evan Chenga8e29892007-01-19 07:51:42 +00005795}
5796
Evan Chenge6c835f2009-08-14 20:09:37 +00005797bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
5798 EVT VT) const {
5799 int Scale = AM.Scale;
5800 if (Scale < 0)
5801 return false;
5802
5803 switch (VT.getSimpleVT().SimpleTy) {
5804 default: return false;
5805 case MVT::i1:
5806 case MVT::i8:
5807 case MVT::i16:
5808 case MVT::i32:
5809 if (Scale == 1)
5810 return true;
5811 // r + r << imm
5812 Scale = Scale & ~1;
5813 return Scale == 2 || Scale == 4 || Scale == 8;
5814 case MVT::i64:
5815 // r + r
5816 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
5817 return true;
5818 return false;
5819 case MVT::isVoid:
5820 // Note, we allow "void" uses (basically, uses that aren't loads or
5821 // stores), because arm allows folding a scale into many arithmetic
5822 // operations. This should be made more precise and revisited later.
5823
5824 // Allow r << imm, but the imm has to be a multiple of two.
5825 if (Scale & 1) return false;
5826 return isPowerOf2_32(Scale);
5827 }
5828}
5829
Chris Lattner37caf8c2007-04-09 23:33:39 +00005830/// isLegalAddressingMode - Return true if the addressing mode represented
5831/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005832bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005833 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005834 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00005835 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00005836 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005837
Chris Lattner37caf8c2007-04-09 23:33:39 +00005838 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005839 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005840 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005841
Chris Lattner37caf8c2007-04-09 23:33:39 +00005842 switch (AM.Scale) {
5843 case 0: // no scale reg, must be "r+i" or "r", or "i".
5844 break;
5845 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00005846 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00005847 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005848 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00005849 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005850 // ARM doesn't support any R+R*scale+imm addr modes.
5851 if (AM.BaseOffs)
5852 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005853
Bob Wilson2c7dab12009-04-08 17:55:28 +00005854 if (!VT.isSimple())
5855 return false;
5856
Evan Chenge6c835f2009-08-14 20:09:37 +00005857 if (Subtarget->isThumb2())
5858 return isLegalT2ScaledAddressingMode(AM, VT);
5859
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005860 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00005861 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00005862 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 case MVT::i1:
5864 case MVT::i8:
5865 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005866 if (Scale < 0) Scale = -Scale;
5867 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005868 return true;
5869 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00005870 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00005871 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00005872 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005873 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005874 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005875 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00005876 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005877
Owen Anderson825b72b2009-08-11 20:47:22 +00005878 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005879 // Note, we allow "void" uses (basically, uses that aren't loads or
5880 // stores), because arm allows folding a scale into many arithmetic
5881 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005882
Chris Lattner37caf8c2007-04-09 23:33:39 +00005883 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00005884 if (Scale & 1) return false;
5885 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00005886 }
5887 break;
Evan Chengb01fad62007-03-12 23:30:29 +00005888 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00005889 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00005890}
5891
Evan Cheng77e47512009-11-11 19:05:52 +00005892/// isLegalICmpImmediate - Return true if the specified immediate is legal
5893/// icmp immediate, that is the target has icmp instructions which can compare
5894/// a register against the immediate without having to materialize the
5895/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00005896bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00005897 if (!Subtarget->isThumb())
5898 return ARM_AM::getSOImmVal(Imm) != -1;
5899 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00005900 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00005901 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00005902}
5903
Owen Andersone50ed302009-08-10 22:56:29 +00005904static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005905 bool isSEXTLoad, SDValue &Base,
5906 SDValue &Offset, bool &isInc,
5907 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00005908 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5909 return false;
5910
Owen Anderson825b72b2009-08-11 20:47:22 +00005911 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005912 // AddressingMode 3
5913 Base = Ptr->getOperand(0);
5914 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005915 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005916 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005917 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005918 isInc = false;
5919 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5920 return true;
5921 }
5922 }
5923 isInc = (Ptr->getOpcode() == ISD::ADD);
5924 Offset = Ptr->getOperand(1);
5925 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005926 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005927 // AddressingMode 2
5928 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005929 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005930 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005931 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005932 isInc = false;
5933 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5934 Base = Ptr->getOperand(0);
5935 return true;
5936 }
5937 }
5938
5939 if (Ptr->getOpcode() == ISD::ADD) {
5940 isInc = true;
5941 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5942 if (ShOpcVal != ARM_AM::no_shift) {
5943 Base = Ptr->getOperand(1);
5944 Offset = Ptr->getOperand(0);
5945 } else {
5946 Base = Ptr->getOperand(0);
5947 Offset = Ptr->getOperand(1);
5948 }
5949 return true;
5950 }
5951
5952 isInc = (Ptr->getOpcode() == ISD::ADD);
5953 Base = Ptr->getOperand(0);
5954 Offset = Ptr->getOperand(1);
5955 return true;
5956 }
5957
Jim Grosbache5165492009-11-09 00:11:35 +00005958 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005959 return false;
5960}
5961
Owen Andersone50ed302009-08-10 22:56:29 +00005962static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005963 bool isSEXTLoad, SDValue &Base,
5964 SDValue &Offset, bool &isInc,
5965 SelectionDAG &DAG) {
5966 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5967 return false;
5968
5969 Base = Ptr->getOperand(0);
5970 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5971 int RHSC = (int)RHS->getZExtValue();
5972 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
5973 assert(Ptr->getOpcode() == ISD::ADD);
5974 isInc = false;
5975 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5976 return true;
5977 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
5978 isInc = Ptr->getOpcode() == ISD::ADD;
5979 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
5980 return true;
5981 }
5982 }
5983
5984 return false;
5985}
5986
Evan Chenga8e29892007-01-19 07:51:42 +00005987/// getPreIndexedAddressParts - returns true by value, base pointer and
5988/// offset pointer and addressing mode by reference if the node's address
5989/// can be legally represented as pre-indexed load / store address.
5990bool
Dan Gohman475871a2008-07-27 21:46:04 +00005991ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
5992 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005993 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005994 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005995 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005996 return false;
5997
Owen Andersone50ed302009-08-10 22:56:29 +00005998 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005999 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006000 bool isSEXTLoad = false;
6001 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6002 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006003 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006004 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6005 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6006 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006007 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006008 } else
6009 return false;
6010
6011 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006012 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006013 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006014 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6015 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006016 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006017 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006018 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006019 if (!isLegal)
6020 return false;
6021
6022 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6023 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006024}
6025
6026/// getPostIndexedAddressParts - returns true by value, base pointer and
6027/// offset pointer and addressing mode by reference if this node can be
6028/// combined with a load / store to form a post-indexed load / store.
6029bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006030 SDValue &Base,
6031 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006032 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006033 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006034 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006035 return false;
6036
Owen Andersone50ed302009-08-10 22:56:29 +00006037 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006038 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006039 bool isSEXTLoad = false;
6040 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006041 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006042 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006043 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6044 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006045 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006046 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006047 } else
6048 return false;
6049
6050 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006051 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006052 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006053 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006054 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006055 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006056 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6057 isInc, DAG);
6058 if (!isLegal)
6059 return false;
6060
Evan Cheng28dad2a2010-05-18 21:31:17 +00006061 if (Ptr != Base) {
6062 // Swap base ptr and offset to catch more post-index load / store when
6063 // it's legal. In Thumb2 mode, offset must be an immediate.
6064 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6065 !Subtarget->isThumb2())
6066 std::swap(Base, Offset);
6067
6068 // Post-indexed load / store update the base pointer.
6069 if (Ptr != Base)
6070 return false;
6071 }
6072
Evan Chenge88d5ce2009-07-02 07:28:31 +00006073 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6074 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006075}
6076
Dan Gohman475871a2008-07-27 21:46:04 +00006077void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006078 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006079 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006080 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006081 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006082 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006083 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006084 switch (Op.getOpcode()) {
6085 default: break;
6086 case ARMISD::CMOV: {
6087 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006088 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006089 if (KnownZero == 0 && KnownOne == 0) return;
6090
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006091 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006092 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6093 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006094 KnownZero &= KnownZeroRHS;
6095 KnownOne &= KnownOneRHS;
6096 return;
6097 }
6098 }
6099}
6100
6101//===----------------------------------------------------------------------===//
6102// ARM Inline Assembly Support
6103//===----------------------------------------------------------------------===//
6104
Evan Cheng55d42002011-01-08 01:24:27 +00006105bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6106 // Looking for "rev" which is V6+.
6107 if (!Subtarget->hasV6Ops())
6108 return false;
6109
6110 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6111 std::string AsmStr = IA->getAsmString();
6112 SmallVector<StringRef, 4> AsmPieces;
6113 SplitString(AsmStr, AsmPieces, ";\n");
6114
6115 switch (AsmPieces.size()) {
6116 default: return false;
6117 case 1:
6118 AsmStr = AsmPieces[0];
6119 AsmPieces.clear();
6120 SplitString(AsmStr, AsmPieces, " \t,");
6121
6122 // rev $0, $1
6123 if (AsmPieces.size() == 3 &&
6124 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6125 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6126 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6127 if (Ty && Ty->getBitWidth() == 32)
6128 return IntrinsicLowering::LowerToByteSwap(CI);
6129 }
6130 break;
6131 }
6132
6133 return false;
6134}
6135
Evan Chenga8e29892007-01-19 07:51:42 +00006136/// getConstraintType - Given a constraint letter, return the type of
6137/// constraint it is for this target.
6138ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006139ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6140 if (Constraint.size() == 1) {
6141 switch (Constraint[0]) {
6142 default: break;
6143 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006144 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006145 }
Evan Chenga8e29892007-01-19 07:51:42 +00006146 }
Chris Lattner4234f572007-03-25 02:14:49 +00006147 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006148}
6149
John Thompson44ab89e2010-10-29 17:29:13 +00006150/// Examine constraint type and operand type and determine a weight value.
6151/// This object must already have been set up with the operand type
6152/// and the current alternative constraint selected.
6153TargetLowering::ConstraintWeight
6154ARMTargetLowering::getSingleConstraintMatchWeight(
6155 AsmOperandInfo &info, const char *constraint) const {
6156 ConstraintWeight weight = CW_Invalid;
6157 Value *CallOperandVal = info.CallOperandVal;
6158 // If we don't have a value, we can't do a match,
6159 // but allow it at the lowest weight.
6160 if (CallOperandVal == NULL)
6161 return CW_Default;
6162 const Type *type = CallOperandVal->getType();
6163 // Look at the constraint type.
6164 switch (*constraint) {
6165 default:
6166 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6167 break;
6168 case 'l':
6169 if (type->isIntegerTy()) {
6170 if (Subtarget->isThumb())
6171 weight = CW_SpecificReg;
6172 else
6173 weight = CW_Register;
6174 }
6175 break;
6176 case 'w':
6177 if (type->isFloatingPointTy())
6178 weight = CW_Register;
6179 break;
6180 }
6181 return weight;
6182}
6183
Bob Wilson2dc4f542009-03-20 22:42:55 +00006184std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006185ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006186 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006187 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006188 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006189 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006190 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006191 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006192 return std::make_pair(0U, ARM::tGPRRegisterClass);
6193 else
6194 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006195 case 'r':
6196 return std::make_pair(0U, ARM::GPRRegisterClass);
6197 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006198 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006199 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006200 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006201 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006202 if (VT.getSizeInBits() == 128)
6203 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006204 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006205 }
6206 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006207 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006208 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006209
Evan Chenga8e29892007-01-19 07:51:42 +00006210 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6211}
6212
6213std::vector<unsigned> ARMTargetLowering::
6214getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006215 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006216 if (Constraint.size() != 1)
6217 return std::vector<unsigned>();
6218
6219 switch (Constraint[0]) { // GCC ARM Constraint Letters
6220 default: break;
6221 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006222 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6223 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6224 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006225 case 'r':
6226 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6227 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6228 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6229 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006230 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006231 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006232 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6233 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6234 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6235 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6236 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6237 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6238 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6239 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006240 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006241 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6242 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6243 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6244 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006245 if (VT.getSizeInBits() == 128)
6246 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6247 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006248 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006249 }
6250
6251 return std::vector<unsigned>();
6252}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006253
6254/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6255/// vector. If it is invalid, don't add anything to Ops.
6256void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6257 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006258 std::vector<SDValue>&Ops,
6259 SelectionDAG &DAG) const {
6260 SDValue Result(0, 0);
6261
6262 switch (Constraint) {
6263 default: break;
6264 case 'I': case 'J': case 'K': case 'L':
6265 case 'M': case 'N': case 'O':
6266 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6267 if (!C)
6268 return;
6269
6270 int64_t CVal64 = C->getSExtValue();
6271 int CVal = (int) CVal64;
6272 // None of these constraints allow values larger than 32 bits. Check
6273 // that the value fits in an int.
6274 if (CVal != CVal64)
6275 return;
6276
6277 switch (Constraint) {
6278 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006279 if (Subtarget->isThumb1Only()) {
6280 // This must be a constant between 0 and 255, for ADD
6281 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006282 if (CVal >= 0 && CVal <= 255)
6283 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006284 } else if (Subtarget->isThumb2()) {
6285 // A constant that can be used as an immediate value in a
6286 // data-processing instruction.
6287 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6288 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006289 } else {
6290 // A constant that can be used as an immediate value in a
6291 // data-processing instruction.
6292 if (ARM_AM::getSOImmVal(CVal) != -1)
6293 break;
6294 }
6295 return;
6296
6297 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006298 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006299 // This must be a constant between -255 and -1, for negated ADD
6300 // immediates. This can be used in GCC with an "n" modifier that
6301 // prints the negated value, for use with SUB instructions. It is
6302 // not useful otherwise but is implemented for compatibility.
6303 if (CVal >= -255 && CVal <= -1)
6304 break;
6305 } else {
6306 // This must be a constant between -4095 and 4095. It is not clear
6307 // what this constraint is intended for. Implemented for
6308 // compatibility with GCC.
6309 if (CVal >= -4095 && CVal <= 4095)
6310 break;
6311 }
6312 return;
6313
6314 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006315 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006316 // A 32-bit value where only one byte has a nonzero value. Exclude
6317 // zero to match GCC. This constraint is used by GCC internally for
6318 // constants that can be loaded with a move/shift combination.
6319 // It is not useful otherwise but is implemented for compatibility.
6320 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
6321 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006322 } else if (Subtarget->isThumb2()) {
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::getT2SOImmVal(~CVal) != -1)
6329 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006330 } else {
6331 // A constant whose bitwise inverse can be used as an immediate
6332 // value in a data-processing instruction. This can be used in GCC
6333 // with a "B" modifier that prints the inverted value, for use with
6334 // BIC and MVN instructions. It is not useful otherwise but is
6335 // implemented for compatibility.
6336 if (ARM_AM::getSOImmVal(~CVal) != -1)
6337 break;
6338 }
6339 return;
6340
6341 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006342 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006343 // This must be a constant between -7 and 7,
6344 // for 3-operand ADD/SUB immediate instructions.
6345 if (CVal >= -7 && CVal < 7)
6346 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006347 } else if (Subtarget->isThumb2()) {
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::getT2SOImmVal(-CVal) != -1)
6354 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006355 } else {
6356 // A constant whose negation can be used as an immediate value in a
6357 // data-processing instruction. This can be used in GCC with an "n"
6358 // modifier that prints the negated value, for use with SUB
6359 // instructions. It is not useful otherwise but is implemented for
6360 // compatibility.
6361 if (ARM_AM::getSOImmVal(-CVal) != -1)
6362 break;
6363 }
6364 return;
6365
6366 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006367 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006368 // This must be a multiple of 4 between 0 and 1020, for
6369 // ADD sp + immediate.
6370 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
6371 break;
6372 } else {
6373 // A power of two or a constant between 0 and 32. This is used in
6374 // GCC for the shift amount on shifted register operands, but it is
6375 // useful in general for any shift amounts.
6376 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
6377 break;
6378 }
6379 return;
6380
6381 case 'N':
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 constant between 0 and 31, for shift amounts.
6384 if (CVal >= 0 && CVal <= 31)
6385 break;
6386 }
6387 return;
6388
6389 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006390 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006391 // This must be a multiple of 4 between -508 and 508, for
6392 // ADD/SUB sp = sp + immediate.
6393 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
6394 break;
6395 }
6396 return;
6397 }
6398 Result = DAG.getTargetConstant(CVal, Op.getValueType());
6399 break;
6400 }
6401
6402 if (Result.getNode()) {
6403 Ops.push_back(Result);
6404 return;
6405 }
Dale Johannesen1784d162010-06-25 21:55:36 +00006406 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006407}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00006408
6409bool
6410ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
6411 // The ARM target isn't yet aware of offsets.
6412 return false;
6413}
Evan Cheng39382422009-10-28 01:44:26 +00006414
6415int ARM::getVFPf32Imm(const APFloat &FPImm) {
6416 APInt Imm = FPImm.bitcastToAPInt();
6417 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
6418 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
6419 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
6420
6421 // We can handle 4 bits of mantissa.
6422 // mantissa = (16+UInt(e:f:g:h))/16.
6423 if (Mantissa & 0x7ffff)
6424 return -1;
6425 Mantissa >>= 19;
6426 if ((Mantissa & 0xf) != Mantissa)
6427 return -1;
6428
6429 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6430 if (Exp < -3 || Exp > 4)
6431 return -1;
6432 Exp = ((Exp+3) & 0x7) ^ 4;
6433
6434 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6435}
6436
6437int ARM::getVFPf64Imm(const APFloat &FPImm) {
6438 APInt Imm = FPImm.bitcastToAPInt();
6439 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
6440 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
6441 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
6442
6443 // We can handle 4 bits of mantissa.
6444 // mantissa = (16+UInt(e:f:g:h))/16.
6445 if (Mantissa & 0xffffffffffffLL)
6446 return -1;
6447 Mantissa >>= 48;
6448 if ((Mantissa & 0xf) != Mantissa)
6449 return -1;
6450
6451 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6452 if (Exp < -3 || Exp > 4)
6453 return -1;
6454 Exp = ((Exp+3) & 0x7) ^ 4;
6455
6456 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6457}
6458
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006459bool ARM::isBitFieldInvertedMask(unsigned v) {
6460 if (v == 0xffffffff)
6461 return 0;
6462 // there can be 1's on either or both "outsides", all the "inside"
6463 // bits must be 0's
6464 unsigned int lsb = 0, msb = 31;
6465 while (v & (1 << msb)) --msb;
6466 while (v & (1 << lsb)) ++lsb;
6467 for (unsigned int i = lsb; i <= msb; ++i) {
6468 if (v & (1 << i))
6469 return 0;
6470 }
6471 return 1;
6472}
6473
Evan Cheng39382422009-10-28 01:44:26 +00006474/// isFPImmLegal - Returns true if the target can instruction select the
6475/// specified FP immediate natively. If false, the legalizer will
6476/// materialize the FP immediate as a load from a constant pool.
6477bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
6478 if (!Subtarget->hasVFP3())
6479 return false;
6480 if (VT == MVT::f32)
6481 return ARM::getVFPf32Imm(Imm) != -1;
6482 if (VT == MVT::f64)
6483 return ARM::getVFPf64Imm(Imm) != -1;
6484 return false;
6485}
Bob Wilson65ffec42010-09-21 17:56:22 +00006486
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006487/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00006488/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
6489/// specified in the intrinsic calls.
6490bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
6491 const CallInst &I,
6492 unsigned Intrinsic) const {
6493 switch (Intrinsic) {
6494 case Intrinsic::arm_neon_vld1:
6495 case Intrinsic::arm_neon_vld2:
6496 case Intrinsic::arm_neon_vld3:
6497 case Intrinsic::arm_neon_vld4:
6498 case Intrinsic::arm_neon_vld2lane:
6499 case Intrinsic::arm_neon_vld3lane:
6500 case Intrinsic::arm_neon_vld4lane: {
6501 Info.opc = ISD::INTRINSIC_W_CHAIN;
6502 // Conservatively set memVT to the entire set of vectors loaded.
6503 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
6504 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6505 Info.ptrVal = I.getArgOperand(0);
6506 Info.offset = 0;
6507 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6508 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6509 Info.vol = false; // volatile loads with NEON intrinsics not supported
6510 Info.readMem = true;
6511 Info.writeMem = false;
6512 return true;
6513 }
6514 case Intrinsic::arm_neon_vst1:
6515 case Intrinsic::arm_neon_vst2:
6516 case Intrinsic::arm_neon_vst3:
6517 case Intrinsic::arm_neon_vst4:
6518 case Intrinsic::arm_neon_vst2lane:
6519 case Intrinsic::arm_neon_vst3lane:
6520 case Intrinsic::arm_neon_vst4lane: {
6521 Info.opc = ISD::INTRINSIC_VOID;
6522 // Conservatively set memVT to the entire set of vectors stored.
6523 unsigned NumElts = 0;
6524 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
6525 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
6526 if (!ArgTy->isVectorTy())
6527 break;
6528 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
6529 }
6530 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6531 Info.ptrVal = I.getArgOperand(0);
6532 Info.offset = 0;
6533 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6534 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6535 Info.vol = false; // volatile stores with NEON intrinsics not supported
6536 Info.readMem = false;
6537 Info.writeMem = true;
6538 return true;
6539 }
6540 default:
6541 break;
6542 }
6543
6544 return false;
6545}