blob: 46478cf2c4faac37885e1d19b1815e68bcc73757 [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.
3655SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op, SelectionDAG &DAG) const {
3656 DebugLoc dl = Op.getDebugLoc();
3657 EVT VT = Op.getValueType();
3658 unsigned NumElts = VT.getVectorNumElements();
3659
3660 SmallVector<SDValue, 2> SourceVecs;
3661 SmallVector<unsigned, 2> MinElts;
3662 SmallVector<unsigned, 2> MaxElts;
3663
3664 for (unsigned i = 0; i < NumElts; ++i) {
3665 SDValue V = Op.getOperand(i);
3666 if (V.getOpcode() == ISD::UNDEF)
3667 continue;
3668 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3669 // A shuffle can only come from building a vector from various
3670 // elements of other vectors.
3671 return SDValue();
3672 }
3673
3674 // Record this extraction against the appropriate vector if possible...
3675 SDValue SourceVec = V.getOperand(0);
3676 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3677 bool FoundSource = false;
3678 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3679 if (SourceVecs[j] == SourceVec) {
3680 if (MinElts[j] > EltNo)
3681 MinElts[j] = EltNo;
3682 if (MaxElts[j] < EltNo)
3683 MaxElts[j] = EltNo;
3684 FoundSource = true;
3685 break;
3686 }
3687 }
3688
3689 // Or record a new source if not...
3690 if (!FoundSource) {
3691 SourceVecs.push_back(SourceVec);
3692 MinElts.push_back(EltNo);
3693 MaxElts.push_back(EltNo);
3694 }
3695 }
3696
3697 // Currently only do something sane when at most two source vectors
3698 // involved.
3699 if (SourceVecs.size() > 2)
3700 return SDValue();
3701
3702 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3703 int VEXTOffsets[2] = {0, 0};
3704
3705 // This loop extracts the usage patterns of the source vectors
3706 // and prepares appropriate SDValues for a shuffle if possible.
3707 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3708 if (SourceVecs[i].getValueType() == VT) {
3709 // No VEXT necessary
3710 ShuffleSrcs[i] = SourceVecs[i];
3711 VEXTOffsets[i] = 0;
3712 continue;
3713 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3714 // It probably isn't worth padding out a smaller vector just to
3715 // break it down again in a shuffle.
3716 return SDValue();
3717 }
Bob Wilson11a1dff2011-01-07 21:37:30 +00003718
3719 // Since only 64-bit and 128-bit vectors are legal on ARM and
3720 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003721 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3722 "unexpected vector sizes in ReconstructShuffle");
Bob Wilson11a1dff2011-01-07 21:37:30 +00003723
3724 if (MaxElts[i] - MinElts[i] >= NumElts) {
3725 // Span too large for a VEXT to cope
3726 return SDValue();
3727 }
3728
3729 if (MinElts[i] >= NumElts) {
3730 // The extraction can just take the second half
3731 VEXTOffsets[i] = NumElts;
3732 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SourceVecs[i],
3733 DAG.getIntPtrConstant(NumElts));
3734 } else if (MaxElts[i] < NumElts) {
3735 // The extraction can just take the first half
3736 VEXTOffsets[i] = 0;
3737 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SourceVecs[i],
3738 DAG.getIntPtrConstant(0));
3739 } else {
3740 // An actual VEXT is needed
3741 VEXTOffsets[i] = MinElts[i];
3742 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SourceVecs[i],
3743 DAG.getIntPtrConstant(0));
3744 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, SourceVecs[i],
3745 DAG.getIntPtrConstant(NumElts));
3746 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3747 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3748 }
3749 }
3750
3751 SmallVector<int, 8> Mask;
3752
3753 for (unsigned i = 0; i < NumElts; ++i) {
3754 SDValue Entry = Op.getOperand(i);
3755 if (Entry.getOpcode() == ISD::UNDEF) {
3756 Mask.push_back(-1);
3757 continue;
3758 }
3759
3760 SDValue ExtractVec = Entry.getOperand(0);
3761 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i).getOperand(1))->getSExtValue();
3762 if (ExtractVec == SourceVecs[0]) {
3763 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3764 } else {
3765 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3766 }
3767 }
3768
3769 // Final check before we try to produce nonsense...
3770 if (isShuffleMaskLegal(Mask, VT))
3771 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1], &Mask[0]);
3772
3773 return SDValue();
3774}
3775
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003776/// isShuffleMaskLegal - Targets can use this to indicate that they only
3777/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3778/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3779/// are assumed to be legal.
3780bool
3781ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3782 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003783 if (VT.getVectorNumElements() == 4 &&
3784 (VT.is128BitVector() || VT.is64BitVector())) {
3785 unsigned PFIndexes[4];
3786 for (unsigned i = 0; i != 4; ++i) {
3787 if (M[i] < 0)
3788 PFIndexes[i] = 8;
3789 else
3790 PFIndexes[i] = M[i];
3791 }
3792
3793 // Compute the index in the perfect shuffle table.
3794 unsigned PFTableIndex =
3795 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3796 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3797 unsigned Cost = (PFEntry >> 30);
3798
3799 if (Cost <= 4)
3800 return true;
3801 }
3802
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003803 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003804 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003805
Bob Wilson53dd2452010-06-07 23:53:38 +00003806 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3807 return (EltSize >= 32 ||
3808 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003809 isVREVMask(M, VT, 64) ||
3810 isVREVMask(M, VT, 32) ||
3811 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003812 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3813 isVTRNMask(M, VT, WhichResult) ||
3814 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003815 isVZIPMask(M, VT, WhichResult) ||
3816 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3817 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3818 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003819}
3820
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003821/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3822/// the specified operations to build the shuffle.
3823static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3824 SDValue RHS, SelectionDAG &DAG,
3825 DebugLoc dl) {
3826 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3827 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3828 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3829
3830 enum {
3831 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3832 OP_VREV,
3833 OP_VDUP0,
3834 OP_VDUP1,
3835 OP_VDUP2,
3836 OP_VDUP3,
3837 OP_VEXT1,
3838 OP_VEXT2,
3839 OP_VEXT3,
3840 OP_VUZPL, // VUZP, left result
3841 OP_VUZPR, // VUZP, right result
3842 OP_VZIPL, // VZIP, left result
3843 OP_VZIPR, // VZIP, right result
3844 OP_VTRNL, // VTRN, left result
3845 OP_VTRNR // VTRN, right result
3846 };
3847
3848 if (OpNum == OP_COPY) {
3849 if (LHSID == (1*9+2)*9+3) return LHS;
3850 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3851 return RHS;
3852 }
3853
3854 SDValue OpLHS, OpRHS;
3855 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3856 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3857 EVT VT = OpLHS.getValueType();
3858
3859 switch (OpNum) {
3860 default: llvm_unreachable("Unknown shuffle opcode!");
3861 case OP_VREV:
3862 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3863 case OP_VDUP0:
3864 case OP_VDUP1:
3865 case OP_VDUP2:
3866 case OP_VDUP3:
3867 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003868 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003869 case OP_VEXT1:
3870 case OP_VEXT2:
3871 case OP_VEXT3:
3872 return DAG.getNode(ARMISD::VEXT, dl, VT,
3873 OpLHS, OpRHS,
3874 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3875 case OP_VUZPL:
3876 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003877 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003878 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3879 case OP_VZIPL:
3880 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003881 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003882 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3883 case OP_VTRNL:
3884 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003885 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3886 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003887 }
3888}
3889
Bob Wilson5bafff32009-06-22 23:27:02 +00003890static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003891 SDValue V1 = Op.getOperand(0);
3892 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003893 DebugLoc dl = Op.getDebugLoc();
3894 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003895 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003896 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003897
Bob Wilson28865062009-08-13 02:13:04 +00003898 // Convert shuffles that are directly supported on NEON to target-specific
3899 // DAG nodes, instead of keeping them as shuffles and matching them again
3900 // during code selection. This is more efficient and avoids the possibility
3901 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003902 // FIXME: floating-point vectors should be canonicalized to integer vectors
3903 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003904 SVN->getMask(ShuffleMask);
3905
Bob Wilson53dd2452010-06-07 23:53:38 +00003906 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3907 if (EltSize <= 32) {
3908 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3909 int Lane = SVN->getSplatIndex();
3910 // If this is undef splat, generate it via "just" vdup, if possible.
3911 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003912
Bob Wilson53dd2452010-06-07 23:53:38 +00003913 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3914 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3915 }
3916 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3917 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003918 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003919
3920 bool ReverseVEXT;
3921 unsigned Imm;
3922 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3923 if (ReverseVEXT)
3924 std::swap(V1, V2);
3925 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3926 DAG.getConstant(Imm, MVT::i32));
3927 }
3928
3929 if (isVREVMask(ShuffleMask, VT, 64))
3930 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3931 if (isVREVMask(ShuffleMask, VT, 32))
3932 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3933 if (isVREVMask(ShuffleMask, VT, 16))
3934 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3935
3936 // Check for Neon shuffles that modify both input vectors in place.
3937 // If both results are used, i.e., if there are two shuffles with the same
3938 // source operands and with masks corresponding to both results of one of
3939 // these operations, DAG memoization will ensure that a single node is
3940 // used for both shuffles.
3941 unsigned WhichResult;
3942 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3943 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3944 V1, V2).getValue(WhichResult);
3945 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3946 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3947 V1, V2).getValue(WhichResult);
3948 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3949 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3950 V1, V2).getValue(WhichResult);
3951
3952 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3953 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3954 V1, V1).getValue(WhichResult);
3955 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3956 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3957 V1, V1).getValue(WhichResult);
3958 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3959 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3960 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003961 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003962
Bob Wilsonc692cb72009-08-21 20:54:19 +00003963 // If the shuffle is not directly supported and it has 4 elements, use
3964 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003965 unsigned NumElts = VT.getVectorNumElements();
3966 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003967 unsigned PFIndexes[4];
3968 for (unsigned i = 0; i != 4; ++i) {
3969 if (ShuffleMask[i] < 0)
3970 PFIndexes[i] = 8;
3971 else
3972 PFIndexes[i] = ShuffleMask[i];
3973 }
3974
3975 // Compute the index in the perfect shuffle table.
3976 unsigned PFTableIndex =
3977 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003978 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3979 unsigned Cost = (PFEntry >> 30);
3980
3981 if (Cost <= 4)
3982 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3983 }
Bob Wilsond8e17572009-08-12 22:31:50 +00003984
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003985 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003986 if (EltSize >= 32) {
3987 // Do the expansion with floating-point types, since that is what the VFP
3988 // registers are defined to use, and since i64 is not legal.
3989 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3990 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003991 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
3992 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003993 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003994 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00003995 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003996 Ops.push_back(DAG.getUNDEF(EltVT));
3997 else
3998 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3999 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4000 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4001 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004002 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004003 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004004 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004005 }
4006
Bob Wilson22cac0d2009-08-14 05:16:33 +00004007 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004008}
4009
Bob Wilson5bafff32009-06-22 23:27:02 +00004010static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004011 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004012 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004013 if (!isa<ConstantSDNode>(Lane))
4014 return SDValue();
4015
4016 SDValue Vec = Op.getOperand(0);
4017 if (Op.getValueType() == MVT::i32 &&
4018 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4019 DebugLoc dl = Op.getDebugLoc();
4020 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4021 }
4022
4023 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004024}
4025
Bob Wilsona6d65862009-08-03 20:36:38 +00004026static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4027 // The only time a CONCAT_VECTORS operation can have legal types is when
4028 // two 64-bit vectors are concatenated to a 128-bit vector.
4029 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4030 "unexpected CONCAT_VECTORS");
4031 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004032 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004033 SDValue Op0 = Op.getOperand(0);
4034 SDValue Op1 = Op.getOperand(1);
4035 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004036 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004037 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004038 DAG.getIntPtrConstant(0));
4039 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004040 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004041 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004042 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004043 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004044}
4045
Bob Wilson626613d2010-11-23 19:38:38 +00004046/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4047/// element has been zero/sign-extended, depending on the isSigned parameter,
4048/// from an integer type half its size.
4049static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4050 bool isSigned) {
4051 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4052 EVT VT = N->getValueType(0);
4053 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4054 SDNode *BVN = N->getOperand(0).getNode();
4055 if (BVN->getValueType(0) != MVT::v4i32 ||
4056 BVN->getOpcode() != ISD::BUILD_VECTOR)
4057 return false;
4058 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4059 unsigned HiElt = 1 - LoElt;
4060 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4061 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4062 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4063 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4064 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4065 return false;
4066 if (isSigned) {
4067 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4068 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4069 return true;
4070 } else {
4071 if (Hi0->isNullValue() && Hi1->isNullValue())
4072 return true;
4073 }
4074 return false;
4075 }
4076
4077 if (N->getOpcode() != ISD::BUILD_VECTOR)
4078 return false;
4079
4080 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4081 SDNode *Elt = N->getOperand(i).getNode();
4082 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4083 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4084 unsigned HalfSize = EltSize / 2;
4085 if (isSigned) {
4086 int64_t SExtVal = C->getSExtValue();
4087 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4088 return false;
4089 } else {
4090 if ((C->getZExtValue() >> HalfSize) != 0)
4091 return false;
4092 }
4093 continue;
4094 }
4095 return false;
4096 }
4097
4098 return true;
4099}
4100
4101/// isSignExtended - Check if a node is a vector value that is sign-extended
4102/// or a constant BUILD_VECTOR with sign-extended elements.
4103static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4104 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4105 return true;
4106 if (isExtendedBUILD_VECTOR(N, DAG, true))
4107 return true;
4108 return false;
4109}
4110
4111/// isZeroExtended - Check if a node is a vector value that is zero-extended
4112/// or a constant BUILD_VECTOR with zero-extended elements.
4113static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4114 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4115 return true;
4116 if (isExtendedBUILD_VECTOR(N, DAG, false))
4117 return true;
4118 return false;
4119}
4120
4121/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4122/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004123static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4124 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4125 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004126 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4127 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4128 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4129 LD->isNonTemporal(), LD->getAlignment());
4130 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4131 // have been legalized as a BITCAST from v4i32.
4132 if (N->getOpcode() == ISD::BITCAST) {
4133 SDNode *BVN = N->getOperand(0).getNode();
4134 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4135 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4136 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4137 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4138 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4139 }
4140 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4141 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4142 EVT VT = N->getValueType(0);
4143 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4144 unsigned NumElts = VT.getVectorNumElements();
4145 MVT TruncVT = MVT::getIntegerVT(EltSize);
4146 SmallVector<SDValue, 8> Ops;
4147 for (unsigned i = 0; i != NumElts; ++i) {
4148 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4149 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004150 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004151 }
4152 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4153 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004154}
4155
4156static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4157 // Multiplications are only custom-lowered for 128-bit vectors so that
4158 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4159 EVT VT = Op.getValueType();
4160 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4161 SDNode *N0 = Op.getOperand(0).getNode();
4162 SDNode *N1 = Op.getOperand(1).getNode();
4163 unsigned NewOpc = 0;
Bob Wilson626613d2010-11-23 19:38:38 +00004164 if (isSignExtended(N0, DAG) && isSignExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004165 NewOpc = ARMISD::VMULLs;
Bob Wilson626613d2010-11-23 19:38:38 +00004166 else if (isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004167 NewOpc = ARMISD::VMULLu;
Bob Wilson626613d2010-11-23 19:38:38 +00004168 else if (VT == MVT::v2i64)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004169 // Fall through to expand this. It is not legal.
4170 return SDValue();
Bob Wilson626613d2010-11-23 19:38:38 +00004171 else
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004172 // Other vector multiplications are legal.
4173 return Op;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004174
4175 // Legalize to a VMULL instruction.
4176 DebugLoc DL = Op.getDebugLoc();
4177 SDValue Op0 = SkipExtension(N0, DAG);
4178 SDValue Op1 = SkipExtension(N1, DAG);
4179
4180 assert(Op0.getValueType().is64BitVector() &&
4181 Op1.getValueType().is64BitVector() &&
4182 "unexpected types for extended operands to VMULL");
4183 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4184}
4185
Dan Gohmand858e902010-04-17 15:26:15 +00004186SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004187 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004188 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004189 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004190 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004191 case ISD::GlobalAddress:
4192 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4193 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00004194 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004195 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004196 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4197 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004198 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004199 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004200 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004201 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004202 case ISD::SINT_TO_FP:
4203 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4204 case ISD::FP_TO_SINT:
4205 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004206 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004207 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004208 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004209 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004210 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004211 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004212 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004213 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4214 Subtarget);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004215 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004216 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004217 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004218 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004219 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004220 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004221 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004222 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004223 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004224 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004225 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004226 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004227 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004228 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004229 case ISD::MUL: return LowerMUL(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004230 }
Dan Gohman475871a2008-07-27 21:46:04 +00004231 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004232}
4233
Duncan Sands1607f052008-12-01 11:39:25 +00004234/// ReplaceNodeResults - Replace the results of node with an illegal result
4235/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004236void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4237 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004238 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004239 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004240 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004241 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004242 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004243 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004244 case ISD::BITCAST:
4245 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004246 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004247 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004248 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004249 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004250 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004251 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004252 if (Res.getNode())
4253 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004254}
Chris Lattner27a6c732007-11-24 07:07:01 +00004255
Evan Chenga8e29892007-01-19 07:51:42 +00004256//===----------------------------------------------------------------------===//
4257// ARM Scheduler Hooks
4258//===----------------------------------------------------------------------===//
4259
4260MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004261ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4262 MachineBasicBlock *BB,
4263 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004264 unsigned dest = MI->getOperand(0).getReg();
4265 unsigned ptr = MI->getOperand(1).getReg();
4266 unsigned oldval = MI->getOperand(2).getReg();
4267 unsigned newval = MI->getOperand(3).getReg();
4268 unsigned scratch = BB->getParent()->getRegInfo()
4269 .createVirtualRegister(ARM::GPRRegisterClass);
4270 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4271 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004272 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004273
4274 unsigned ldrOpc, strOpc;
4275 switch (Size) {
4276 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004277 case 1:
4278 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
4279 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
4280 break;
4281 case 2:
4282 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4283 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4284 break;
4285 case 4:
4286 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4287 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4288 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004289 }
4290
4291 MachineFunction *MF = BB->getParent();
4292 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4293 MachineFunction::iterator It = BB;
4294 ++It; // insert the new blocks after the current block
4295
4296 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4297 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4298 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4299 MF->insert(It, loop1MBB);
4300 MF->insert(It, loop2MBB);
4301 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004302
4303 // Transfer the remainder of BB and its successor edges to exitMBB.
4304 exitMBB->splice(exitMBB->begin(), BB,
4305 llvm::next(MachineBasicBlock::iterator(MI)),
4306 BB->end());
4307 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004308
4309 // thisMBB:
4310 // ...
4311 // fallthrough --> loop1MBB
4312 BB->addSuccessor(loop1MBB);
4313
4314 // loop1MBB:
4315 // ldrex dest, [ptr]
4316 // cmp dest, oldval
4317 // bne exitMBB
4318 BB = loop1MBB;
4319 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004320 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004321 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004322 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4323 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004324 BB->addSuccessor(loop2MBB);
4325 BB->addSuccessor(exitMBB);
4326
4327 // loop2MBB:
4328 // strex scratch, newval, [ptr]
4329 // cmp scratch, #0
4330 // bne loop1MBB
4331 BB = loop2MBB;
4332 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4333 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004334 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004335 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004336 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4337 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004338 BB->addSuccessor(loop1MBB);
4339 BB->addSuccessor(exitMBB);
4340
4341 // exitMBB:
4342 // ...
4343 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004344
Dan Gohman14152b42010-07-06 20:24:04 +00004345 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004346
Jim Grosbach5278eb82009-12-11 01:42:04 +00004347 return BB;
4348}
4349
4350MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004351ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4352 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004353 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4354 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4355
4356 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004357 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004358 MachineFunction::iterator It = BB;
4359 ++It;
4360
4361 unsigned dest = MI->getOperand(0).getReg();
4362 unsigned ptr = MI->getOperand(1).getReg();
4363 unsigned incr = MI->getOperand(2).getReg();
4364 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004365
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004366 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004367 unsigned ldrOpc, strOpc;
4368 switch (Size) {
4369 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004370 case 1:
4371 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004372 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004373 break;
4374 case 2:
4375 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4376 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4377 break;
4378 case 4:
4379 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4380 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4381 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004382 }
4383
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004384 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4385 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4386 MF->insert(It, loopMBB);
4387 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004388
4389 // Transfer the remainder of BB and its successor edges to exitMBB.
4390 exitMBB->splice(exitMBB->begin(), BB,
4391 llvm::next(MachineBasicBlock::iterator(MI)),
4392 BB->end());
4393 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004394
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004395 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004396 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4397 unsigned scratch2 = (!BinOpcode) ? incr :
4398 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4399
4400 // thisMBB:
4401 // ...
4402 // fallthrough --> loopMBB
4403 BB->addSuccessor(loopMBB);
4404
4405 // loopMBB:
4406 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004407 // <binop> scratch2, dest, incr
4408 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004409 // cmp scratch, #0
4410 // bne- loopMBB
4411 // fallthrough --> exitMBB
4412 BB = loopMBB;
4413 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004414 if (BinOpcode) {
4415 // operand order needs to go the other way for NAND
4416 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4417 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4418 addReg(incr).addReg(dest)).addReg(0);
4419 else
4420 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4421 addReg(dest).addReg(incr)).addReg(0);
4422 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004423
4424 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4425 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004426 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004427 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004428 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4429 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004430
4431 BB->addSuccessor(loopMBB);
4432 BB->addSuccessor(exitMBB);
4433
4434 // exitMBB:
4435 // ...
4436 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004437
Dan Gohman14152b42010-07-06 20:24:04 +00004438 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004439
Jim Grosbachc3c23542009-12-14 04:22:04 +00004440 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004441}
4442
Evan Cheng218977b2010-07-13 19:27:42 +00004443static
4444MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4445 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4446 E = MBB->succ_end(); I != E; ++I)
4447 if (*I != Succ)
4448 return *I;
4449 llvm_unreachable("Expecting a BB with two successors!");
4450}
4451
Jim Grosbache801dc42009-12-12 01:40:06 +00004452MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004453ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004454 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004455 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004456 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004457 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004458 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004459 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004460 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004461 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004462
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004463 case ARM::ATOMIC_LOAD_ADD_I8:
4464 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4465 case ARM::ATOMIC_LOAD_ADD_I16:
4466 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4467 case ARM::ATOMIC_LOAD_ADD_I32:
4468 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004469
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004470 case ARM::ATOMIC_LOAD_AND_I8:
4471 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4472 case ARM::ATOMIC_LOAD_AND_I16:
4473 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4474 case ARM::ATOMIC_LOAD_AND_I32:
4475 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004476
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004477 case ARM::ATOMIC_LOAD_OR_I8:
4478 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4479 case ARM::ATOMIC_LOAD_OR_I16:
4480 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4481 case ARM::ATOMIC_LOAD_OR_I32:
4482 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004483
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004484 case ARM::ATOMIC_LOAD_XOR_I8:
4485 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4486 case ARM::ATOMIC_LOAD_XOR_I16:
4487 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4488 case ARM::ATOMIC_LOAD_XOR_I32:
4489 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004490
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004491 case ARM::ATOMIC_LOAD_NAND_I8:
4492 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4493 case ARM::ATOMIC_LOAD_NAND_I16:
4494 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4495 case ARM::ATOMIC_LOAD_NAND_I32:
4496 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004497
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004498 case ARM::ATOMIC_LOAD_SUB_I8:
4499 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4500 case ARM::ATOMIC_LOAD_SUB_I16:
4501 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4502 case ARM::ATOMIC_LOAD_SUB_I32:
4503 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004504
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004505 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4506 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4507 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004508
4509 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4510 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4511 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004512
Evan Cheng007ea272009-08-12 05:17:19 +00004513 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004514 // To "insert" a SELECT_CC instruction, we actually have to insert the
4515 // diamond control-flow pattern. The incoming instruction knows the
4516 // destination vreg to set, the condition code register to branch on, the
4517 // true/false values to select between, and a branch opcode to use.
4518 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004519 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004520 ++It;
4521
4522 // thisMBB:
4523 // ...
4524 // TrueVal = ...
4525 // cmpTY ccX, r1, r2
4526 // bCC copy1MBB
4527 // fallthrough --> copy0MBB
4528 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004529 MachineFunction *F = BB->getParent();
4530 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4531 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004532 F->insert(It, copy0MBB);
4533 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004534
4535 // Transfer the remainder of BB and its successor edges to sinkMBB.
4536 sinkMBB->splice(sinkMBB->begin(), BB,
4537 llvm::next(MachineBasicBlock::iterator(MI)),
4538 BB->end());
4539 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4540
Dan Gohman258c58c2010-07-06 15:49:48 +00004541 BB->addSuccessor(copy0MBB);
4542 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004543
Dan Gohman14152b42010-07-06 20:24:04 +00004544 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4545 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4546
Evan Chenga8e29892007-01-19 07:51:42 +00004547 // copy0MBB:
4548 // %FalseValue = ...
4549 // # fallthrough to sinkMBB
4550 BB = copy0MBB;
4551
4552 // Update machine-CFG edges
4553 BB->addSuccessor(sinkMBB);
4554
4555 // sinkMBB:
4556 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4557 // ...
4558 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004559 BuildMI(*BB, BB->begin(), dl,
4560 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004561 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4562 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4563
Dan Gohman14152b42010-07-06 20:24:04 +00004564 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004565 return BB;
4566 }
Evan Cheng86198642009-08-07 00:34:42 +00004567
Evan Cheng218977b2010-07-13 19:27:42 +00004568 case ARM::BCCi64:
4569 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00004570 // If there is an unconditional branch to the other successor, remove it.
4571 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
4572
Evan Cheng218977b2010-07-13 19:27:42 +00004573 // Compare both parts that make up the double comparison separately for
4574 // equality.
4575 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4576
4577 unsigned LHS1 = MI->getOperand(1).getReg();
4578 unsigned LHS2 = MI->getOperand(2).getReg();
4579 if (RHSisZero) {
4580 AddDefaultPred(BuildMI(BB, dl,
4581 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4582 .addReg(LHS1).addImm(0));
4583 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4584 .addReg(LHS2).addImm(0)
4585 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4586 } else {
4587 unsigned RHS1 = MI->getOperand(3).getReg();
4588 unsigned RHS2 = MI->getOperand(4).getReg();
4589 AddDefaultPred(BuildMI(BB, dl,
4590 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4591 .addReg(LHS1).addReg(RHS1));
4592 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4593 .addReg(LHS2).addReg(RHS2)
4594 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4595 }
4596
4597 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4598 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4599 if (MI->getOperand(0).getImm() == ARMCC::NE)
4600 std::swap(destMBB, exitMBB);
4601
4602 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4603 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4604 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4605 .addMBB(exitMBB);
4606
4607 MI->eraseFromParent(); // The pseudo instruction is gone now.
4608 return BB;
4609 }
Evan Chenga8e29892007-01-19 07:51:42 +00004610 }
4611}
4612
4613//===----------------------------------------------------------------------===//
4614// ARM Optimization Hooks
4615//===----------------------------------------------------------------------===//
4616
Chris Lattnerd1980a52009-03-12 06:52:53 +00004617static
4618SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4619 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004620 SelectionDAG &DAG = DCI.DAG;
4621 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004622 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004623 unsigned Opc = N->getOpcode();
4624 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4625 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4626 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4627 ISD::CondCode CC = ISD::SETCC_INVALID;
4628
4629 if (isSlctCC) {
4630 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4631 } else {
4632 SDValue CCOp = Slct.getOperand(0);
4633 if (CCOp.getOpcode() == ISD::SETCC)
4634 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4635 }
4636
4637 bool DoXform = false;
4638 bool InvCC = false;
4639 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4640 "Bad input!");
4641
4642 if (LHS.getOpcode() == ISD::Constant &&
4643 cast<ConstantSDNode>(LHS)->isNullValue()) {
4644 DoXform = true;
4645 } else if (CC != ISD::SETCC_INVALID &&
4646 RHS.getOpcode() == ISD::Constant &&
4647 cast<ConstantSDNode>(RHS)->isNullValue()) {
4648 std::swap(LHS, RHS);
4649 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004650 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004651 Op0.getOperand(0).getValueType();
4652 bool isInt = OpVT.isInteger();
4653 CC = ISD::getSetCCInverse(CC, isInt);
4654
4655 if (!TLI.isCondCodeLegal(CC, OpVT))
4656 return SDValue(); // Inverse operator isn't legal.
4657
4658 DoXform = true;
4659 InvCC = true;
4660 }
4661
4662 if (DoXform) {
4663 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4664 if (isSlctCC)
4665 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4666 Slct.getOperand(0), Slct.getOperand(1), CC);
4667 SDValue CCOp = Slct.getOperand(0);
4668 if (InvCC)
4669 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4670 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4671 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4672 CCOp, OtherOp, Result);
4673 }
4674 return SDValue();
4675}
4676
Bob Wilson3d5792a2010-07-29 20:34:14 +00004677/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4678/// operands N0 and N1. This is a helper for PerformADDCombine that is
4679/// called with the default operands, and if that fails, with commuted
4680/// operands.
4681static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4682 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004683 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4684 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4685 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4686 if (Result.getNode()) return Result;
4687 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00004688 return SDValue();
4689}
4690
Bob Wilson3d5792a2010-07-29 20:34:14 +00004691/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4692///
4693static SDValue PerformADDCombine(SDNode *N,
4694 TargetLowering::DAGCombinerInfo &DCI) {
4695 SDValue N0 = N->getOperand(0);
4696 SDValue N1 = N->getOperand(1);
4697
4698 // First try with the default operand order.
4699 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4700 if (Result.getNode())
4701 return Result;
4702
4703 // If that didn't work, try again with the operands commuted.
4704 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4705}
4706
Chris Lattnerd1980a52009-03-12 06:52:53 +00004707/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004708///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004709static SDValue PerformSUBCombine(SDNode *N,
4710 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004711 SDValue N0 = N->getOperand(0);
4712 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004713
Chris Lattnerd1980a52009-03-12 06:52:53 +00004714 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4715 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4716 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4717 if (Result.getNode()) return Result;
4718 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004719
Chris Lattnerd1980a52009-03-12 06:52:53 +00004720 return SDValue();
4721}
4722
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004723static SDValue PerformMULCombine(SDNode *N,
4724 TargetLowering::DAGCombinerInfo &DCI,
4725 const ARMSubtarget *Subtarget) {
4726 SelectionDAG &DAG = DCI.DAG;
4727
4728 if (Subtarget->isThumb1Only())
4729 return SDValue();
4730
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004731 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4732 return SDValue();
4733
4734 EVT VT = N->getValueType(0);
4735 if (VT != MVT::i32)
4736 return SDValue();
4737
4738 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4739 if (!C)
4740 return SDValue();
4741
4742 uint64_t MulAmt = C->getZExtValue();
4743 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4744 ShiftAmt = ShiftAmt & (32 - 1);
4745 SDValue V = N->getOperand(0);
4746 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004747
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004748 SDValue Res;
4749 MulAmt >>= ShiftAmt;
4750 if (isPowerOf2_32(MulAmt - 1)) {
4751 // (mul x, 2^N + 1) => (add (shl x, N), x)
4752 Res = DAG.getNode(ISD::ADD, DL, VT,
4753 V, DAG.getNode(ISD::SHL, DL, VT,
4754 V, DAG.getConstant(Log2_32(MulAmt-1),
4755 MVT::i32)));
4756 } else if (isPowerOf2_32(MulAmt + 1)) {
4757 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4758 Res = DAG.getNode(ISD::SUB, DL, VT,
4759 DAG.getNode(ISD::SHL, DL, VT,
4760 V, DAG.getConstant(Log2_32(MulAmt+1),
4761 MVT::i32)),
4762 V);
4763 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004764 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004765
4766 if (ShiftAmt != 0)
4767 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4768 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004769
4770 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004771 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004772 return SDValue();
4773}
4774
Owen Anderson080c0922010-11-05 19:27:46 +00004775static SDValue PerformANDCombine(SDNode *N,
4776 TargetLowering::DAGCombinerInfo &DCI) {
4777 // Attempt to use immediate-form VBIC
4778 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4779 DebugLoc dl = N->getDebugLoc();
4780 EVT VT = N->getValueType(0);
4781 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004782
Owen Anderson080c0922010-11-05 19:27:46 +00004783 APInt SplatBits, SplatUndef;
4784 unsigned SplatBitSize;
4785 bool HasAnyUndefs;
4786 if (BVN &&
4787 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4788 if (SplatBitSize <= 64) {
4789 EVT VbicVT;
4790 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
4791 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004792 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004793 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00004794 if (Val.getNode()) {
4795 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004796 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00004797 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004798 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00004799 }
4800 }
4801 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004802
Owen Anderson080c0922010-11-05 19:27:46 +00004803 return SDValue();
4804}
4805
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004806/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4807static SDValue PerformORCombine(SDNode *N,
4808 TargetLowering::DAGCombinerInfo &DCI,
4809 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00004810 // Attempt to use immediate-form VORR
4811 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4812 DebugLoc dl = N->getDebugLoc();
4813 EVT VT = N->getValueType(0);
4814 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004815
Owen Anderson60f48702010-11-03 23:15:26 +00004816 APInt SplatBits, SplatUndef;
4817 unsigned SplatBitSize;
4818 bool HasAnyUndefs;
4819 if (BVN && Subtarget->hasNEON() &&
4820 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4821 if (SplatBitSize <= 64) {
4822 EVT VorrVT;
4823 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
4824 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004825 DAG, VorrVT, VT.is128BitVector(),
4826 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00004827 if (Val.getNode()) {
4828 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004829 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00004830 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004831 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00004832 }
4833 }
4834 }
4835
Jim Grosbach54238562010-07-17 03:30:54 +00004836 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4837 // reasonable.
4838
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004839 // BFI is only available on V6T2+
4840 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4841 return SDValue();
4842
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004843 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004844 DebugLoc DL = N->getDebugLoc();
4845 // 1) or (and A, mask), val => ARMbfi A, val, mask
4846 // iff (val & mask) == val
4847 //
4848 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4849 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4850 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4851 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4852 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4853 // (i.e., copy a bitfield value into another bitfield of the same width)
4854 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004855 return SDValue();
4856
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004857 if (VT != MVT::i32)
4858 return SDValue();
4859
Evan Cheng30fb13f2010-12-13 20:32:54 +00004860 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00004861
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004862 // The value and the mask need to be constants so we can verify this is
4863 // actually a bitfield set. If the mask is 0xffff, we can do better
4864 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00004865 SDValue MaskOp = N0.getOperand(1);
4866 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
4867 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004868 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004869 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004870 if (Mask == 0xffff)
4871 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004872 SDValue Res;
4873 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004874 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4875 if (N1C) {
4876 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004877 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00004878 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004879
Evan Chenga9688c42010-12-11 04:11:38 +00004880 if (ARM::isBitFieldInvertedMask(Mask)) {
4881 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004882
Evan Cheng30fb13f2010-12-13 20:32:54 +00004883 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00004884 DAG.getConstant(Val, MVT::i32),
4885 DAG.getConstant(Mask, MVT::i32));
4886
4887 // Do not add new nodes to DAG combiner worklist.
4888 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004889 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004890 }
Jim Grosbach54238562010-07-17 03:30:54 +00004891 } else if (N1.getOpcode() == ISD::AND) {
4892 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004893 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4894 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00004895 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004896 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004897
4898 if (ARM::isBitFieldInvertedMask(Mask) &&
4899 ARM::isBitFieldInvertedMask(~Mask2) &&
4900 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4901 // The pack halfword instruction works better for masks that fit it,
4902 // so use that when it's available.
4903 if (Subtarget->hasT2ExtractPack() &&
4904 (Mask == 0xffff || Mask == 0xffff0000))
4905 return SDValue();
4906 // 2a
4907 unsigned lsb = CountTrailingZeros_32(Mask2);
4908 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4909 DAG.getConstant(lsb, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00004910 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00004911 DAG.getConstant(Mask, MVT::i32));
4912 // Do not add new nodes to DAG combiner worklist.
4913 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004914 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004915 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4916 ARM::isBitFieldInvertedMask(Mask2) &&
4917 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4918 // The pack halfword instruction works better for masks that fit it,
4919 // so use that when it's available.
4920 if (Subtarget->hasT2ExtractPack() &&
4921 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4922 return SDValue();
4923 // 2b
4924 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004925 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00004926 DAG.getConstant(lsb, MVT::i32));
4927 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4928 DAG.getConstant(Mask2, MVT::i32));
4929 // Do not add new nodes to DAG combiner worklist.
4930 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004931 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004932 }
4933 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004934
Evan Cheng30fb13f2010-12-13 20:32:54 +00004935 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
4936 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
4937 ARM::isBitFieldInvertedMask(~Mask)) {
4938 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
4939 // where lsb(mask) == #shamt and masked bits of B are known zero.
4940 SDValue ShAmt = N00.getOperand(1);
4941 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
4942 unsigned LSB = CountTrailingZeros_32(Mask);
4943 if (ShAmtC != LSB)
4944 return SDValue();
4945
4946 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
4947 DAG.getConstant(~Mask, MVT::i32));
4948
4949 // Do not add new nodes to DAG combiner worklist.
4950 DCI.CombineTo(N, Res, false);
4951 }
4952
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004953 return SDValue();
4954}
4955
Evan Cheng0c1aec12010-12-14 03:22:07 +00004956/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
4957/// C1 & C2 == C1.
4958static SDValue PerformBFICombine(SDNode *N,
4959 TargetLowering::DAGCombinerInfo &DCI) {
4960 SDValue N1 = N->getOperand(1);
4961 if (N1.getOpcode() == ISD::AND) {
4962 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4963 if (!N11C)
4964 return SDValue();
4965 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
4966 unsigned Mask2 = N11C->getZExtValue();
4967 if ((Mask & Mask2) == Mask2)
4968 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
4969 N->getOperand(0), N1.getOperand(0),
4970 N->getOperand(2));
4971 }
4972 return SDValue();
4973}
4974
Bob Wilson0b8ccb82010-09-22 22:09:21 +00004975/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
4976/// ARMISD::VMOVRRD.
4977static SDValue PerformVMOVRRDCombine(SDNode *N,
4978 TargetLowering::DAGCombinerInfo &DCI) {
4979 // vmovrrd(vmovdrr x, y) -> x,y
4980 SDValue InDouble = N->getOperand(0);
4981 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
4982 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
4983 return SDValue();
4984}
4985
4986/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
4987/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
4988static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
4989 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
4990 SDValue Op0 = N->getOperand(0);
4991 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004992 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00004993 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004994 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00004995 Op1 = Op1.getOperand(0);
4996 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
4997 Op0.getNode() == Op1.getNode() &&
4998 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004999 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005000 N->getValueType(0), Op0.getOperand(0));
5001 return SDValue();
5002}
5003
Bob Wilson31600902010-12-21 06:43:19 +00005004/// PerformSTORECombine - Target-specific dag combine xforms for
5005/// ISD::STORE.
5006static SDValue PerformSTORECombine(SDNode *N,
5007 TargetLowering::DAGCombinerInfo &DCI) {
5008 // Bitcast an i64 store extracted from a vector to f64.
5009 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5010 StoreSDNode *St = cast<StoreSDNode>(N);
5011 SDValue StVal = St->getValue();
5012 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5013 StVal.getValueType() != MVT::i64 ||
5014 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5015 return SDValue();
5016
5017 SelectionDAG &DAG = DCI.DAG;
5018 DebugLoc dl = StVal.getDebugLoc();
5019 SDValue IntVec = StVal.getOperand(0);
5020 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5021 IntVec.getValueType().getVectorNumElements());
5022 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5023 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5024 Vec, StVal.getOperand(1));
5025 dl = N->getDebugLoc();
5026 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5027 // Make the DAGCombiner fold the bitcasts.
5028 DCI.AddToWorklist(Vec.getNode());
5029 DCI.AddToWorklist(ExtElt.getNode());
5030 DCI.AddToWorklist(V.getNode());
5031 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5032 St->getPointerInfo(), St->isVolatile(),
5033 St->isNonTemporal(), St->getAlignment(),
5034 St->getTBAAInfo());
5035}
5036
5037/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5038/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5039/// i64 vector to have f64 elements, since the value can then be loaded
5040/// directly into a VFP register.
5041static bool hasNormalLoadOperand(SDNode *N) {
5042 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5043 for (unsigned i = 0; i < NumElts; ++i) {
5044 SDNode *Elt = N->getOperand(i).getNode();
5045 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5046 return true;
5047 }
5048 return false;
5049}
5050
Bob Wilson75f02882010-09-17 22:59:05 +00005051/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5052/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005053static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5054 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005055 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5056 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5057 // into a pair of GPRs, which is fine when the value is used as a scalar,
5058 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005059 SelectionDAG &DAG = DCI.DAG;
5060 if (N->getNumOperands() == 2) {
5061 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5062 if (RV.getNode())
5063 return RV;
5064 }
Bob Wilson75f02882010-09-17 22:59:05 +00005065
Bob Wilson31600902010-12-21 06:43:19 +00005066 // Load i64 elements as f64 values so that type legalization does not split
5067 // them up into i32 values.
5068 EVT VT = N->getValueType(0);
5069 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5070 return SDValue();
5071 DebugLoc dl = N->getDebugLoc();
5072 SmallVector<SDValue, 8> Ops;
5073 unsigned NumElts = VT.getVectorNumElements();
5074 for (unsigned i = 0; i < NumElts; ++i) {
5075 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5076 Ops.push_back(V);
5077 // Make the DAGCombiner fold the bitcast.
5078 DCI.AddToWorklist(V.getNode());
5079 }
5080 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5081 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5082 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5083}
5084
5085/// PerformInsertEltCombine - Target-specific dag combine xforms for
5086/// ISD::INSERT_VECTOR_ELT.
5087static SDValue PerformInsertEltCombine(SDNode *N,
5088 TargetLowering::DAGCombinerInfo &DCI) {
5089 // Bitcast an i64 load inserted into a vector to f64.
5090 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5091 EVT VT = N->getValueType(0);
5092 SDNode *Elt = N->getOperand(1).getNode();
5093 if (VT.getVectorElementType() != MVT::i64 ||
5094 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5095 return SDValue();
5096
5097 SelectionDAG &DAG = DCI.DAG;
5098 DebugLoc dl = N->getDebugLoc();
5099 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5100 VT.getVectorNumElements());
5101 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5102 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5103 // Make the DAGCombiner fold the bitcasts.
5104 DCI.AddToWorklist(Vec.getNode());
5105 DCI.AddToWorklist(V.getNode());
5106 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5107 Vec, V, N->getOperand(2));
5108 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005109}
5110
Bob Wilsonf20700c2010-10-27 20:38:28 +00005111/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5112/// ISD::VECTOR_SHUFFLE.
5113static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5114 // The LLVM shufflevector instruction does not require the shuffle mask
5115 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5116 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5117 // operands do not match the mask length, they are extended by concatenating
5118 // them with undef vectors. That is probably the right thing for other
5119 // targets, but for NEON it is better to concatenate two double-register
5120 // size vector operands into a single quad-register size vector. Do that
5121 // transformation here:
5122 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5123 // shuffle(concat(v1, v2), undef)
5124 SDValue Op0 = N->getOperand(0);
5125 SDValue Op1 = N->getOperand(1);
5126 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5127 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5128 Op0.getNumOperands() != 2 ||
5129 Op1.getNumOperands() != 2)
5130 return SDValue();
5131 SDValue Concat0Op1 = Op0.getOperand(1);
5132 SDValue Concat1Op1 = Op1.getOperand(1);
5133 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5134 Concat1Op1.getOpcode() != ISD::UNDEF)
5135 return SDValue();
5136 // Skip the transformation if any of the types are illegal.
5137 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5138 EVT VT = N->getValueType(0);
5139 if (!TLI.isTypeLegal(VT) ||
5140 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5141 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5142 return SDValue();
5143
5144 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5145 Op0.getOperand(0), Op1.getOperand(0));
5146 // Translate the shuffle mask.
5147 SmallVector<int, 16> NewMask;
5148 unsigned NumElts = VT.getVectorNumElements();
5149 unsigned HalfElts = NumElts/2;
5150 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5151 for (unsigned n = 0; n < NumElts; ++n) {
5152 int MaskElt = SVN->getMaskElt(n);
5153 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005154 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005155 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005156 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005157 NewElt = HalfElts + MaskElt - NumElts;
5158 NewMask.push_back(NewElt);
5159 }
5160 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5161 DAG.getUNDEF(VT), NewMask.data());
5162}
5163
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005164/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5165/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5166/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5167/// return true.
5168static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5169 SelectionDAG &DAG = DCI.DAG;
5170 EVT VT = N->getValueType(0);
5171 // vldN-dup instructions only support 64-bit vectors for N > 1.
5172 if (!VT.is64BitVector())
5173 return false;
5174
5175 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5176 SDNode *VLD = N->getOperand(0).getNode();
5177 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5178 return false;
5179 unsigned NumVecs = 0;
5180 unsigned NewOpc = 0;
5181 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5182 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5183 NumVecs = 2;
5184 NewOpc = ARMISD::VLD2DUP;
5185 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5186 NumVecs = 3;
5187 NewOpc = ARMISD::VLD3DUP;
5188 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5189 NumVecs = 4;
5190 NewOpc = ARMISD::VLD4DUP;
5191 } else {
5192 return false;
5193 }
5194
5195 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5196 // numbers match the load.
5197 unsigned VLDLaneNo =
5198 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5199 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5200 UI != UE; ++UI) {
5201 // Ignore uses of the chain result.
5202 if (UI.getUse().getResNo() == NumVecs)
5203 continue;
5204 SDNode *User = *UI;
5205 if (User->getOpcode() != ARMISD::VDUPLANE ||
5206 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5207 return false;
5208 }
5209
5210 // Create the vldN-dup node.
5211 EVT Tys[5];
5212 unsigned n;
5213 for (n = 0; n < NumVecs; ++n)
5214 Tys[n] = VT;
5215 Tys[n] = MVT::Other;
5216 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5217 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5218 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5219 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5220 Ops, 2, VLDMemInt->getMemoryVT(),
5221 VLDMemInt->getMemOperand());
5222
5223 // Update the uses.
5224 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5225 UI != UE; ++UI) {
5226 unsigned ResNo = UI.getUse().getResNo();
5227 // Ignore uses of the chain result.
5228 if (ResNo == NumVecs)
5229 continue;
5230 SDNode *User = *UI;
5231 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5232 }
5233
5234 // Now the vldN-lane intrinsic is dead except for its chain result.
5235 // Update uses of the chain.
5236 std::vector<SDValue> VLDDupResults;
5237 for (unsigned n = 0; n < NumVecs; ++n)
5238 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5239 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5240 DCI.CombineTo(VLD, VLDDupResults);
5241
5242 return true;
5243}
5244
Bob Wilson9e82bf12010-07-14 01:22:12 +00005245/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5246/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005247static SDValue PerformVDUPLANECombine(SDNode *N,
5248 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005249 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005250
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005251 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5252 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5253 if (CombineVLDDUP(N, DCI))
5254 return SDValue(N, 0);
5255
5256 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5257 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005258 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005259 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005260 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005261 return SDValue();
5262
5263 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5264 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5265 // The canonical VMOV for a zero vector uses a 32-bit element size.
5266 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5267 unsigned EltBits;
5268 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5269 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005270 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005271 if (EltSize > VT.getVectorElementType().getSizeInBits())
5272 return SDValue();
5273
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005274 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005275}
5276
Bob Wilson5bafff32009-06-22 23:27:02 +00005277/// getVShiftImm - Check if this is a valid build_vector for the immediate
5278/// operand of a vector shift operation, where all the elements of the
5279/// build_vector must have the same constant integer value.
5280static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5281 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005282 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005283 Op = Op.getOperand(0);
5284 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5285 APInt SplatBits, SplatUndef;
5286 unsigned SplatBitSize;
5287 bool HasAnyUndefs;
5288 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5289 HasAnyUndefs, ElementBits) ||
5290 SplatBitSize > ElementBits)
5291 return false;
5292 Cnt = SplatBits.getSExtValue();
5293 return true;
5294}
5295
5296/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5297/// operand of a vector shift left operation. That value must be in the range:
5298/// 0 <= Value < ElementBits for a left shift; or
5299/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005300static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005301 assert(VT.isVector() && "vector shift count is not a vector type");
5302 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5303 if (! getVShiftImm(Op, ElementBits, Cnt))
5304 return false;
5305 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5306}
5307
5308/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5309/// operand of a vector shift right operation. For a shift opcode, the value
5310/// is positive, but for an intrinsic the value count must be negative. The
5311/// absolute value must be in the range:
5312/// 1 <= |Value| <= ElementBits for a right shift; or
5313/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005314static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005315 int64_t &Cnt) {
5316 assert(VT.isVector() && "vector shift count is not a vector type");
5317 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5318 if (! getVShiftImm(Op, ElementBits, Cnt))
5319 return false;
5320 if (isIntrinsic)
5321 Cnt = -Cnt;
5322 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5323}
5324
5325/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5326static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5327 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5328 switch (IntNo) {
5329 default:
5330 // Don't do anything for most intrinsics.
5331 break;
5332
5333 // Vector shifts: check for immediate versions and lower them.
5334 // Note: This is done during DAG combining instead of DAG legalizing because
5335 // the build_vectors for 64-bit vector element shift counts are generally
5336 // not legal, and it is hard to see their values after they get legalized to
5337 // loads from a constant pool.
5338 case Intrinsic::arm_neon_vshifts:
5339 case Intrinsic::arm_neon_vshiftu:
5340 case Intrinsic::arm_neon_vshiftls:
5341 case Intrinsic::arm_neon_vshiftlu:
5342 case Intrinsic::arm_neon_vshiftn:
5343 case Intrinsic::arm_neon_vrshifts:
5344 case Intrinsic::arm_neon_vrshiftu:
5345 case Intrinsic::arm_neon_vrshiftn:
5346 case Intrinsic::arm_neon_vqshifts:
5347 case Intrinsic::arm_neon_vqshiftu:
5348 case Intrinsic::arm_neon_vqshiftsu:
5349 case Intrinsic::arm_neon_vqshiftns:
5350 case Intrinsic::arm_neon_vqshiftnu:
5351 case Intrinsic::arm_neon_vqshiftnsu:
5352 case Intrinsic::arm_neon_vqrshiftns:
5353 case Intrinsic::arm_neon_vqrshiftnu:
5354 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00005355 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005356 int64_t Cnt;
5357 unsigned VShiftOpc = 0;
5358
5359 switch (IntNo) {
5360 case Intrinsic::arm_neon_vshifts:
5361 case Intrinsic::arm_neon_vshiftu:
5362 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
5363 VShiftOpc = ARMISD::VSHL;
5364 break;
5365 }
5366 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
5367 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
5368 ARMISD::VSHRs : ARMISD::VSHRu);
5369 break;
5370 }
5371 return SDValue();
5372
5373 case Intrinsic::arm_neon_vshiftls:
5374 case Intrinsic::arm_neon_vshiftlu:
5375 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
5376 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005377 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005378
5379 case Intrinsic::arm_neon_vrshifts:
5380 case Intrinsic::arm_neon_vrshiftu:
5381 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
5382 break;
5383 return SDValue();
5384
5385 case Intrinsic::arm_neon_vqshifts:
5386 case Intrinsic::arm_neon_vqshiftu:
5387 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5388 break;
5389 return SDValue();
5390
5391 case Intrinsic::arm_neon_vqshiftsu:
5392 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5393 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005394 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005395
5396 case Intrinsic::arm_neon_vshiftn:
5397 case Intrinsic::arm_neon_vrshiftn:
5398 case Intrinsic::arm_neon_vqshiftns:
5399 case Intrinsic::arm_neon_vqshiftnu:
5400 case Intrinsic::arm_neon_vqshiftnsu:
5401 case Intrinsic::arm_neon_vqrshiftns:
5402 case Intrinsic::arm_neon_vqrshiftnu:
5403 case Intrinsic::arm_neon_vqrshiftnsu:
5404 // Narrowing shifts require an immediate right shift.
5405 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
5406 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00005407 llvm_unreachable("invalid shift count for narrowing vector shift "
5408 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005409
5410 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005411 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00005412 }
5413
5414 switch (IntNo) {
5415 case Intrinsic::arm_neon_vshifts:
5416 case Intrinsic::arm_neon_vshiftu:
5417 // Opcode already set above.
5418 break;
5419 case Intrinsic::arm_neon_vshiftls:
5420 case Intrinsic::arm_neon_vshiftlu:
5421 if (Cnt == VT.getVectorElementType().getSizeInBits())
5422 VShiftOpc = ARMISD::VSHLLi;
5423 else
5424 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
5425 ARMISD::VSHLLs : ARMISD::VSHLLu);
5426 break;
5427 case Intrinsic::arm_neon_vshiftn:
5428 VShiftOpc = ARMISD::VSHRN; break;
5429 case Intrinsic::arm_neon_vrshifts:
5430 VShiftOpc = ARMISD::VRSHRs; break;
5431 case Intrinsic::arm_neon_vrshiftu:
5432 VShiftOpc = ARMISD::VRSHRu; break;
5433 case Intrinsic::arm_neon_vrshiftn:
5434 VShiftOpc = ARMISD::VRSHRN; break;
5435 case Intrinsic::arm_neon_vqshifts:
5436 VShiftOpc = ARMISD::VQSHLs; break;
5437 case Intrinsic::arm_neon_vqshiftu:
5438 VShiftOpc = ARMISD::VQSHLu; break;
5439 case Intrinsic::arm_neon_vqshiftsu:
5440 VShiftOpc = ARMISD::VQSHLsu; break;
5441 case Intrinsic::arm_neon_vqshiftns:
5442 VShiftOpc = ARMISD::VQSHRNs; break;
5443 case Intrinsic::arm_neon_vqshiftnu:
5444 VShiftOpc = ARMISD::VQSHRNu; break;
5445 case Intrinsic::arm_neon_vqshiftnsu:
5446 VShiftOpc = ARMISD::VQSHRNsu; break;
5447 case Intrinsic::arm_neon_vqrshiftns:
5448 VShiftOpc = ARMISD::VQRSHRNs; break;
5449 case Intrinsic::arm_neon_vqrshiftnu:
5450 VShiftOpc = ARMISD::VQRSHRNu; break;
5451 case Intrinsic::arm_neon_vqrshiftnsu:
5452 VShiftOpc = ARMISD::VQRSHRNsu; break;
5453 }
5454
5455 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005456 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005457 }
5458
5459 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00005460 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005461 int64_t Cnt;
5462 unsigned VShiftOpc = 0;
5463
5464 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
5465 VShiftOpc = ARMISD::VSLI;
5466 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
5467 VShiftOpc = ARMISD::VSRI;
5468 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005469 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005470 }
5471
5472 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
5473 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00005474 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005475 }
5476
5477 case Intrinsic::arm_neon_vqrshifts:
5478 case Intrinsic::arm_neon_vqrshiftu:
5479 // No immediate versions of these to check for.
5480 break;
5481 }
5482
5483 return SDValue();
5484}
5485
5486/// PerformShiftCombine - Checks for immediate versions of vector shifts and
5487/// lowers them. As with the vector shift intrinsics, this is done during DAG
5488/// combining instead of DAG legalizing because the build_vectors for 64-bit
5489/// vector element shift counts are generally not legal, and it is hard to see
5490/// their values after they get legalized to loads from a constant pool.
5491static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
5492 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00005493 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00005494
5495 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00005496 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5497 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00005498 return SDValue();
5499
5500 assert(ST->hasNEON() && "unexpected vector shift");
5501 int64_t Cnt;
5502
5503 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005504 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005505
5506 case ISD::SHL:
5507 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
5508 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005509 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005510 break;
5511
5512 case ISD::SRA:
5513 case ISD::SRL:
5514 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
5515 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
5516 ARMISD::VSHRs : ARMISD::VSHRu);
5517 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005518 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005519 }
5520 }
5521 return SDValue();
5522}
5523
5524/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
5525/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
5526static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
5527 const ARMSubtarget *ST) {
5528 SDValue N0 = N->getOperand(0);
5529
5530 // Check for sign- and zero-extensions of vector extract operations of 8-
5531 // and 16-bit vector elements. NEON supports these directly. They are
5532 // handled during DAG combining because type legalization will promote them
5533 // to 32-bit types and it is messy to recognize the operations after that.
5534 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5535 SDValue Vec = N0.getOperand(0);
5536 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005537 EVT VT = N->getValueType(0);
5538 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005539 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5540
Owen Anderson825b72b2009-08-11 20:47:22 +00005541 if (VT == MVT::i32 &&
5542 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00005543 TLI.isTypeLegal(Vec.getValueType()) &&
5544 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005545
5546 unsigned Opc = 0;
5547 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005548 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005549 case ISD::SIGN_EXTEND:
5550 Opc = ARMISD::VGETLANEs;
5551 break;
5552 case ISD::ZERO_EXTEND:
5553 case ISD::ANY_EXTEND:
5554 Opc = ARMISD::VGETLANEu;
5555 break;
5556 }
5557 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
5558 }
5559 }
5560
5561 return SDValue();
5562}
5563
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005564/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
5565/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
5566static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
5567 const ARMSubtarget *ST) {
5568 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00005569 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005570 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
5571 // a NaN; only do the transformation when it matches that behavior.
5572
5573 // For now only do this when using NEON for FP operations; if using VFP, it
5574 // is not obvious that the benefit outweighs the cost of switching to the
5575 // NEON pipeline.
5576 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
5577 N->getValueType(0) != MVT::f32)
5578 return SDValue();
5579
5580 SDValue CondLHS = N->getOperand(0);
5581 SDValue CondRHS = N->getOperand(1);
5582 SDValue LHS = N->getOperand(2);
5583 SDValue RHS = N->getOperand(3);
5584 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
5585
5586 unsigned Opcode = 0;
5587 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00005588 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005589 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00005590 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005591 IsReversed = true ; // x CC y ? y : x
5592 } else {
5593 return SDValue();
5594 }
5595
Bob Wilsone742bb52010-02-24 22:15:53 +00005596 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005597 switch (CC) {
5598 default: break;
5599 case ISD::SETOLT:
5600 case ISD::SETOLE:
5601 case ISD::SETLT:
5602 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005603 case ISD::SETULT:
5604 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005605 // If LHS is NaN, an ordered comparison will be false and the result will
5606 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
5607 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5608 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
5609 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5610 break;
5611 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
5612 // will return -0, so vmin can only be used for unsafe math or if one of
5613 // the operands is known to be nonzero.
5614 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
5615 !UnsafeFPMath &&
5616 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5617 break;
5618 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005619 break;
5620
5621 case ISD::SETOGT:
5622 case ISD::SETOGE:
5623 case ISD::SETGT:
5624 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005625 case ISD::SETUGT:
5626 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005627 // If LHS is NaN, an ordered comparison will be false and the result will
5628 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
5629 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5630 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
5631 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5632 break;
5633 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
5634 // will return +0, so vmax can only be used for unsafe math or if one of
5635 // the operands is known to be nonzero.
5636 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
5637 !UnsafeFPMath &&
5638 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5639 break;
5640 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005641 break;
5642 }
5643
5644 if (!Opcode)
5645 return SDValue();
5646 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
5647}
5648
Dan Gohman475871a2008-07-27 21:46:04 +00005649SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005650 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005651 switch (N->getOpcode()) {
5652 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005653 case ISD::ADD: return PerformADDCombine(N, DCI);
5654 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005655 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005656 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00005657 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00005658 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00005659 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005660 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00005661 case ISD::STORE: return PerformSTORECombine(N, DCI);
5662 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
5663 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00005664 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005665 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005666 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005667 case ISD::SHL:
5668 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005669 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005670 case ISD::SIGN_EXTEND:
5671 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005672 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
5673 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005674 }
Dan Gohman475871a2008-07-27 21:46:04 +00005675 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005676}
5677
Bill Wendlingaf566342009-08-15 21:21:19 +00005678bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00005679 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00005680 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00005681
5682 switch (VT.getSimpleVT().SimpleTy) {
5683 default:
5684 return false;
5685 case MVT::i8:
5686 case MVT::i16:
5687 case MVT::i32:
5688 return true;
5689 // FIXME: VLD1 etc with standard alignment is legal.
5690 }
5691}
5692
Evan Chenge6c835f2009-08-14 20:09:37 +00005693static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
5694 if (V < 0)
5695 return false;
5696
5697 unsigned Scale = 1;
5698 switch (VT.getSimpleVT().SimpleTy) {
5699 default: return false;
5700 case MVT::i1:
5701 case MVT::i8:
5702 // Scale == 1;
5703 break;
5704 case MVT::i16:
5705 // Scale == 2;
5706 Scale = 2;
5707 break;
5708 case MVT::i32:
5709 // Scale == 4;
5710 Scale = 4;
5711 break;
5712 }
5713
5714 if ((V & (Scale - 1)) != 0)
5715 return false;
5716 V /= Scale;
5717 return V == (V & ((1LL << 5) - 1));
5718}
5719
5720static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
5721 const ARMSubtarget *Subtarget) {
5722 bool isNeg = false;
5723 if (V < 0) {
5724 isNeg = true;
5725 V = - V;
5726 }
5727
5728 switch (VT.getSimpleVT().SimpleTy) {
5729 default: return false;
5730 case MVT::i1:
5731 case MVT::i8:
5732 case MVT::i16:
5733 case MVT::i32:
5734 // + imm12 or - imm8
5735 if (isNeg)
5736 return V == (V & ((1LL << 8) - 1));
5737 return V == (V & ((1LL << 12) - 1));
5738 case MVT::f32:
5739 case MVT::f64:
5740 // Same as ARM mode. FIXME: NEON?
5741 if (!Subtarget->hasVFP2())
5742 return false;
5743 if ((V & 3) != 0)
5744 return false;
5745 V >>= 2;
5746 return V == (V & ((1LL << 8) - 1));
5747 }
5748}
5749
Evan Chengb01fad62007-03-12 23:30:29 +00005750/// isLegalAddressImmediate - Return true if the integer value can be used
5751/// as the offset of the target addressing mode for load / store of the
5752/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00005753static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005754 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00005755 if (V == 0)
5756 return true;
5757
Evan Cheng65011532009-03-09 19:15:00 +00005758 if (!VT.isSimple())
5759 return false;
5760
Evan Chenge6c835f2009-08-14 20:09:37 +00005761 if (Subtarget->isThumb1Only())
5762 return isLegalT1AddressImmediate(V, VT);
5763 else if (Subtarget->isThumb2())
5764 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00005765
Evan Chenge6c835f2009-08-14 20:09:37 +00005766 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00005767 if (V < 0)
5768 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00005769 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00005770 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 case MVT::i1:
5772 case MVT::i8:
5773 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00005774 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005775 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005776 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00005777 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005778 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005779 case MVT::f32:
5780 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00005781 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00005782 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00005783 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00005784 return false;
5785 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005786 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00005787 }
Evan Chenga8e29892007-01-19 07:51:42 +00005788}
5789
Evan Chenge6c835f2009-08-14 20:09:37 +00005790bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
5791 EVT VT) const {
5792 int Scale = AM.Scale;
5793 if (Scale < 0)
5794 return false;
5795
5796 switch (VT.getSimpleVT().SimpleTy) {
5797 default: return false;
5798 case MVT::i1:
5799 case MVT::i8:
5800 case MVT::i16:
5801 case MVT::i32:
5802 if (Scale == 1)
5803 return true;
5804 // r + r << imm
5805 Scale = Scale & ~1;
5806 return Scale == 2 || Scale == 4 || Scale == 8;
5807 case MVT::i64:
5808 // r + r
5809 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
5810 return true;
5811 return false;
5812 case MVT::isVoid:
5813 // Note, we allow "void" uses (basically, uses that aren't loads or
5814 // stores), because arm allows folding a scale into many arithmetic
5815 // operations. This should be made more precise and revisited later.
5816
5817 // Allow r << imm, but the imm has to be a multiple of two.
5818 if (Scale & 1) return false;
5819 return isPowerOf2_32(Scale);
5820 }
5821}
5822
Chris Lattner37caf8c2007-04-09 23:33:39 +00005823/// isLegalAddressingMode - Return true if the addressing mode represented
5824/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005825bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005826 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005827 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00005828 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00005829 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005830
Chris Lattner37caf8c2007-04-09 23:33:39 +00005831 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005832 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005833 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005834
Chris Lattner37caf8c2007-04-09 23:33:39 +00005835 switch (AM.Scale) {
5836 case 0: // no scale reg, must be "r+i" or "r", or "i".
5837 break;
5838 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00005839 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00005840 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005841 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00005842 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005843 // ARM doesn't support any R+R*scale+imm addr modes.
5844 if (AM.BaseOffs)
5845 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005846
Bob Wilson2c7dab12009-04-08 17:55:28 +00005847 if (!VT.isSimple())
5848 return false;
5849
Evan Chenge6c835f2009-08-14 20:09:37 +00005850 if (Subtarget->isThumb2())
5851 return isLegalT2ScaledAddressingMode(AM, VT);
5852
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005853 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00005854 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00005855 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005856 case MVT::i1:
5857 case MVT::i8:
5858 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005859 if (Scale < 0) Scale = -Scale;
5860 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005861 return true;
5862 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00005863 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00005864 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00005865 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005866 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005867 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005868 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00005869 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005870
Owen Anderson825b72b2009-08-11 20:47:22 +00005871 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005872 // Note, we allow "void" uses (basically, uses that aren't loads or
5873 // stores), because arm allows folding a scale into many arithmetic
5874 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005875
Chris Lattner37caf8c2007-04-09 23:33:39 +00005876 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00005877 if (Scale & 1) return false;
5878 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00005879 }
5880 break;
Evan Chengb01fad62007-03-12 23:30:29 +00005881 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00005882 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00005883}
5884
Evan Cheng77e47512009-11-11 19:05:52 +00005885/// isLegalICmpImmediate - Return true if the specified immediate is legal
5886/// icmp immediate, that is the target has icmp instructions which can compare
5887/// a register against the immediate without having to materialize the
5888/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00005889bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00005890 if (!Subtarget->isThumb())
5891 return ARM_AM::getSOImmVal(Imm) != -1;
5892 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00005893 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00005894 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00005895}
5896
Owen Andersone50ed302009-08-10 22:56:29 +00005897static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005898 bool isSEXTLoad, SDValue &Base,
5899 SDValue &Offset, bool &isInc,
5900 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00005901 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5902 return false;
5903
Owen Anderson825b72b2009-08-11 20:47:22 +00005904 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005905 // AddressingMode 3
5906 Base = Ptr->getOperand(0);
5907 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005908 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005909 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005910 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005911 isInc = false;
5912 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5913 return true;
5914 }
5915 }
5916 isInc = (Ptr->getOpcode() == ISD::ADD);
5917 Offset = Ptr->getOperand(1);
5918 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005919 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005920 // AddressingMode 2
5921 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005922 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005923 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005924 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005925 isInc = false;
5926 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5927 Base = Ptr->getOperand(0);
5928 return true;
5929 }
5930 }
5931
5932 if (Ptr->getOpcode() == ISD::ADD) {
5933 isInc = true;
5934 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5935 if (ShOpcVal != ARM_AM::no_shift) {
5936 Base = Ptr->getOperand(1);
5937 Offset = Ptr->getOperand(0);
5938 } else {
5939 Base = Ptr->getOperand(0);
5940 Offset = Ptr->getOperand(1);
5941 }
5942 return true;
5943 }
5944
5945 isInc = (Ptr->getOpcode() == ISD::ADD);
5946 Base = Ptr->getOperand(0);
5947 Offset = Ptr->getOperand(1);
5948 return true;
5949 }
5950
Jim Grosbache5165492009-11-09 00:11:35 +00005951 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005952 return false;
5953}
5954
Owen Andersone50ed302009-08-10 22:56:29 +00005955static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005956 bool isSEXTLoad, SDValue &Base,
5957 SDValue &Offset, bool &isInc,
5958 SelectionDAG &DAG) {
5959 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5960 return false;
5961
5962 Base = Ptr->getOperand(0);
5963 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5964 int RHSC = (int)RHS->getZExtValue();
5965 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
5966 assert(Ptr->getOpcode() == ISD::ADD);
5967 isInc = false;
5968 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5969 return true;
5970 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
5971 isInc = Ptr->getOpcode() == ISD::ADD;
5972 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
5973 return true;
5974 }
5975 }
5976
5977 return false;
5978}
5979
Evan Chenga8e29892007-01-19 07:51:42 +00005980/// getPreIndexedAddressParts - returns true by value, base pointer and
5981/// offset pointer and addressing mode by reference if the node's address
5982/// can be legally represented as pre-indexed load / store address.
5983bool
Dan Gohman475871a2008-07-27 21:46:04 +00005984ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
5985 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00005986 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00005987 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005988 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00005989 return false;
5990
Owen Andersone50ed302009-08-10 22:56:29 +00005991 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00005992 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00005993 bool isSEXTLoad = false;
5994 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5995 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005996 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00005997 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
5998 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5999 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006000 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006001 } else
6002 return false;
6003
6004 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006005 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006006 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006007 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6008 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006009 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006010 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006011 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006012 if (!isLegal)
6013 return false;
6014
6015 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6016 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006017}
6018
6019/// getPostIndexedAddressParts - returns true by value, base pointer and
6020/// offset pointer and addressing mode by reference if this node can be
6021/// combined with a load / store to form a post-indexed load / store.
6022bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006023 SDValue &Base,
6024 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006025 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006026 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006027 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006028 return false;
6029
Owen Andersone50ed302009-08-10 22:56:29 +00006030 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006031 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006032 bool isSEXTLoad = false;
6033 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006034 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006035 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006036 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6037 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006038 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006039 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006040 } else
6041 return false;
6042
6043 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006044 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006045 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006046 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006047 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006048 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006049 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6050 isInc, DAG);
6051 if (!isLegal)
6052 return false;
6053
Evan Cheng28dad2a2010-05-18 21:31:17 +00006054 if (Ptr != Base) {
6055 // Swap base ptr and offset to catch more post-index load / store when
6056 // it's legal. In Thumb2 mode, offset must be an immediate.
6057 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6058 !Subtarget->isThumb2())
6059 std::swap(Base, Offset);
6060
6061 // Post-indexed load / store update the base pointer.
6062 if (Ptr != Base)
6063 return false;
6064 }
6065
Evan Chenge88d5ce2009-07-02 07:28:31 +00006066 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6067 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006068}
6069
Dan Gohman475871a2008-07-27 21:46:04 +00006070void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006071 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006072 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006073 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006074 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006075 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006076 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006077 switch (Op.getOpcode()) {
6078 default: break;
6079 case ARMISD::CMOV: {
6080 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006081 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006082 if (KnownZero == 0 && KnownOne == 0) return;
6083
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006084 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006085 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6086 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006087 KnownZero &= KnownZeroRHS;
6088 KnownOne &= KnownOneRHS;
6089 return;
6090 }
6091 }
6092}
6093
6094//===----------------------------------------------------------------------===//
6095// ARM Inline Assembly Support
6096//===----------------------------------------------------------------------===//
6097
Evan Cheng55d42002011-01-08 01:24:27 +00006098bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6099 // Looking for "rev" which is V6+.
6100 if (!Subtarget->hasV6Ops())
6101 return false;
6102
6103 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6104 std::string AsmStr = IA->getAsmString();
6105 SmallVector<StringRef, 4> AsmPieces;
6106 SplitString(AsmStr, AsmPieces, ";\n");
6107
6108 switch (AsmPieces.size()) {
6109 default: return false;
6110 case 1:
6111 AsmStr = AsmPieces[0];
6112 AsmPieces.clear();
6113 SplitString(AsmStr, AsmPieces, " \t,");
6114
6115 // rev $0, $1
6116 if (AsmPieces.size() == 3 &&
6117 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6118 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6119 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6120 if (Ty && Ty->getBitWidth() == 32)
6121 return IntrinsicLowering::LowerToByteSwap(CI);
6122 }
6123 break;
6124 }
6125
6126 return false;
6127}
6128
Evan Chenga8e29892007-01-19 07:51:42 +00006129/// getConstraintType - Given a constraint letter, return the type of
6130/// constraint it is for this target.
6131ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006132ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6133 if (Constraint.size() == 1) {
6134 switch (Constraint[0]) {
6135 default: break;
6136 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006137 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006138 }
Evan Chenga8e29892007-01-19 07:51:42 +00006139 }
Chris Lattner4234f572007-03-25 02:14:49 +00006140 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006141}
6142
John Thompson44ab89e2010-10-29 17:29:13 +00006143/// Examine constraint type and operand type and determine a weight value.
6144/// This object must already have been set up with the operand type
6145/// and the current alternative constraint selected.
6146TargetLowering::ConstraintWeight
6147ARMTargetLowering::getSingleConstraintMatchWeight(
6148 AsmOperandInfo &info, const char *constraint) const {
6149 ConstraintWeight weight = CW_Invalid;
6150 Value *CallOperandVal = info.CallOperandVal;
6151 // If we don't have a value, we can't do a match,
6152 // but allow it at the lowest weight.
6153 if (CallOperandVal == NULL)
6154 return CW_Default;
6155 const Type *type = CallOperandVal->getType();
6156 // Look at the constraint type.
6157 switch (*constraint) {
6158 default:
6159 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6160 break;
6161 case 'l':
6162 if (type->isIntegerTy()) {
6163 if (Subtarget->isThumb())
6164 weight = CW_SpecificReg;
6165 else
6166 weight = CW_Register;
6167 }
6168 break;
6169 case 'w':
6170 if (type->isFloatingPointTy())
6171 weight = CW_Register;
6172 break;
6173 }
6174 return weight;
6175}
6176
Bob Wilson2dc4f542009-03-20 22:42:55 +00006177std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006178ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006179 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006180 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006181 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006182 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006183 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006184 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006185 return std::make_pair(0U, ARM::tGPRRegisterClass);
6186 else
6187 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006188 case 'r':
6189 return std::make_pair(0U, ARM::GPRRegisterClass);
6190 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006191 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006192 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006193 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006194 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006195 if (VT.getSizeInBits() == 128)
6196 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006197 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006198 }
6199 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006200 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006201 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006202
Evan Chenga8e29892007-01-19 07:51:42 +00006203 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6204}
6205
6206std::vector<unsigned> ARMTargetLowering::
6207getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006208 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006209 if (Constraint.size() != 1)
6210 return std::vector<unsigned>();
6211
6212 switch (Constraint[0]) { // GCC ARM Constraint Letters
6213 default: break;
6214 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006215 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6216 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6217 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006218 case 'r':
6219 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6220 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6221 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6222 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006223 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006224 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006225 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6226 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6227 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6228 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6229 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6230 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6231 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6232 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006233 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006234 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6235 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6236 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6237 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006238 if (VT.getSizeInBits() == 128)
6239 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6240 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006241 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006242 }
6243
6244 return std::vector<unsigned>();
6245}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006246
6247/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6248/// vector. If it is invalid, don't add anything to Ops.
6249void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6250 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006251 std::vector<SDValue>&Ops,
6252 SelectionDAG &DAG) const {
6253 SDValue Result(0, 0);
6254
6255 switch (Constraint) {
6256 default: break;
6257 case 'I': case 'J': case 'K': case 'L':
6258 case 'M': case 'N': case 'O':
6259 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6260 if (!C)
6261 return;
6262
6263 int64_t CVal64 = C->getSExtValue();
6264 int CVal = (int) CVal64;
6265 // None of these constraints allow values larger than 32 bits. Check
6266 // that the value fits in an int.
6267 if (CVal != CVal64)
6268 return;
6269
6270 switch (Constraint) {
6271 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006272 if (Subtarget->isThumb1Only()) {
6273 // This must be a constant between 0 and 255, for ADD
6274 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006275 if (CVal >= 0 && CVal <= 255)
6276 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006277 } else if (Subtarget->isThumb2()) {
6278 // A constant that can be used as an immediate value in a
6279 // data-processing instruction.
6280 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6281 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006282 } else {
6283 // A constant that can be used as an immediate value in a
6284 // data-processing instruction.
6285 if (ARM_AM::getSOImmVal(CVal) != -1)
6286 break;
6287 }
6288 return;
6289
6290 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006291 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006292 // This must be a constant between -255 and -1, for negated ADD
6293 // immediates. This can be used in GCC with an "n" modifier that
6294 // prints the negated value, for use with SUB instructions. It is
6295 // not useful otherwise but is implemented for compatibility.
6296 if (CVal >= -255 && CVal <= -1)
6297 break;
6298 } else {
6299 // This must be a constant between -4095 and 4095. It is not clear
6300 // what this constraint is intended for. Implemented for
6301 // compatibility with GCC.
6302 if (CVal >= -4095 && CVal <= 4095)
6303 break;
6304 }
6305 return;
6306
6307 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006308 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006309 // A 32-bit value where only one byte has a nonzero value. Exclude
6310 // zero to match GCC. This constraint is used by GCC internally for
6311 // constants that can be loaded with a move/shift combination.
6312 // It is not useful otherwise but is implemented for compatibility.
6313 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
6314 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006315 } else if (Subtarget->isThumb2()) {
6316 // A constant whose bitwise inverse can be used as an immediate
6317 // value in a data-processing instruction. This can be used in GCC
6318 // with a "B" modifier that prints the inverted value, for use with
6319 // BIC and MVN instructions. It is not useful otherwise but is
6320 // implemented for compatibility.
6321 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
6322 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006323 } else {
6324 // A constant whose bitwise inverse can be used as an immediate
6325 // value in a data-processing instruction. This can be used in GCC
6326 // with a "B" modifier that prints the inverted value, for use with
6327 // BIC and MVN instructions. It is not useful otherwise but is
6328 // implemented for compatibility.
6329 if (ARM_AM::getSOImmVal(~CVal) != -1)
6330 break;
6331 }
6332 return;
6333
6334 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006335 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006336 // This must be a constant between -7 and 7,
6337 // for 3-operand ADD/SUB immediate instructions.
6338 if (CVal >= -7 && CVal < 7)
6339 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006340 } else if (Subtarget->isThumb2()) {
6341 // A constant whose negation can be used as an immediate value in a
6342 // data-processing instruction. This can be used in GCC with an "n"
6343 // modifier that prints the negated value, for use with SUB
6344 // instructions. It is not useful otherwise but is implemented for
6345 // compatibility.
6346 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
6347 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006348 } else {
6349 // A constant whose negation can be used as an immediate value in a
6350 // data-processing instruction. This can be used in GCC with an "n"
6351 // modifier that prints the negated value, for use with SUB
6352 // instructions. It is not useful otherwise but is implemented for
6353 // compatibility.
6354 if (ARM_AM::getSOImmVal(-CVal) != -1)
6355 break;
6356 }
6357 return;
6358
6359 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006360 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006361 // This must be a multiple of 4 between 0 and 1020, for
6362 // ADD sp + immediate.
6363 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
6364 break;
6365 } else {
6366 // A power of two or a constant between 0 and 32. This is used in
6367 // GCC for the shift amount on shifted register operands, but it is
6368 // useful in general for any shift amounts.
6369 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
6370 break;
6371 }
6372 return;
6373
6374 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006375 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006376 // This must be a constant between 0 and 31, for shift amounts.
6377 if (CVal >= 0 && CVal <= 31)
6378 break;
6379 }
6380 return;
6381
6382 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006383 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006384 // This must be a multiple of 4 between -508 and 508, for
6385 // ADD/SUB sp = sp + immediate.
6386 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
6387 break;
6388 }
6389 return;
6390 }
6391 Result = DAG.getTargetConstant(CVal, Op.getValueType());
6392 break;
6393 }
6394
6395 if (Result.getNode()) {
6396 Ops.push_back(Result);
6397 return;
6398 }
Dale Johannesen1784d162010-06-25 21:55:36 +00006399 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006400}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00006401
6402bool
6403ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
6404 // The ARM target isn't yet aware of offsets.
6405 return false;
6406}
Evan Cheng39382422009-10-28 01:44:26 +00006407
6408int ARM::getVFPf32Imm(const APFloat &FPImm) {
6409 APInt Imm = FPImm.bitcastToAPInt();
6410 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
6411 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
6412 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
6413
6414 // We can handle 4 bits of mantissa.
6415 // mantissa = (16+UInt(e:f:g:h))/16.
6416 if (Mantissa & 0x7ffff)
6417 return -1;
6418 Mantissa >>= 19;
6419 if ((Mantissa & 0xf) != Mantissa)
6420 return -1;
6421
6422 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6423 if (Exp < -3 || Exp > 4)
6424 return -1;
6425 Exp = ((Exp+3) & 0x7) ^ 4;
6426
6427 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6428}
6429
6430int ARM::getVFPf64Imm(const APFloat &FPImm) {
6431 APInt Imm = FPImm.bitcastToAPInt();
6432 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
6433 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
6434 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
6435
6436 // We can handle 4 bits of mantissa.
6437 // mantissa = (16+UInt(e:f:g:h))/16.
6438 if (Mantissa & 0xffffffffffffLL)
6439 return -1;
6440 Mantissa >>= 48;
6441 if ((Mantissa & 0xf) != Mantissa)
6442 return -1;
6443
6444 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6445 if (Exp < -3 || Exp > 4)
6446 return -1;
6447 Exp = ((Exp+3) & 0x7) ^ 4;
6448
6449 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6450}
6451
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006452bool ARM::isBitFieldInvertedMask(unsigned v) {
6453 if (v == 0xffffffff)
6454 return 0;
6455 // there can be 1's on either or both "outsides", all the "inside"
6456 // bits must be 0's
6457 unsigned int lsb = 0, msb = 31;
6458 while (v & (1 << msb)) --msb;
6459 while (v & (1 << lsb)) ++lsb;
6460 for (unsigned int i = lsb; i <= msb; ++i) {
6461 if (v & (1 << i))
6462 return 0;
6463 }
6464 return 1;
6465}
6466
Evan Cheng39382422009-10-28 01:44:26 +00006467/// isFPImmLegal - Returns true if the target can instruction select the
6468/// specified FP immediate natively. If false, the legalizer will
6469/// materialize the FP immediate as a load from a constant pool.
6470bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
6471 if (!Subtarget->hasVFP3())
6472 return false;
6473 if (VT == MVT::f32)
6474 return ARM::getVFPf32Imm(Imm) != -1;
6475 if (VT == MVT::f64)
6476 return ARM::getVFPf64Imm(Imm) != -1;
6477 return false;
6478}
Bob Wilson65ffec42010-09-21 17:56:22 +00006479
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006480/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00006481/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
6482/// specified in the intrinsic calls.
6483bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
6484 const CallInst &I,
6485 unsigned Intrinsic) const {
6486 switch (Intrinsic) {
6487 case Intrinsic::arm_neon_vld1:
6488 case Intrinsic::arm_neon_vld2:
6489 case Intrinsic::arm_neon_vld3:
6490 case Intrinsic::arm_neon_vld4:
6491 case Intrinsic::arm_neon_vld2lane:
6492 case Intrinsic::arm_neon_vld3lane:
6493 case Intrinsic::arm_neon_vld4lane: {
6494 Info.opc = ISD::INTRINSIC_W_CHAIN;
6495 // Conservatively set memVT to the entire set of vectors loaded.
6496 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
6497 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6498 Info.ptrVal = I.getArgOperand(0);
6499 Info.offset = 0;
6500 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6501 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6502 Info.vol = false; // volatile loads with NEON intrinsics not supported
6503 Info.readMem = true;
6504 Info.writeMem = false;
6505 return true;
6506 }
6507 case Intrinsic::arm_neon_vst1:
6508 case Intrinsic::arm_neon_vst2:
6509 case Intrinsic::arm_neon_vst3:
6510 case Intrinsic::arm_neon_vst4:
6511 case Intrinsic::arm_neon_vst2lane:
6512 case Intrinsic::arm_neon_vst3lane:
6513 case Intrinsic::arm_neon_vst4lane: {
6514 Info.opc = ISD::INTRINSIC_VOID;
6515 // Conservatively set memVT to the entire set of vectors stored.
6516 unsigned NumElts = 0;
6517 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
6518 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
6519 if (!ArgTy->isVectorTy())
6520 break;
6521 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
6522 }
6523 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6524 Info.ptrVal = I.getArgOperand(0);
6525 Info.offset = 0;
6526 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6527 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6528 Info.vol = false; // volatile stores with NEON intrinsics not supported
6529 Info.readMem = false;
6530 Info.writeMem = true;
6531 return true;
6532 }
6533 default:
6534 break;
6535 }
6536
6537 return false;
6538}