blob: 3710be21eb8bdc0779b2b73ca0dad48ea56ec3c4 [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 Cheng5de5d4b2011-01-17 08:03:18 +0000737 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000738 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
739 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000740 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000741 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
742 case ARMISD::tCALL: return "ARMISD::tCALL";
743 case ARMISD::BRCOND: return "ARMISD::BRCOND";
744 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000745 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000746 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
747 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
748 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000749 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000750 case ARMISD::CMPFP: return "ARMISD::CMPFP";
751 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000752 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000753 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
754 case ARMISD::CMOV: return "ARMISD::CMOV";
755 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000756
Jim Grosbach3482c802010-01-18 19:58:49 +0000757 case ARMISD::RBIT: return "ARMISD::RBIT";
758
Bob Wilson76a312b2010-03-19 22:51:32 +0000759 case ARMISD::FTOSI: return "ARMISD::FTOSI";
760 case ARMISD::FTOUI: return "ARMISD::FTOUI";
761 case ARMISD::SITOF: return "ARMISD::SITOF";
762 case ARMISD::UITOF: return "ARMISD::UITOF";
763
Evan Chenga8e29892007-01-19 07:51:42 +0000764 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
765 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
766 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000767
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000768 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
769 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000770
Evan Chengc5942082009-10-28 06:55:03 +0000771 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
772 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000773 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000774
Dale Johannesen51e28e62010-06-03 21:09:53 +0000775 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000776
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000777 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000778
Evan Cheng86198642009-08-07 00:34:42 +0000779 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
780
Jim Grosbach3728e962009-12-10 00:11:09 +0000781 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000782 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000783
Evan Chengdfed19f2010-11-03 06:34:55 +0000784 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
785
Bob Wilson5bafff32009-06-22 23:27:02 +0000786 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000787 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000788 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000789 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
790 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000791 case ARMISD::VCGEU: return "ARMISD::VCGEU";
792 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000793 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
794 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000795 case ARMISD::VCGTU: return "ARMISD::VCGTU";
796 case ARMISD::VTST: return "ARMISD::VTST";
797
798 case ARMISD::VSHL: return "ARMISD::VSHL";
799 case ARMISD::VSHRs: return "ARMISD::VSHRs";
800 case ARMISD::VSHRu: return "ARMISD::VSHRu";
801 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
802 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
803 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
804 case ARMISD::VSHRN: return "ARMISD::VSHRN";
805 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
806 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
807 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
808 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
809 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
810 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
811 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
812 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
813 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
814 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
815 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
816 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
817 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
818 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000819 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000820 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000821 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000822 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000823 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000824 case ARMISD::VREV64: return "ARMISD::VREV64";
825 case ARMISD::VREV32: return "ARMISD::VREV32";
826 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000827 case ARMISD::VZIP: return "ARMISD::VZIP";
828 case ARMISD::VUZP: return "ARMISD::VUZP";
829 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000830 case ARMISD::VMULLs: return "ARMISD::VMULLs";
831 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000832 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000833 case ARMISD::FMAX: return "ARMISD::FMAX";
834 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000835 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000836 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
837 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000838 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
839 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
840 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Evan Chenga8e29892007-01-19 07:51:42 +0000841 }
842}
843
Evan Cheng06b666c2010-05-15 02:18:07 +0000844/// getRegClassFor - Return the register class that should be used for the
845/// specified value type.
846TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
847 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
848 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
849 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000850 if (Subtarget->hasNEON()) {
851 if (VT == MVT::v4i64)
852 return ARM::QQPRRegisterClass;
853 else if (VT == MVT::v8i64)
854 return ARM::QQQQPRRegisterClass;
855 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000856 return TargetLowering::getRegClassFor(VT);
857}
858
Eric Christopherab695882010-07-21 22:26:11 +0000859// Create a fast isel object.
860FastISel *
861ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
862 return ARM::createFastISel(funcInfo);
863}
864
Bill Wendlingb4202b82009-07-01 18:50:55 +0000865/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000866unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000867 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000868}
869
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000870/// getMaximalGlobalOffset - Returns the maximal possible offset which can
871/// be used for loads / stores from the global.
872unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
873 return (Subtarget->isThumb1Only() ? 127 : 4095);
874}
875
Evan Cheng1cc39842010-05-20 23:26:43 +0000876Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000877 unsigned NumVals = N->getNumValues();
878 if (!NumVals)
879 return Sched::RegPressure;
880
881 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000882 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000883 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000884 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000885 if (VT.isFloatingPoint() || VT.isVector())
886 return Sched::Latency;
887 }
Evan Chengc10f5432010-05-28 23:25:23 +0000888
889 if (!N->isMachineOpcode())
890 return Sched::RegPressure;
891
892 // Load are scheduled for latency even if there instruction itinerary
893 // is not available.
894 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
895 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000896
897 if (TID.getNumDefs() == 0)
898 return Sched::RegPressure;
899 if (!Itins->isEmpty() &&
900 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000901 return Sched::Latency;
902
Evan Cheng1cc39842010-05-20 23:26:43 +0000903 return Sched::RegPressure;
904}
905
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000906// FIXME: Move to RegInfo
Evan Cheng31446872010-07-23 22:39:59 +0000907unsigned
908ARMTargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
909 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000910 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000911
Evan Cheng31446872010-07-23 22:39:59 +0000912 switch (RC->getID()) {
913 default:
914 return 0;
915 case ARM::tGPRRegClassID:
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000916 return TFI->hasFP(MF) ? 4 : 5;
Evan Chengac096802010-08-10 19:30:19 +0000917 case ARM::GPRRegClassID: {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000918 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
Evan Chengac096802010-08-10 19:30:19 +0000919 return 10 - FP - (Subtarget->isR9Reserved() ? 1 : 0);
920 }
Evan Cheng31446872010-07-23 22:39:59 +0000921 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
922 case ARM::DPRRegClassID:
923 return 32 - 10;
924 }
925}
926
Evan Chenga8e29892007-01-19 07:51:42 +0000927//===----------------------------------------------------------------------===//
928// Lowering Code
929//===----------------------------------------------------------------------===//
930
Evan Chenga8e29892007-01-19 07:51:42 +0000931/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
932static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
933 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000934 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000935 case ISD::SETNE: return ARMCC::NE;
936 case ISD::SETEQ: return ARMCC::EQ;
937 case ISD::SETGT: return ARMCC::GT;
938 case ISD::SETGE: return ARMCC::GE;
939 case ISD::SETLT: return ARMCC::LT;
940 case ISD::SETLE: return ARMCC::LE;
941 case ISD::SETUGT: return ARMCC::HI;
942 case ISD::SETUGE: return ARMCC::HS;
943 case ISD::SETULT: return ARMCC::LO;
944 case ISD::SETULE: return ARMCC::LS;
945 }
946}
947
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000948/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
949static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000950 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000951 CondCode2 = ARMCC::AL;
952 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000953 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000954 case ISD::SETEQ:
955 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
956 case ISD::SETGT:
957 case ISD::SETOGT: CondCode = ARMCC::GT; break;
958 case ISD::SETGE:
959 case ISD::SETOGE: CondCode = ARMCC::GE; break;
960 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000961 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000962 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
963 case ISD::SETO: CondCode = ARMCC::VC; break;
964 case ISD::SETUO: CondCode = ARMCC::VS; break;
965 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
966 case ISD::SETUGT: CondCode = ARMCC::HI; break;
967 case ISD::SETUGE: CondCode = ARMCC::PL; break;
968 case ISD::SETLT:
969 case ISD::SETULT: CondCode = ARMCC::LT; break;
970 case ISD::SETLE:
971 case ISD::SETULE: CondCode = ARMCC::LE; break;
972 case ISD::SETNE:
973 case ISD::SETUNE: CondCode = ARMCC::NE; break;
974 }
Evan Chenga8e29892007-01-19 07:51:42 +0000975}
976
Bob Wilson1f595bb2009-04-17 19:07:39 +0000977//===----------------------------------------------------------------------===//
978// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000979//===----------------------------------------------------------------------===//
980
981#include "ARMGenCallingConv.inc"
982
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000983/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
984/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000985CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000986 bool Return,
987 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000988 switch (CC) {
989 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +0000990 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000991 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +0000992 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +0000993 if (!Subtarget->isAAPCS_ABI())
994 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
995 // For AAPCS ABI targets, just use VFP variant of the calling convention.
996 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
997 }
998 // Fallthrough
999 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001000 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001001 if (!Subtarget->isAAPCS_ABI())
1002 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1003 else if (Subtarget->hasVFP2() &&
1004 FloatABIType == FloatABI::Hard && !isVarArg)
1005 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1006 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1007 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001008 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001009 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001010 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001011 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001012 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001013 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001014 }
1015}
1016
Dan Gohman98ca4f22009-08-05 01:29:28 +00001017/// LowerCallResult - Lower the result values of a call into the
1018/// appropriate copies out of appropriate physical registers.
1019SDValue
1020ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001021 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001022 const SmallVectorImpl<ISD::InputArg> &Ins,
1023 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001024 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001025
Bob Wilson1f595bb2009-04-17 19:07:39 +00001026 // Assign locations to each value returned by this call.
1027 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001028 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001029 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001030 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001031 CCAssignFnForNode(CallConv, /* Return*/ true,
1032 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001033
1034 // Copy all of the result registers out of their specified physreg.
1035 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1036 CCValAssign VA = RVLocs[i];
1037
Bob Wilson80915242009-04-25 00:33:20 +00001038 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001039 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001040 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001042 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001043 Chain = Lo.getValue(1);
1044 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001045 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001046 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001047 InFlag);
1048 Chain = Hi.getValue(1);
1049 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001050 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001051
Owen Anderson825b72b2009-08-11 20:47:22 +00001052 if (VA.getLocVT() == MVT::v2f64) {
1053 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1054 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1055 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001056
1057 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001058 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001059 Chain = Lo.getValue(1);
1060 InFlag = Lo.getValue(2);
1061 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001062 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001063 Chain = Hi.getValue(1);
1064 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001065 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001066 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1067 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001068 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001069 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001070 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1071 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001072 Chain = Val.getValue(1);
1073 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001074 }
Bob Wilson80915242009-04-25 00:33:20 +00001075
1076 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001077 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001078 case CCValAssign::Full: break;
1079 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001080 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001081 break;
1082 }
1083
Dan Gohman98ca4f22009-08-05 01:29:28 +00001084 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001085 }
1086
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001088}
1089
1090/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1091/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001092/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001093/// a byval function parameter.
1094/// Sometimes what we are copying is the end of a larger object, the part that
1095/// does not fit in registers.
1096static SDValue
1097CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1098 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1099 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001100 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001101 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001102 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001103 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104}
1105
Bob Wilsondee46d72009-04-17 20:35:10 +00001106/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001108ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1109 SDValue StackPtr, SDValue Arg,
1110 DebugLoc dl, SelectionDAG &DAG,
1111 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001112 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001113 unsigned LocMemOffset = VA.getLocMemOffset();
1114 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1115 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001116 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001118
Bob Wilson1f595bb2009-04-17 19:07:39 +00001119 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001120 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001121 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001122}
1123
Dan Gohman98ca4f22009-08-05 01:29:28 +00001124void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001125 SDValue Chain, SDValue &Arg,
1126 RegsToPassVector &RegsToPass,
1127 CCValAssign &VA, CCValAssign &NextVA,
1128 SDValue &StackPtr,
1129 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001130 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001131
Jim Grosbache5165492009-11-09 00:11:35 +00001132 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001133 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001134 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1135
1136 if (NextVA.isRegLoc())
1137 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1138 else {
1139 assert(NextVA.isMemLoc());
1140 if (StackPtr.getNode() == 0)
1141 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1142
Dan Gohman98ca4f22009-08-05 01:29:28 +00001143 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1144 dl, DAG, NextVA,
1145 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001146 }
1147}
1148
Dan Gohman98ca4f22009-08-05 01:29:28 +00001149/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001150/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1151/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001152SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001153ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001154 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001155 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001156 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001157 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001158 const SmallVectorImpl<ISD::InputArg> &Ins,
1159 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001160 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001161 MachineFunction &MF = DAG.getMachineFunction();
1162 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1163 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001164 // Temporarily disable tail calls so things don't break.
1165 if (!EnableARMTailCalls)
1166 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001167 if (isTailCall) {
1168 // Check if it's really possible to do a tail call.
1169 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1170 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001171 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001172 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1173 // detected sibcalls.
1174 if (isTailCall) {
1175 ++NumTailCalls;
1176 IsSibCall = true;
1177 }
1178 }
Evan Chenga8e29892007-01-19 07:51:42 +00001179
Bob Wilson1f595bb2009-04-17 19:07:39 +00001180 // Analyze operands of the call, assigning locations to each operand.
1181 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001182 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1183 *DAG.getContext());
1184 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001185 CCAssignFnForNode(CallConv, /* Return*/ false,
1186 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001187
Bob Wilson1f595bb2009-04-17 19:07:39 +00001188 // Get a count of how many bytes are to be pushed on the stack.
1189 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001190
Dale Johannesen51e28e62010-06-03 21:09:53 +00001191 // For tail calls, memory operands are available in our caller's stack.
1192 if (IsSibCall)
1193 NumBytes = 0;
1194
Evan Chenga8e29892007-01-19 07:51:42 +00001195 // Adjust the stack pointer for the new arguments...
1196 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001197 if (!IsSibCall)
1198 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001199
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001200 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001201
Bob Wilson5bafff32009-06-22 23:27:02 +00001202 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001203 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001204
Bob Wilson1f595bb2009-04-17 19:07:39 +00001205 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001206 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1208 i != e;
1209 ++i, ++realArgIdx) {
1210 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001211 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001212 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001213
Bob Wilson1f595bb2009-04-17 19:07:39 +00001214 // Promote the value if needed.
1215 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001216 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001217 case CCValAssign::Full: break;
1218 case CCValAssign::SExt:
1219 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1220 break;
1221 case CCValAssign::ZExt:
1222 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1223 break;
1224 case CCValAssign::AExt:
1225 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1226 break;
1227 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001228 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001229 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001230 }
1231
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001232 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001234 if (VA.getLocVT() == MVT::v2f64) {
1235 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1236 DAG.getConstant(0, MVT::i32));
1237 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1238 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001239
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001241 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1242
1243 VA = ArgLocs[++i]; // skip ahead to next loc
1244 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001246 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1247 } else {
1248 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001249
Dan Gohman98ca4f22009-08-05 01:29:28 +00001250 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1251 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001252 }
1253 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001254 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001255 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 }
1257 } else if (VA.isRegLoc()) {
1258 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001259 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261
Dan Gohman98ca4f22009-08-05 01:29:28 +00001262 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1263 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001264 }
Evan Chenga8e29892007-01-19 07:51:42 +00001265 }
1266
1267 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001269 &MemOpChains[0], MemOpChains.size());
1270
1271 // Build a sequence of copy-to-reg nodes chained together with token chain
1272 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001273 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001274 // Tail call byval lowering might overwrite argument registers so in case of
1275 // tail call optimization the copies to registers are lowered later.
1276 if (!isTailCall)
1277 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1278 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1279 RegsToPass[i].second, InFlag);
1280 InFlag = Chain.getValue(1);
1281 }
Evan Chenga8e29892007-01-19 07:51:42 +00001282
Dale Johannesen51e28e62010-06-03 21:09:53 +00001283 // For tail calls lower the arguments to the 'real' stack slot.
1284 if (isTailCall) {
1285 // Force all the incoming stack arguments to be loaded from the stack
1286 // before any new outgoing arguments are stored to the stack, because the
1287 // outgoing stack slots may alias the incoming argument stack slots, and
1288 // the alias isn't otherwise explicit. This is slightly more conservative
1289 // than necessary, because it means that each store effectively depends
1290 // on every argument instead of just those arguments it would clobber.
1291
1292 // Do not flag preceeding copytoreg stuff together with the following stuff.
1293 InFlag = SDValue();
1294 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1295 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1296 RegsToPass[i].second, InFlag);
1297 InFlag = Chain.getValue(1);
1298 }
1299 InFlag =SDValue();
1300 }
1301
Bill Wendling056292f2008-09-16 21:48:12 +00001302 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1303 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1304 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001305 bool isDirect = false;
1306 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001307 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001308 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001309
1310 if (EnableARMLongCalls) {
1311 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1312 && "long-calls with non-static relocation model!");
1313 // Handle a global address or an external symbol. If it's not one of
1314 // those, the target's already in a register, so we don't need to do
1315 // anything extra.
1316 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001317 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001318 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001319 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001320 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1321 ARMPCLabelIndex,
1322 ARMCP::CPValue, 0);
1323 // Get the address of the callee into a register
1324 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1325 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1326 Callee = DAG.getLoad(getPointerTy(), dl,
1327 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001328 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001329 false, false, 0);
1330 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1331 const char *Sym = S->getSymbol();
1332
1333 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001334 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001335 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1336 Sym, ARMPCLabelIndex, 0);
1337 // Get the address of the callee into a register
1338 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1339 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1340 Callee = DAG.getLoad(getPointerTy(), dl,
1341 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001342 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001343 false, false, 0);
1344 }
1345 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001346 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001347 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001348 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001349 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001350 getTargetMachine().getRelocationModel() != Reloc::Static;
1351 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001352 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001353 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001354 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001355 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001356 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001357 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001358 ARMPCLabelIndex,
1359 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001360 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001361 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001362 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001363 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001364 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001365 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001366 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001367 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001368 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001369 } else {
1370 // On ELF targets for PIC code, direct calls should go through the PLT
1371 unsigned OpFlags = 0;
1372 if (Subtarget->isTargetELF() &&
1373 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1374 OpFlags = ARMII::MO_PLT;
1375 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1376 }
Bill Wendling056292f2008-09-16 21:48:12 +00001377 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001378 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001379 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001380 getTargetMachine().getRelocationModel() != Reloc::Static;
1381 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001382 // tBX takes a register source operand.
1383 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001384 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001385 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001386 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001387 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001388 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001389 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001390 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001391 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001392 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001393 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001394 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001395 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001396 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001397 } else {
1398 unsigned OpFlags = 0;
1399 // On ELF targets for PIC code, direct calls should go through the PLT
1400 if (Subtarget->isTargetELF() &&
1401 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1402 OpFlags = ARMII::MO_PLT;
1403 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1404 }
Evan Chenga8e29892007-01-19 07:51:42 +00001405 }
1406
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001407 // FIXME: handle tail calls differently.
1408 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001409 if (Subtarget->isThumb()) {
1410 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001411 CallOpc = ARMISD::CALL_NOLINK;
1412 else
1413 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1414 } else {
1415 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001416 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1417 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001418 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001419
Dan Gohman475871a2008-07-27 21:46:04 +00001420 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001421 Ops.push_back(Chain);
1422 Ops.push_back(Callee);
1423
1424 // Add argument registers to the end of the list so that they are known live
1425 // into the call.
1426 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1427 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1428 RegsToPass[i].second.getValueType()));
1429
Gabor Greifba36cb52008-08-28 21:40:38 +00001430 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001431 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001432
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001433 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001434 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001435 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001436
Duncan Sands4bdcb612008-07-02 17:40:58 +00001437 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001438 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001439 InFlag = Chain.getValue(1);
1440
Chris Lattnere563bbc2008-10-11 22:08:30 +00001441 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1442 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001443 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001444 InFlag = Chain.getValue(1);
1445
Bob Wilson1f595bb2009-04-17 19:07:39 +00001446 // Handle result values, copying them out of physregs into vregs that we
1447 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1449 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001450}
1451
Dale Johannesen51e28e62010-06-03 21:09:53 +00001452/// MatchingStackOffset - Return true if the given stack call argument is
1453/// already available in the same position (relatively) of the caller's
1454/// incoming argument stack.
1455static
1456bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1457 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1458 const ARMInstrInfo *TII) {
1459 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1460 int FI = INT_MAX;
1461 if (Arg.getOpcode() == ISD::CopyFromReg) {
1462 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001463 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001464 return false;
1465 MachineInstr *Def = MRI->getVRegDef(VR);
1466 if (!Def)
1467 return false;
1468 if (!Flags.isByVal()) {
1469 if (!TII->isLoadFromStackSlot(Def, FI))
1470 return false;
1471 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001472 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001473 }
1474 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1475 if (Flags.isByVal())
1476 // ByVal argument is passed in as a pointer but it's now being
1477 // dereferenced. e.g.
1478 // define @foo(%struct.X* %A) {
1479 // tail call @bar(%struct.X* byval %A)
1480 // }
1481 return false;
1482 SDValue Ptr = Ld->getBasePtr();
1483 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1484 if (!FINode)
1485 return false;
1486 FI = FINode->getIndex();
1487 } else
1488 return false;
1489
1490 assert(FI != INT_MAX);
1491 if (!MFI->isFixedObjectIndex(FI))
1492 return false;
1493 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1494}
1495
1496/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1497/// for tail call optimization. Targets which want to do tail call
1498/// optimization should implement this function.
1499bool
1500ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1501 CallingConv::ID CalleeCC,
1502 bool isVarArg,
1503 bool isCalleeStructRet,
1504 bool isCallerStructRet,
1505 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001506 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001507 const SmallVectorImpl<ISD::InputArg> &Ins,
1508 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001509 const Function *CallerF = DAG.getMachineFunction().getFunction();
1510 CallingConv::ID CallerCC = CallerF->getCallingConv();
1511 bool CCMatch = CallerCC == CalleeCC;
1512
1513 // Look for obvious safe cases to perform tail call optimization that do not
1514 // require ABI changes. This is what gcc calls sibcall.
1515
Jim Grosbach7616b642010-06-16 23:45:49 +00001516 // Do not sibcall optimize vararg calls unless the call site is not passing
1517 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001518 if (isVarArg && !Outs.empty())
1519 return false;
1520
1521 // Also avoid sibcall optimization if either caller or callee uses struct
1522 // return semantics.
1523 if (isCalleeStructRet || isCallerStructRet)
1524 return false;
1525
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001526 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001527 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001528 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1529 // LR. This means if we need to reload LR, it takes an extra instructions,
1530 // which outweighs the value of the tail call; but here we don't know yet
1531 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001532 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001533 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001534
1535 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1536 // but we need to make sure there are enough registers; the only valid
1537 // registers are the 4 used for parameters. We don't currently do this
1538 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001539 if (Subtarget->isThumb1Only())
1540 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001541
Dale Johannesen51e28e62010-06-03 21:09:53 +00001542 // If the calling conventions do not match, then we'd better make sure the
1543 // results are returned in the same way as what the caller expects.
1544 if (!CCMatch) {
1545 SmallVector<CCValAssign, 16> RVLocs1;
1546 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1547 RVLocs1, *DAG.getContext());
1548 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1549
1550 SmallVector<CCValAssign, 16> RVLocs2;
1551 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1552 RVLocs2, *DAG.getContext());
1553 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1554
1555 if (RVLocs1.size() != RVLocs2.size())
1556 return false;
1557 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1558 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1559 return false;
1560 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1561 return false;
1562 if (RVLocs1[i].isRegLoc()) {
1563 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1564 return false;
1565 } else {
1566 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1567 return false;
1568 }
1569 }
1570 }
1571
1572 // If the callee takes no arguments then go on to check the results of the
1573 // call.
1574 if (!Outs.empty()) {
1575 // Check if stack adjustment is needed. For now, do not do this if any
1576 // argument is passed on the stack.
1577 SmallVector<CCValAssign, 16> ArgLocs;
1578 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1579 ArgLocs, *DAG.getContext());
1580 CCInfo.AnalyzeCallOperands(Outs,
1581 CCAssignFnForNode(CalleeCC, false, isVarArg));
1582 if (CCInfo.getNextStackOffset()) {
1583 MachineFunction &MF = DAG.getMachineFunction();
1584
1585 // Check if the arguments are already laid out in the right way as
1586 // the caller's fixed stack objects.
1587 MachineFrameInfo *MFI = MF.getFrameInfo();
1588 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1589 const ARMInstrInfo *TII =
1590 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001591 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1592 i != e;
1593 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001594 CCValAssign &VA = ArgLocs[i];
1595 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001596 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001597 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001598 if (VA.getLocInfo() == CCValAssign::Indirect)
1599 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001600 if (VA.needsCustom()) {
1601 // f64 and vector types are split into multiple registers or
1602 // register/stack-slot combinations. The types will not match
1603 // the registers; give up on memory f64 refs until we figure
1604 // out what to do about this.
1605 if (!VA.isRegLoc())
1606 return false;
1607 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001608 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001609 if (RegVT == MVT::v2f64) {
1610 if (!ArgLocs[++i].isRegLoc())
1611 return false;
1612 if (!ArgLocs[++i].isRegLoc())
1613 return false;
1614 }
1615 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001616 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1617 MFI, MRI, TII))
1618 return false;
1619 }
1620 }
1621 }
1622 }
1623
1624 return true;
1625}
1626
Dan Gohman98ca4f22009-08-05 01:29:28 +00001627SDValue
1628ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001629 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001630 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001631 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001632 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001633
Bob Wilsondee46d72009-04-17 20:35:10 +00001634 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001635 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001636
Bob Wilsondee46d72009-04-17 20:35:10 +00001637 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001638 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1639 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001640
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001642 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1643 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001644
1645 // If this is the first return lowered for this function, add
1646 // the regs to the liveout set for the function.
1647 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1648 for (unsigned i = 0; i != RVLocs.size(); ++i)
1649 if (RVLocs[i].isRegLoc())
1650 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001651 }
1652
Bob Wilson1f595bb2009-04-17 19:07:39 +00001653 SDValue Flag;
1654
1655 // Copy the result values into the output registers.
1656 for (unsigned i = 0, realRVLocIdx = 0;
1657 i != RVLocs.size();
1658 ++i, ++realRVLocIdx) {
1659 CCValAssign &VA = RVLocs[i];
1660 assert(VA.isRegLoc() && "Can only return in registers!");
1661
Dan Gohmanc9403652010-07-07 15:54:55 +00001662 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001663
1664 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001665 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001666 case CCValAssign::Full: break;
1667 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001668 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001669 break;
1670 }
1671
Bob Wilson1f595bb2009-04-17 19:07:39 +00001672 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001673 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001674 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001675 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1676 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001677 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001678 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001679
1680 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1681 Flag = Chain.getValue(1);
1682 VA = RVLocs[++i]; // skip ahead to next loc
1683 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1684 HalfGPRs.getValue(1), Flag);
1685 Flag = Chain.getValue(1);
1686 VA = RVLocs[++i]; // skip ahead to next loc
1687
1688 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001689 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1690 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001691 }
1692 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1693 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001694 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001696 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001697 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001698 VA = RVLocs[++i]; // skip ahead to next loc
1699 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1700 Flag);
1701 } else
1702 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1703
Bob Wilsondee46d72009-04-17 20:35:10 +00001704 // Guarantee that all emitted copies are
1705 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001706 Flag = Chain.getValue(1);
1707 }
1708
1709 SDValue result;
1710 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001712 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001714
1715 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001716}
1717
Evan Cheng3d2125c2010-11-30 23:55:39 +00001718bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1719 if (N->getNumValues() != 1)
1720 return false;
1721 if (!N->hasNUsesOfValue(1, 0))
1722 return false;
1723
1724 unsigned NumCopies = 0;
1725 SDNode* Copies[2];
1726 SDNode *Use = *N->use_begin();
1727 if (Use->getOpcode() == ISD::CopyToReg) {
1728 Copies[NumCopies++] = Use;
1729 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1730 // f64 returned in a pair of GPRs.
1731 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1732 UI != UE; ++UI) {
1733 if (UI->getOpcode() != ISD::CopyToReg)
1734 return false;
1735 Copies[UI.getUse().getResNo()] = *UI;
1736 ++NumCopies;
1737 }
1738 } else if (Use->getOpcode() == ISD::BITCAST) {
1739 // f32 returned in a single GPR.
1740 if (!Use->hasNUsesOfValue(1, 0))
1741 return false;
1742 Use = *Use->use_begin();
1743 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1744 return false;
1745 Copies[NumCopies++] = Use;
1746 } else {
1747 return false;
1748 }
1749
1750 if (NumCopies != 1 && NumCopies != 2)
1751 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001752
1753 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001754 for (unsigned i = 0; i < NumCopies; ++i) {
1755 SDNode *Copy = Copies[i];
1756 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1757 UI != UE; ++UI) {
1758 if (UI->getOpcode() == ISD::CopyToReg) {
1759 SDNode *Use = *UI;
1760 if (Use == Copies[0] || Use == Copies[1])
1761 continue;
1762 return false;
1763 }
1764 if (UI->getOpcode() != ARMISD::RET_FLAG)
1765 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001766 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001767 }
1768 }
1769
Evan Cheng1bf891a2010-12-01 22:59:46 +00001770 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001771}
1772
Bob Wilsonb62d2572009-11-03 00:02:05 +00001773// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1774// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1775// one of the above mentioned nodes. It has to be wrapped because otherwise
1776// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1777// be used to form addressing mode. These wrapped nodes will be selected
1778// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001779static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001780 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001781 // FIXME there is no actual debug info here
1782 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001783 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001784 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001785 if (CP->isMachineConstantPoolEntry())
1786 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1787 CP->getAlignment());
1788 else
1789 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1790 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001791 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001792}
1793
Jim Grosbache1102ca2010-07-19 17:20:38 +00001794unsigned ARMTargetLowering::getJumpTableEncoding() const {
1795 return MachineJumpTableInfo::EK_Inline;
1796}
1797
Dan Gohmand858e902010-04-17 15:26:15 +00001798SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1799 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001800 MachineFunction &MF = DAG.getMachineFunction();
1801 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1802 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001803 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001804 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001805 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001806 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1807 SDValue CPAddr;
1808 if (RelocM == Reloc::Static) {
1809 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1810 } else {
1811 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001812 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001813 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1814 ARMCP::CPBlockAddress,
1815 PCAdj);
1816 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1817 }
1818 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1819 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001820 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001821 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001822 if (RelocM == Reloc::Static)
1823 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001824 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001825 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001826}
1827
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001828// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001829SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001830ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001831 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001832 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001833 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001834 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001835 MachineFunction &MF = DAG.getMachineFunction();
1836 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001837 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001838 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001839 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001840 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001841 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001842 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001843 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001844 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001845 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001846 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001847
Evan Chenge7e0d622009-11-06 22:24:13 +00001848 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001849 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001850
1851 // call __tls_get_addr.
1852 ArgListTy Args;
1853 ArgListEntry Entry;
1854 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001855 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001856 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001857 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001858 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001859 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1860 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001861 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001862 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001863 return CallResult.first;
1864}
1865
1866// Lower ISD::GlobalTLSAddress using the "initial exec" or
1867// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001868SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001869ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001870 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001871 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001872 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001873 SDValue Offset;
1874 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001875 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001876 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001877 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001878
Chris Lattner4fb63d02009-07-15 04:12:33 +00001879 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001880 MachineFunction &MF = DAG.getMachineFunction();
1881 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001882 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001883 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001884 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1885 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001886 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001887 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001888 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001889 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001890 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001891 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001892 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001893 Chain = Offset.getValue(1);
1894
Evan Chenge7e0d622009-11-06 22:24:13 +00001895 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001896 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001897
Evan Cheng9eda6892009-10-31 03:39:36 +00001898 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001899 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001900 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001901 } else {
1902 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001903 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001904 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001905 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001906 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001907 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001908 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001909 }
1910
1911 // The address of the thread local variable is the add of the thread
1912 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001913 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001914}
1915
Dan Gohman475871a2008-07-27 21:46:04 +00001916SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001917ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001918 // TODO: implement the "local dynamic" model
1919 assert(Subtarget->isTargetELF() &&
1920 "TLS not implemented for non-ELF targets");
1921 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1922 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1923 // otherwise use the "Local Exec" TLS Model
1924 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1925 return LowerToTLSGeneralDynamicModel(GA, DAG);
1926 else
1927 return LowerToTLSExecModels(GA, DAG);
1928}
1929
Dan Gohman475871a2008-07-27 21:46:04 +00001930SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001931 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001932 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001933 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001934 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001935 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1936 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001937 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001938 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001939 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001940 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001941 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001942 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001943 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001944 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001945 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001946 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001947 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001948 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001949 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001950 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001951 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001952 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001953 }
1954
1955 // If we have T2 ops, we can materialize the address directly via movt/movw
1956 // pair. This is always cheaper.
1957 if (Subtarget->useMovt()) {
1958 // FIXME: Once remat is capable of dealing with instructions with register
1959 // operands, expand this into two nodes.
1960 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1961 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001962 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001963 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1964 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1965 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1966 MachinePointerInfo::getConstantPool(),
1967 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001968 }
1969}
1970
Dan Gohman475871a2008-07-27 21:46:04 +00001971SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001972 SelectionDAG &DAG) const {
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();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001977 MachineFunction &MF = DAG.getMachineFunction();
1978 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1979
1980 if (Subtarget->useMovt()) {
1981 // FIXME: Once remat is capable of dealing with instructions with register
1982 // operands, expand this into two nodes.
1983 if (RelocM != Reloc::PIC_)
1984 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1985 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
1986
1987 // FIXME: Not a constant pool!
1988 unsigned PICLabelIndex = AFI->createPICLabelUId();
1989 SDValue PICLabel = DAG.getConstant(PICLabelIndex, MVT::i32);
1990 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT,
1991 DAG.getTargetGlobalAddress(GV, dl, PtrVT),
1992 PICLabel);
1993 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1994 }
1995
1996 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001997 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001998 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00001999 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002000 } else {
2001 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002002 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2003 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002004 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002005 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002006 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002008
Evan Cheng9eda6892009-10-31 03:39:36 +00002009 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002010 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002011 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002012 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002013
2014 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002015 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002016 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002017 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002018
Evan Cheng63476a82009-09-03 07:04:02 +00002019 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002020 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002021 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002022
2023 return Result;
2024}
2025
Dan Gohman475871a2008-07-27 21:46:04 +00002026SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002027 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002028 assert(Subtarget->isTargetELF() &&
2029 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002030 MachineFunction &MF = DAG.getMachineFunction();
2031 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002032 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002033 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002034 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002035 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002036 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2037 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002038 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002039 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002041 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002042 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002043 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002044 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002045 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002046}
2047
Jim Grosbach0e0da732009-05-12 23:59:14 +00002048SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002049ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2050 const {
2051 DebugLoc dl = Op.getDebugLoc();
2052 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
2053 Op.getOperand(0), Op.getOperand(1));
2054}
2055
2056SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002057ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2058 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002059 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002060 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2061 Op.getOperand(1), Val);
2062}
2063
2064SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002065ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2066 DebugLoc dl = Op.getDebugLoc();
2067 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2068 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2069}
2070
2071SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002072ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002073 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002074 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002075 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002076 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002077 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002078 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002079 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002080 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2081 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002082 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002083 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002084 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002085 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002086 EVT PtrVT = getPointerTy();
2087 DebugLoc dl = Op.getDebugLoc();
2088 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2089 SDValue CPAddr;
2090 unsigned PCAdj = (RelocM != Reloc::PIC_)
2091 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002092 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002093 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2094 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002095 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002096 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002097 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002098 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002099 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002100 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002101
2102 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002103 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002104 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2105 }
2106 return Result;
2107 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002108 }
2109}
2110
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002111static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002112 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002113 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002114 if (!Subtarget->hasDataBarrier()) {
2115 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2116 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2117 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002118 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002119 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002120 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002121 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002122 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002123
2124 SDValue Op5 = Op.getOperand(5);
2125 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2126 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2127 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2128 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2129
2130 ARM_MB::MemBOpt DMBOpt;
2131 if (isDeviceBarrier)
2132 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2133 else
2134 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2135 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2136 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002137}
2138
Evan Chengdfed19f2010-11-03 06:34:55 +00002139static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2140 const ARMSubtarget *Subtarget) {
2141 // ARM pre v5TE and Thumb1 does not have preload instructions.
2142 if (!(Subtarget->isThumb2() ||
2143 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2144 // Just preserve the chain.
2145 return Op.getOperand(0);
2146
2147 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002148 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2149 if (!isRead &&
2150 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2151 // ARMv7 with MP extension has PLDW.
2152 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002153
2154 if (Subtarget->isThumb())
2155 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002156 isRead = ~isRead & 1;
2157 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002158
Evan Cheng416941d2010-11-04 05:19:35 +00002159 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002160 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002161 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2162 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002163}
2164
Dan Gohman1e93df62010-04-17 14:41:14 +00002165static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2166 MachineFunction &MF = DAG.getMachineFunction();
2167 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2168
Evan Chenga8e29892007-01-19 07:51:42 +00002169 // vastart just stores the address of the VarArgsFrameIndex slot into the
2170 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002171 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002172 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002173 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002174 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002175 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2176 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002177}
2178
Dan Gohman475871a2008-07-27 21:46:04 +00002179SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002180ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2181 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002182 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002183 MachineFunction &MF = DAG.getMachineFunction();
2184 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2185
2186 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002187 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002188 RC = ARM::tGPRRegisterClass;
2189 else
2190 RC = ARM::GPRRegisterClass;
2191
2192 // Transform the arguments stored in physical registers into virtual ones.
Jim Grosbach4725ca72010-09-08 03:54:02 +00002193 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002194 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002195
2196 SDValue ArgValue2;
2197 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002198 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002199 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002200
2201 // Create load node to retrieve arguments from the stack.
2202 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002203 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002204 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002205 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002206 } else {
2207 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002208 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002209 }
2210
Jim Grosbache5165492009-11-09 00:11:35 +00002211 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002212}
2213
2214SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002215ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002216 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002217 const SmallVectorImpl<ISD::InputArg>
2218 &Ins,
2219 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002220 SmallVectorImpl<SDValue> &InVals)
2221 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002222
Bob Wilson1f595bb2009-04-17 19:07:39 +00002223 MachineFunction &MF = DAG.getMachineFunction();
2224 MachineFrameInfo *MFI = MF.getFrameInfo();
2225
Bob Wilson1f595bb2009-04-17 19:07:39 +00002226 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2227
2228 // Assign locations to all of the incoming arguments.
2229 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002230 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2231 *DAG.getContext());
2232 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002233 CCAssignFnForNode(CallConv, /* Return*/ false,
2234 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002235
2236 SmallVector<SDValue, 16> ArgValues;
2237
2238 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2239 CCValAssign &VA = ArgLocs[i];
2240
Bob Wilsondee46d72009-04-17 20:35:10 +00002241 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002242 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002243 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002244
Bob Wilson5bafff32009-06-22 23:27:02 +00002245 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002246 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002247 // f64 and vector types are split up into multiple registers or
2248 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002249 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002250 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002251 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002252 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002253 SDValue ArgValue2;
2254 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002255 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002256 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2257 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002258 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002259 false, false, 0);
2260 } else {
2261 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2262 Chain, DAG, dl);
2263 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002264 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2265 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002266 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002267 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002268 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2269 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002270 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002271
Bob Wilson5bafff32009-06-22 23:27:02 +00002272 } else {
2273 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002274
Owen Anderson825b72b2009-08-11 20:47:22 +00002275 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002276 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002278 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002279 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002280 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002281 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002282 RC = (AFI->isThumb1OnlyFunction() ?
2283 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002284 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002285 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002286
2287 // Transform the arguments in physical registers into virtual ones.
2288 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002289 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002290 }
2291
2292 // If this is an 8 or 16-bit value, it is really passed promoted
2293 // to 32 bits. Insert an assert[sz]ext to capture this, then
2294 // truncate to the right size.
2295 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002296 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002297 case CCValAssign::Full: break;
2298 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002299 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002300 break;
2301 case CCValAssign::SExt:
2302 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2303 DAG.getValueType(VA.getValVT()));
2304 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2305 break;
2306 case CCValAssign::ZExt:
2307 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2308 DAG.getValueType(VA.getValVT()));
2309 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2310 break;
2311 }
2312
Dan Gohman98ca4f22009-08-05 01:29:28 +00002313 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002314
2315 } else { // VA.isRegLoc()
2316
2317 // sanity check
2318 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002319 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002320
2321 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002322 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002323
Bob Wilsondee46d72009-04-17 20:35:10 +00002324 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002325 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002326 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002327 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002328 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002329 }
2330 }
2331
2332 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002333 if (isVarArg) {
2334 static const unsigned GPRArgRegs[] = {
2335 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2336 };
2337
Bob Wilsondee46d72009-04-17 20:35:10 +00002338 unsigned NumGPRs = CCInfo.getFirstUnallocated
2339 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002340
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002341 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002342 unsigned VARegSize = (4 - NumGPRs) * 4;
2343 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002344 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002345 if (VARegSaveSize) {
2346 // If this function is vararg, store any remaining integer argument regs
2347 // to their spots on the stack so that they may be loaded by deferencing
2348 // the result of va_next.
2349 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002350 AFI->setVarArgsFrameIndex(
2351 MFI->CreateFixedObject(VARegSaveSize,
2352 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002353 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002354 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2355 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002356
Dan Gohman475871a2008-07-27 21:46:04 +00002357 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002358 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002359 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002360 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002361 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002362 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002363 RC = ARM::GPRRegisterClass;
2364
Bob Wilson998e1252009-04-20 18:36:57 +00002365 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002366 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002367 SDValue Store =
2368 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002369 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2370 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002371 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002372 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002373 DAG.getConstant(4, getPointerTy()));
2374 }
2375 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002376 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002377 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002378 } else
2379 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002380 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002381 }
2382
Dan Gohman98ca4f22009-08-05 01:29:28 +00002383 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002384}
2385
2386/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002387static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002388 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002389 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002390 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002391 // Maybe this has already been legalized into the constant pool?
2392 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002393 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002394 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002395 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002396 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002397 }
2398 }
2399 return false;
2400}
2401
Evan Chenga8e29892007-01-19 07:51:42 +00002402/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2403/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002404SDValue
2405ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002406 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002407 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002408 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002409 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002410 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002411 // Constant does not fit, try adjusting it by one?
2412 switch (CC) {
2413 default: break;
2414 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002415 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002416 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002417 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002418 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002419 }
2420 break;
2421 case ISD::SETULT:
2422 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002423 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002424 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002425 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002426 }
2427 break;
2428 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002429 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002430 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002431 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002432 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002433 }
2434 break;
2435 case ISD::SETULE:
2436 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002437 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002438 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002439 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002440 }
2441 break;
2442 }
2443 }
2444 }
2445
2446 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002447 ARMISD::NodeType CompareType;
2448 switch (CondCode) {
2449 default:
2450 CompareType = ARMISD::CMP;
2451 break;
2452 case ARMCC::EQ:
2453 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002454 // Uses only Z Flag
2455 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002456 break;
2457 }
Evan Cheng218977b2010-07-13 19:27:42 +00002458 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002459 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002460}
2461
2462/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002463SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002464ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002465 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002466 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002467 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002468 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002469 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002470 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2471 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002472}
2473
Bill Wendlingde2b1512010-08-11 08:43:16 +00002474SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2475 SDValue Cond = Op.getOperand(0);
2476 SDValue SelectTrue = Op.getOperand(1);
2477 SDValue SelectFalse = Op.getOperand(2);
2478 DebugLoc dl = Op.getDebugLoc();
2479
2480 // Convert:
2481 //
2482 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2483 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2484 //
2485 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2486 const ConstantSDNode *CMOVTrue =
2487 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2488 const ConstantSDNode *CMOVFalse =
2489 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2490
2491 if (CMOVTrue && CMOVFalse) {
2492 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2493 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2494
2495 SDValue True;
2496 SDValue False;
2497 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2498 True = SelectTrue;
2499 False = SelectFalse;
2500 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2501 True = SelectFalse;
2502 False = SelectTrue;
2503 }
2504
2505 if (True.getNode() && False.getNode()) {
2506 EVT VT = Cond.getValueType();
2507 SDValue ARMcc = Cond.getOperand(2);
2508 SDValue CCR = Cond.getOperand(3);
2509 SDValue Cmp = Cond.getOperand(4);
2510 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2511 }
2512 }
2513 }
2514
2515 return DAG.getSelectCC(dl, Cond,
2516 DAG.getConstant(0, Cond.getValueType()),
2517 SelectTrue, SelectFalse, ISD::SETNE);
2518}
2519
Dan Gohmand858e902010-04-17 15:26:15 +00002520SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002521 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002522 SDValue LHS = Op.getOperand(0);
2523 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002524 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002525 SDValue TrueVal = Op.getOperand(2);
2526 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002527 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002528
Owen Anderson825b72b2009-08-11 20:47:22 +00002529 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002530 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002531 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002532 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2533 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002534 }
2535
2536 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002537 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002538
Evan Cheng218977b2010-07-13 19:27:42 +00002539 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2540 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002542 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002543 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002544 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002545 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002546 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002547 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002548 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002549 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002550 }
2551 return Result;
2552}
2553
Evan Cheng218977b2010-07-13 19:27:42 +00002554/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2555/// to morph to an integer compare sequence.
2556static bool canChangeToInt(SDValue Op, bool &SeenZero,
2557 const ARMSubtarget *Subtarget) {
2558 SDNode *N = Op.getNode();
2559 if (!N->hasOneUse())
2560 // Otherwise it requires moving the value from fp to integer registers.
2561 return false;
2562 if (!N->getNumValues())
2563 return false;
2564 EVT VT = Op.getValueType();
2565 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2566 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2567 // vmrs are very slow, e.g. cortex-a8.
2568 return false;
2569
2570 if (isFloatingPointZero(Op)) {
2571 SeenZero = true;
2572 return true;
2573 }
2574 return ISD::isNormalLoad(N);
2575}
2576
2577static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2578 if (isFloatingPointZero(Op))
2579 return DAG.getConstant(0, MVT::i32);
2580
2581 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2582 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002583 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002584 Ld->isVolatile(), Ld->isNonTemporal(),
2585 Ld->getAlignment());
2586
2587 llvm_unreachable("Unknown VFP cmp argument!");
2588}
2589
2590static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2591 SDValue &RetVal1, SDValue &RetVal2) {
2592 if (isFloatingPointZero(Op)) {
2593 RetVal1 = DAG.getConstant(0, MVT::i32);
2594 RetVal2 = DAG.getConstant(0, MVT::i32);
2595 return;
2596 }
2597
2598 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2599 SDValue Ptr = Ld->getBasePtr();
2600 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2601 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002602 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002603 Ld->isVolatile(), Ld->isNonTemporal(),
2604 Ld->getAlignment());
2605
2606 EVT PtrType = Ptr.getValueType();
2607 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2608 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2609 PtrType, Ptr, DAG.getConstant(4, PtrType));
2610 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2611 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002612 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002613 Ld->isVolatile(), Ld->isNonTemporal(),
2614 NewAlign);
2615 return;
2616 }
2617
2618 llvm_unreachable("Unknown VFP cmp argument!");
2619}
2620
2621/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2622/// f32 and even f64 comparisons to integer ones.
2623SDValue
2624ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2625 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002626 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002627 SDValue LHS = Op.getOperand(2);
2628 SDValue RHS = Op.getOperand(3);
2629 SDValue Dest = Op.getOperand(4);
2630 DebugLoc dl = Op.getDebugLoc();
2631
2632 bool SeenZero = false;
2633 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2634 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002635 // If one of the operand is zero, it's safe to ignore the NaN case since
2636 // we only care about equality comparisons.
2637 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002638 // If unsafe fp math optimization is enabled and there are no othter uses of
2639 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2640 // to an integer comparison.
2641 if (CC == ISD::SETOEQ)
2642 CC = ISD::SETEQ;
2643 else if (CC == ISD::SETUNE)
2644 CC = ISD::SETNE;
2645
2646 SDValue ARMcc;
2647 if (LHS.getValueType() == MVT::f32) {
2648 LHS = bitcastf32Toi32(LHS, DAG);
2649 RHS = bitcastf32Toi32(RHS, DAG);
2650 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2651 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2652 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2653 Chain, Dest, ARMcc, CCR, Cmp);
2654 }
2655
2656 SDValue LHS1, LHS2;
2657 SDValue RHS1, RHS2;
2658 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2659 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2660 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2661 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002662 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002663 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2664 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2665 }
2666
2667 return SDValue();
2668}
2669
2670SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2671 SDValue Chain = Op.getOperand(0);
2672 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2673 SDValue LHS = Op.getOperand(2);
2674 SDValue RHS = Op.getOperand(3);
2675 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002676 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002677
Owen Anderson825b72b2009-08-11 20:47:22 +00002678 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002679 SDValue ARMcc;
2680 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002681 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002682 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002683 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002684 }
2685
Owen Anderson825b72b2009-08-11 20:47:22 +00002686 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002687
2688 if (UnsafeFPMath &&
2689 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2690 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2691 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2692 if (Result.getNode())
2693 return Result;
2694 }
2695
Evan Chenga8e29892007-01-19 07:51:42 +00002696 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002697 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002698
Evan Cheng218977b2010-07-13 19:27:42 +00002699 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2700 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002701 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002702 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002703 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002704 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002705 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002706 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2707 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002708 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002709 }
2710 return Res;
2711}
2712
Dan Gohmand858e902010-04-17 15:26:15 +00002713SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002714 SDValue Chain = Op.getOperand(0);
2715 SDValue Table = Op.getOperand(1);
2716 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002717 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002718
Owen Andersone50ed302009-08-10 22:56:29 +00002719 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002720 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2721 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002722 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002723 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002724 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002725 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2726 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002727 if (Subtarget->isThumb2()) {
2728 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2729 // which does another jump to the destination. This also makes it easier
2730 // to translate it to TBB / TBH later.
2731 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002732 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002733 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002734 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002735 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002736 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002737 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002738 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002739 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002740 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002741 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002742 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002743 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002744 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002745 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002746 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002747 }
Evan Chenga8e29892007-01-19 07:51:42 +00002748}
2749
Bob Wilson76a312b2010-03-19 22:51:32 +00002750static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2751 DebugLoc dl = Op.getDebugLoc();
2752 unsigned Opc;
2753
2754 switch (Op.getOpcode()) {
2755 default:
2756 assert(0 && "Invalid opcode!");
2757 case ISD::FP_TO_SINT:
2758 Opc = ARMISD::FTOSI;
2759 break;
2760 case ISD::FP_TO_UINT:
2761 Opc = ARMISD::FTOUI;
2762 break;
2763 }
2764 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002765 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002766}
2767
2768static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2769 EVT VT = Op.getValueType();
2770 DebugLoc dl = Op.getDebugLoc();
2771 unsigned Opc;
2772
2773 switch (Op.getOpcode()) {
2774 default:
2775 assert(0 && "Invalid opcode!");
2776 case ISD::SINT_TO_FP:
2777 Opc = ARMISD::SITOF;
2778 break;
2779 case ISD::UINT_TO_FP:
2780 Opc = ARMISD::UITOF;
2781 break;
2782 }
2783
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002784 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002785 return DAG.getNode(Opc, dl, VT, Op);
2786}
2787
Evan Cheng515fe3a2010-07-08 02:08:50 +00002788SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002789 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002790 SDValue Tmp0 = Op.getOperand(0);
2791 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002792 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002793 EVT VT = Op.getValueType();
2794 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002795 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002796 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002797 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002798 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002799 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002800 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002801}
2802
Evan Cheng2457f2c2010-05-22 01:47:14 +00002803SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2804 MachineFunction &MF = DAG.getMachineFunction();
2805 MachineFrameInfo *MFI = MF.getFrameInfo();
2806 MFI->setReturnAddressIsTaken(true);
2807
2808 EVT VT = Op.getValueType();
2809 DebugLoc dl = Op.getDebugLoc();
2810 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2811 if (Depth) {
2812 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2813 SDValue Offset = DAG.getConstant(4, MVT::i32);
2814 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2815 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002816 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002817 }
2818
2819 // Return LR, which contains the return address. Mark it an implicit live-in.
Jim Grosbachc2723a52010-07-23 23:50:35 +00002820 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00002821 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2822}
2823
Dan Gohmand858e902010-04-17 15:26:15 +00002824SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002825 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2826 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002827
Owen Andersone50ed302009-08-10 22:56:29 +00002828 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002829 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2830 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002831 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002832 ? ARM::R7 : ARM::R11;
2833 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2834 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002835 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
2836 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00002837 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002838 return FrameAddr;
2839}
2840
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002841/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00002842/// expand a bit convert where either the source or destination type is i64 to
2843/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2844/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2845/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002846static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002847 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2848 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002849 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002850
Bob Wilson9f3f0612010-04-17 05:30:19 +00002851 // This function is only supposed to be called for i64 types, either as the
2852 // source or destination of the bit convert.
2853 EVT SrcVT = Op.getValueType();
2854 EVT DstVT = N->getValueType(0);
2855 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002856 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002857
Bob Wilson9f3f0612010-04-17 05:30:19 +00002858 // Turn i64->f64 into VMOVDRR.
2859 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002860 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2861 DAG.getConstant(0, MVT::i32));
2862 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2863 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002864 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00002865 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002866 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002867
Jim Grosbache5165492009-11-09 00:11:35 +00002868 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002869 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2870 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2871 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2872 // Merge the pieces into a single i64 value.
2873 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2874 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002875
Bob Wilson9f3f0612010-04-17 05:30:19 +00002876 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002877}
2878
Bob Wilson5bafff32009-06-22 23:27:02 +00002879/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002880/// Zero vectors are used to represent vector negation and in those cases
2881/// will be implemented with the NEON VNEG instruction. However, VNEG does
2882/// not support i64 elements, so sometimes the zero vectors will need to be
2883/// explicitly constructed. Regardless, use a canonical VMOV to create the
2884/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002885static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002886 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002887 // The canonical modified immediate encoding of a zero vector is....0!
2888 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2889 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2890 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002891 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002892}
2893
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002894/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2895/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002896SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2897 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002898 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2899 EVT VT = Op.getValueType();
2900 unsigned VTBits = VT.getSizeInBits();
2901 DebugLoc dl = Op.getDebugLoc();
2902 SDValue ShOpLo = Op.getOperand(0);
2903 SDValue ShOpHi = Op.getOperand(1);
2904 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002905 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002906 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002907
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002908 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2909
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002910 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2911 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2912 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2913 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2914 DAG.getConstant(VTBits, MVT::i32));
2915 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2916 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002917 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002918
2919 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2920 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002921 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002922 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002923 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002924 CCR, Cmp);
2925
2926 SDValue Ops[2] = { Lo, Hi };
2927 return DAG.getMergeValues(Ops, 2, dl);
2928}
2929
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002930/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2931/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002932SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2933 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002934 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2935 EVT VT = Op.getValueType();
2936 unsigned VTBits = VT.getSizeInBits();
2937 DebugLoc dl = Op.getDebugLoc();
2938 SDValue ShOpLo = Op.getOperand(0);
2939 SDValue ShOpHi = Op.getOperand(1);
2940 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002941 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002942
2943 assert(Op.getOpcode() == ISD::SHL_PARTS);
2944 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2945 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2946 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2947 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2948 DAG.getConstant(VTBits, MVT::i32));
2949 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2950 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2951
2952 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2953 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2954 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002955 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002956 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002957 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002958 CCR, Cmp);
2959
2960 SDValue Ops[2] = { Lo, Hi };
2961 return DAG.getMergeValues(Ops, 2, dl);
2962}
2963
Jim Grosbach4725ca72010-09-08 03:54:02 +00002964SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00002965 SelectionDAG &DAG) const {
2966 // The rounding mode is in bits 23:22 of the FPSCR.
2967 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
2968 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
2969 // so that the shift + and get folded into a bitfield extract.
2970 DebugLoc dl = Op.getDebugLoc();
2971 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
2972 DAG.getConstant(Intrinsic::arm_get_fpscr,
2973 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00002974 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00002975 DAG.getConstant(1U << 22, MVT::i32));
2976 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
2977 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00002978 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00002979 DAG.getConstant(3, MVT::i32));
2980}
2981
Jim Grosbach3482c802010-01-18 19:58:49 +00002982static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2983 const ARMSubtarget *ST) {
2984 EVT VT = N->getValueType(0);
2985 DebugLoc dl = N->getDebugLoc();
2986
2987 if (!ST->hasV6T2Ops())
2988 return SDValue();
2989
2990 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2991 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2992}
2993
Bob Wilson5bafff32009-06-22 23:27:02 +00002994static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2995 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00002996 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002997 DebugLoc dl = N->getDebugLoc();
2998
Bob Wilsond5448bb2010-11-18 21:16:28 +00002999 if (!VT.isVector())
3000 return SDValue();
3001
Bob Wilson5bafff32009-06-22 23:27:02 +00003002 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003003 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003004
Bob Wilsond5448bb2010-11-18 21:16:28 +00003005 // Left shifts translate directly to the vshiftu intrinsic.
3006 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003007 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003008 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3009 N->getOperand(0), N->getOperand(1));
3010
3011 assert((N->getOpcode() == ISD::SRA ||
3012 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3013
3014 // NEON uses the same intrinsics for both left and right shifts. For
3015 // right shifts, the shift amounts are negative, so negate the vector of
3016 // shift amounts.
3017 EVT ShiftVT = N->getOperand(1).getValueType();
3018 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3019 getZeroVector(ShiftVT, DAG, dl),
3020 N->getOperand(1));
3021 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3022 Intrinsic::arm_neon_vshifts :
3023 Intrinsic::arm_neon_vshiftu);
3024 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3025 DAG.getConstant(vshiftInt, MVT::i32),
3026 N->getOperand(0), NegatedCount);
3027}
3028
3029static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3030 const ARMSubtarget *ST) {
3031 EVT VT = N->getValueType(0);
3032 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003033
Eli Friedmance392eb2009-08-22 03:13:10 +00003034 // We can get here for a node like i32 = ISD::SHL i32, i64
3035 if (VT != MVT::i64)
3036 return SDValue();
3037
3038 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003039 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003040
Chris Lattner27a6c732007-11-24 07:07:01 +00003041 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3042 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003043 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003044 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003045
Chris Lattner27a6c732007-11-24 07:07:01 +00003046 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003047 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003048
Chris Lattner27a6c732007-11-24 07:07:01 +00003049 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003050 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003051 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003052 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003053 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003054
Chris Lattner27a6c732007-11-24 07:07:01 +00003055 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3056 // captures the result into a carry flag.
3057 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003058 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003059
Chris Lattner27a6c732007-11-24 07:07:01 +00003060 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003061 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003062
Chris Lattner27a6c732007-11-24 07:07:01 +00003063 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003064 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003065}
3066
Bob Wilson5bafff32009-06-22 23:27:02 +00003067static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3068 SDValue TmpOp0, TmpOp1;
3069 bool Invert = false;
3070 bool Swap = false;
3071 unsigned Opc = 0;
3072
3073 SDValue Op0 = Op.getOperand(0);
3074 SDValue Op1 = Op.getOperand(1);
3075 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003076 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003077 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3078 DebugLoc dl = Op.getDebugLoc();
3079
3080 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3081 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003082 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003083 case ISD::SETUNE:
3084 case ISD::SETNE: Invert = true; // Fallthrough
3085 case ISD::SETOEQ:
3086 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3087 case ISD::SETOLT:
3088 case ISD::SETLT: Swap = true; // Fallthrough
3089 case ISD::SETOGT:
3090 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3091 case ISD::SETOLE:
3092 case ISD::SETLE: Swap = true; // Fallthrough
3093 case ISD::SETOGE:
3094 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3095 case ISD::SETUGE: Swap = true; // Fallthrough
3096 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3097 case ISD::SETUGT: Swap = true; // Fallthrough
3098 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3099 case ISD::SETUEQ: Invert = true; // Fallthrough
3100 case ISD::SETONE:
3101 // Expand this to (OLT | OGT).
3102 TmpOp0 = Op0;
3103 TmpOp1 = Op1;
3104 Opc = ISD::OR;
3105 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3106 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3107 break;
3108 case ISD::SETUO: Invert = true; // Fallthrough
3109 case ISD::SETO:
3110 // Expand this to (OLT | OGE).
3111 TmpOp0 = Op0;
3112 TmpOp1 = Op1;
3113 Opc = ISD::OR;
3114 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3115 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3116 break;
3117 }
3118 } else {
3119 // Integer comparisons.
3120 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003121 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003122 case ISD::SETNE: Invert = true;
3123 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3124 case ISD::SETLT: Swap = true;
3125 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3126 case ISD::SETLE: Swap = true;
3127 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3128 case ISD::SETULT: Swap = true;
3129 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3130 case ISD::SETULE: Swap = true;
3131 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3132 }
3133
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003134 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003135 if (Opc == ARMISD::VCEQ) {
3136
3137 SDValue AndOp;
3138 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3139 AndOp = Op0;
3140 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3141 AndOp = Op1;
3142
3143 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003144 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003145 AndOp = AndOp.getOperand(0);
3146
3147 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3148 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003149 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3150 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003151 Invert = !Invert;
3152 }
3153 }
3154 }
3155
3156 if (Swap)
3157 std::swap(Op0, Op1);
3158
Owen Andersonc24cb352010-11-08 23:21:22 +00003159 // If one of the operands is a constant vector zero, attempt to fold the
3160 // comparison to a specialized compare-against-zero form.
3161 SDValue SingleOp;
3162 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3163 SingleOp = Op0;
3164 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3165 if (Opc == ARMISD::VCGE)
3166 Opc = ARMISD::VCLEZ;
3167 else if (Opc == ARMISD::VCGT)
3168 Opc = ARMISD::VCLTZ;
3169 SingleOp = Op1;
3170 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003171
Owen Andersonc24cb352010-11-08 23:21:22 +00003172 SDValue Result;
3173 if (SingleOp.getNode()) {
3174 switch (Opc) {
3175 case ARMISD::VCEQ:
3176 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3177 case ARMISD::VCGE:
3178 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3179 case ARMISD::VCLEZ:
3180 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3181 case ARMISD::VCGT:
3182 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3183 case ARMISD::VCLTZ:
3184 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3185 default:
3186 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3187 }
3188 } else {
3189 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3190 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003191
3192 if (Invert)
3193 Result = DAG.getNOT(dl, Result, VT);
3194
3195 return Result;
3196}
3197
Bob Wilsond3c42842010-06-14 22:19:57 +00003198/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3199/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003200/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003201static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3202 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003203 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003204 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003205
Bob Wilson827b2102010-06-15 19:05:35 +00003206 // SplatBitSize is set to the smallest size that splats the vector, so a
3207 // zero vector will always have SplatBitSize == 8. However, NEON modified
3208 // immediate instructions others than VMOV do not support the 8-bit encoding
3209 // of a zero vector, and the default encoding of zero is supposed to be the
3210 // 32-bit version.
3211 if (SplatBits == 0)
3212 SplatBitSize = 32;
3213
Bob Wilson5bafff32009-06-22 23:27:02 +00003214 switch (SplatBitSize) {
3215 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003216 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003217 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003218 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003219 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003220 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003221 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003222 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003223 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003224
3225 case 16:
3226 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003227 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003228 if ((SplatBits & ~0xff) == 0) {
3229 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003230 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003231 Imm = SplatBits;
3232 break;
3233 }
3234 if ((SplatBits & ~0xff00) == 0) {
3235 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003236 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003237 Imm = SplatBits >> 8;
3238 break;
3239 }
3240 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003241
3242 case 32:
3243 // NEON's 32-bit VMOV supports splat values where:
3244 // * only one byte is nonzero, or
3245 // * the least significant byte is 0xff and the second byte is nonzero, or
3246 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003247 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003248 if ((SplatBits & ~0xff) == 0) {
3249 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003250 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003251 Imm = SplatBits;
3252 break;
3253 }
3254 if ((SplatBits & ~0xff00) == 0) {
3255 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003256 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003257 Imm = SplatBits >> 8;
3258 break;
3259 }
3260 if ((SplatBits & ~0xff0000) == 0) {
3261 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003262 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003263 Imm = SplatBits >> 16;
3264 break;
3265 }
3266 if ((SplatBits & ~0xff000000) == 0) {
3267 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003268 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003269 Imm = SplatBits >> 24;
3270 break;
3271 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003272
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003273 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3274 if (type == OtherModImm) return SDValue();
3275
Bob Wilson5bafff32009-06-22 23:27:02 +00003276 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003277 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3278 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003279 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003280 Imm = SplatBits >> 8;
3281 SplatBits |= 0xff;
3282 break;
3283 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003284
3285 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003286 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3287 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003288 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003289 Imm = SplatBits >> 16;
3290 SplatBits |= 0xffff;
3291 break;
3292 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003293
3294 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3295 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3296 // VMOV.I32. A (very) minor optimization would be to replicate the value
3297 // and fall through here to test for a valid 64-bit splat. But, then the
3298 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003299 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003300
3301 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003302 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003303 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003304 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003305 uint64_t BitMask = 0xff;
3306 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003307 unsigned ImmMask = 1;
3308 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003309 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003310 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003311 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003312 Imm |= ImmMask;
3313 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003314 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003315 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003316 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003317 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003318 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003319 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003320 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003321 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003322 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003323 break;
3324 }
3325
Bob Wilson1a913ed2010-06-11 21:34:50 +00003326 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003327 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003328 return SDValue();
3329 }
3330
Bob Wilsoncba270d2010-07-13 21:16:48 +00003331 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3332 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003333}
3334
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003335static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3336 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003337 unsigned NumElts = VT.getVectorNumElements();
3338 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003339
3340 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3341 if (M[0] < 0)
3342 return false;
3343
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003344 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003345
3346 // If this is a VEXT shuffle, the immediate value is the index of the first
3347 // element. The other shuffle indices must be the successive elements after
3348 // the first one.
3349 unsigned ExpectedElt = Imm;
3350 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003351 // Increment the expected index. If it wraps around, it may still be
3352 // a VEXT but the source vectors must be swapped.
3353 ExpectedElt += 1;
3354 if (ExpectedElt == NumElts * 2) {
3355 ExpectedElt = 0;
3356 ReverseVEXT = true;
3357 }
3358
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003359 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003360 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003361 return false;
3362 }
3363
3364 // Adjust the index value if the source operands will be swapped.
3365 if (ReverseVEXT)
3366 Imm -= NumElts;
3367
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003368 return true;
3369}
3370
Bob Wilson8bb9e482009-07-26 00:39:34 +00003371/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3372/// instruction with the specified blocksize. (The order of the elements
3373/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003374static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3375 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003376 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3377 "Only possible block sizes for VREV are: 16, 32, 64");
3378
Bob Wilson8bb9e482009-07-26 00:39:34 +00003379 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003380 if (EltSz == 64)
3381 return false;
3382
3383 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003384 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003385 // If the first shuffle index is UNDEF, be optimistic.
3386 if (M[0] < 0)
3387 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003388
3389 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3390 return false;
3391
3392 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003393 if (M[i] < 0) continue; // ignore UNDEF indices
3394 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003395 return false;
3396 }
3397
3398 return true;
3399}
3400
Bob Wilsonc692cb72009-08-21 20:54:19 +00003401static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3402 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003403 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3404 if (EltSz == 64)
3405 return false;
3406
Bob Wilsonc692cb72009-08-21 20:54:19 +00003407 unsigned NumElts = VT.getVectorNumElements();
3408 WhichResult = (M[0] == 0 ? 0 : 1);
3409 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003410 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3411 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003412 return false;
3413 }
3414 return true;
3415}
3416
Bob Wilson324f4f12009-12-03 06:40:55 +00003417/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3418/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3419/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3420static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3421 unsigned &WhichResult) {
3422 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3423 if (EltSz == 64)
3424 return false;
3425
3426 unsigned NumElts = VT.getVectorNumElements();
3427 WhichResult = (M[0] == 0 ? 0 : 1);
3428 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003429 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3430 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003431 return false;
3432 }
3433 return true;
3434}
3435
Bob Wilsonc692cb72009-08-21 20:54:19 +00003436static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3437 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003438 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3439 if (EltSz == 64)
3440 return false;
3441
Bob Wilsonc692cb72009-08-21 20:54:19 +00003442 unsigned NumElts = VT.getVectorNumElements();
3443 WhichResult = (M[0] == 0 ? 0 : 1);
3444 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003445 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003446 if ((unsigned) M[i] != 2 * i + WhichResult)
3447 return false;
3448 }
3449
3450 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003451 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003452 return false;
3453
3454 return true;
3455}
3456
Bob Wilson324f4f12009-12-03 06:40:55 +00003457/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3458/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3459/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3460static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3461 unsigned &WhichResult) {
3462 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3463 if (EltSz == 64)
3464 return false;
3465
3466 unsigned Half = VT.getVectorNumElements() / 2;
3467 WhichResult = (M[0] == 0 ? 0 : 1);
3468 for (unsigned j = 0; j != 2; ++j) {
3469 unsigned Idx = WhichResult;
3470 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003471 int MIdx = M[i + j * Half];
3472 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003473 return false;
3474 Idx += 2;
3475 }
3476 }
3477
3478 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3479 if (VT.is64BitVector() && EltSz == 32)
3480 return false;
3481
3482 return true;
3483}
3484
Bob Wilsonc692cb72009-08-21 20:54:19 +00003485static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3486 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003487 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3488 if (EltSz == 64)
3489 return false;
3490
Bob Wilsonc692cb72009-08-21 20:54:19 +00003491 unsigned NumElts = VT.getVectorNumElements();
3492 WhichResult = (M[0] == 0 ? 0 : 1);
3493 unsigned Idx = WhichResult * NumElts / 2;
3494 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003495 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3496 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003497 return false;
3498 Idx += 1;
3499 }
3500
3501 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003502 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003503 return false;
3504
3505 return true;
3506}
3507
Bob Wilson324f4f12009-12-03 06:40:55 +00003508/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3509/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3510/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3511static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3512 unsigned &WhichResult) {
3513 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3514 if (EltSz == 64)
3515 return false;
3516
3517 unsigned NumElts = VT.getVectorNumElements();
3518 WhichResult = (M[0] == 0 ? 0 : 1);
3519 unsigned Idx = WhichResult * NumElts / 2;
3520 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003521 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3522 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003523 return false;
3524 Idx += 1;
3525 }
3526
3527 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3528 if (VT.is64BitVector() && EltSz == 32)
3529 return false;
3530
3531 return true;
3532}
3533
Dale Johannesenf630c712010-07-29 20:10:08 +00003534// If N is an integer constant that can be moved into a register in one
3535// instruction, return an SDValue of such a constant (will become a MOV
3536// instruction). Otherwise return null.
3537static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3538 const ARMSubtarget *ST, DebugLoc dl) {
3539 uint64_t Val;
3540 if (!isa<ConstantSDNode>(N))
3541 return SDValue();
3542 Val = cast<ConstantSDNode>(N)->getZExtValue();
3543
3544 if (ST->isThumb1Only()) {
3545 if (Val <= 255 || ~Val <= 255)
3546 return DAG.getConstant(Val, MVT::i32);
3547 } else {
3548 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3549 return DAG.getConstant(Val, MVT::i32);
3550 }
3551 return SDValue();
3552}
3553
Bob Wilson5bafff32009-06-22 23:27:02 +00003554// If this is a case we can't handle, return null and let the default
3555// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003556SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3557 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003558 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003559 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003560 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003561
3562 APInt SplatBits, SplatUndef;
3563 unsigned SplatBitSize;
3564 bool HasAnyUndefs;
3565 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003566 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003567 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003568 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003569 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003570 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003571 DAG, VmovVT, VT.is128BitVector(),
3572 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003573 if (Val.getNode()) {
3574 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003575 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003576 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003577
3578 // Try an immediate VMVN.
3579 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3580 ((1LL << SplatBitSize) - 1));
3581 Val = isNEONModifiedImm(NegatedImm,
3582 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003583 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003584 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003585 if (Val.getNode()) {
3586 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003587 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003588 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003589 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003590 }
3591
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003592 // Scan through the operands to see if only one value is used.
3593 unsigned NumElts = VT.getVectorNumElements();
3594 bool isOnlyLowElement = true;
3595 bool usesOnlyOneValue = true;
3596 bool isConstant = true;
3597 SDValue Value;
3598 for (unsigned i = 0; i < NumElts; ++i) {
3599 SDValue V = Op.getOperand(i);
3600 if (V.getOpcode() == ISD::UNDEF)
3601 continue;
3602 if (i > 0)
3603 isOnlyLowElement = false;
3604 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3605 isConstant = false;
3606
3607 if (!Value.getNode())
3608 Value = V;
3609 else if (V != Value)
3610 usesOnlyOneValue = false;
3611 }
3612
3613 if (!Value.getNode())
3614 return DAG.getUNDEF(VT);
3615
3616 if (isOnlyLowElement)
3617 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3618
Dale Johannesenf630c712010-07-29 20:10:08 +00003619 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3620
Dale Johannesen575cd142010-10-19 20:00:17 +00003621 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3622 // i32 and try again.
3623 if (usesOnlyOneValue && EltSize <= 32) {
3624 if (!isConstant)
3625 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3626 if (VT.getVectorElementType().isFloatingPoint()) {
3627 SmallVector<SDValue, 8> Ops;
3628 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003629 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003630 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003631 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3632 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003633 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3634 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003635 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003636 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003637 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3638 if (Val.getNode())
3639 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003640 }
3641
3642 // If all elements are constants and the case above didn't get hit, fall back
3643 // to the default expansion, which will generate a load from the constant
3644 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003645 if (isConstant)
3646 return SDValue();
3647
Bob Wilson11a1dff2011-01-07 21:37:30 +00003648 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3649 if (NumElts >= 4) {
3650 SDValue shuffle = ReconstructShuffle(Op, DAG);
3651 if (shuffle != SDValue())
3652 return shuffle;
3653 }
3654
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003655 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003656 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3657 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003658 if (EltSize >= 32) {
3659 // Do the expansion with floating-point types, since that is what the VFP
3660 // registers are defined to use, and since i64 is not legal.
3661 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3662 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003663 SmallVector<SDValue, 8> Ops;
3664 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003665 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003666 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003667 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003668 }
3669
3670 return SDValue();
3671}
3672
Bob Wilson11a1dff2011-01-07 21:37:30 +00003673// Gather data to see if the operation can be modelled as a
3674// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003675SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3676 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003677 DebugLoc dl = Op.getDebugLoc();
3678 EVT VT = Op.getValueType();
3679 unsigned NumElts = VT.getVectorNumElements();
3680
3681 SmallVector<SDValue, 2> SourceVecs;
3682 SmallVector<unsigned, 2> MinElts;
3683 SmallVector<unsigned, 2> MaxElts;
3684
3685 for (unsigned i = 0; i < NumElts; ++i) {
3686 SDValue V = Op.getOperand(i);
3687 if (V.getOpcode() == ISD::UNDEF)
3688 continue;
3689 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3690 // A shuffle can only come from building a vector from various
3691 // elements of other vectors.
3692 return SDValue();
3693 }
3694
3695 // Record this extraction against the appropriate vector if possible...
3696 SDValue SourceVec = V.getOperand(0);
3697 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3698 bool FoundSource = false;
3699 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3700 if (SourceVecs[j] == SourceVec) {
3701 if (MinElts[j] > EltNo)
3702 MinElts[j] = EltNo;
3703 if (MaxElts[j] < EltNo)
3704 MaxElts[j] = EltNo;
3705 FoundSource = true;
3706 break;
3707 }
3708 }
3709
3710 // Or record a new source if not...
3711 if (!FoundSource) {
3712 SourceVecs.push_back(SourceVec);
3713 MinElts.push_back(EltNo);
3714 MaxElts.push_back(EltNo);
3715 }
3716 }
3717
3718 // Currently only do something sane when at most two source vectors
3719 // involved.
3720 if (SourceVecs.size() > 2)
3721 return SDValue();
3722
3723 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3724 int VEXTOffsets[2] = {0, 0};
3725
3726 // This loop extracts the usage patterns of the source vectors
3727 // and prepares appropriate SDValues for a shuffle if possible.
3728 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3729 if (SourceVecs[i].getValueType() == VT) {
3730 // No VEXT necessary
3731 ShuffleSrcs[i] = SourceVecs[i];
3732 VEXTOffsets[i] = 0;
3733 continue;
3734 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3735 // It probably isn't worth padding out a smaller vector just to
3736 // break it down again in a shuffle.
3737 return SDValue();
3738 }
Bob Wilson11a1dff2011-01-07 21:37:30 +00003739
3740 // Since only 64-bit and 128-bit vectors are legal on ARM and
3741 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003742 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3743 "unexpected vector sizes in ReconstructShuffle");
Bob Wilson11a1dff2011-01-07 21:37:30 +00003744
3745 if (MaxElts[i] - MinElts[i] >= NumElts) {
3746 // Span too large for a VEXT to cope
3747 return SDValue();
3748 }
3749
3750 if (MinElts[i] >= NumElts) {
3751 // The extraction can just take the second half
3752 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003753 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3754 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003755 DAG.getIntPtrConstant(NumElts));
3756 } else if (MaxElts[i] < NumElts) {
3757 // The extraction can just take the first half
3758 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003759 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3760 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003761 DAG.getIntPtrConstant(0));
3762 } else {
3763 // An actual VEXT is needed
3764 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003765 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3766 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003767 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003768 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3769 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003770 DAG.getIntPtrConstant(NumElts));
3771 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3772 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3773 }
3774 }
3775
3776 SmallVector<int, 8> Mask;
3777
3778 for (unsigned i = 0; i < NumElts; ++i) {
3779 SDValue Entry = Op.getOperand(i);
3780 if (Entry.getOpcode() == ISD::UNDEF) {
3781 Mask.push_back(-1);
3782 continue;
3783 }
3784
3785 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00003786 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
3787 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00003788 if (ExtractVec == SourceVecs[0]) {
3789 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3790 } else {
3791 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3792 }
3793 }
3794
3795 // Final check before we try to produce nonsense...
3796 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00003797 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
3798 &Mask[0]);
Bob Wilson11a1dff2011-01-07 21:37:30 +00003799
3800 return SDValue();
3801}
3802
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003803/// isShuffleMaskLegal - Targets can use this to indicate that they only
3804/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3805/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3806/// are assumed to be legal.
3807bool
3808ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3809 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003810 if (VT.getVectorNumElements() == 4 &&
3811 (VT.is128BitVector() || VT.is64BitVector())) {
3812 unsigned PFIndexes[4];
3813 for (unsigned i = 0; i != 4; ++i) {
3814 if (M[i] < 0)
3815 PFIndexes[i] = 8;
3816 else
3817 PFIndexes[i] = M[i];
3818 }
3819
3820 // Compute the index in the perfect shuffle table.
3821 unsigned PFTableIndex =
3822 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3823 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3824 unsigned Cost = (PFEntry >> 30);
3825
3826 if (Cost <= 4)
3827 return true;
3828 }
3829
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003830 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003831 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003832
Bob Wilson53dd2452010-06-07 23:53:38 +00003833 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3834 return (EltSize >= 32 ||
3835 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003836 isVREVMask(M, VT, 64) ||
3837 isVREVMask(M, VT, 32) ||
3838 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003839 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3840 isVTRNMask(M, VT, WhichResult) ||
3841 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003842 isVZIPMask(M, VT, WhichResult) ||
3843 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3844 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3845 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003846}
3847
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003848/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3849/// the specified operations to build the shuffle.
3850static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3851 SDValue RHS, SelectionDAG &DAG,
3852 DebugLoc dl) {
3853 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3854 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3855 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3856
3857 enum {
3858 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3859 OP_VREV,
3860 OP_VDUP0,
3861 OP_VDUP1,
3862 OP_VDUP2,
3863 OP_VDUP3,
3864 OP_VEXT1,
3865 OP_VEXT2,
3866 OP_VEXT3,
3867 OP_VUZPL, // VUZP, left result
3868 OP_VUZPR, // VUZP, right result
3869 OP_VZIPL, // VZIP, left result
3870 OP_VZIPR, // VZIP, right result
3871 OP_VTRNL, // VTRN, left result
3872 OP_VTRNR // VTRN, right result
3873 };
3874
3875 if (OpNum == OP_COPY) {
3876 if (LHSID == (1*9+2)*9+3) return LHS;
3877 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3878 return RHS;
3879 }
3880
3881 SDValue OpLHS, OpRHS;
3882 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3883 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3884 EVT VT = OpLHS.getValueType();
3885
3886 switch (OpNum) {
3887 default: llvm_unreachable("Unknown shuffle opcode!");
3888 case OP_VREV:
3889 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3890 case OP_VDUP0:
3891 case OP_VDUP1:
3892 case OP_VDUP2:
3893 case OP_VDUP3:
3894 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003895 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003896 case OP_VEXT1:
3897 case OP_VEXT2:
3898 case OP_VEXT3:
3899 return DAG.getNode(ARMISD::VEXT, dl, VT,
3900 OpLHS, OpRHS,
3901 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3902 case OP_VUZPL:
3903 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003904 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003905 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3906 case OP_VZIPL:
3907 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003908 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003909 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3910 case OP_VTRNL:
3911 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003912 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3913 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003914 }
3915}
3916
Bob Wilson5bafff32009-06-22 23:27:02 +00003917static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003918 SDValue V1 = Op.getOperand(0);
3919 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003920 DebugLoc dl = Op.getDebugLoc();
3921 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003922 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003923 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003924
Bob Wilson28865062009-08-13 02:13:04 +00003925 // Convert shuffles that are directly supported on NEON to target-specific
3926 // DAG nodes, instead of keeping them as shuffles and matching them again
3927 // during code selection. This is more efficient and avoids the possibility
3928 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003929 // FIXME: floating-point vectors should be canonicalized to integer vectors
3930 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003931 SVN->getMask(ShuffleMask);
3932
Bob Wilson53dd2452010-06-07 23:53:38 +00003933 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3934 if (EltSize <= 32) {
3935 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3936 int Lane = SVN->getSplatIndex();
3937 // If this is undef splat, generate it via "just" vdup, if possible.
3938 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003939
Bob Wilson53dd2452010-06-07 23:53:38 +00003940 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3941 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3942 }
3943 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3944 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003945 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003946
3947 bool ReverseVEXT;
3948 unsigned Imm;
3949 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3950 if (ReverseVEXT)
3951 std::swap(V1, V2);
3952 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3953 DAG.getConstant(Imm, MVT::i32));
3954 }
3955
3956 if (isVREVMask(ShuffleMask, VT, 64))
3957 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3958 if (isVREVMask(ShuffleMask, VT, 32))
3959 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3960 if (isVREVMask(ShuffleMask, VT, 16))
3961 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3962
3963 // Check for Neon shuffles that modify both input vectors in place.
3964 // If both results are used, i.e., if there are two shuffles with the same
3965 // source operands and with masks corresponding to both results of one of
3966 // these operations, DAG memoization will ensure that a single node is
3967 // used for both shuffles.
3968 unsigned WhichResult;
3969 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3970 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3971 V1, V2).getValue(WhichResult);
3972 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3973 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3974 V1, V2).getValue(WhichResult);
3975 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3976 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3977 V1, V2).getValue(WhichResult);
3978
3979 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3980 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3981 V1, V1).getValue(WhichResult);
3982 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3983 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3984 V1, V1).getValue(WhichResult);
3985 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3986 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3987 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00003988 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003989
Bob Wilsonc692cb72009-08-21 20:54:19 +00003990 // If the shuffle is not directly supported and it has 4 elements, use
3991 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003992 unsigned NumElts = VT.getVectorNumElements();
3993 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003994 unsigned PFIndexes[4];
3995 for (unsigned i = 0; i != 4; ++i) {
3996 if (ShuffleMask[i] < 0)
3997 PFIndexes[i] = 8;
3998 else
3999 PFIndexes[i] = ShuffleMask[i];
4000 }
4001
4002 // Compute the index in the perfect shuffle table.
4003 unsigned PFTableIndex =
4004 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004005 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4006 unsigned Cost = (PFEntry >> 30);
4007
4008 if (Cost <= 4)
4009 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4010 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004011
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004012 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004013 if (EltSize >= 32) {
4014 // Do the expansion with floating-point types, since that is what the VFP
4015 // registers are defined to use, and since i64 is not legal.
4016 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4017 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004018 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4019 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004020 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004021 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004022 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004023 Ops.push_back(DAG.getUNDEF(EltVT));
4024 else
4025 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4026 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4027 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4028 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004029 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004030 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004031 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004032 }
4033
Bob Wilson22cac0d2009-08-14 05:16:33 +00004034 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004035}
4036
Bob Wilson5bafff32009-06-22 23:27:02 +00004037static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004038 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004039 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004040 if (!isa<ConstantSDNode>(Lane))
4041 return SDValue();
4042
4043 SDValue Vec = Op.getOperand(0);
4044 if (Op.getValueType() == MVT::i32 &&
4045 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4046 DebugLoc dl = Op.getDebugLoc();
4047 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4048 }
4049
4050 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004051}
4052
Bob Wilsona6d65862009-08-03 20:36:38 +00004053static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4054 // The only time a CONCAT_VECTORS operation can have legal types is when
4055 // two 64-bit vectors are concatenated to a 128-bit vector.
4056 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4057 "unexpected CONCAT_VECTORS");
4058 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004059 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004060 SDValue Op0 = Op.getOperand(0);
4061 SDValue Op1 = Op.getOperand(1);
4062 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004063 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004064 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004065 DAG.getIntPtrConstant(0));
4066 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004067 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004068 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004069 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004070 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004071}
4072
Bob Wilson626613d2010-11-23 19:38:38 +00004073/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4074/// element has been zero/sign-extended, depending on the isSigned parameter,
4075/// from an integer type half its size.
4076static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4077 bool isSigned) {
4078 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4079 EVT VT = N->getValueType(0);
4080 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4081 SDNode *BVN = N->getOperand(0).getNode();
4082 if (BVN->getValueType(0) != MVT::v4i32 ||
4083 BVN->getOpcode() != ISD::BUILD_VECTOR)
4084 return false;
4085 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4086 unsigned HiElt = 1 - LoElt;
4087 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4088 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4089 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4090 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4091 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4092 return false;
4093 if (isSigned) {
4094 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4095 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4096 return true;
4097 } else {
4098 if (Hi0->isNullValue() && Hi1->isNullValue())
4099 return true;
4100 }
4101 return false;
4102 }
4103
4104 if (N->getOpcode() != ISD::BUILD_VECTOR)
4105 return false;
4106
4107 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4108 SDNode *Elt = N->getOperand(i).getNode();
4109 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4110 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4111 unsigned HalfSize = EltSize / 2;
4112 if (isSigned) {
4113 int64_t SExtVal = C->getSExtValue();
4114 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4115 return false;
4116 } else {
4117 if ((C->getZExtValue() >> HalfSize) != 0)
4118 return false;
4119 }
4120 continue;
4121 }
4122 return false;
4123 }
4124
4125 return true;
4126}
4127
4128/// isSignExtended - Check if a node is a vector value that is sign-extended
4129/// or a constant BUILD_VECTOR with sign-extended elements.
4130static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4131 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4132 return true;
4133 if (isExtendedBUILD_VECTOR(N, DAG, true))
4134 return true;
4135 return false;
4136}
4137
4138/// isZeroExtended - Check if a node is a vector value that is zero-extended
4139/// or a constant BUILD_VECTOR with zero-extended elements.
4140static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4141 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4142 return true;
4143 if (isExtendedBUILD_VECTOR(N, DAG, false))
4144 return true;
4145 return false;
4146}
4147
4148/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4149/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004150static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4151 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4152 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004153 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4154 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4155 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4156 LD->isNonTemporal(), LD->getAlignment());
4157 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4158 // have been legalized as a BITCAST from v4i32.
4159 if (N->getOpcode() == ISD::BITCAST) {
4160 SDNode *BVN = N->getOperand(0).getNode();
4161 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4162 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4163 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4164 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4165 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4166 }
4167 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4168 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4169 EVT VT = N->getValueType(0);
4170 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4171 unsigned NumElts = VT.getVectorNumElements();
4172 MVT TruncVT = MVT::getIntegerVT(EltSize);
4173 SmallVector<SDValue, 8> Ops;
4174 for (unsigned i = 0; i != NumElts; ++i) {
4175 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4176 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004177 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004178 }
4179 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4180 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004181}
4182
4183static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4184 // Multiplications are only custom-lowered for 128-bit vectors so that
4185 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4186 EVT VT = Op.getValueType();
4187 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4188 SDNode *N0 = Op.getOperand(0).getNode();
4189 SDNode *N1 = Op.getOperand(1).getNode();
4190 unsigned NewOpc = 0;
Bob Wilson626613d2010-11-23 19:38:38 +00004191 if (isSignExtended(N0, DAG) && isSignExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004192 NewOpc = ARMISD::VMULLs;
Bob Wilson626613d2010-11-23 19:38:38 +00004193 else if (isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004194 NewOpc = ARMISD::VMULLu;
Bob Wilson626613d2010-11-23 19:38:38 +00004195 else if (VT == MVT::v2i64)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004196 // Fall through to expand this. It is not legal.
4197 return SDValue();
Bob Wilson626613d2010-11-23 19:38:38 +00004198 else
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004199 // Other vector multiplications are legal.
4200 return Op;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004201
4202 // Legalize to a VMULL instruction.
4203 DebugLoc DL = Op.getDebugLoc();
4204 SDValue Op0 = SkipExtension(N0, DAG);
4205 SDValue Op1 = SkipExtension(N1, DAG);
4206
4207 assert(Op0.getValueType().is64BitVector() &&
4208 Op1.getValueType().is64BitVector() &&
4209 "unexpected types for extended operands to VMULL");
4210 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4211}
4212
Dan Gohmand858e902010-04-17 15:26:15 +00004213SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004214 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004215 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004216 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004217 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004218 case ISD::GlobalAddress:
4219 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4220 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00004221 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004222 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004223 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4224 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004225 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004226 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004227 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004228 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004229 case ISD::SINT_TO_FP:
4230 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4231 case ISD::FP_TO_SINT:
4232 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004233 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004234 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004235 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004236 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004237 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004238 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004239 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004240 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4241 Subtarget);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004242 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004243 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004244 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004245 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004246 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004247 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004248 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004249 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004250 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004251 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004252 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004253 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004254 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004255 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004256 case ISD::MUL: return LowerMUL(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004257 }
Dan Gohman475871a2008-07-27 21:46:04 +00004258 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004259}
4260
Duncan Sands1607f052008-12-01 11:39:25 +00004261/// ReplaceNodeResults - Replace the results of node with an illegal result
4262/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004263void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4264 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004265 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004266 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004267 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004268 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004269 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004270 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004271 case ISD::BITCAST:
4272 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004273 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004274 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004275 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004276 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004277 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004278 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004279 if (Res.getNode())
4280 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004281}
Chris Lattner27a6c732007-11-24 07:07:01 +00004282
Evan Chenga8e29892007-01-19 07:51:42 +00004283//===----------------------------------------------------------------------===//
4284// ARM Scheduler Hooks
4285//===----------------------------------------------------------------------===//
4286
4287MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004288ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4289 MachineBasicBlock *BB,
4290 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004291 unsigned dest = MI->getOperand(0).getReg();
4292 unsigned ptr = MI->getOperand(1).getReg();
4293 unsigned oldval = MI->getOperand(2).getReg();
4294 unsigned newval = MI->getOperand(3).getReg();
4295 unsigned scratch = BB->getParent()->getRegInfo()
4296 .createVirtualRegister(ARM::GPRRegisterClass);
4297 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4298 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004299 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004300
4301 unsigned ldrOpc, strOpc;
4302 switch (Size) {
4303 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004304 case 1:
4305 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
4306 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
4307 break;
4308 case 2:
4309 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4310 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4311 break;
4312 case 4:
4313 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4314 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4315 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004316 }
4317
4318 MachineFunction *MF = BB->getParent();
4319 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4320 MachineFunction::iterator It = BB;
4321 ++It; // insert the new blocks after the current block
4322
4323 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4324 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4325 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4326 MF->insert(It, loop1MBB);
4327 MF->insert(It, loop2MBB);
4328 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004329
4330 // Transfer the remainder of BB and its successor edges to exitMBB.
4331 exitMBB->splice(exitMBB->begin(), BB,
4332 llvm::next(MachineBasicBlock::iterator(MI)),
4333 BB->end());
4334 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004335
4336 // thisMBB:
4337 // ...
4338 // fallthrough --> loop1MBB
4339 BB->addSuccessor(loop1MBB);
4340
4341 // loop1MBB:
4342 // ldrex dest, [ptr]
4343 // cmp dest, oldval
4344 // bne exitMBB
4345 BB = loop1MBB;
4346 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004347 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004348 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004349 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4350 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004351 BB->addSuccessor(loop2MBB);
4352 BB->addSuccessor(exitMBB);
4353
4354 // loop2MBB:
4355 // strex scratch, newval, [ptr]
4356 // cmp scratch, #0
4357 // bne loop1MBB
4358 BB = loop2MBB;
4359 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4360 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004361 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004362 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004363 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4364 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004365 BB->addSuccessor(loop1MBB);
4366 BB->addSuccessor(exitMBB);
4367
4368 // exitMBB:
4369 // ...
4370 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004371
Dan Gohman14152b42010-07-06 20:24:04 +00004372 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004373
Jim Grosbach5278eb82009-12-11 01:42:04 +00004374 return BB;
4375}
4376
4377MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004378ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4379 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004380 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4381 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4382
4383 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004384 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004385 MachineFunction::iterator It = BB;
4386 ++It;
4387
4388 unsigned dest = MI->getOperand(0).getReg();
4389 unsigned ptr = MI->getOperand(1).getReg();
4390 unsigned incr = MI->getOperand(2).getReg();
4391 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004392
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004393 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004394 unsigned ldrOpc, strOpc;
4395 switch (Size) {
4396 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004397 case 1:
4398 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004399 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004400 break;
4401 case 2:
4402 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4403 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4404 break;
4405 case 4:
4406 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4407 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4408 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004409 }
4410
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004411 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4412 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4413 MF->insert(It, loopMBB);
4414 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004415
4416 // Transfer the remainder of BB and its successor edges to exitMBB.
4417 exitMBB->splice(exitMBB->begin(), BB,
4418 llvm::next(MachineBasicBlock::iterator(MI)),
4419 BB->end());
4420 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004421
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004422 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004423 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4424 unsigned scratch2 = (!BinOpcode) ? incr :
4425 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4426
4427 // thisMBB:
4428 // ...
4429 // fallthrough --> loopMBB
4430 BB->addSuccessor(loopMBB);
4431
4432 // loopMBB:
4433 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004434 // <binop> scratch2, dest, incr
4435 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004436 // cmp scratch, #0
4437 // bne- loopMBB
4438 // fallthrough --> exitMBB
4439 BB = loopMBB;
4440 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004441 if (BinOpcode) {
4442 // operand order needs to go the other way for NAND
4443 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4444 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4445 addReg(incr).addReg(dest)).addReg(0);
4446 else
4447 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4448 addReg(dest).addReg(incr)).addReg(0);
4449 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004450
4451 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4452 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004453 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004454 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004455 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4456 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004457
4458 BB->addSuccessor(loopMBB);
4459 BB->addSuccessor(exitMBB);
4460
4461 // exitMBB:
4462 // ...
4463 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004464
Dan Gohman14152b42010-07-06 20:24:04 +00004465 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004466
Jim Grosbachc3c23542009-12-14 04:22:04 +00004467 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004468}
4469
Evan Cheng218977b2010-07-13 19:27:42 +00004470static
4471MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4472 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4473 E = MBB->succ_end(); I != E; ++I)
4474 if (*I != Succ)
4475 return *I;
4476 llvm_unreachable("Expecting a BB with two successors!");
4477}
4478
Jim Grosbache801dc42009-12-12 01:40:06 +00004479MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004480ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004481 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004482 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004483 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004484 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004485 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004486 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004487 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004488 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004489
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004490 case ARM::ATOMIC_LOAD_ADD_I8:
4491 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4492 case ARM::ATOMIC_LOAD_ADD_I16:
4493 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4494 case ARM::ATOMIC_LOAD_ADD_I32:
4495 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004496
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004497 case ARM::ATOMIC_LOAD_AND_I8:
4498 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4499 case ARM::ATOMIC_LOAD_AND_I16:
4500 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4501 case ARM::ATOMIC_LOAD_AND_I32:
4502 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004503
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004504 case ARM::ATOMIC_LOAD_OR_I8:
4505 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4506 case ARM::ATOMIC_LOAD_OR_I16:
4507 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4508 case ARM::ATOMIC_LOAD_OR_I32:
4509 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004510
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004511 case ARM::ATOMIC_LOAD_XOR_I8:
4512 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4513 case ARM::ATOMIC_LOAD_XOR_I16:
4514 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4515 case ARM::ATOMIC_LOAD_XOR_I32:
4516 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004517
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004518 case ARM::ATOMIC_LOAD_NAND_I8:
4519 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4520 case ARM::ATOMIC_LOAD_NAND_I16:
4521 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4522 case ARM::ATOMIC_LOAD_NAND_I32:
4523 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004524
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004525 case ARM::ATOMIC_LOAD_SUB_I8:
4526 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4527 case ARM::ATOMIC_LOAD_SUB_I16:
4528 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4529 case ARM::ATOMIC_LOAD_SUB_I32:
4530 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004531
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004532 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4533 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4534 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004535
4536 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4537 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4538 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004539
Evan Cheng007ea272009-08-12 05:17:19 +00004540 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004541 // To "insert" a SELECT_CC instruction, we actually have to insert the
4542 // diamond control-flow pattern. The incoming instruction knows the
4543 // destination vreg to set, the condition code register to branch on, the
4544 // true/false values to select between, and a branch opcode to use.
4545 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004546 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004547 ++It;
4548
4549 // thisMBB:
4550 // ...
4551 // TrueVal = ...
4552 // cmpTY ccX, r1, r2
4553 // bCC copy1MBB
4554 // fallthrough --> copy0MBB
4555 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004556 MachineFunction *F = BB->getParent();
4557 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4558 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004559 F->insert(It, copy0MBB);
4560 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004561
4562 // Transfer the remainder of BB and its successor edges to sinkMBB.
4563 sinkMBB->splice(sinkMBB->begin(), BB,
4564 llvm::next(MachineBasicBlock::iterator(MI)),
4565 BB->end());
4566 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4567
Dan Gohman258c58c2010-07-06 15:49:48 +00004568 BB->addSuccessor(copy0MBB);
4569 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004570
Dan Gohman14152b42010-07-06 20:24:04 +00004571 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4572 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4573
Evan Chenga8e29892007-01-19 07:51:42 +00004574 // copy0MBB:
4575 // %FalseValue = ...
4576 // # fallthrough to sinkMBB
4577 BB = copy0MBB;
4578
4579 // Update machine-CFG edges
4580 BB->addSuccessor(sinkMBB);
4581
4582 // sinkMBB:
4583 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4584 // ...
4585 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004586 BuildMI(*BB, BB->begin(), dl,
4587 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004588 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4589 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4590
Dan Gohman14152b42010-07-06 20:24:04 +00004591 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004592 return BB;
4593 }
Evan Cheng86198642009-08-07 00:34:42 +00004594
Evan Cheng218977b2010-07-13 19:27:42 +00004595 case ARM::BCCi64:
4596 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00004597 // If there is an unconditional branch to the other successor, remove it.
4598 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
4599
Evan Cheng218977b2010-07-13 19:27:42 +00004600 // Compare both parts that make up the double comparison separately for
4601 // equality.
4602 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4603
4604 unsigned LHS1 = MI->getOperand(1).getReg();
4605 unsigned LHS2 = MI->getOperand(2).getReg();
4606 if (RHSisZero) {
4607 AddDefaultPred(BuildMI(BB, dl,
4608 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4609 .addReg(LHS1).addImm(0));
4610 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4611 .addReg(LHS2).addImm(0)
4612 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4613 } else {
4614 unsigned RHS1 = MI->getOperand(3).getReg();
4615 unsigned RHS2 = MI->getOperand(4).getReg();
4616 AddDefaultPred(BuildMI(BB, dl,
4617 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4618 .addReg(LHS1).addReg(RHS1));
4619 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4620 .addReg(LHS2).addReg(RHS2)
4621 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4622 }
4623
4624 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4625 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4626 if (MI->getOperand(0).getImm() == ARMCC::NE)
4627 std::swap(destMBB, exitMBB);
4628
4629 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4630 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4631 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4632 .addMBB(exitMBB);
4633
4634 MI->eraseFromParent(); // The pseudo instruction is gone now.
4635 return BB;
4636 }
Evan Chenga8e29892007-01-19 07:51:42 +00004637 }
4638}
4639
4640//===----------------------------------------------------------------------===//
4641// ARM Optimization Hooks
4642//===----------------------------------------------------------------------===//
4643
Chris Lattnerd1980a52009-03-12 06:52:53 +00004644static
4645SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4646 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004647 SelectionDAG &DAG = DCI.DAG;
4648 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004649 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004650 unsigned Opc = N->getOpcode();
4651 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4652 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4653 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4654 ISD::CondCode CC = ISD::SETCC_INVALID;
4655
4656 if (isSlctCC) {
4657 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4658 } else {
4659 SDValue CCOp = Slct.getOperand(0);
4660 if (CCOp.getOpcode() == ISD::SETCC)
4661 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4662 }
4663
4664 bool DoXform = false;
4665 bool InvCC = false;
4666 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4667 "Bad input!");
4668
4669 if (LHS.getOpcode() == ISD::Constant &&
4670 cast<ConstantSDNode>(LHS)->isNullValue()) {
4671 DoXform = true;
4672 } else if (CC != ISD::SETCC_INVALID &&
4673 RHS.getOpcode() == ISD::Constant &&
4674 cast<ConstantSDNode>(RHS)->isNullValue()) {
4675 std::swap(LHS, RHS);
4676 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004677 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004678 Op0.getOperand(0).getValueType();
4679 bool isInt = OpVT.isInteger();
4680 CC = ISD::getSetCCInverse(CC, isInt);
4681
4682 if (!TLI.isCondCodeLegal(CC, OpVT))
4683 return SDValue(); // Inverse operator isn't legal.
4684
4685 DoXform = true;
4686 InvCC = true;
4687 }
4688
4689 if (DoXform) {
4690 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4691 if (isSlctCC)
4692 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4693 Slct.getOperand(0), Slct.getOperand(1), CC);
4694 SDValue CCOp = Slct.getOperand(0);
4695 if (InvCC)
4696 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4697 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4698 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4699 CCOp, OtherOp, Result);
4700 }
4701 return SDValue();
4702}
4703
Bob Wilson3d5792a2010-07-29 20:34:14 +00004704/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4705/// operands N0 and N1. This is a helper for PerformADDCombine that is
4706/// called with the default operands, and if that fails, with commuted
4707/// operands.
4708static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4709 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004710 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4711 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4712 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4713 if (Result.getNode()) return Result;
4714 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00004715 return SDValue();
4716}
4717
Bob Wilson3d5792a2010-07-29 20:34:14 +00004718/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4719///
4720static SDValue PerformADDCombine(SDNode *N,
4721 TargetLowering::DAGCombinerInfo &DCI) {
4722 SDValue N0 = N->getOperand(0);
4723 SDValue N1 = N->getOperand(1);
4724
4725 // First try with the default operand order.
4726 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4727 if (Result.getNode())
4728 return Result;
4729
4730 // If that didn't work, try again with the operands commuted.
4731 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4732}
4733
Chris Lattnerd1980a52009-03-12 06:52:53 +00004734/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004735///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004736static SDValue PerformSUBCombine(SDNode *N,
4737 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004738 SDValue N0 = N->getOperand(0);
4739 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004740
Chris Lattnerd1980a52009-03-12 06:52:53 +00004741 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4742 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4743 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4744 if (Result.getNode()) return Result;
4745 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004746
Chris Lattnerd1980a52009-03-12 06:52:53 +00004747 return SDValue();
4748}
4749
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004750static SDValue PerformMULCombine(SDNode *N,
4751 TargetLowering::DAGCombinerInfo &DCI,
4752 const ARMSubtarget *Subtarget) {
4753 SelectionDAG &DAG = DCI.DAG;
4754
4755 if (Subtarget->isThumb1Only())
4756 return SDValue();
4757
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004758 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4759 return SDValue();
4760
4761 EVT VT = N->getValueType(0);
4762 if (VT != MVT::i32)
4763 return SDValue();
4764
4765 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4766 if (!C)
4767 return SDValue();
4768
4769 uint64_t MulAmt = C->getZExtValue();
4770 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4771 ShiftAmt = ShiftAmt & (32 - 1);
4772 SDValue V = N->getOperand(0);
4773 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004774
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004775 SDValue Res;
4776 MulAmt >>= ShiftAmt;
4777 if (isPowerOf2_32(MulAmt - 1)) {
4778 // (mul x, 2^N + 1) => (add (shl x, N), x)
4779 Res = DAG.getNode(ISD::ADD, DL, VT,
4780 V, DAG.getNode(ISD::SHL, DL, VT,
4781 V, DAG.getConstant(Log2_32(MulAmt-1),
4782 MVT::i32)));
4783 } else if (isPowerOf2_32(MulAmt + 1)) {
4784 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4785 Res = DAG.getNode(ISD::SUB, DL, VT,
4786 DAG.getNode(ISD::SHL, DL, VT,
4787 V, DAG.getConstant(Log2_32(MulAmt+1),
4788 MVT::i32)),
4789 V);
4790 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004791 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004792
4793 if (ShiftAmt != 0)
4794 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4795 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004796
4797 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004798 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004799 return SDValue();
4800}
4801
Owen Anderson080c0922010-11-05 19:27:46 +00004802static SDValue PerformANDCombine(SDNode *N,
4803 TargetLowering::DAGCombinerInfo &DCI) {
4804 // Attempt to use immediate-form VBIC
4805 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4806 DebugLoc dl = N->getDebugLoc();
4807 EVT VT = N->getValueType(0);
4808 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004809
Owen Anderson080c0922010-11-05 19:27:46 +00004810 APInt SplatBits, SplatUndef;
4811 unsigned SplatBitSize;
4812 bool HasAnyUndefs;
4813 if (BVN &&
4814 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4815 if (SplatBitSize <= 64) {
4816 EVT VbicVT;
4817 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
4818 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004819 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004820 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00004821 if (Val.getNode()) {
4822 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004823 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00004824 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004825 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00004826 }
4827 }
4828 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004829
Owen Anderson080c0922010-11-05 19:27:46 +00004830 return SDValue();
4831}
4832
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004833/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4834static SDValue PerformORCombine(SDNode *N,
4835 TargetLowering::DAGCombinerInfo &DCI,
4836 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00004837 // Attempt to use immediate-form VORR
4838 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4839 DebugLoc dl = N->getDebugLoc();
4840 EVT VT = N->getValueType(0);
4841 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004842
Owen Anderson60f48702010-11-03 23:15:26 +00004843 APInt SplatBits, SplatUndef;
4844 unsigned SplatBitSize;
4845 bool HasAnyUndefs;
4846 if (BVN && Subtarget->hasNEON() &&
4847 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4848 if (SplatBitSize <= 64) {
4849 EVT VorrVT;
4850 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
4851 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004852 DAG, VorrVT, VT.is128BitVector(),
4853 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00004854 if (Val.getNode()) {
4855 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004856 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00004857 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004858 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00004859 }
4860 }
4861 }
4862
Jim Grosbach54238562010-07-17 03:30:54 +00004863 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4864 // reasonable.
4865
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004866 // BFI is only available on V6T2+
4867 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4868 return SDValue();
4869
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004870 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004871 DebugLoc DL = N->getDebugLoc();
4872 // 1) or (and A, mask), val => ARMbfi A, val, mask
4873 // iff (val & mask) == val
4874 //
4875 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4876 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4877 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4878 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4879 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4880 // (i.e., copy a bitfield value into another bitfield of the same width)
4881 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004882 return SDValue();
4883
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004884 if (VT != MVT::i32)
4885 return SDValue();
4886
Evan Cheng30fb13f2010-12-13 20:32:54 +00004887 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00004888
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004889 // The value and the mask need to be constants so we can verify this is
4890 // actually a bitfield set. If the mask is 0xffff, we can do better
4891 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00004892 SDValue MaskOp = N0.getOperand(1);
4893 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
4894 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004895 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004896 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004897 if (Mask == 0xffff)
4898 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004899 SDValue Res;
4900 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004901 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4902 if (N1C) {
4903 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004904 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00004905 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004906
Evan Chenga9688c42010-12-11 04:11:38 +00004907 if (ARM::isBitFieldInvertedMask(Mask)) {
4908 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004909
Evan Cheng30fb13f2010-12-13 20:32:54 +00004910 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00004911 DAG.getConstant(Val, MVT::i32),
4912 DAG.getConstant(Mask, MVT::i32));
4913
4914 // Do not add new nodes to DAG combiner worklist.
4915 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004916 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004917 }
Jim Grosbach54238562010-07-17 03:30:54 +00004918 } else if (N1.getOpcode() == ISD::AND) {
4919 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004920 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4921 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00004922 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004923 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004924
4925 if (ARM::isBitFieldInvertedMask(Mask) &&
4926 ARM::isBitFieldInvertedMask(~Mask2) &&
4927 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4928 // The pack halfword instruction works better for masks that fit it,
4929 // so use that when it's available.
4930 if (Subtarget->hasT2ExtractPack() &&
4931 (Mask == 0xffff || Mask == 0xffff0000))
4932 return SDValue();
4933 // 2a
4934 unsigned lsb = CountTrailingZeros_32(Mask2);
4935 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4936 DAG.getConstant(lsb, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00004937 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00004938 DAG.getConstant(Mask, MVT::i32));
4939 // Do not add new nodes to DAG combiner worklist.
4940 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004941 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004942 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4943 ARM::isBitFieldInvertedMask(Mask2) &&
4944 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4945 // The pack halfword instruction works better for masks that fit it,
4946 // so use that when it's available.
4947 if (Subtarget->hasT2ExtractPack() &&
4948 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4949 return SDValue();
4950 // 2b
4951 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004952 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00004953 DAG.getConstant(lsb, MVT::i32));
4954 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4955 DAG.getConstant(Mask2, MVT::i32));
4956 // Do not add new nodes to DAG combiner worklist.
4957 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004958 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004959 }
4960 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004961
Evan Cheng30fb13f2010-12-13 20:32:54 +00004962 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
4963 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
4964 ARM::isBitFieldInvertedMask(~Mask)) {
4965 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
4966 // where lsb(mask) == #shamt and masked bits of B are known zero.
4967 SDValue ShAmt = N00.getOperand(1);
4968 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
4969 unsigned LSB = CountTrailingZeros_32(Mask);
4970 if (ShAmtC != LSB)
4971 return SDValue();
4972
4973 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
4974 DAG.getConstant(~Mask, MVT::i32));
4975
4976 // Do not add new nodes to DAG combiner worklist.
4977 DCI.CombineTo(N, Res, false);
4978 }
4979
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004980 return SDValue();
4981}
4982
Evan Cheng0c1aec12010-12-14 03:22:07 +00004983/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
4984/// C1 & C2 == C1.
4985static SDValue PerformBFICombine(SDNode *N,
4986 TargetLowering::DAGCombinerInfo &DCI) {
4987 SDValue N1 = N->getOperand(1);
4988 if (N1.getOpcode() == ISD::AND) {
4989 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4990 if (!N11C)
4991 return SDValue();
4992 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
4993 unsigned Mask2 = N11C->getZExtValue();
4994 if ((Mask & Mask2) == Mask2)
4995 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
4996 N->getOperand(0), N1.getOperand(0),
4997 N->getOperand(2));
4998 }
4999 return SDValue();
5000}
5001
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005002/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5003/// ARMISD::VMOVRRD.
5004static SDValue PerformVMOVRRDCombine(SDNode *N,
5005 TargetLowering::DAGCombinerInfo &DCI) {
5006 // vmovrrd(vmovdrr x, y) -> x,y
5007 SDValue InDouble = N->getOperand(0);
5008 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5009 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
5010 return SDValue();
5011}
5012
5013/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5014/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5015static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5016 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5017 SDValue Op0 = N->getOperand(0);
5018 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005019 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005020 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005021 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005022 Op1 = Op1.getOperand(0);
5023 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5024 Op0.getNode() == Op1.getNode() &&
5025 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005026 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005027 N->getValueType(0), Op0.getOperand(0));
5028 return SDValue();
5029}
5030
Bob Wilson31600902010-12-21 06:43:19 +00005031/// PerformSTORECombine - Target-specific dag combine xforms for
5032/// ISD::STORE.
5033static SDValue PerformSTORECombine(SDNode *N,
5034 TargetLowering::DAGCombinerInfo &DCI) {
5035 // Bitcast an i64 store extracted from a vector to f64.
5036 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5037 StoreSDNode *St = cast<StoreSDNode>(N);
5038 SDValue StVal = St->getValue();
5039 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5040 StVal.getValueType() != MVT::i64 ||
5041 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5042 return SDValue();
5043
5044 SelectionDAG &DAG = DCI.DAG;
5045 DebugLoc dl = StVal.getDebugLoc();
5046 SDValue IntVec = StVal.getOperand(0);
5047 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5048 IntVec.getValueType().getVectorNumElements());
5049 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5050 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5051 Vec, StVal.getOperand(1));
5052 dl = N->getDebugLoc();
5053 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5054 // Make the DAGCombiner fold the bitcasts.
5055 DCI.AddToWorklist(Vec.getNode());
5056 DCI.AddToWorklist(ExtElt.getNode());
5057 DCI.AddToWorklist(V.getNode());
5058 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5059 St->getPointerInfo(), St->isVolatile(),
5060 St->isNonTemporal(), St->getAlignment(),
5061 St->getTBAAInfo());
5062}
5063
5064/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5065/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5066/// i64 vector to have f64 elements, since the value can then be loaded
5067/// directly into a VFP register.
5068static bool hasNormalLoadOperand(SDNode *N) {
5069 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5070 for (unsigned i = 0; i < NumElts; ++i) {
5071 SDNode *Elt = N->getOperand(i).getNode();
5072 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5073 return true;
5074 }
5075 return false;
5076}
5077
Bob Wilson75f02882010-09-17 22:59:05 +00005078/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5079/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005080static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5081 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005082 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5083 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5084 // into a pair of GPRs, which is fine when the value is used as a scalar,
5085 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005086 SelectionDAG &DAG = DCI.DAG;
5087 if (N->getNumOperands() == 2) {
5088 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5089 if (RV.getNode())
5090 return RV;
5091 }
Bob Wilson75f02882010-09-17 22:59:05 +00005092
Bob Wilson31600902010-12-21 06:43:19 +00005093 // Load i64 elements as f64 values so that type legalization does not split
5094 // them up into i32 values.
5095 EVT VT = N->getValueType(0);
5096 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5097 return SDValue();
5098 DebugLoc dl = N->getDebugLoc();
5099 SmallVector<SDValue, 8> Ops;
5100 unsigned NumElts = VT.getVectorNumElements();
5101 for (unsigned i = 0; i < NumElts; ++i) {
5102 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5103 Ops.push_back(V);
5104 // Make the DAGCombiner fold the bitcast.
5105 DCI.AddToWorklist(V.getNode());
5106 }
5107 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5108 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5109 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5110}
5111
5112/// PerformInsertEltCombine - Target-specific dag combine xforms for
5113/// ISD::INSERT_VECTOR_ELT.
5114static SDValue PerformInsertEltCombine(SDNode *N,
5115 TargetLowering::DAGCombinerInfo &DCI) {
5116 // Bitcast an i64 load inserted into a vector to f64.
5117 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5118 EVT VT = N->getValueType(0);
5119 SDNode *Elt = N->getOperand(1).getNode();
5120 if (VT.getVectorElementType() != MVT::i64 ||
5121 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5122 return SDValue();
5123
5124 SelectionDAG &DAG = DCI.DAG;
5125 DebugLoc dl = N->getDebugLoc();
5126 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5127 VT.getVectorNumElements());
5128 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5129 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5130 // Make the DAGCombiner fold the bitcasts.
5131 DCI.AddToWorklist(Vec.getNode());
5132 DCI.AddToWorklist(V.getNode());
5133 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5134 Vec, V, N->getOperand(2));
5135 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005136}
5137
Bob Wilsonf20700c2010-10-27 20:38:28 +00005138/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5139/// ISD::VECTOR_SHUFFLE.
5140static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5141 // The LLVM shufflevector instruction does not require the shuffle mask
5142 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5143 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5144 // operands do not match the mask length, they are extended by concatenating
5145 // them with undef vectors. That is probably the right thing for other
5146 // targets, but for NEON it is better to concatenate two double-register
5147 // size vector operands into a single quad-register size vector. Do that
5148 // transformation here:
5149 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5150 // shuffle(concat(v1, v2), undef)
5151 SDValue Op0 = N->getOperand(0);
5152 SDValue Op1 = N->getOperand(1);
5153 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5154 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5155 Op0.getNumOperands() != 2 ||
5156 Op1.getNumOperands() != 2)
5157 return SDValue();
5158 SDValue Concat0Op1 = Op0.getOperand(1);
5159 SDValue Concat1Op1 = Op1.getOperand(1);
5160 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5161 Concat1Op1.getOpcode() != ISD::UNDEF)
5162 return SDValue();
5163 // Skip the transformation if any of the types are illegal.
5164 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5165 EVT VT = N->getValueType(0);
5166 if (!TLI.isTypeLegal(VT) ||
5167 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5168 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5169 return SDValue();
5170
5171 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5172 Op0.getOperand(0), Op1.getOperand(0));
5173 // Translate the shuffle mask.
5174 SmallVector<int, 16> NewMask;
5175 unsigned NumElts = VT.getVectorNumElements();
5176 unsigned HalfElts = NumElts/2;
5177 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5178 for (unsigned n = 0; n < NumElts; ++n) {
5179 int MaskElt = SVN->getMaskElt(n);
5180 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005181 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005182 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005183 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005184 NewElt = HalfElts + MaskElt - NumElts;
5185 NewMask.push_back(NewElt);
5186 }
5187 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5188 DAG.getUNDEF(VT), NewMask.data());
5189}
5190
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005191/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5192/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5193/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5194/// return true.
5195static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5196 SelectionDAG &DAG = DCI.DAG;
5197 EVT VT = N->getValueType(0);
5198 // vldN-dup instructions only support 64-bit vectors for N > 1.
5199 if (!VT.is64BitVector())
5200 return false;
5201
5202 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5203 SDNode *VLD = N->getOperand(0).getNode();
5204 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5205 return false;
5206 unsigned NumVecs = 0;
5207 unsigned NewOpc = 0;
5208 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5209 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5210 NumVecs = 2;
5211 NewOpc = ARMISD::VLD2DUP;
5212 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5213 NumVecs = 3;
5214 NewOpc = ARMISD::VLD3DUP;
5215 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5216 NumVecs = 4;
5217 NewOpc = ARMISD::VLD4DUP;
5218 } else {
5219 return false;
5220 }
5221
5222 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5223 // numbers match the load.
5224 unsigned VLDLaneNo =
5225 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5226 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5227 UI != UE; ++UI) {
5228 // Ignore uses of the chain result.
5229 if (UI.getUse().getResNo() == NumVecs)
5230 continue;
5231 SDNode *User = *UI;
5232 if (User->getOpcode() != ARMISD::VDUPLANE ||
5233 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5234 return false;
5235 }
5236
5237 // Create the vldN-dup node.
5238 EVT Tys[5];
5239 unsigned n;
5240 for (n = 0; n < NumVecs; ++n)
5241 Tys[n] = VT;
5242 Tys[n] = MVT::Other;
5243 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5244 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5245 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5246 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5247 Ops, 2, VLDMemInt->getMemoryVT(),
5248 VLDMemInt->getMemOperand());
5249
5250 // Update the uses.
5251 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5252 UI != UE; ++UI) {
5253 unsigned ResNo = UI.getUse().getResNo();
5254 // Ignore uses of the chain result.
5255 if (ResNo == NumVecs)
5256 continue;
5257 SDNode *User = *UI;
5258 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5259 }
5260
5261 // Now the vldN-lane intrinsic is dead except for its chain result.
5262 // Update uses of the chain.
5263 std::vector<SDValue> VLDDupResults;
5264 for (unsigned n = 0; n < NumVecs; ++n)
5265 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5266 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5267 DCI.CombineTo(VLD, VLDDupResults);
5268
5269 return true;
5270}
5271
Bob Wilson9e82bf12010-07-14 01:22:12 +00005272/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5273/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005274static SDValue PerformVDUPLANECombine(SDNode *N,
5275 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005276 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005277
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005278 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5279 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5280 if (CombineVLDDUP(N, DCI))
5281 return SDValue(N, 0);
5282
5283 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5284 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005285 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005286 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005287 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005288 return SDValue();
5289
5290 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5291 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5292 // The canonical VMOV for a zero vector uses a 32-bit element size.
5293 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5294 unsigned EltBits;
5295 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5296 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005297 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005298 if (EltSize > VT.getVectorElementType().getSizeInBits())
5299 return SDValue();
5300
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005301 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005302}
5303
Bob Wilson5bafff32009-06-22 23:27:02 +00005304/// getVShiftImm - Check if this is a valid build_vector for the immediate
5305/// operand of a vector shift operation, where all the elements of the
5306/// build_vector must have the same constant integer value.
5307static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5308 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005309 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005310 Op = Op.getOperand(0);
5311 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5312 APInt SplatBits, SplatUndef;
5313 unsigned SplatBitSize;
5314 bool HasAnyUndefs;
5315 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5316 HasAnyUndefs, ElementBits) ||
5317 SplatBitSize > ElementBits)
5318 return false;
5319 Cnt = SplatBits.getSExtValue();
5320 return true;
5321}
5322
5323/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5324/// operand of a vector shift left operation. That value must be in the range:
5325/// 0 <= Value < ElementBits for a left shift; or
5326/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005327static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005328 assert(VT.isVector() && "vector shift count is not a vector type");
5329 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5330 if (! getVShiftImm(Op, ElementBits, Cnt))
5331 return false;
5332 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5333}
5334
5335/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5336/// operand of a vector shift right operation. For a shift opcode, the value
5337/// is positive, but for an intrinsic the value count must be negative. The
5338/// absolute value must be in the range:
5339/// 1 <= |Value| <= ElementBits for a right shift; or
5340/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005341static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005342 int64_t &Cnt) {
5343 assert(VT.isVector() && "vector shift count is not a vector type");
5344 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5345 if (! getVShiftImm(Op, ElementBits, Cnt))
5346 return false;
5347 if (isIntrinsic)
5348 Cnt = -Cnt;
5349 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5350}
5351
5352/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5353static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5354 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5355 switch (IntNo) {
5356 default:
5357 // Don't do anything for most intrinsics.
5358 break;
5359
5360 // Vector shifts: check for immediate versions and lower them.
5361 // Note: This is done during DAG combining instead of DAG legalizing because
5362 // the build_vectors for 64-bit vector element shift counts are generally
5363 // not legal, and it is hard to see their values after they get legalized to
5364 // loads from a constant pool.
5365 case Intrinsic::arm_neon_vshifts:
5366 case Intrinsic::arm_neon_vshiftu:
5367 case Intrinsic::arm_neon_vshiftls:
5368 case Intrinsic::arm_neon_vshiftlu:
5369 case Intrinsic::arm_neon_vshiftn:
5370 case Intrinsic::arm_neon_vrshifts:
5371 case Intrinsic::arm_neon_vrshiftu:
5372 case Intrinsic::arm_neon_vrshiftn:
5373 case Intrinsic::arm_neon_vqshifts:
5374 case Intrinsic::arm_neon_vqshiftu:
5375 case Intrinsic::arm_neon_vqshiftsu:
5376 case Intrinsic::arm_neon_vqshiftns:
5377 case Intrinsic::arm_neon_vqshiftnu:
5378 case Intrinsic::arm_neon_vqshiftnsu:
5379 case Intrinsic::arm_neon_vqrshiftns:
5380 case Intrinsic::arm_neon_vqrshiftnu:
5381 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00005382 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005383 int64_t Cnt;
5384 unsigned VShiftOpc = 0;
5385
5386 switch (IntNo) {
5387 case Intrinsic::arm_neon_vshifts:
5388 case Intrinsic::arm_neon_vshiftu:
5389 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
5390 VShiftOpc = ARMISD::VSHL;
5391 break;
5392 }
5393 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
5394 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
5395 ARMISD::VSHRs : ARMISD::VSHRu);
5396 break;
5397 }
5398 return SDValue();
5399
5400 case Intrinsic::arm_neon_vshiftls:
5401 case Intrinsic::arm_neon_vshiftlu:
5402 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
5403 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005404 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005405
5406 case Intrinsic::arm_neon_vrshifts:
5407 case Intrinsic::arm_neon_vrshiftu:
5408 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
5409 break;
5410 return SDValue();
5411
5412 case Intrinsic::arm_neon_vqshifts:
5413 case Intrinsic::arm_neon_vqshiftu:
5414 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5415 break;
5416 return SDValue();
5417
5418 case Intrinsic::arm_neon_vqshiftsu:
5419 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5420 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005421 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005422
5423 case Intrinsic::arm_neon_vshiftn:
5424 case Intrinsic::arm_neon_vrshiftn:
5425 case Intrinsic::arm_neon_vqshiftns:
5426 case Intrinsic::arm_neon_vqshiftnu:
5427 case Intrinsic::arm_neon_vqshiftnsu:
5428 case Intrinsic::arm_neon_vqrshiftns:
5429 case Intrinsic::arm_neon_vqrshiftnu:
5430 case Intrinsic::arm_neon_vqrshiftnsu:
5431 // Narrowing shifts require an immediate right shift.
5432 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
5433 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00005434 llvm_unreachable("invalid shift count for narrowing vector shift "
5435 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005436
5437 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005438 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00005439 }
5440
5441 switch (IntNo) {
5442 case Intrinsic::arm_neon_vshifts:
5443 case Intrinsic::arm_neon_vshiftu:
5444 // Opcode already set above.
5445 break;
5446 case Intrinsic::arm_neon_vshiftls:
5447 case Intrinsic::arm_neon_vshiftlu:
5448 if (Cnt == VT.getVectorElementType().getSizeInBits())
5449 VShiftOpc = ARMISD::VSHLLi;
5450 else
5451 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
5452 ARMISD::VSHLLs : ARMISD::VSHLLu);
5453 break;
5454 case Intrinsic::arm_neon_vshiftn:
5455 VShiftOpc = ARMISD::VSHRN; break;
5456 case Intrinsic::arm_neon_vrshifts:
5457 VShiftOpc = ARMISD::VRSHRs; break;
5458 case Intrinsic::arm_neon_vrshiftu:
5459 VShiftOpc = ARMISD::VRSHRu; break;
5460 case Intrinsic::arm_neon_vrshiftn:
5461 VShiftOpc = ARMISD::VRSHRN; break;
5462 case Intrinsic::arm_neon_vqshifts:
5463 VShiftOpc = ARMISD::VQSHLs; break;
5464 case Intrinsic::arm_neon_vqshiftu:
5465 VShiftOpc = ARMISD::VQSHLu; break;
5466 case Intrinsic::arm_neon_vqshiftsu:
5467 VShiftOpc = ARMISD::VQSHLsu; break;
5468 case Intrinsic::arm_neon_vqshiftns:
5469 VShiftOpc = ARMISD::VQSHRNs; break;
5470 case Intrinsic::arm_neon_vqshiftnu:
5471 VShiftOpc = ARMISD::VQSHRNu; break;
5472 case Intrinsic::arm_neon_vqshiftnsu:
5473 VShiftOpc = ARMISD::VQSHRNsu; break;
5474 case Intrinsic::arm_neon_vqrshiftns:
5475 VShiftOpc = ARMISD::VQRSHRNs; break;
5476 case Intrinsic::arm_neon_vqrshiftnu:
5477 VShiftOpc = ARMISD::VQRSHRNu; break;
5478 case Intrinsic::arm_neon_vqrshiftnsu:
5479 VShiftOpc = ARMISD::VQRSHRNsu; break;
5480 }
5481
5482 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005483 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005484 }
5485
5486 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00005487 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005488 int64_t Cnt;
5489 unsigned VShiftOpc = 0;
5490
5491 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
5492 VShiftOpc = ARMISD::VSLI;
5493 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
5494 VShiftOpc = ARMISD::VSRI;
5495 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005496 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005497 }
5498
5499 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
5500 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00005501 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005502 }
5503
5504 case Intrinsic::arm_neon_vqrshifts:
5505 case Intrinsic::arm_neon_vqrshiftu:
5506 // No immediate versions of these to check for.
5507 break;
5508 }
5509
5510 return SDValue();
5511}
5512
5513/// PerformShiftCombine - Checks for immediate versions of vector shifts and
5514/// lowers them. As with the vector shift intrinsics, this is done during DAG
5515/// combining instead of DAG legalizing because the build_vectors for 64-bit
5516/// vector element shift counts are generally not legal, and it is hard to see
5517/// their values after they get legalized to loads from a constant pool.
5518static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
5519 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00005520 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00005521
5522 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00005523 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5524 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00005525 return SDValue();
5526
5527 assert(ST->hasNEON() && "unexpected vector shift");
5528 int64_t Cnt;
5529
5530 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005531 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005532
5533 case ISD::SHL:
5534 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
5535 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005536 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005537 break;
5538
5539 case ISD::SRA:
5540 case ISD::SRL:
5541 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
5542 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
5543 ARMISD::VSHRs : ARMISD::VSHRu);
5544 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005545 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005546 }
5547 }
5548 return SDValue();
5549}
5550
5551/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
5552/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
5553static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
5554 const ARMSubtarget *ST) {
5555 SDValue N0 = N->getOperand(0);
5556
5557 // Check for sign- and zero-extensions of vector extract operations of 8-
5558 // and 16-bit vector elements. NEON supports these directly. They are
5559 // handled during DAG combining because type legalization will promote them
5560 // to 32-bit types and it is messy to recognize the operations after that.
5561 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5562 SDValue Vec = N0.getOperand(0);
5563 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005564 EVT VT = N->getValueType(0);
5565 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005566 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5567
Owen Anderson825b72b2009-08-11 20:47:22 +00005568 if (VT == MVT::i32 &&
5569 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00005570 TLI.isTypeLegal(Vec.getValueType()) &&
5571 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005572
5573 unsigned Opc = 0;
5574 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005575 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005576 case ISD::SIGN_EXTEND:
5577 Opc = ARMISD::VGETLANEs;
5578 break;
5579 case ISD::ZERO_EXTEND:
5580 case ISD::ANY_EXTEND:
5581 Opc = ARMISD::VGETLANEu;
5582 break;
5583 }
5584 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
5585 }
5586 }
5587
5588 return SDValue();
5589}
5590
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005591/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
5592/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
5593static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
5594 const ARMSubtarget *ST) {
5595 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00005596 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005597 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
5598 // a NaN; only do the transformation when it matches that behavior.
5599
5600 // For now only do this when using NEON for FP operations; if using VFP, it
5601 // is not obvious that the benefit outweighs the cost of switching to the
5602 // NEON pipeline.
5603 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
5604 N->getValueType(0) != MVT::f32)
5605 return SDValue();
5606
5607 SDValue CondLHS = N->getOperand(0);
5608 SDValue CondRHS = N->getOperand(1);
5609 SDValue LHS = N->getOperand(2);
5610 SDValue RHS = N->getOperand(3);
5611 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
5612
5613 unsigned Opcode = 0;
5614 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00005615 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005616 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00005617 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005618 IsReversed = true ; // x CC y ? y : x
5619 } else {
5620 return SDValue();
5621 }
5622
Bob Wilsone742bb52010-02-24 22:15:53 +00005623 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005624 switch (CC) {
5625 default: break;
5626 case ISD::SETOLT:
5627 case ISD::SETOLE:
5628 case ISD::SETLT:
5629 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005630 case ISD::SETULT:
5631 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005632 // If LHS is NaN, an ordered comparison will be false and the result will
5633 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
5634 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5635 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
5636 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5637 break;
5638 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
5639 // will return -0, so vmin can only be used for unsafe math or if one of
5640 // the operands is known to be nonzero.
5641 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
5642 !UnsafeFPMath &&
5643 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5644 break;
5645 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005646 break;
5647
5648 case ISD::SETOGT:
5649 case ISD::SETOGE:
5650 case ISD::SETGT:
5651 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005652 case ISD::SETUGT:
5653 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005654 // If LHS is NaN, an ordered comparison will be false and the result will
5655 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
5656 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5657 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
5658 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5659 break;
5660 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
5661 // will return +0, so vmax can only be used for unsafe math or if one of
5662 // the operands is known to be nonzero.
5663 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
5664 !UnsafeFPMath &&
5665 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5666 break;
5667 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005668 break;
5669 }
5670
5671 if (!Opcode)
5672 return SDValue();
5673 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
5674}
5675
Dan Gohman475871a2008-07-27 21:46:04 +00005676SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005677 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005678 switch (N->getOpcode()) {
5679 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005680 case ISD::ADD: return PerformADDCombine(N, DCI);
5681 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005682 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005683 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00005684 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00005685 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00005686 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005687 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00005688 case ISD::STORE: return PerformSTORECombine(N, DCI);
5689 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
5690 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00005691 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005692 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005693 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005694 case ISD::SHL:
5695 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005696 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005697 case ISD::SIGN_EXTEND:
5698 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005699 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
5700 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005701 }
Dan Gohman475871a2008-07-27 21:46:04 +00005702 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005703}
5704
Bill Wendlingaf566342009-08-15 21:21:19 +00005705bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00005706 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00005707 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00005708
5709 switch (VT.getSimpleVT().SimpleTy) {
5710 default:
5711 return false;
5712 case MVT::i8:
5713 case MVT::i16:
5714 case MVT::i32:
5715 return true;
5716 // FIXME: VLD1 etc with standard alignment is legal.
5717 }
5718}
5719
Evan Chenge6c835f2009-08-14 20:09:37 +00005720static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
5721 if (V < 0)
5722 return false;
5723
5724 unsigned Scale = 1;
5725 switch (VT.getSimpleVT().SimpleTy) {
5726 default: return false;
5727 case MVT::i1:
5728 case MVT::i8:
5729 // Scale == 1;
5730 break;
5731 case MVT::i16:
5732 // Scale == 2;
5733 Scale = 2;
5734 break;
5735 case MVT::i32:
5736 // Scale == 4;
5737 Scale = 4;
5738 break;
5739 }
5740
5741 if ((V & (Scale - 1)) != 0)
5742 return false;
5743 V /= Scale;
5744 return V == (V & ((1LL << 5) - 1));
5745}
5746
5747static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
5748 const ARMSubtarget *Subtarget) {
5749 bool isNeg = false;
5750 if (V < 0) {
5751 isNeg = true;
5752 V = - V;
5753 }
5754
5755 switch (VT.getSimpleVT().SimpleTy) {
5756 default: return false;
5757 case MVT::i1:
5758 case MVT::i8:
5759 case MVT::i16:
5760 case MVT::i32:
5761 // + imm12 or - imm8
5762 if (isNeg)
5763 return V == (V & ((1LL << 8) - 1));
5764 return V == (V & ((1LL << 12) - 1));
5765 case MVT::f32:
5766 case MVT::f64:
5767 // Same as ARM mode. FIXME: NEON?
5768 if (!Subtarget->hasVFP2())
5769 return false;
5770 if ((V & 3) != 0)
5771 return false;
5772 V >>= 2;
5773 return V == (V & ((1LL << 8) - 1));
5774 }
5775}
5776
Evan Chengb01fad62007-03-12 23:30:29 +00005777/// isLegalAddressImmediate - Return true if the integer value can be used
5778/// as the offset of the target addressing mode for load / store of the
5779/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00005780static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005781 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00005782 if (V == 0)
5783 return true;
5784
Evan Cheng65011532009-03-09 19:15:00 +00005785 if (!VT.isSimple())
5786 return false;
5787
Evan Chenge6c835f2009-08-14 20:09:37 +00005788 if (Subtarget->isThumb1Only())
5789 return isLegalT1AddressImmediate(V, VT);
5790 else if (Subtarget->isThumb2())
5791 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00005792
Evan Chenge6c835f2009-08-14 20:09:37 +00005793 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00005794 if (V < 0)
5795 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00005796 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00005797 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005798 case MVT::i1:
5799 case MVT::i8:
5800 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00005801 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005802 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005803 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00005804 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005805 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 case MVT::f32:
5807 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00005808 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00005809 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00005810 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00005811 return false;
5812 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005813 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00005814 }
Evan Chenga8e29892007-01-19 07:51:42 +00005815}
5816
Evan Chenge6c835f2009-08-14 20:09:37 +00005817bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
5818 EVT VT) const {
5819 int Scale = AM.Scale;
5820 if (Scale < 0)
5821 return false;
5822
5823 switch (VT.getSimpleVT().SimpleTy) {
5824 default: return false;
5825 case MVT::i1:
5826 case MVT::i8:
5827 case MVT::i16:
5828 case MVT::i32:
5829 if (Scale == 1)
5830 return true;
5831 // r + r << imm
5832 Scale = Scale & ~1;
5833 return Scale == 2 || Scale == 4 || Scale == 8;
5834 case MVT::i64:
5835 // r + r
5836 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
5837 return true;
5838 return false;
5839 case MVT::isVoid:
5840 // Note, we allow "void" uses (basically, uses that aren't loads or
5841 // stores), because arm allows folding a scale into many arithmetic
5842 // operations. This should be made more precise and revisited later.
5843
5844 // Allow r << imm, but the imm has to be a multiple of two.
5845 if (Scale & 1) return false;
5846 return isPowerOf2_32(Scale);
5847 }
5848}
5849
Chris Lattner37caf8c2007-04-09 23:33:39 +00005850/// isLegalAddressingMode - Return true if the addressing mode represented
5851/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005852bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005853 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005854 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00005855 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00005856 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005857
Chris Lattner37caf8c2007-04-09 23:33:39 +00005858 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005859 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005860 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005861
Chris Lattner37caf8c2007-04-09 23:33:39 +00005862 switch (AM.Scale) {
5863 case 0: // no scale reg, must be "r+i" or "r", or "i".
5864 break;
5865 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00005866 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00005867 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005868 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00005869 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005870 // ARM doesn't support any R+R*scale+imm addr modes.
5871 if (AM.BaseOffs)
5872 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005873
Bob Wilson2c7dab12009-04-08 17:55:28 +00005874 if (!VT.isSimple())
5875 return false;
5876
Evan Chenge6c835f2009-08-14 20:09:37 +00005877 if (Subtarget->isThumb2())
5878 return isLegalT2ScaledAddressingMode(AM, VT);
5879
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005880 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00005881 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00005882 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005883 case MVT::i1:
5884 case MVT::i8:
5885 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005886 if (Scale < 0) Scale = -Scale;
5887 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005888 return true;
5889 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00005890 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00005891 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00005892 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005893 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005894 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005895 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00005896 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005897
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005899 // Note, we allow "void" uses (basically, uses that aren't loads or
5900 // stores), because arm allows folding a scale into many arithmetic
5901 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005902
Chris Lattner37caf8c2007-04-09 23:33:39 +00005903 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00005904 if (Scale & 1) return false;
5905 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00005906 }
5907 break;
Evan Chengb01fad62007-03-12 23:30:29 +00005908 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00005909 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00005910}
5911
Evan Cheng77e47512009-11-11 19:05:52 +00005912/// isLegalICmpImmediate - Return true if the specified immediate is legal
5913/// icmp immediate, that is the target has icmp instructions which can compare
5914/// a register against the immediate without having to materialize the
5915/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00005916bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00005917 if (!Subtarget->isThumb())
5918 return ARM_AM::getSOImmVal(Imm) != -1;
5919 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00005920 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00005921 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00005922}
5923
Owen Andersone50ed302009-08-10 22:56:29 +00005924static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005925 bool isSEXTLoad, SDValue &Base,
5926 SDValue &Offset, bool &isInc,
5927 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00005928 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5929 return false;
5930
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005932 // AddressingMode 3
5933 Base = Ptr->getOperand(0);
5934 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005935 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005936 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005937 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005938 isInc = false;
5939 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5940 return true;
5941 }
5942 }
5943 isInc = (Ptr->getOpcode() == ISD::ADD);
5944 Offset = Ptr->getOperand(1);
5945 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005946 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005947 // AddressingMode 2
5948 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005949 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005950 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005951 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005952 isInc = false;
5953 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5954 Base = Ptr->getOperand(0);
5955 return true;
5956 }
5957 }
5958
5959 if (Ptr->getOpcode() == ISD::ADD) {
5960 isInc = true;
5961 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5962 if (ShOpcVal != ARM_AM::no_shift) {
5963 Base = Ptr->getOperand(1);
5964 Offset = Ptr->getOperand(0);
5965 } else {
5966 Base = Ptr->getOperand(0);
5967 Offset = Ptr->getOperand(1);
5968 }
5969 return true;
5970 }
5971
5972 isInc = (Ptr->getOpcode() == ISD::ADD);
5973 Base = Ptr->getOperand(0);
5974 Offset = Ptr->getOperand(1);
5975 return true;
5976 }
5977
Jim Grosbache5165492009-11-09 00:11:35 +00005978 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00005979 return false;
5980}
5981
Owen Andersone50ed302009-08-10 22:56:29 +00005982static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005983 bool isSEXTLoad, SDValue &Base,
5984 SDValue &Offset, bool &isInc,
5985 SelectionDAG &DAG) {
5986 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5987 return false;
5988
5989 Base = Ptr->getOperand(0);
5990 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
5991 int RHSC = (int)RHS->getZExtValue();
5992 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
5993 assert(Ptr->getOpcode() == ISD::ADD);
5994 isInc = false;
5995 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5996 return true;
5997 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
5998 isInc = Ptr->getOpcode() == ISD::ADD;
5999 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6000 return true;
6001 }
6002 }
6003
6004 return false;
6005}
6006
Evan Chenga8e29892007-01-19 07:51:42 +00006007/// getPreIndexedAddressParts - returns true by value, base pointer and
6008/// offset pointer and addressing mode by reference if the node's address
6009/// can be legally represented as pre-indexed load / store address.
6010bool
Dan Gohman475871a2008-07-27 21:46:04 +00006011ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6012 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006013 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006014 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006015 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006016 return false;
6017
Owen Andersone50ed302009-08-10 22:56:29 +00006018 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006019 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006020 bool isSEXTLoad = false;
6021 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6022 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006023 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006024 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6025 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6026 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006027 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006028 } else
6029 return false;
6030
6031 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006032 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006033 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006034 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6035 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006036 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006037 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006038 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006039 if (!isLegal)
6040 return false;
6041
6042 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6043 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006044}
6045
6046/// getPostIndexedAddressParts - returns true by value, base pointer and
6047/// offset pointer and addressing mode by reference if this node can be
6048/// combined with a load / store to form a post-indexed load / store.
6049bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006050 SDValue &Base,
6051 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006052 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006053 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006054 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006055 return false;
6056
Owen Andersone50ed302009-08-10 22:56:29 +00006057 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006058 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006059 bool isSEXTLoad = false;
6060 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006061 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006062 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006063 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6064 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006065 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006066 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006067 } else
6068 return false;
6069
6070 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006071 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006072 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006073 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006074 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006075 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006076 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6077 isInc, DAG);
6078 if (!isLegal)
6079 return false;
6080
Evan Cheng28dad2a2010-05-18 21:31:17 +00006081 if (Ptr != Base) {
6082 // Swap base ptr and offset to catch more post-index load / store when
6083 // it's legal. In Thumb2 mode, offset must be an immediate.
6084 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6085 !Subtarget->isThumb2())
6086 std::swap(Base, Offset);
6087
6088 // Post-indexed load / store update the base pointer.
6089 if (Ptr != Base)
6090 return false;
6091 }
6092
Evan Chenge88d5ce2009-07-02 07:28:31 +00006093 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6094 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006095}
6096
Dan Gohman475871a2008-07-27 21:46:04 +00006097void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006098 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006099 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006100 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006101 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006102 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006103 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006104 switch (Op.getOpcode()) {
6105 default: break;
6106 case ARMISD::CMOV: {
6107 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006108 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006109 if (KnownZero == 0 && KnownOne == 0) return;
6110
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006111 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006112 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6113 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006114 KnownZero &= KnownZeroRHS;
6115 KnownOne &= KnownOneRHS;
6116 return;
6117 }
6118 }
6119}
6120
6121//===----------------------------------------------------------------------===//
6122// ARM Inline Assembly Support
6123//===----------------------------------------------------------------------===//
6124
Evan Cheng55d42002011-01-08 01:24:27 +00006125bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6126 // Looking for "rev" which is V6+.
6127 if (!Subtarget->hasV6Ops())
6128 return false;
6129
6130 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6131 std::string AsmStr = IA->getAsmString();
6132 SmallVector<StringRef, 4> AsmPieces;
6133 SplitString(AsmStr, AsmPieces, ";\n");
6134
6135 switch (AsmPieces.size()) {
6136 default: return false;
6137 case 1:
6138 AsmStr = AsmPieces[0];
6139 AsmPieces.clear();
6140 SplitString(AsmStr, AsmPieces, " \t,");
6141
6142 // rev $0, $1
6143 if (AsmPieces.size() == 3 &&
6144 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6145 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6146 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6147 if (Ty && Ty->getBitWidth() == 32)
6148 return IntrinsicLowering::LowerToByteSwap(CI);
6149 }
6150 break;
6151 }
6152
6153 return false;
6154}
6155
Evan Chenga8e29892007-01-19 07:51:42 +00006156/// getConstraintType - Given a constraint letter, return the type of
6157/// constraint it is for this target.
6158ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006159ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6160 if (Constraint.size() == 1) {
6161 switch (Constraint[0]) {
6162 default: break;
6163 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006164 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006165 }
Evan Chenga8e29892007-01-19 07:51:42 +00006166 }
Chris Lattner4234f572007-03-25 02:14:49 +00006167 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006168}
6169
John Thompson44ab89e2010-10-29 17:29:13 +00006170/// Examine constraint type and operand type and determine a weight value.
6171/// This object must already have been set up with the operand type
6172/// and the current alternative constraint selected.
6173TargetLowering::ConstraintWeight
6174ARMTargetLowering::getSingleConstraintMatchWeight(
6175 AsmOperandInfo &info, const char *constraint) const {
6176 ConstraintWeight weight = CW_Invalid;
6177 Value *CallOperandVal = info.CallOperandVal;
6178 // If we don't have a value, we can't do a match,
6179 // but allow it at the lowest weight.
6180 if (CallOperandVal == NULL)
6181 return CW_Default;
6182 const Type *type = CallOperandVal->getType();
6183 // Look at the constraint type.
6184 switch (*constraint) {
6185 default:
6186 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6187 break;
6188 case 'l':
6189 if (type->isIntegerTy()) {
6190 if (Subtarget->isThumb())
6191 weight = CW_SpecificReg;
6192 else
6193 weight = CW_Register;
6194 }
6195 break;
6196 case 'w':
6197 if (type->isFloatingPointTy())
6198 weight = CW_Register;
6199 break;
6200 }
6201 return weight;
6202}
6203
Bob Wilson2dc4f542009-03-20 22:42:55 +00006204std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006205ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006206 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006207 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006208 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006209 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006210 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006211 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006212 return std::make_pair(0U, ARM::tGPRRegisterClass);
6213 else
6214 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006215 case 'r':
6216 return std::make_pair(0U, ARM::GPRRegisterClass);
6217 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006218 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006219 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006220 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006221 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006222 if (VT.getSizeInBits() == 128)
6223 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006224 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006225 }
6226 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006227 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006228 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006229
Evan Chenga8e29892007-01-19 07:51:42 +00006230 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6231}
6232
6233std::vector<unsigned> ARMTargetLowering::
6234getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006235 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006236 if (Constraint.size() != 1)
6237 return std::vector<unsigned>();
6238
6239 switch (Constraint[0]) { // GCC ARM Constraint Letters
6240 default: break;
6241 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006242 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6243 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6244 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006245 case 'r':
6246 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6247 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6248 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6249 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006250 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006251 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006252 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6253 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6254 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6255 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6256 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6257 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6258 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6259 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006260 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006261 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6262 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6263 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6264 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006265 if (VT.getSizeInBits() == 128)
6266 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6267 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006268 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006269 }
6270
6271 return std::vector<unsigned>();
6272}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006273
6274/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6275/// vector. If it is invalid, don't add anything to Ops.
6276void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6277 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006278 std::vector<SDValue>&Ops,
6279 SelectionDAG &DAG) const {
6280 SDValue Result(0, 0);
6281
6282 switch (Constraint) {
6283 default: break;
6284 case 'I': case 'J': case 'K': case 'L':
6285 case 'M': case 'N': case 'O':
6286 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6287 if (!C)
6288 return;
6289
6290 int64_t CVal64 = C->getSExtValue();
6291 int CVal = (int) CVal64;
6292 // None of these constraints allow values larger than 32 bits. Check
6293 // that the value fits in an int.
6294 if (CVal != CVal64)
6295 return;
6296
6297 switch (Constraint) {
6298 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006299 if (Subtarget->isThumb1Only()) {
6300 // This must be a constant between 0 and 255, for ADD
6301 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006302 if (CVal >= 0 && CVal <= 255)
6303 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006304 } else if (Subtarget->isThumb2()) {
6305 // A constant that can be used as an immediate value in a
6306 // data-processing instruction.
6307 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6308 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006309 } else {
6310 // A constant that can be used as an immediate value in a
6311 // data-processing instruction.
6312 if (ARM_AM::getSOImmVal(CVal) != -1)
6313 break;
6314 }
6315 return;
6316
6317 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006318 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006319 // This must be a constant between -255 and -1, for negated ADD
6320 // immediates. This can be used in GCC with an "n" modifier that
6321 // prints the negated value, for use with SUB instructions. It is
6322 // not useful otherwise but is implemented for compatibility.
6323 if (CVal >= -255 && CVal <= -1)
6324 break;
6325 } else {
6326 // This must be a constant between -4095 and 4095. It is not clear
6327 // what this constraint is intended for. Implemented for
6328 // compatibility with GCC.
6329 if (CVal >= -4095 && CVal <= 4095)
6330 break;
6331 }
6332 return;
6333
6334 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006335 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006336 // A 32-bit value where only one byte has a nonzero value. Exclude
6337 // zero to match GCC. This constraint is used by GCC internally for
6338 // constants that can be loaded with a move/shift combination.
6339 // It is not useful otherwise but is implemented for compatibility.
6340 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
6341 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006342 } else if (Subtarget->isThumb2()) {
6343 // A constant whose bitwise inverse can be used as an immediate
6344 // value in a data-processing instruction. This can be used in GCC
6345 // with a "B" modifier that prints the inverted value, for use with
6346 // BIC and MVN instructions. It is not useful otherwise but is
6347 // implemented for compatibility.
6348 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
6349 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006350 } else {
6351 // A constant whose bitwise inverse can be used as an immediate
6352 // value in a data-processing instruction. This can be used in GCC
6353 // with a "B" modifier that prints the inverted value, for use with
6354 // BIC and MVN instructions. It is not useful otherwise but is
6355 // implemented for compatibility.
6356 if (ARM_AM::getSOImmVal(~CVal) != -1)
6357 break;
6358 }
6359 return;
6360
6361 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006362 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006363 // This must be a constant between -7 and 7,
6364 // for 3-operand ADD/SUB immediate instructions.
6365 if (CVal >= -7 && CVal < 7)
6366 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006367 } else if (Subtarget->isThumb2()) {
6368 // A constant whose negation can be used as an immediate value in a
6369 // data-processing instruction. This can be used in GCC with an "n"
6370 // modifier that prints the negated value, for use with SUB
6371 // instructions. It is not useful otherwise but is implemented for
6372 // compatibility.
6373 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
6374 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006375 } else {
6376 // A constant whose negation can be used as an immediate value in a
6377 // data-processing instruction. This can be used in GCC with an "n"
6378 // modifier that prints the negated value, for use with SUB
6379 // instructions. It is not useful otherwise but is implemented for
6380 // compatibility.
6381 if (ARM_AM::getSOImmVal(-CVal) != -1)
6382 break;
6383 }
6384 return;
6385
6386 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006387 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006388 // This must be a multiple of 4 between 0 and 1020, for
6389 // ADD sp + immediate.
6390 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
6391 break;
6392 } else {
6393 // A power of two or a constant between 0 and 32. This is used in
6394 // GCC for the shift amount on shifted register operands, but it is
6395 // useful in general for any shift amounts.
6396 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
6397 break;
6398 }
6399 return;
6400
6401 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006402 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006403 // This must be a constant between 0 and 31, for shift amounts.
6404 if (CVal >= 0 && CVal <= 31)
6405 break;
6406 }
6407 return;
6408
6409 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006410 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006411 // This must be a multiple of 4 between -508 and 508, for
6412 // ADD/SUB sp = sp + immediate.
6413 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
6414 break;
6415 }
6416 return;
6417 }
6418 Result = DAG.getTargetConstant(CVal, Op.getValueType());
6419 break;
6420 }
6421
6422 if (Result.getNode()) {
6423 Ops.push_back(Result);
6424 return;
6425 }
Dale Johannesen1784d162010-06-25 21:55:36 +00006426 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006427}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00006428
6429bool
6430ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
6431 // The ARM target isn't yet aware of offsets.
6432 return false;
6433}
Evan Cheng39382422009-10-28 01:44:26 +00006434
6435int ARM::getVFPf32Imm(const APFloat &FPImm) {
6436 APInt Imm = FPImm.bitcastToAPInt();
6437 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
6438 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
6439 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
6440
6441 // We can handle 4 bits of mantissa.
6442 // mantissa = (16+UInt(e:f:g:h))/16.
6443 if (Mantissa & 0x7ffff)
6444 return -1;
6445 Mantissa >>= 19;
6446 if ((Mantissa & 0xf) != Mantissa)
6447 return -1;
6448
6449 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6450 if (Exp < -3 || Exp > 4)
6451 return -1;
6452 Exp = ((Exp+3) & 0x7) ^ 4;
6453
6454 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6455}
6456
6457int ARM::getVFPf64Imm(const APFloat &FPImm) {
6458 APInt Imm = FPImm.bitcastToAPInt();
6459 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
6460 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
6461 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
6462
6463 // We can handle 4 bits of mantissa.
6464 // mantissa = (16+UInt(e:f:g:h))/16.
6465 if (Mantissa & 0xffffffffffffLL)
6466 return -1;
6467 Mantissa >>= 48;
6468 if ((Mantissa & 0xf) != Mantissa)
6469 return -1;
6470
6471 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6472 if (Exp < -3 || Exp > 4)
6473 return -1;
6474 Exp = ((Exp+3) & 0x7) ^ 4;
6475
6476 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6477}
6478
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006479bool ARM::isBitFieldInvertedMask(unsigned v) {
6480 if (v == 0xffffffff)
6481 return 0;
6482 // there can be 1's on either or both "outsides", all the "inside"
6483 // bits must be 0's
6484 unsigned int lsb = 0, msb = 31;
6485 while (v & (1 << msb)) --msb;
6486 while (v & (1 << lsb)) ++lsb;
6487 for (unsigned int i = lsb; i <= msb; ++i) {
6488 if (v & (1 << i))
6489 return 0;
6490 }
6491 return 1;
6492}
6493
Evan Cheng39382422009-10-28 01:44:26 +00006494/// isFPImmLegal - Returns true if the target can instruction select the
6495/// specified FP immediate natively. If false, the legalizer will
6496/// materialize the FP immediate as a load from a constant pool.
6497bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
6498 if (!Subtarget->hasVFP3())
6499 return false;
6500 if (VT == MVT::f32)
6501 return ARM::getVFPf32Imm(Imm) != -1;
6502 if (VT == MVT::f64)
6503 return ARM::getVFPf64Imm(Imm) != -1;
6504 return false;
6505}
Bob Wilson65ffec42010-09-21 17:56:22 +00006506
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006507/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00006508/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
6509/// specified in the intrinsic calls.
6510bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
6511 const CallInst &I,
6512 unsigned Intrinsic) const {
6513 switch (Intrinsic) {
6514 case Intrinsic::arm_neon_vld1:
6515 case Intrinsic::arm_neon_vld2:
6516 case Intrinsic::arm_neon_vld3:
6517 case Intrinsic::arm_neon_vld4:
6518 case Intrinsic::arm_neon_vld2lane:
6519 case Intrinsic::arm_neon_vld3lane:
6520 case Intrinsic::arm_neon_vld4lane: {
6521 Info.opc = ISD::INTRINSIC_W_CHAIN;
6522 // Conservatively set memVT to the entire set of vectors loaded.
6523 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
6524 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6525 Info.ptrVal = I.getArgOperand(0);
6526 Info.offset = 0;
6527 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6528 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6529 Info.vol = false; // volatile loads with NEON intrinsics not supported
6530 Info.readMem = true;
6531 Info.writeMem = false;
6532 return true;
6533 }
6534 case Intrinsic::arm_neon_vst1:
6535 case Intrinsic::arm_neon_vst2:
6536 case Intrinsic::arm_neon_vst3:
6537 case Intrinsic::arm_neon_vst4:
6538 case Intrinsic::arm_neon_vst2lane:
6539 case Intrinsic::arm_neon_vst3lane:
6540 case Intrinsic::arm_neon_vst4lane: {
6541 Info.opc = ISD::INTRINSIC_VOID;
6542 // Conservatively set memVT to the entire set of vectors stored.
6543 unsigned NumElts = 0;
6544 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
6545 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
6546 if (!ArgTy->isVectorTy())
6547 break;
6548 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
6549 }
6550 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6551 Info.ptrVal = I.getArgOperand(0);
6552 Info.offset = 0;
6553 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6554 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6555 Info.vol = false; // volatile stores with NEON intrinsics not supported
6556 Info.readMem = false;
6557 Info.writeMem = true;
6558 return true;
6559 }
6560 default:
6561 break;
6562 }
6563
6564 return false;
6565}